bond-intra-unit-line.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) 2020-2022 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 { Unit, Structure, StructureElement } from '../../../mol-model/structure';
  9. import { Theme } from '../../../mol-theme/theme';
  10. import { Vec3 } from '../../../mol-math/linear-algebra';
  11. import { arrayEqual } from '../../../mol-util';
  12. import { LinkStyle, createLinkLines, LinkBuilderProps } from './util/link';
  13. import { UnitsVisual, UnitsLinesParams, UnitsLinesVisual } from '../units-visual';
  14. import { VisualUpdateState } from '../../util';
  15. import { BondType } from '../../../mol-model/structure/model/types';
  16. import { BondIterator, BondLineParams, getIntraBondLoci, eachIntraBond, makeIntraBondIgnoreTest, ignoreBondType } from './util/bond';
  17. import { Sphere3D } from '../../../mol-math/geometry';
  18. import { Lines } from '../../../mol-geo/geometry/lines/lines';
  19. import { IntAdjacencyGraph } from '../../../mol-math/graph';
  20. import { arrayIntersectionSize } from '../../../mol-util/array';
  21. import { StructureGroup } from './util/common';
  22. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  23. const isBondType = BondType.is;
  24. function createIntraUnitBondLines(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondLineParams>, lines?: Lines) {
  25. if (!Unit.isAtomic(unit)) return Lines.createEmpty(lines);
  26. const { child } = structure;
  27. const childUnit = child?.unitMap.get(unit.id);
  28. if (child && !childUnit) return Lines.createEmpty(lines);
  29. const location = StructureElement.Location.create(structure, unit);
  30. const elements = unit.elements;
  31. const bonds = unit.bonds;
  32. const { edgeCount, a, b, edgeProps, offset } = bonds;
  33. if (!edgeCount) return Lines.createEmpty(lines);
  34. const { order: _order, flags: _flags } = edgeProps;
  35. const { sizeFactor, aromaticBonds, includeTypes, excludeTypes, multipleBonds } = props;
  36. const mbOff = multipleBonds === 'off';
  37. const mbSymmetric = multipleBonds === 'symmetric';
  38. const include = BondType.fromNames(includeTypes);
  39. const exclude = BondType.fromNames(excludeTypes);
  40. const ignoreComputedAromatic = ignoreBondType(include, exclude, BondType.Flag.Computed);
  41. const vRef = Vec3();
  42. const pos = unit.conformation.invariantPosition;
  43. const { elementRingIndices, elementAromaticRingIndices } = unit.rings;
  44. const deloTriplets = aromaticBonds ? unit.resonance.delocalizedTriplets : undefined;
  45. const builderProps: LinkBuilderProps = {
  46. linkCount: edgeCount * 2,
  47. referencePosition: (edgeIndex: number) => {
  48. let aI = a[edgeIndex], bI = b[edgeIndex];
  49. const rI = deloTriplets?.getThirdElement(aI, bI);
  50. if (rI !== undefined) return pos(elements[rI], vRef);
  51. if (aI > bI) [aI, bI] = [bI, aI];
  52. if (offset[aI + 1] - offset[aI] === 1) [aI, bI] = [bI, aI];
  53. const aR = elementAromaticRingIndices.get(aI) || elementRingIndices.get(aI);
  54. let maxSize = 0;
  55. for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
  56. const _bI = b[i];
  57. if (_bI !== bI && _bI !== aI) {
  58. if (aR) {
  59. const _bR = elementAromaticRingIndices.get(_bI) || elementRingIndices.get(_bI);
  60. if (!_bR) continue;
  61. const size = arrayIntersectionSize(aR, _bR);
  62. if (size > maxSize) {
  63. maxSize = size;
  64. pos(elements[_bI], vRef);
  65. }
  66. } else {
  67. return pos(elements[_bI], vRef);
  68. }
  69. }
  70. }
  71. return maxSize > 0 ? vRef : null;
  72. },
  73. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  74. pos(elements[a[edgeIndex]], posA);
  75. pos(elements[b[edgeIndex]], posB);
  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 metallic coordinations and hydrogen bonds with dashed cylinders
  82. return LinkStyle.Dashed;
  83. } else if (o === 3) {
  84. return mbOff ? LinkStyle.Solid :
  85. mbSymmetric ? LinkStyle.Triple :
  86. LinkStyle.OffsetTriple;
  87. } else if (aromaticBonds) {
  88. const aI = a[edgeIndex], bI = b[edgeIndex];
  89. const aR = elementAromaticRingIndices.get(aI);
  90. const bR = elementAromaticRingIndices.get(bI);
  91. const arCount = (aR && bR) ? arrayIntersectionSize(aR, bR) : 0;
  92. if (isBondType(f, BondType.Flag.Aromatic) || (arCount && !ignoreComputedAromatic)) {
  93. if (arCount === 2) {
  94. return LinkStyle.MirroredAromatic;
  95. } else {
  96. return LinkStyle.Aromatic;
  97. }
  98. }
  99. }
  100. return (o !== 2 || mbOff) ? LinkStyle.Solid :
  101. mbSymmetric ? LinkStyle.Double :
  102. LinkStyle.OffsetDouble;
  103. },
  104. radius: (edgeIndex: number) => {
  105. location.element = elements[a[edgeIndex]];
  106. const sizeA = theme.size.size(location);
  107. location.element = elements[b[edgeIndex]];
  108. const sizeB = theme.size.size(location);
  109. return Math.min(sizeA, sizeB) * sizeFactor;
  110. },
  111. ignore: makeIntraBondIgnoreTest(structure, unit, props)
  112. };
  113. const { lines: l, boundingSphere } = createLinkLines(ctx, builderProps, props, lines);
  114. if (boundingSphere) {
  115. l.setBoundingSphere(boundingSphere);
  116. } else if (l.lineCount > 0) {
  117. const sphere = Sphere3D.expand(Sphere3D(), (childUnit ?? unit).boundary.sphere, 1 * sizeFactor);
  118. l.setBoundingSphere(sphere);
  119. }
  120. return l;
  121. }
  122. export const IntraUnitBondLineParams = {
  123. ...UnitsLinesParams,
  124. ...BondLineParams,
  125. includeParent: PD.Boolean(false),
  126. };
  127. export type IntraUnitBondLineParams = typeof IntraUnitBondLineParams
  128. export function IntraUnitBondLineVisual(materialId: number): UnitsVisual<IntraUnitBondLineParams> {
  129. return UnitsLinesVisual<IntraUnitBondLineParams>({
  130. defaultProps: PD.getDefaultValues(IntraUnitBondLineParams),
  131. createGeometry: createIntraUnitBondLines,
  132. createLocationIterator: BondIterator.fromGroup,
  133. getLoci: getIntraBondLoci,
  134. eachLocation: eachIntraBond,
  135. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitBondLineParams>, currentProps: PD.Values<IntraUnitBondLineParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  136. state.createGeometry = (
  137. newProps.sizeFactor !== currentProps.sizeFactor ||
  138. newProps.linkScale !== currentProps.linkScale ||
  139. newProps.linkSpacing !== currentProps.linkSpacing ||
  140. newProps.aromaticDashCount !== currentProps.aromaticDashCount ||
  141. newProps.dashCount !== currentProps.dashCount ||
  142. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  143. newProps.onlyPolarHydrogens !== currentProps.onlyPolarHydrogens ||
  144. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  145. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) ||
  146. newProps.aromaticBonds !== currentProps.aromaticBonds ||
  147. newProps.multipleBonds !== currentProps.multipleBonds
  148. );
  149. const newUnit = newStructureGroup.group.units[0];
  150. const currentUnit = currentStructureGroup.group.units[0];
  151. if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) {
  152. if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) {
  153. state.createGeometry = true;
  154. state.updateTransform = true;
  155. state.updateColor = true;
  156. state.updateSize = true;
  157. }
  158. }
  159. }
  160. }, materialId);
  161. }