bond.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Copyright (c) 2018-2019 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 { BondType } from '../../../../mol-model/structure/model/types';
  8. import { Unit, StructureElement, Structure, Bond } 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 '../../../visual';
  16. import { StructureGroup } from '../../units-visual';
  17. export const BondCylinderParams = {
  18. bondScale: PD.Numeric(0.4, { min: 0, max: 1, step: 0.1 }),
  19. bondSpacing: PD.Numeric(1, { min: 0, max: 2, step: 0.01 }),
  20. bondCap: PD.Boolean(false),
  21. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }),
  22. includeTypes: PD.MultiSelect(Object.keys(BondType.Names) as BondType.Names[], PD.objectToOptions(BondType.Names)),
  23. excludeTypes: PD.MultiSelect([] as BondType.Names[], PD.objectToOptions(BondType.Names)),
  24. }
  25. export const DefaultBondCylinderProps = PD.getDefaultValues(BondCylinderParams)
  26. export type BondCylinderProps = typeof DefaultBondCylinderProps
  27. export function ignoreBondType(include: BondType.Flag, exclude: BondType.Flag, f: BondType.Flag) {
  28. return !BondType.is(include, f) || BondType.is(exclude, f)
  29. }
  30. const tmpShiftV12 = Vec3.zero()
  31. const tmpShiftV13 = Vec3.zero()
  32. /** Calculate 'shift' direction that is perpendiculat to v1 - v2 and goes through v3 */
  33. export function calculateShiftDir (out: Vec3, v1: Vec3, v2: Vec3, v3: Vec3 | null) {
  34. Vec3.normalize(tmpShiftV12, Vec3.sub(tmpShiftV12, v1, v2))
  35. if (v3 !== null) {
  36. Vec3.sub(tmpShiftV13, v1, v3)
  37. } else {
  38. Vec3.copy(tmpShiftV13, v1) // no reference point, use v1
  39. }
  40. Vec3.normalize(tmpShiftV13, tmpShiftV13)
  41. // ensure v13 and v12 are not colinear
  42. let dp = Vec3.dot(tmpShiftV12, tmpShiftV13)
  43. if (1 - Math.abs(dp) < 1e-5) {
  44. Vec3.set(tmpShiftV13, 1, 0, 0)
  45. dp = Vec3.dot(tmpShiftV12, tmpShiftV13)
  46. if (1 - Math.abs(dp) < 1e-5) {
  47. Vec3.set(tmpShiftV13, 0, 1, 0)
  48. dp = Vec3.dot(tmpShiftV12, tmpShiftV13)
  49. }
  50. }
  51. Vec3.setMagnitude(tmpShiftV12, tmpShiftV12, dp)
  52. Vec3.sub(tmpShiftV13, tmpShiftV13, tmpShiftV12)
  53. return Vec3.normalize(out, tmpShiftV13)
  54. }
  55. export interface BondCylinderMeshBuilderProps {
  56. bondCount: number
  57. referencePosition(edgeIndex: number): Vec3 | null
  58. position(posA: Vec3, posB: Vec3, edgeIndex: number): void
  59. order(edgeIndex: number): number
  60. flags(edgeIndex: number): BondType
  61. radius(edgeIndex: number): number
  62. ignore(edgeIndex: number): boolean
  63. }
  64. /**
  65. * Each edge is included twice to allow for coloring/picking
  66. * the half closer to the first vertex, i.e. vertex a.
  67. */
  68. export function createBondCylinderMesh(ctx: VisualContext, bondBuilder: BondCylinderMeshBuilderProps, props: BondCylinderProps, mesh?: Mesh) {
  69. const { bondCount, referencePosition, position, order, flags, radius, ignore } = bondBuilder
  70. if (!bondCount) return Mesh.createEmpty(mesh)
  71. const { bondScale, bondSpacing, radialSegments, bondCap } = props
  72. const vertexCountEstimate = radialSegments * 2 * bondCount * 2
  73. const builderState = MeshBuilder.createState(vertexCountEstimate, vertexCountEstimate / 4, mesh)
  74. const va = Vec3.zero()
  75. const vb = Vec3.zero()
  76. const vShift = Vec3.zero()
  77. const cylinderProps: CylinderProps = {
  78. radiusTop: 1,
  79. radiusBottom: 1,
  80. radialSegments,
  81. topCap: bondCap,
  82. bottomCap: bondCap
  83. }
  84. for (let edgeIndex = 0, _eI = bondCount; edgeIndex < _eI; ++edgeIndex) {
  85. if (ignore(edgeIndex)) continue
  86. position(va, vb, edgeIndex)
  87. const linkRadius = radius(edgeIndex)
  88. const o = order(edgeIndex)
  89. const f = flags(edgeIndex)
  90. builderState.currentGroup = edgeIndex
  91. if (BondType.is(f, BondType.Flag.MetallicCoordination) || BondType.is(f, BondType.Flag.HydrogenBond)) {
  92. // show metall coordinations and hydrogen bonds with dashed cylinders
  93. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius / 3
  94. cylinderProps.topCap = cylinderProps.bottomCap = true
  95. addFixedCountDashedCylinder(builderState, va, vb, 0.5, 7, cylinderProps)
  96. } else if (o === 2 || o === 3) {
  97. // show bonds with order 2 or 3 using 2 or 3 parallel cylinders
  98. const multiRadius = linkRadius * (bondScale / (0.5 * o))
  99. const absOffset = (linkRadius - multiRadius) * bondSpacing
  100. calculateShiftDir(vShift, va, vb, referencePosition(edgeIndex))
  101. Vec3.setMagnitude(vShift, vShift, absOffset)
  102. cylinderProps.radiusTop = cylinderProps.radiusBottom = multiRadius
  103. cylinderProps.topCap = cylinderProps.bottomCap = bondCap
  104. if (o === 3) addCylinder(builderState, va, vb, 0.5, cylinderProps)
  105. addDoubleCylinder(builderState, va, vb, 0.5, vShift, cylinderProps)
  106. } else {
  107. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius
  108. cylinderProps.topCap = cylinderProps.bottomCap = bondCap
  109. addCylinder(builderState, va, vb, 0.5, cylinderProps)
  110. }
  111. }
  112. return MeshBuilder.getMesh(builderState)
  113. }
  114. export namespace BondIterator {
  115. export function fromGroup(structureGroup: StructureGroup): LocationIterator {
  116. const { group } = structureGroup
  117. const unit = group.units[0]
  118. const groupCount = Unit.isAtomic(unit) ? unit.bonds.edgeCount * 2 : 0
  119. const instanceCount = group.units.length
  120. const location = StructureElement.Location.create()
  121. const getLocation = (groupIndex: number, instanceIndex: number) => {
  122. const unit = group.units[instanceIndex]
  123. location.unit = unit
  124. location.element = unit.elements[(unit as Unit.Atomic).bonds.a[groupIndex]]
  125. return location
  126. }
  127. return LocationIterator(groupCount, instanceCount, getLocation)
  128. }
  129. export function fromStructure(structure: Structure): LocationIterator {
  130. const groupCount = structure.interUnitBonds.edgeCount
  131. const instanceCount = 1
  132. const location = Bond.Location()
  133. const getLocation = (groupIndex: number) => {
  134. const bond = structure.interUnitBonds.edges[groupIndex]
  135. location.aUnit = bond.unitA
  136. location.aIndex = bond.indexA as StructureElement.UnitIndex
  137. location.bUnit = bond.unitB
  138. location.bIndex = bond.indexB as StructureElement.UnitIndex
  139. return location
  140. }
  141. return LocationIterator(groupCount, instanceCount, getLocation, true)
  142. }
  143. }