link.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/visual';
  16. export const LinkCylinderParams = {
  17. linkScale: PD.Numeric(0.4, { min: 0, max: 1, step: 0.1 }),
  18. linkSpacing: PD.Numeric(1, { min: 0, max: 2, step: 0.01 }),
  19. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }),
  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. ignore(edgeIndex: number): boolean
  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 function createLinkCylinderMesh(ctx: VisualContext, linkBuilder: LinkCylinderMeshBuilderProps, props: LinkCylinderProps, mesh?: Mesh) {
  62. const { linkCount, referencePosition, position, order, flags, radius, ignore } = linkBuilder
  63. if (!linkCount) return Mesh.createEmpty(mesh)
  64. const { linkScale, linkSpacing, radialSegments } = props
  65. const vertexCountEstimate = radialSegments * 2 * linkCount * 2
  66. const builderState = MeshBuilder.createState(vertexCountEstimate, vertexCountEstimate / 4, mesh)
  67. const va = Vec3.zero()
  68. const vb = Vec3.zero()
  69. const vShift = Vec3.zero()
  70. const cylinderProps: CylinderProps = {
  71. radiusTop: 1,
  72. radiusBottom: 1,
  73. radialSegments,
  74. topCap: false,
  75. bottomCap: false
  76. }
  77. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  78. if (ignore(edgeIndex)) continue
  79. position(va, vb, edgeIndex)
  80. const linkRadius = radius(edgeIndex)
  81. const o = order(edgeIndex)
  82. const f = flags(edgeIndex)
  83. builderState.currentGroup = edgeIndex
  84. if (LinkType.is(f, LinkType.Flag.MetallicCoordination) || LinkType.is(f, LinkType.Flag.Hydrogen)) {
  85. // show metall coordinations and hydrogen bonds with dashed cylinders
  86. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius / 3
  87. cylinderProps.topCap = cylinderProps.bottomCap = true
  88. addFixedCountDashedCylinder(builderState, va, vb, 0.5, 7, cylinderProps)
  89. } else if (o === 2 || o === 3) {
  90. // show bonds with order 2 or 3 using 2 or 3 parallel cylinders
  91. const multiRadius = linkRadius * (linkScale / (0.5 * o))
  92. const absOffset = (linkRadius - multiRadius) * linkSpacing
  93. calculateShiftDir(vShift, va, vb, referencePosition(edgeIndex))
  94. Vec3.setMagnitude(vShift, vShift, absOffset)
  95. cylinderProps.radiusTop = cylinderProps.radiusBottom = multiRadius
  96. cylinderProps.topCap = cylinderProps.bottomCap = false
  97. if (o === 3) addCylinder(builderState, va, vb, 0.5, cylinderProps)
  98. addDoubleCylinder(builderState, va, vb, 0.5, vShift, cylinderProps)
  99. } else {
  100. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius
  101. cylinderProps.topCap = cylinderProps.bottomCap = false
  102. addCylinder(builderState, va, vb, 0.5, cylinderProps)
  103. }
  104. }
  105. return MeshBuilder.getMesh(builderState)
  106. }
  107. export namespace LinkIterator {
  108. export function fromGroup(group: Unit.SymmetryGroup): LocationIterator {
  109. const unit = group.units[0]
  110. const groupCount = Unit.isAtomic(unit) ? unit.links.edgeCount * 2 : 0
  111. const instanceCount = group.units.length
  112. const location = StructureElement.Location.create()
  113. const getLocation = (groupIndex: number, instanceIndex: number) => {
  114. const unit = group.units[instanceIndex]
  115. location.unit = unit
  116. location.element = unit.elements[(unit as Unit.Atomic).links.a[groupIndex]]
  117. return location
  118. }
  119. return LocationIterator(groupCount, instanceCount, getLocation)
  120. }
  121. export function fromStructure(structure: Structure): LocationIterator {
  122. const groupCount = structure.links.bondCount
  123. const instanceCount = 1
  124. const location = Link.Location()
  125. const getLocation = (groupIndex: number) => {
  126. const bond = structure.links.bonds[groupIndex]
  127. location.aUnit = bond.unitA
  128. location.aIndex = bond.indexA as StructureElement.UnitIndex
  129. location.bUnit = bond.unitB
  130. location.bIndex = bond.indexB as StructureElement.UnitIndex
  131. return location
  132. }
  133. return LocationIterator(groupCount, instanceCount, getLocation, true)
  134. }
  135. }