link.ts 6.1 KB

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