bond-intra-unit-cylinder.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright (c) 2018-2020 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, 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. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  24. const isBondType = BondType.is;
  25. function getIntraUnitBondCylinderBuilderProps(unit: Unit.Atomic, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondCylinderParams>) {
  26. const locE = StructureElement.Location.create(structure, unit);
  27. const locB = Bond.Location(structure, unit, undefined, structure, unit, undefined);
  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 } = props;
  33. const vRef = Vec3(), delta = Vec3();
  34. const pos = unit.conformation.invariantPosition;
  35. const radius = (edgeIndex: number) => {
  36. locB.aIndex = a[edgeIndex];
  37. locB.bIndex = b[edgeIndex];
  38. return theme.size.size(locB) * sizeFactor;
  39. };
  40. const radiusA = (edgeIndex: number) => {
  41. locE.element = elements[a[edgeIndex]];
  42. return theme.size.size(locE) * sizeFactor;
  43. };
  44. const radiusB = (edgeIndex: number) => {
  45. locE.element = elements[b[edgeIndex]];
  46. return theme.size.size(locE) * sizeFactor;
  47. };
  48. return {
  49. linkCount: edgeCount * 2,
  50. referencePosition: (edgeIndex: number) => {
  51. let aI = a[edgeIndex], bI = b[edgeIndex];
  52. if (aI > bI) [aI, bI] = [bI, aI];
  53. if (offset[aI + 1] - offset[aI] === 1) [aI, bI] = [bI, aI];
  54. // TODO prefer reference atoms within rings
  55. for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
  56. const _bI = b[i];
  57. if (_bI !== bI && _bI !== aI) return pos(elements[_bI], vRef);
  58. }
  59. for (let i = offset[bI], il = offset[bI + 1]; i < il; ++i) {
  60. const _aI = a[i];
  61. if (_aI !== aI && _aI !== bI) return pos(elements[_aI], vRef);
  62. }
  63. return null;
  64. },
  65. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  66. const rA = radiusA(edgeIndex), rB = radiusB(edgeIndex);
  67. const r = Math.min(rA, rB) * sizeAspectRatio;
  68. const oA = Math.sqrt(Math.max(0, rA * rA - r * r)) - 0.05;
  69. const oB = Math.sqrt(Math.max(0, rB * rB - r * r)) - 0.05;
  70. pos(elements[a[edgeIndex]], posA);
  71. pos(elements[b[edgeIndex]], posB);
  72. if (oA <= 0.01 && oB <= 0.01) return;
  73. Vec3.normalize(delta, Vec3.sub(delta, posB, posA));
  74. Vec3.scaleAndAdd(posA, posA, delta, oA);
  75. Vec3.scaleAndAdd(posB, posB, delta, -oB);
  76. },
  77. style: (edgeIndex: number) => {
  78. const o = _order[edgeIndex];
  79. const f = _flags[edgeIndex];
  80. if (isBondType(f, BondType.Flag.MetallicCoordination) || isBondType(f, BondType.Flag.HydrogenBond)) {
  81. // show metall coordinations and hydrogen bonds with dashed cylinders
  82. return LinkStyle.Dashed;
  83. } else if (o === 2) {
  84. return LinkStyle.Double;
  85. } else if (o === 3) {
  86. return LinkStyle.Triple;
  87. } else {
  88. return LinkStyle.Solid;
  89. }
  90. },
  91. radius: (edgeIndex: number) => {
  92. return radius(edgeIndex) * sizeAspectRatio;
  93. },
  94. ignore: makeIntraBondIgnoreTest(unit, props)
  95. };
  96. }
  97. function createIntraUnitBondCylinderImpostors(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondCylinderParams>, cylinders?: Cylinders): Cylinders {
  98. if (!Unit.isAtomic(unit)) return Cylinders.createEmpty(cylinders);
  99. if (!unit.bonds.edgeCount) return Cylinders.createEmpty(cylinders);
  100. const builderProps = getIntraUnitBondCylinderBuilderProps(unit, structure, theme, props);
  101. const c = createLinkCylinderImpostors(ctx, builderProps, props, cylinders);
  102. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  103. c.setBoundingSphere(sphere);
  104. return c;
  105. }
  106. function createIntraUnitBondCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondCylinderParams>, mesh?: Mesh): Mesh {
  107. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh);
  108. if (!unit.bonds.edgeCount) return Mesh.createEmpty(mesh);
  109. const builderProps = getIntraUnitBondCylinderBuilderProps(unit, structure, theme, props);
  110. const m = createLinkCylinderMesh(ctx, builderProps, props, mesh);
  111. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  112. m.setBoundingSphere(sphere);
  113. return m;
  114. }
  115. export const IntraUnitBondCylinderParams = {
  116. ...UnitsMeshParams,
  117. ...UnitsCylindersParams,
  118. ...BondCylinderParams,
  119. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  120. sizeAspectRatio: PD.Numeric(2 / 3, { min: 0, max: 3, step: 0.01 }),
  121. tryUseImpostor: PD.Boolean(true),
  122. };
  123. export type IntraUnitBondCylinderParams = typeof IntraUnitBondCylinderParams
  124. export function IntraUnitBondCylinderVisual(materialId: number, structure: Structure, props: PD.Values<IntraUnitBondCylinderParams>, webgl?: WebGLContext) {
  125. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
  126. ? IntraUnitBondCylinderImpostorVisual(materialId)
  127. : IntraUnitBondCylinderMeshVisual(materialId);
  128. }
  129. export function IntraUnitBondCylinderImpostorVisual(materialId: number): UnitsVisual<IntraUnitBondCylinderParams> {
  130. return UnitsCylindersVisual<IntraUnitBondCylinderParams>({
  131. defaultProps: PD.getDefaultValues(IntraUnitBondCylinderParams),
  132. createGeometry: createIntraUnitBondCylinderImpostors,
  133. createLocationIterator: BondIterator.fromGroup,
  134. getLoci: getIntraBondLoci,
  135. eachLocation: eachIntraBond,
  136. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitBondCylinderParams>, currentProps: PD.Values<IntraUnitBondCylinderParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  137. state.createGeometry = (
  138. newProps.linkScale !== currentProps.linkScale ||
  139. newProps.linkSpacing !== currentProps.linkSpacing ||
  140. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  141. newProps.linkCap !== currentProps.linkCap ||
  142. newProps.dashCount !== currentProps.dashCount ||
  143. newProps.dashScale !== currentProps.dashScale ||
  144. newProps.dashCap !== currentProps.dashCap ||
  145. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  146. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes)
  147. );
  148. const newUnit = newStructureGroup.group.units[0];
  149. const currentUnit = currentStructureGroup.group.units[0];
  150. if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) {
  151. if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) {
  152. state.createGeometry = true;
  153. state.updateTransform = true;
  154. state.updateColor = true;
  155. state.updateSize = true;
  156. }
  157. }
  158. },
  159. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<IntraUnitBondCylinderParams>, webgl?: WebGLContext) => {
  160. return !props.tryUseImpostor || !webgl;
  161. }
  162. }, materialId);
  163. }
  164. export function IntraUnitBondCylinderMeshVisual(materialId: number): UnitsVisual<IntraUnitBondCylinderParams> {
  165. return UnitsMeshVisual<IntraUnitBondCylinderParams>({
  166. defaultProps: PD.getDefaultValues(IntraUnitBondCylinderParams),
  167. createGeometry: createIntraUnitBondCylinderMesh,
  168. createLocationIterator: BondIterator.fromGroup,
  169. getLoci: getIntraBondLoci,
  170. eachLocation: eachIntraBond,
  171. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitBondCylinderParams>, currentProps: PD.Values<IntraUnitBondCylinderParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  172. state.createGeometry = (
  173. newProps.sizeFactor !== currentProps.sizeFactor ||
  174. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  175. newProps.radialSegments !== currentProps.radialSegments ||
  176. newProps.linkScale !== currentProps.linkScale ||
  177. newProps.linkSpacing !== currentProps.linkSpacing ||
  178. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  179. newProps.linkCap !== currentProps.linkCap ||
  180. newProps.dashCount !== currentProps.dashCount ||
  181. newProps.dashScale !== currentProps.dashScale ||
  182. newProps.dashCap !== currentProps.dashCap ||
  183. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  184. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes)
  185. );
  186. const newUnit = newStructureGroup.group.units[0];
  187. const currentUnit = currentStructureGroup.group.units[0];
  188. if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) {
  189. if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) {
  190. state.createGeometry = true;
  191. state.updateTransform = true;
  192. state.updateColor = true;
  193. state.updateSize = true;
  194. }
  195. }
  196. },
  197. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<IntraUnitBondCylinderParams>, webgl?: WebGLContext) => {
  198. return props.tryUseImpostor && !!webgl;
  199. }
  200. }, materialId);
  201. }