bond-inter-unit-cylinder.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { VisualContext } from '../../visual';
  8. import { Structure, StructureElement, Bond, Unit } from '../../../mol-model/structure';
  9. import { Theme } from '../../../mol-theme/theme';
  10. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  11. import { Vec3 } from '../../../mol-math/linear-algebra';
  12. import { BitFlags, arrayEqual } from '../../../mol-util';
  13. import { createLinkCylinderImpostors, createLinkCylinderMesh, LinkBuilderProps, LinkStyle } from './util/link';
  14. import { ComplexMeshParams, ComplexVisual, ComplexMeshVisual, ComplexCylindersParams, ComplexCylindersVisual } from '../complex-visual';
  15. import { VisualUpdateState } from '../../util';
  16. import { BondType } from '../../../mol-model/structure/model/types';
  17. import { BondCylinderParams, BondIterator, getInterBondLoci, eachInterBond, makeInterBondIgnoreTest } from './util/bond';
  18. import { Sphere3D } from '../../../mol-math/geometry';
  19. import { Cylinders } from '../../../mol-geo/geometry/cylinders/cylinders';
  20. import { WebGLContext } from '../../../mol-gl/webgl/context';
  21. import { SortedArray } from '../../../mol-data/int/sorted-array';
  22. const tmpRefPosBondIt = new Bond.ElementBondIterator();
  23. function setRefPosition(pos: Vec3, structure: Structure, unit: Unit.Atomic, index: StructureElement.UnitIndex) {
  24. tmpRefPosBondIt.setElement(structure, unit, index);
  25. while (tmpRefPosBondIt.hasNext) {
  26. const bA = tmpRefPosBondIt.move();
  27. bA.otherUnit.conformation.position(bA.otherUnit.elements[bA.otherIndex], pos);
  28. return pos;
  29. }
  30. return null;
  31. }
  32. const tmpRef = Vec3();
  33. function getInterUnitBondCylinderBuilderProps(structure: Structure, theme: Theme, props: PD.Values<InterUnitBondCylinderParams>): LinkBuilderProps {
  34. const locE = StructureElement.Location.create(structure);
  35. const locB = Bond.Location(structure, undefined, undefined, structure, undefined, undefined);
  36. const bonds = structure.interUnitBonds;
  37. const { edgeCount, edges } = bonds;
  38. const { sizeFactor, sizeAspectRatio, adjustCylinderLength, aromaticBonds } = props;
  39. const delta = Vec3();
  40. let stub: undefined | ((edgeIndex: number) => boolean);
  41. const { child } = structure;
  42. if (props.includeParent && child) {
  43. stub = (edgeIndex: number) => {
  44. const b = edges[edgeIndex];
  45. const childUnitA = child.unitMap.get(b.unitA);
  46. const childUnitB = child.unitMap.get(b.unitB);
  47. const unitA = structure.unitMap.get(b.unitA);
  48. const eA = unitA.elements[b.indexA];
  49. const unitB = structure.unitMap.get(b.unitB);
  50. const eB = unitB.elements[b.indexB];
  51. return (
  52. childUnitA && SortedArray.has(childUnitA.elements, eA) &&
  53. (!childUnitB || !SortedArray.has(childUnitB.elements, eB))
  54. );
  55. };
  56. }
  57. const radius = (edgeIndex: number) => {
  58. const b = edges[edgeIndex];
  59. locB.aUnit = structure.unitMap.get(b.unitA);
  60. locB.aIndex = b.indexA;
  61. locB.bUnit = structure.unitMap.get(b.unitB);
  62. locB.bIndex = b.indexB;
  63. return theme.size.size(locB) * sizeFactor;
  64. };
  65. const radiusA = (edgeIndex: number) => {
  66. const b = edges[edgeIndex];
  67. locE.unit = structure.unitMap.get(b.unitA);
  68. locE.element = locE.unit.elements[b.indexA];
  69. return theme.size.size(locE) * sizeFactor;
  70. };
  71. const radiusB = (edgeIndex: number) => {
  72. const b = edges[edgeIndex];
  73. locE.unit = structure.unitMap.get(b.unitB);
  74. locE.element = locE.unit.elements[b.indexB];
  75. return theme.size.size(locE) * sizeFactor;
  76. };
  77. return {
  78. linkCount: edgeCount,
  79. referencePosition: (edgeIndex: number) => {
  80. const b = edges[edgeIndex];
  81. let unitA: Unit.Atomic, unitB: Unit.Atomic;
  82. let indexA: StructureElement.UnitIndex, indexB: StructureElement.UnitIndex;
  83. if (b.unitA < b.unitB) {
  84. unitA = structure.unitMap.get(b.unitA) as Unit.Atomic;
  85. unitB = structure.unitMap.get(b.unitB) as Unit.Atomic;
  86. indexA = b.indexA;
  87. indexB = b.indexB;
  88. } else if (b.unitA > b.unitB) {
  89. unitA = structure.unitMap.get(b.unitB) as Unit.Atomic;
  90. unitB = structure.unitMap.get(b.unitA) as Unit.Atomic;
  91. indexA = b.indexB;
  92. indexB = b.indexA;
  93. } else {
  94. throw new Error('same units in createInterUnitBondCylinderMesh');
  95. }
  96. return setRefPosition(tmpRef, structure, unitA, indexA) || setRefPosition(tmpRef, structure, unitB, indexB);
  97. },
  98. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  99. const b = edges[edgeIndex];
  100. const uA = structure.unitMap.get(b.unitA);
  101. const uB = structure.unitMap.get(b.unitB);
  102. uA.conformation.position(uA.elements[b.indexA], posA);
  103. uB.conformation.position(uB.elements[b.indexB], posB);
  104. if (adjustCylinderLength) {
  105. const rA = radiusA(edgeIndex), rB = radiusB(edgeIndex);
  106. const r = Math.min(rA, rB) * sizeAspectRatio;
  107. const oA = Math.sqrt(Math.max(0, rA * rA - r * r)) - 0.05;
  108. const oB = Math.sqrt(Math.max(0, rB * rB - r * r)) - 0.05;
  109. if (oA <= 0.01 && oB <= 0.01) return;
  110. Vec3.normalize(delta, Vec3.sub(delta, posB, posA));
  111. Vec3.scaleAndAdd(posA, posA, delta, oA);
  112. Vec3.scaleAndAdd(posB, posB, delta, -oB);
  113. }
  114. },
  115. style: (edgeIndex: number) => {
  116. const o = edges[edgeIndex].props.order;
  117. const f = BitFlags.create(edges[edgeIndex].props.flag);
  118. if (BondType.is(f, BondType.Flag.MetallicCoordination) || BondType.is(f, BondType.Flag.HydrogenBond)) {
  119. // show metall coordinations and hydrogen bonds with dashed cylinders
  120. return LinkStyle.Dashed;
  121. } else if (o === 3) {
  122. return LinkStyle.Triple;
  123. } else if (aromaticBonds && BondType.is(f, BondType.Flag.Aromatic)) {
  124. return LinkStyle.Aromatic;
  125. } else if (o === 2) {
  126. return LinkStyle.Double;
  127. }
  128. return LinkStyle.Solid;
  129. },
  130. radius: (edgeIndex: number) => {
  131. return radius(edgeIndex) * sizeAspectRatio;
  132. },
  133. ignore: makeInterBondIgnoreTest(structure, props),
  134. stub
  135. };
  136. }
  137. function createInterUnitBondCylinderImpostors(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<InterUnitBondCylinderParams>, cylinders?: Cylinders) {
  138. if (!structure.interUnitBonds.edgeCount) return Cylinders.createEmpty(cylinders);
  139. const builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props);
  140. const m = createLinkCylinderImpostors(ctx, builderProps, props, cylinders);
  141. const { child } = structure;
  142. const sphere = Sphere3D.expand(Sphere3D(), (child ?? structure).boundary.sphere, 1 * props.sizeFactor);
  143. m.setBoundingSphere(sphere);
  144. return m;
  145. }
  146. function createInterUnitBondCylinderMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<InterUnitBondCylinderParams>, mesh?: Mesh) {
  147. if (!structure.interUnitBonds.edgeCount) return Mesh.createEmpty(mesh);
  148. const builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props);
  149. const m = createLinkCylinderMesh(ctx, builderProps, props, mesh);
  150. const { child } = structure;
  151. const sphere = Sphere3D.expand(Sphere3D(), (child ?? structure).boundary.sphere, 1 * props.sizeFactor);
  152. m.setBoundingSphere(sphere);
  153. return m;
  154. }
  155. export const InterUnitBondCylinderParams = {
  156. ...ComplexMeshParams,
  157. ...ComplexCylindersParams,
  158. ...BondCylinderParams,
  159. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  160. sizeAspectRatio: PD.Numeric(2 / 3, { min: 0, max: 3, step: 0.01 }),
  161. tryUseImpostor: PD.Boolean(true),
  162. includeParent: PD.Boolean(false),
  163. };
  164. export type InterUnitBondCylinderParams = typeof InterUnitBondCylinderParams
  165. export function InterUnitBondCylinderVisual(materialId: number, structure: Structure, props: PD.Values<InterUnitBondCylinderParams>, webgl?: WebGLContext) {
  166. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
  167. ? InterUnitBondCylinderImpostorVisual(materialId)
  168. : InterUnitBondCylinderMeshVisual(materialId);
  169. }
  170. export function InterUnitBondCylinderImpostorVisual(materialId: number): ComplexVisual<InterUnitBondCylinderParams> {
  171. return ComplexCylindersVisual<InterUnitBondCylinderParams>({
  172. defaultProps: PD.getDefaultValues(InterUnitBondCylinderParams),
  173. createGeometry: createInterUnitBondCylinderImpostors,
  174. createLocationIterator: BondIterator.fromStructure,
  175. getLoci: getInterBondLoci,
  176. eachLocation: eachInterBond,
  177. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InterUnitBondCylinderParams>, currentProps: PD.Values<InterUnitBondCylinderParams>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  178. state.createGeometry = (
  179. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  180. newProps.linkScale !== currentProps.linkScale ||
  181. newProps.linkSpacing !== currentProps.linkSpacing ||
  182. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  183. newProps.linkCap !== currentProps.linkCap ||
  184. newProps.dashCount !== currentProps.dashCount ||
  185. newProps.dashScale !== currentProps.dashScale ||
  186. newProps.dashCap !== currentProps.dashCap ||
  187. newProps.stubCap !== currentProps.stubCap ||
  188. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  189. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
  190. newProps.adjustCylinderLength !== currentProps.adjustCylinderLength
  191. );
  192. if (newStructure.interUnitBonds !== currentStructure.interUnitBonds) {
  193. state.createGeometry = true;
  194. state.updateTransform = true;
  195. state.updateColor = true;
  196. state.updateSize = true;
  197. }
  198. },
  199. mustRecreate: (structure: Structure, props: PD.Values<InterUnitBondCylinderParams>, webgl?: WebGLContext) => {
  200. return !props.tryUseImpostor || !webgl;
  201. }
  202. }, materialId);
  203. }
  204. export function InterUnitBondCylinderMeshVisual(materialId: number): ComplexVisual<InterUnitBondCylinderParams> {
  205. return ComplexMeshVisual<InterUnitBondCylinderParams>({
  206. defaultProps: PD.getDefaultValues(InterUnitBondCylinderParams),
  207. createGeometry: createInterUnitBondCylinderMesh,
  208. createLocationIterator: BondIterator.fromStructure,
  209. getLoci: getInterBondLoci,
  210. eachLocation: eachInterBond,
  211. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InterUnitBondCylinderParams>, currentProps: PD.Values<InterUnitBondCylinderParams>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  212. state.createGeometry = (
  213. newProps.sizeFactor !== currentProps.sizeFactor ||
  214. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  215. newProps.radialSegments !== currentProps.radialSegments ||
  216. newProps.linkScale !== currentProps.linkScale ||
  217. newProps.linkSpacing !== currentProps.linkSpacing ||
  218. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  219. newProps.linkCap !== currentProps.linkCap ||
  220. newProps.dashCount !== currentProps.dashCount ||
  221. newProps.dashScale !== currentProps.dashScale ||
  222. newProps.dashCap !== currentProps.dashCap ||
  223. newProps.stubCap !== currentProps.stubCap ||
  224. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  225. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
  226. newProps.adjustCylinderLength !== currentProps.adjustCylinderLength
  227. );
  228. if (newStructure.interUnitBonds !== currentStructure.interUnitBonds) {
  229. state.createGeometry = true;
  230. state.updateTransform = true;
  231. state.updateColor = true;
  232. state.updateSize = true;
  233. }
  234. },
  235. mustRecreate: (structure: Structure, props: PD.Values<InterUnitBondCylinderParams>, webgl?: WebGLContext) => {
  236. return props.tryUseImpostor && !!webgl;
  237. }
  238. }, materialId);
  239. }