bond-intra-unit-cylinder.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  8. import { VisualContext } from '../../visual';
  9. import { Unit, Structure, StructureElement, Bond } from '../../../mol-model/structure';
  10. import { Theme } from '../../../mol-theme/theme';
  11. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  12. import { Vec3 } from '../../../mol-math/linear-algebra';
  13. import { arrayEqual } from '../../../mol-util';
  14. import { createLinkCylinderImpostors, createLinkCylinderMesh, LinkBuilderProps, LinkStyle } from './util/link';
  15. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual, StructureGroup, UnitsCylindersParams, UnitsCylindersVisual } from '../units-visual';
  16. import { VisualUpdateState } from '../../util';
  17. import { BondType } from '../../../mol-model/structure/model/types';
  18. import { BondCylinderParams, BondIterator, eachIntraBond, getIntraBondLoci, makeIntraBondIgnoreTest } from './util/bond';
  19. import { Sphere3D } from '../../../mol-math/geometry';
  20. import { IntAdjacencyGraph } from '../../../mol-math/graph';
  21. import { WebGLContext } from '../../../mol-gl/webgl/context';
  22. import { Cylinders } from '../../../mol-geo/geometry/cylinders/cylinders';
  23. import { SortedArray } from '../../../mol-data/int';
  24. import { arrayIntersectionSize } from '../../../mol-util/array';
  25. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  26. const isBondType = BondType.is;
  27. function getIntraUnitBondCylinderBuilderProps(unit: Unit.Atomic, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondCylinderParams>): LinkBuilderProps {
  28. const elements = unit.elements;
  29. const bonds = unit.bonds;
  30. const { edgeCount, a, b, edgeProps, offset } = bonds;
  31. const { order: _order, flags: _flags } = edgeProps;
  32. const { sizeFactor, sizeAspectRatio, adjustCylinderLength, aromaticBonds } = props;
  33. const vRef = Vec3(), delta = Vec3();
  34. const pos = unit.conformation.invariantPosition;
  35. let stub: undefined | ((edgeIndex: number) => boolean);
  36. const locE = StructureElement.Location.create(structure, unit);
  37. const locB = Bond.Location(structure, unit, undefined, structure, unit, undefined);
  38. const { child } = structure;
  39. if (props.includeParent && child) {
  40. const childUnit = child.unitMap.get(unit.id);
  41. if (!childUnit) throw new Error('expected childUnit to exist');
  42. stub = (edgeIndex: number) => {
  43. const eA = elements[a[edgeIndex]];
  44. const eB = elements[b[edgeIndex]];
  45. return SortedArray.has(childUnit.elements, eA) && !SortedArray.has(childUnit.elements, eB);
  46. };
  47. }
  48. const radius = (edgeIndex: number) => {
  49. locB.aIndex = a[edgeIndex];
  50. locB.bIndex = b[edgeIndex];
  51. return theme.size.size(locB) * sizeFactor;
  52. };
  53. const radiusA = (edgeIndex: number) => {
  54. locE.element = elements[a[edgeIndex]];
  55. return theme.size.size(locE) * sizeFactor;
  56. };
  57. const radiusB = (edgeIndex: number) => {
  58. locE.element = elements[b[edgeIndex]];
  59. return theme.size.size(locE) * sizeFactor;
  60. };
  61. const { elementRingIndices, elementAromaticRingIndices } = unit.rings;
  62. return {
  63. linkCount: edgeCount * 2,
  64. referencePosition: (edgeIndex: number) => {
  65. let aI = a[edgeIndex], bI = b[edgeIndex];
  66. if (aI > bI) [aI, bI] = [bI, aI];
  67. if (offset[aI + 1] - offset[aI] === 1) [aI, bI] = [bI, aI];
  68. const aR = elementRingIndices.get(aI);
  69. let maxSize = 0;
  70. for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
  71. const _bI = b[i];
  72. if (_bI !== bI && _bI !== aI) {
  73. if (aR) {
  74. const _bR = elementRingIndices.get(_bI);
  75. if (!_bR) continue;
  76. const size = arrayIntersectionSize(aR, _bR);
  77. if (size > maxSize) {
  78. maxSize = size;
  79. pos(elements[_bI], vRef);
  80. }
  81. } else {
  82. return pos(elements[_bI], vRef);
  83. }
  84. }
  85. }
  86. return maxSize > 0 ? vRef : null;
  87. },
  88. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  89. pos(elements[a[edgeIndex]], posA);
  90. pos(elements[b[edgeIndex]], posB);
  91. if (adjustCylinderLength) {
  92. const rA = radiusA(edgeIndex), rB = radiusB(edgeIndex);
  93. const r = Math.min(rA, rB) * sizeAspectRatio;
  94. const oA = Math.sqrt(Math.max(0, rA * rA - r * r)) - 0.05;
  95. const oB = Math.sqrt(Math.max(0, rB * rB - r * r)) - 0.05;
  96. if (oA <= 0.01 && oB <= 0.01) return;
  97. Vec3.normalize(delta, Vec3.sub(delta, posB, posA));
  98. Vec3.scaleAndAdd(posA, posA, delta, oA);
  99. Vec3.scaleAndAdd(posB, posB, delta, -oB);
  100. }
  101. },
  102. style: (edgeIndex: number) => {
  103. const o = _order[edgeIndex];
  104. const f = _flags[edgeIndex];
  105. if (isBondType(f, BondType.Flag.MetallicCoordination) || isBondType(f, BondType.Flag.HydrogenBond)) {
  106. // show metall coordinations and hydrogen bonds with dashed cylinders
  107. return LinkStyle.Dashed;
  108. } else if (o === 3) {
  109. return LinkStyle.Triple;
  110. } else if (aromaticBonds) {
  111. const aI = a[edgeIndex], bI = b[edgeIndex];
  112. const aR = elementAromaticRingIndices.get(aI);
  113. const bR = elementAromaticRingIndices.get(bI);
  114. const arCount = (aR && bR) ? arrayIntersectionSize(aR, bR) : 0;
  115. if (arCount || isBondType(f, BondType.Flag.Aromatic)) {
  116. if (arCount === 2) {
  117. return LinkStyle.MirroredAromatic;
  118. } else {
  119. return LinkStyle.Aromatic;
  120. }
  121. }
  122. }
  123. return o === 2 ? LinkStyle.Double : LinkStyle.Solid;
  124. },
  125. radius: (edgeIndex: number) => {
  126. return radius(edgeIndex) * sizeAspectRatio;
  127. },
  128. ignore: makeIntraBondIgnoreTest(structure, unit, props),
  129. stub
  130. };
  131. }
  132. function createIntraUnitBondCylinderImpostors(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondCylinderParams>, cylinders?: Cylinders): Cylinders {
  133. if (!Unit.isAtomic(unit)) return Cylinders.createEmpty(cylinders);
  134. if (!unit.bonds.edgeCount) return Cylinders.createEmpty(cylinders);
  135. const { child } = structure;
  136. const childUnit = child?.unitMap.get(unit.id);
  137. if (child && !childUnit) return Cylinders.createEmpty(cylinders);
  138. const builderProps = getIntraUnitBondCylinderBuilderProps(unit, structure, theme, props);
  139. const c = createLinkCylinderImpostors(ctx, builderProps, props, cylinders);
  140. const sphere = Sphere3D.expand(Sphere3D(), (childUnit ?? unit).boundary.sphere, 1 * props.sizeFactor);
  141. c.setBoundingSphere(sphere);
  142. return c;
  143. }
  144. function createIntraUnitBondCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondCylinderParams>, mesh?: Mesh): Mesh {
  145. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh);
  146. if (!unit.bonds.edgeCount) return Mesh.createEmpty(mesh);
  147. const { child } = structure;
  148. const childUnit = child?.unitMap.get(unit.id);
  149. if (child && !childUnit) return Mesh.createEmpty(mesh);
  150. const builderProps = getIntraUnitBondCylinderBuilderProps(unit, structure, theme, props);
  151. const m = createLinkCylinderMesh(ctx, builderProps, props, mesh);
  152. const sphere = Sphere3D.expand(Sphere3D(), (childUnit ?? unit).boundary.sphere, 1 * props.sizeFactor);
  153. m.setBoundingSphere(sphere);
  154. return m;
  155. }
  156. export const IntraUnitBondCylinderParams = {
  157. ...UnitsMeshParams,
  158. ...UnitsCylindersParams,
  159. ...BondCylinderParams,
  160. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  161. sizeAspectRatio: PD.Numeric(2 / 3, { min: 0, max: 3, step: 0.01 }),
  162. tryUseImpostor: PD.Boolean(true),
  163. includeParent: PD.Boolean(false),
  164. };
  165. export type IntraUnitBondCylinderParams = typeof IntraUnitBondCylinderParams
  166. export function IntraUnitBondCylinderVisual(materialId: number, structure: Structure, props: PD.Values<IntraUnitBondCylinderParams>, webgl?: WebGLContext) {
  167. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
  168. ? IntraUnitBondCylinderImpostorVisual(materialId)
  169. : IntraUnitBondCylinderMeshVisual(materialId);
  170. }
  171. export function IntraUnitBondCylinderImpostorVisual(materialId: number): UnitsVisual<IntraUnitBondCylinderParams> {
  172. return UnitsCylindersVisual<IntraUnitBondCylinderParams>({
  173. defaultProps: PD.getDefaultValues(IntraUnitBondCylinderParams),
  174. createGeometry: createIntraUnitBondCylinderImpostors,
  175. createLocationIterator: BondIterator.fromGroup,
  176. getLoci: getIntraBondLoci,
  177. eachLocation: eachIntraBond,
  178. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitBondCylinderParams>, currentProps: PD.Values<IntraUnitBondCylinderParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  179. state.createGeometry = (
  180. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  181. newProps.linkScale !== currentProps.linkScale ||
  182. newProps.linkSpacing !== currentProps.linkSpacing ||
  183. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  184. newProps.linkCap !== currentProps.linkCap ||
  185. newProps.dashCount !== currentProps.dashCount ||
  186. newProps.dashScale !== currentProps.dashScale ||
  187. newProps.dashCap !== currentProps.dashCap ||
  188. newProps.stubCap !== currentProps.stubCap ||
  189. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  190. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
  191. newProps.adjustCylinderLength !== currentProps.adjustCylinderLength ||
  192. newProps.aromaticBonds !== currentProps.aromaticBonds
  193. );
  194. const newUnit = newStructureGroup.group.units[0];
  195. const currentUnit = currentStructureGroup.group.units[0];
  196. if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) {
  197. if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) {
  198. state.createGeometry = true;
  199. state.updateTransform = true;
  200. state.updateColor = true;
  201. state.updateSize = true;
  202. }
  203. }
  204. },
  205. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<IntraUnitBondCylinderParams>, webgl?: WebGLContext) => {
  206. return !props.tryUseImpostor || !webgl;
  207. }
  208. }, materialId);
  209. }
  210. export function IntraUnitBondCylinderMeshVisual(materialId: number): UnitsVisual<IntraUnitBondCylinderParams> {
  211. return UnitsMeshVisual<IntraUnitBondCylinderParams>({
  212. defaultProps: PD.getDefaultValues(IntraUnitBondCylinderParams),
  213. createGeometry: createIntraUnitBondCylinderMesh,
  214. createLocationIterator: BondIterator.fromGroup,
  215. getLoci: getIntraBondLoci,
  216. eachLocation: eachIntraBond,
  217. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitBondCylinderParams>, currentProps: PD.Values<IntraUnitBondCylinderParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  218. state.createGeometry = (
  219. newProps.sizeFactor !== currentProps.sizeFactor ||
  220. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  221. newProps.radialSegments !== currentProps.radialSegments ||
  222. newProps.linkScale !== currentProps.linkScale ||
  223. newProps.linkSpacing !== currentProps.linkSpacing ||
  224. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  225. newProps.linkCap !== currentProps.linkCap ||
  226. newProps.dashCount !== currentProps.dashCount ||
  227. newProps.dashScale !== currentProps.dashScale ||
  228. newProps.dashCap !== currentProps.dashCap ||
  229. newProps.stubCap !== currentProps.stubCap ||
  230. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  231. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
  232. newProps.adjustCylinderLength !== currentProps.adjustCylinderLength ||
  233. newProps.aromaticBonds !== currentProps.aromaticBonds
  234. );
  235. const newUnit = newStructureGroup.group.units[0];
  236. const currentUnit = currentStructureGroup.group.units[0];
  237. if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) {
  238. if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) {
  239. state.createGeometry = true;
  240. state.updateTransform = true;
  241. state.updateColor = true;
  242. state.updateSize = true;
  243. }
  244. }
  245. },
  246. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<IntraUnitBondCylinderParams>, webgl?: WebGLContext) => {
  247. return props.tryUseImpostor && !!webgl;
  248. }
  249. }, materialId);
  250. }