bond-inter-unit-cylinder.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, 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>) {
  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 } = props;
  39. const delta = Vec3();
  40. let stub: undefined | ((edgeIndex: number) => boolean);
  41. if (props.includeParent) {
  42. const child = Structure.WithChild.getChild(structure);
  43. if (!child) throw new Error('expected child to exist');
  44. stub = (edgeIndex: number) => {
  45. const b = edges[edgeIndex];
  46. const childUnitA = child.unitMap.get(b.unitA);
  47. const childUnitB = child.unitMap.get(b.unitB);
  48. const unitA = structure.unitMap.get(b.unitA);
  49. const eA = unitA.elements[b.indexA];
  50. const unitB = structure.unitMap.get(b.unitB);
  51. const eB = unitB.elements[b.indexB];
  52. return (
  53. childUnitA && SortedArray.has(childUnitA.elements, eA) &&
  54. (!childUnitB || !SortedArray.has(childUnitB.elements, eB))
  55. );
  56. };
  57. }
  58. const radius = (edgeIndex: number) => {
  59. const b = edges[edgeIndex];
  60. locB.aUnit = structure.unitMap.get(b.unitA);
  61. locB.aIndex = b.indexA;
  62. locB.bUnit = structure.unitMap.get(b.unitB);
  63. locB.bIndex = b.indexB;
  64. return theme.size.size(locB) * sizeFactor;
  65. };
  66. const radiusA = (edgeIndex: number) => {
  67. const b = edges[edgeIndex];
  68. locE.unit = structure.unitMap.get(b.unitA);
  69. locE.element = locE.unit.elements[b.indexA];
  70. return theme.size.size(locE) * sizeFactor;
  71. };
  72. const radiusB = (edgeIndex: number) => {
  73. const b = edges[edgeIndex];
  74. locE.unit = structure.unitMap.get(b.unitB);
  75. locE.element = locE.unit.elements[b.indexB];
  76. return theme.size.size(locE) * sizeFactor;
  77. };
  78. return {
  79. linkCount: edgeCount,
  80. referencePosition: (edgeIndex: number) => {
  81. const b = edges[edgeIndex];
  82. let unitA: Unit.Atomic, unitB: Unit.Atomic;
  83. let indexA: StructureElement.UnitIndex, indexB: StructureElement.UnitIndex;
  84. if (b.unitA < b.unitB) {
  85. unitA = structure.unitMap.get(b.unitA) as Unit.Atomic;
  86. unitB = structure.unitMap.get(b.unitB) as Unit.Atomic;
  87. indexA = b.indexA;
  88. indexB = b.indexB;
  89. } else if (b.unitA > b.unitB) {
  90. unitA = structure.unitMap.get(b.unitB) as Unit.Atomic;
  91. unitB = structure.unitMap.get(b.unitA) as Unit.Atomic;
  92. indexA = b.indexB;
  93. indexB = b.indexA;
  94. } else {
  95. throw new Error('same units in createInterUnitBondCylinderMesh');
  96. }
  97. return setRefPosition(tmpRef, structure, unitA, indexA) || setRefPosition(tmpRef, structure, unitB, indexB);
  98. },
  99. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  100. const b = edges[edgeIndex];
  101. const uA = structure.unitMap.get(b.unitA);
  102. const uB = structure.unitMap.get(b.unitB);
  103. const rA = radiusA(edgeIndex), rB = radiusB(edgeIndex);
  104. const r = Math.min(rA, rB) * sizeAspectRatio;
  105. const oA = Math.sqrt(Math.max(0, rA * rA - r * r)) - 0.05;
  106. const oB = Math.sqrt(Math.max(0, rB * rB - r * r)) - 0.05;
  107. uA.conformation.position(uA.elements[b.indexA], posA);
  108. uB.conformation.position(uB.elements[b.indexB], posB);
  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. style: (edgeIndex: number) => {
  115. const o = edges[edgeIndex].props.order;
  116. const f = BitFlags.create(edges[edgeIndex].props.flag);
  117. if (BondType.is(f, BondType.Flag.MetallicCoordination) || BondType.is(f, BondType.Flag.HydrogenBond)) {
  118. // show metall coordinations and hydrogen bonds with dashed cylinders
  119. return LinkStyle.Dashed;
  120. } else if (o === 2) {
  121. return LinkStyle.Double;
  122. } else if (o === 3) {
  123. return LinkStyle.Triple;
  124. } else {
  125. return LinkStyle.Solid;
  126. }
  127. },
  128. radius: (edgeIndex: number) => {
  129. return radius(edgeIndex) * sizeAspectRatio;
  130. },
  131. ignore: makeInterBondIgnoreTest(structure, props),
  132. stub
  133. };
  134. }
  135. function createInterUnitBondCylinderImpostors(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<InterUnitBondCylinderParams>, cylinders?: Cylinders) {
  136. if (!structure.interUnitBonds.edgeCount) return Cylinders.createEmpty(cylinders);
  137. const builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props);
  138. const m = createLinkCylinderImpostors(ctx, builderProps, props, cylinders);
  139. const child = Structure.WithChild.getChild(structure);
  140. const sphere = Sphere3D.expand(Sphere3D(), (child ?? structure).boundary.sphere, 1 * props.sizeFactor);
  141. m.setBoundingSphere(sphere);
  142. return m;
  143. }
  144. function createInterUnitBondCylinderMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<InterUnitBondCylinderParams>, mesh?: Mesh) {
  145. if (!structure.interUnitBonds.edgeCount) return Mesh.createEmpty(mesh);
  146. const builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props);
  147. const m = createLinkCylinderMesh(ctx, builderProps, props, mesh);
  148. const child = Structure.WithChild.getChild(structure);
  149. const sphere = Sphere3D.expand(Sphere3D(), (child ?? structure).boundary.sphere, 1 * props.sizeFactor);
  150. m.setBoundingSphere(sphere);
  151. return m;
  152. }
  153. export const InterUnitBondCylinderParams = {
  154. ...ComplexMeshParams,
  155. ...ComplexCylindersParams,
  156. ...BondCylinderParams,
  157. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  158. sizeAspectRatio: PD.Numeric(2 / 3, { min: 0, max: 3, step: 0.01 }),
  159. tryUseImpostor: PD.Boolean(true),
  160. includeParent: PD.Boolean(false),
  161. };
  162. export type InterUnitBondCylinderParams = typeof InterUnitBondCylinderParams
  163. export function InterUnitBondCylinderVisual(materialId: number, structure: Structure, props: PD.Values<InterUnitBondCylinderParams>, webgl?: WebGLContext) {
  164. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
  165. ? InterUnitBondCylinderImpostorVisual(materialId)
  166. : InterUnitBondCylinderMeshVisual(materialId);
  167. }
  168. export function InterUnitBondCylinderImpostorVisual(materialId: number): ComplexVisual<InterUnitBondCylinderParams> {
  169. return ComplexCylindersVisual<InterUnitBondCylinderParams>({
  170. defaultProps: PD.getDefaultValues(InterUnitBondCylinderParams),
  171. createGeometry: createInterUnitBondCylinderImpostors,
  172. createLocationIterator: BondIterator.fromStructure,
  173. getLoci: getInterBondLoci,
  174. eachLocation: eachInterBond,
  175. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InterUnitBondCylinderParams>, currentProps: PD.Values<InterUnitBondCylinderParams>) => {
  176. state.createGeometry = (
  177. newProps.linkScale !== currentProps.linkScale ||
  178. newProps.linkSpacing !== currentProps.linkSpacing ||
  179. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  180. newProps.linkCap !== currentProps.linkCap ||
  181. newProps.dashCount !== currentProps.dashCount ||
  182. newProps.dashScale !== currentProps.dashScale ||
  183. newProps.dashCap !== currentProps.dashCap ||
  184. newProps.stubCap !== currentProps.stubCap ||
  185. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  186. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes)
  187. );
  188. },
  189. mustRecreate: (structure: Structure, props: PD.Values<InterUnitBondCylinderParams>, webgl?: WebGLContext) => {
  190. return !props.tryUseImpostor || !webgl;
  191. }
  192. }, materialId);
  193. }
  194. export function InterUnitBondCylinderMeshVisual(materialId: number): ComplexVisual<InterUnitBondCylinderParams> {
  195. return ComplexMeshVisual<InterUnitBondCylinderParams>({
  196. defaultProps: PD.getDefaultValues(InterUnitBondCylinderParams),
  197. createGeometry: createInterUnitBondCylinderMesh,
  198. createLocationIterator: BondIterator.fromStructure,
  199. getLoci: getInterBondLoci,
  200. eachLocation: eachInterBond,
  201. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InterUnitBondCylinderParams>, currentProps: PD.Values<InterUnitBondCylinderParams>) => {
  202. state.createGeometry = (
  203. newProps.sizeFactor !== currentProps.sizeFactor ||
  204. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  205. newProps.radialSegments !== currentProps.radialSegments ||
  206. newProps.linkScale !== currentProps.linkScale ||
  207. newProps.linkSpacing !== currentProps.linkSpacing ||
  208. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  209. newProps.linkCap !== currentProps.linkCap ||
  210. newProps.dashCount !== currentProps.dashCount ||
  211. newProps.dashScale !== currentProps.dashScale ||
  212. newProps.dashCap !== currentProps.dashCap ||
  213. newProps.stubCap !== currentProps.stubCap ||
  214. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  215. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes)
  216. );
  217. },
  218. mustRecreate: (structure: Structure, props: PD.Values<InterUnitBondCylinderParams>, webgl?: WebGLContext) => {
  219. return props.tryUseImpostor && !!webgl;
  220. }
  221. }, materialId);
  222. }