link.ts 6.1 KB

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