link.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Vec3 } from 'mol-math/linear-algebra';
  7. import { LinkType } from 'mol-model/structure/model/types';
  8. import { Unit, StructureElement, Structure, Link } from 'mol-model/structure';
  9. import { ParamDefinition as PD } from 'mol-util/param-definition';
  10. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  11. import { MeshBuilder } from 'mol-geo/geometry/mesh/mesh-builder';
  12. import { CylinderProps } from 'mol-geo/primitive/cylinder';
  13. import { addFixedCountDashedCylinder, addCylinder, addDoubleCylinder } from 'mol-geo/geometry/mesh/builder/cylinder';
  14. import { LocationIterator } from 'mol-geo/util/location-iterator';
  15. import { VisualContext } from 'mol-repr/representation';
  16. export const LinkCylinderParams = {
  17. linkScale: PD.Range('Link Scale', '', 0.4, 0, 1, 0.1),
  18. linkSpacing: PD.Range('Link Spacing', '', 1, 0, 2, 0.01),
  19. radialSegments: PD.Numeric('Radial Segments', '', 16, 3, 56, 1),
  20. }
  21. export const DefaultLinkCylinderProps = PD.getDefaultValues(LinkCylinderParams)
  22. export type LinkCylinderProps = typeof DefaultLinkCylinderProps
  23. const tmpShiftV12 = Vec3.zero()
  24. const tmpShiftV13 = Vec3.zero()
  25. /** Calculate 'shift' direction that is perpendiculat to v1 - v2 and goes through v3 */
  26. export function calculateShiftDir (out: Vec3, v1: Vec3, v2: Vec3, v3: Vec3 | null) {
  27. Vec3.normalize(tmpShiftV12, Vec3.sub(tmpShiftV12, v1, v2))
  28. if (v3 !== null) {
  29. Vec3.sub(tmpShiftV13, v1, v3)
  30. } else {
  31. Vec3.copy(tmpShiftV13, v1) // no reference point, use v1
  32. }
  33. Vec3.normalize(tmpShiftV13, tmpShiftV13)
  34. // ensure v13 and v12 are not colinear
  35. let dp = Vec3.dot(tmpShiftV12, tmpShiftV13)
  36. if (1 - Math.abs(dp) < 1e-5) {
  37. Vec3.set(tmpShiftV13, 1, 0, 0)
  38. dp = Vec3.dot(tmpShiftV12, tmpShiftV13)
  39. if (1 - Math.abs(dp) < 1e-5) {
  40. Vec3.set(tmpShiftV13, 0, 1, 0)
  41. dp = Vec3.dot(tmpShiftV12, tmpShiftV13)
  42. }
  43. }
  44. Vec3.setMagnitude(tmpShiftV12, tmpShiftV12, dp)
  45. Vec3.sub(tmpShiftV13, tmpShiftV13, tmpShiftV12)
  46. return Vec3.normalize(out, tmpShiftV13)
  47. }
  48. export interface LinkCylinderMeshBuilderProps {
  49. linkCount: number
  50. referencePosition(edgeIndex: number): Vec3 | null
  51. position(posA: Vec3, posB: Vec3, edgeIndex: number): void
  52. order(edgeIndex: number): number
  53. flags(edgeIndex: number): LinkType
  54. radius(edgeIndex: number): number
  55. }
  56. /**
  57. * Each edge is included twice to allow for coloring/picking
  58. * the half closer to the first vertex, i.e. vertex a.
  59. */
  60. export async function createLinkCylinderMesh(ctx: VisualContext, linkBuilder: LinkCylinderMeshBuilderProps, props: LinkCylinderProps, mesh?: Mesh) {
  61. const { linkCount, referencePosition, position, order, flags, radius } = linkBuilder
  62. if (!linkCount) return Mesh.createEmpty(mesh)
  63. const { linkScale, linkSpacing, radialSegments } = props
  64. const vertexCountEstimate = radialSegments * 2 * linkCount * 2
  65. const meshBuilder = MeshBuilder.create(vertexCountEstimate, vertexCountEstimate / 4, mesh)
  66. const va = Vec3.zero()
  67. const vb = Vec3.zero()
  68. const vShift = Vec3.zero()
  69. const cylinderProps: CylinderProps = { radiusTop: 1, radiusBottom: 1, radialSegments }
  70. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  71. position(va, vb, edgeIndex)
  72. const linkRadius = radius(edgeIndex)
  73. const o = order(edgeIndex)
  74. const f = flags(edgeIndex)
  75. meshBuilder.setGroup(edgeIndex)
  76. if (LinkType.is(f, LinkType.Flag.MetallicCoordination) || LinkType.is(f, LinkType.Flag.Hydrogen)) {
  77. // show metall coordinations and hydrogen bonds with dashed cylinders
  78. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius / 3
  79. addFixedCountDashedCylinder(meshBuilder, va, vb, 0.5, 7, cylinderProps)
  80. } else if (o === 2 || o === 3) {
  81. // show bonds with order 2 or 3 using 2 or 3 parallel cylinders
  82. const multiRadius = linkRadius * (linkScale / (0.5 * o))
  83. const absOffset = (linkRadius - multiRadius) * linkSpacing
  84. calculateShiftDir(vShift, va, vb, referencePosition(edgeIndex))
  85. Vec3.setMagnitude(vShift, vShift, absOffset)
  86. cylinderProps.radiusTop = cylinderProps.radiusBottom = multiRadius
  87. if (o === 3) addCylinder(meshBuilder, va, vb, 0.5, cylinderProps)
  88. addDoubleCylinder(meshBuilder, va, vb, 0.5, vShift, cylinderProps)
  89. } else {
  90. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius
  91. addCylinder(meshBuilder, va, vb, 0.5, cylinderProps)
  92. }
  93. if (edgeIndex % 10000 === 0 && ctx.runtime.shouldUpdate) {
  94. await ctx.runtime.update({ message: 'Cylinder mesh', current: edgeIndex, max: linkCount });
  95. }
  96. }
  97. return meshBuilder.getMesh()
  98. }
  99. export namespace LinkIterator {
  100. export function fromGroup(group: Unit.SymmetryGroup): LocationIterator {
  101. const unit = group.units[0]
  102. const groupCount = Unit.isAtomic(unit) ? unit.links.edgeCount * 2 : 0
  103. const instanceCount = group.units.length
  104. const location = StructureElement.create()
  105. const getLocation = (groupIndex: number, instanceIndex: number) => {
  106. const unit = group.units[instanceIndex]
  107. location.unit = unit
  108. location.element = unit.elements[(unit as Unit.Atomic).links.a[groupIndex]]
  109. return location
  110. }
  111. return LocationIterator(groupCount, instanceCount, getLocation)
  112. }
  113. export function fromStructure(structure: Structure): LocationIterator {
  114. const groupCount = structure.links.bondCount
  115. const instanceCount = 1
  116. const location = Link.Location()
  117. const getLocation = (groupIndex: number) => {
  118. const bond = structure.links.bonds[groupIndex]
  119. location.aUnit = bond.unitA
  120. location.aIndex = bond.indexA as StructureElement.UnitIndex
  121. location.bUnit = bond.unitB
  122. location.bIndex = bond.indexB as StructureElement.UnitIndex
  123. return location
  124. }
  125. return LocationIterator(groupCount, instanceCount, getLocation, true)
  126. }
  127. }