link.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 '../../../../shape/mesh';
  9. import { MeshBuilder } from '../../../../shape/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. export const DefaultLinkCylinderProps = {
  15. ...DefaultMeshProps,
  16. sizeTheme: { name: 'uniform', value: 0.15 } as SizeThemeProps,
  17. linkScale: 0.4,
  18. linkSpacing: 1,
  19. linkRadius: 0.25,
  20. radialSegments: 16
  21. }
  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: RuntimeContext, 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.setId(edgeIndex)
  76. if (LinkType.is(f, LinkType.Flag.MetallicCoordination)) {
  77. // show metall coordinations with dashed cylinders
  78. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius / 3
  79. meshBuilder.addFixedCountDashedCylinder(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) meshBuilder.addCylinder(va, vb, 0.5, cylinderProps)
  88. meshBuilder.addDoubleCylinder(va, vb, 0.5, vShift, cylinderProps)
  89. } else {
  90. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius
  91. meshBuilder.addCylinder(va, vb, 0.5, cylinderProps)
  92. }
  93. if (edgeIndex % 10000 === 0 && ctx.shouldUpdate) {
  94. await ctx.update({ message: 'Cylinder mesh', current: edgeIndex, max: linkCount });
  95. }
  96. }
  97. return meshBuilder.getMesh()
  98. }