polymer-trace-mesh.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { VisualContext } from '../../visual';
  8. import { Unit, Structure } from '../../../mol-model/structure';
  9. import { Theme } from '../../../mol-theme/theme';
  10. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  11. import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder';
  12. import { createCurveSegmentState, PolymerTraceIterator, interpolateCurveSegment, interpolateSizes, PolymerLocationIterator, getPolymerElementLoci, eachPolymerElement, HelixTension, NucleicShift, StandardShift, StandardTension, OverhangFactor } from './util/polymer';
  13. import { isNucleic, SecondaryStructureType } from '../../../mol-model/structure/model/types';
  14. import { addSheet } from '../../../mol-geo/geometry/mesh/builder/sheet';
  15. import { addTube } from '../../../mol-geo/geometry/mesh/builder/tube';
  16. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual, StructureGroup } from '../units-visual';
  17. import { VisualUpdateState } from '../../util';
  18. import { SecondaryStructureProvider } from '../../../mol-model-props/computed/secondary-structure';
  19. import { addRibbon } from '../../../mol-geo/geometry/mesh/builder/ribbon';
  20. import { addSphere } from '../../../mol-geo/geometry/mesh/builder/sphere';
  21. import { Vec3 } from '../../../mol-math/linear-algebra';
  22. export const PolymerTraceMeshParams = {
  23. sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
  24. detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }),
  25. linearSegments: PD.Numeric(8, { min: 1, max: 48, step: 1 }),
  26. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }),
  27. aspectRatio: PD.Numeric(5, { min: 0.1, max: 10, step: 0.1 }),
  28. arrowFactor: PD.Numeric(1.5, { min: 0, max: 3, step: 0.1 }),
  29. }
  30. export const DefaultPolymerTraceMeshProps = PD.getDefaultValues(PolymerTraceMeshParams)
  31. export type PolymerTraceMeshProps = typeof DefaultPolymerTraceMeshProps
  32. const tmpV1 = Vec3()
  33. function createPolymerTraceMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerTraceMeshProps, mesh?: Mesh) {
  34. const polymerElementCount = unit.polymerElements.length
  35. if (!polymerElementCount) return Mesh.createEmpty(mesh)
  36. const { sizeFactor, detail, linearSegments, radialSegments, aspectRatio, arrowFactor } = props
  37. const vertexCount = linearSegments * radialSegments * polymerElementCount + (radialSegments + 1) * polymerElementCount * 2
  38. const builderState = MeshBuilder.createState(vertexCount, vertexCount / 10, mesh)
  39. const isCoarse = Unit.isCoarse(unit)
  40. const state = createCurveSegmentState(linearSegments)
  41. const { curvePoints, normalVectors, binormalVectors, widthValues, heightValues } = state
  42. let i = 0
  43. const polymerTraceIt = PolymerTraceIterator(unit, structure)
  44. while (polymerTraceIt.hasNext) {
  45. const v = polymerTraceIt.move()
  46. builderState.currentGroup = i
  47. const isNucleicType = isNucleic(v.moleculeType)
  48. const isSheet = SecondaryStructureType.is(v.secStrucType, SecondaryStructureType.Flag.Beta)
  49. const isHelix = SecondaryStructureType.is(v.secStrucType, SecondaryStructureType.Flag.Helix)
  50. const tension = isHelix ? HelixTension : StandardTension
  51. const shift = isNucleicType ? NucleicShift : StandardShift
  52. interpolateCurveSegment(state, v, tension, shift)
  53. let w0 = theme.size.size(v.centerPrev) * sizeFactor
  54. let w1 = theme.size.size(v.center) * sizeFactor
  55. let w2 = theme.size.size(v.centerNext) * sizeFactor
  56. if (isCoarse) {
  57. w0 *= aspectRatio / 2
  58. w1 *= aspectRatio / 2
  59. w2 *= aspectRatio / 2
  60. }
  61. const startCap = v.secStrucFirst || v.coarseBackboneFirst || v.first
  62. const endCap = v.secStrucLast || v.coarseBackboneLast || v.last
  63. let segmentCount = linearSegments
  64. if (v.initial) {
  65. segmentCount = Math.max(Math.round(linearSegments * shift), 1)
  66. const offset = linearSegments - segmentCount
  67. curvePoints.copyWithin(0, offset * 3)
  68. binormalVectors.copyWithin(0, offset * 3)
  69. normalVectors.copyWithin(0, offset * 3)
  70. Vec3.fromArray(tmpV1, curvePoints, 3)
  71. Vec3.normalize(tmpV1, Vec3.sub(tmpV1, v.p2, tmpV1))
  72. Vec3.scaleAndAdd(tmpV1, v.p2, tmpV1, w1 * OverhangFactor)
  73. Vec3.toArray(tmpV1, curvePoints, 0)
  74. } else if (v.final) {
  75. segmentCount = Math.max(Math.round(linearSegments * (1 - shift)), 1)
  76. Vec3.fromArray(tmpV1, curvePoints, segmentCount * 3 - 3)
  77. Vec3.normalize(tmpV1, Vec3.sub(tmpV1, v.p2, tmpV1))
  78. Vec3.scaleAndAdd(tmpV1, v.p2, tmpV1, w1 * OverhangFactor)
  79. Vec3.toArray(tmpV1, curvePoints, segmentCount * 3)
  80. }
  81. if (v.initial === true && v.final === true) {
  82. addSphere(builderState, v.p2, w1 * 2, detail)
  83. } else if (isSheet) {
  84. const h0 = w0 * aspectRatio
  85. const h1 = w1 * aspectRatio
  86. const h2 = w2 * aspectRatio
  87. const arrowHeight = v.secStrucLast ? h1 * arrowFactor : 0
  88. interpolateSizes(state, w0, w1, w2, h0, h1, h2, shift)
  89. if (radialSegments === 2) {
  90. addRibbon(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, arrowHeight)
  91. } else {
  92. addSheet(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, arrowHeight, startCap, endCap)
  93. }
  94. } else {
  95. let h0: number, h1: number, h2: number
  96. if (isHelix && !v.isCoarseBackbone) {
  97. h0 = w0 * aspectRatio
  98. h1 = w1 * aspectRatio
  99. h2 = w2 * aspectRatio
  100. } else if (isNucleicType && !v.isCoarseBackbone) {
  101. h0 = w0 * aspectRatio;
  102. h1 = w1 * aspectRatio;
  103. h2 = w2 * aspectRatio;
  104. [w0, h0] = [h0, w0];
  105. [w1, h1] = [h1, w1];
  106. [w2, h2] = [h2, w2];
  107. } else {
  108. h0 = w0
  109. h1 = w1
  110. h2 = w2
  111. }
  112. interpolateSizes(state, w0, w1, w2, h0, h1, h2, shift)
  113. if (radialSegments === 2) {
  114. if (isNucleicType && !v.isCoarseBackbone) {
  115. // TODO find a cleaner way to swap normal and binormal for nucleic types
  116. for (let i = 0, il = binormalVectors.length; i < il; i++) binormalVectors[i] *= -1
  117. addRibbon(builderState, curvePoints, binormalVectors, normalVectors, segmentCount, heightValues, widthValues, 0)
  118. } else {
  119. addRibbon(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, 0)
  120. }
  121. } else if (radialSegments === 4) {
  122. addSheet(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, 0, startCap, endCap)
  123. } else {
  124. addTube(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, radialSegments, widthValues, heightValues, 1, startCap, endCap)
  125. }
  126. }
  127. ++i
  128. }
  129. return MeshBuilder.getMesh(builderState)
  130. }
  131. export const PolymerTraceParams = {
  132. ...UnitsMeshParams,
  133. ...PolymerTraceMeshParams
  134. }
  135. export type PolymerTraceParams = typeof PolymerTraceParams
  136. export function PolymerTraceVisual(materialId: number): UnitsVisual<PolymerTraceParams> {
  137. return UnitsMeshVisual<PolymerTraceParams>({
  138. defaultProps: PD.getDefaultValues(PolymerTraceParams),
  139. createGeometry: createPolymerTraceMesh,
  140. createLocationIterator: PolymerLocationIterator.fromGroup,
  141. getLoci: getPolymerElementLoci,
  142. eachLocation: eachPolymerElement,
  143. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerTraceParams>, currentProps: PD.Values<PolymerTraceParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  144. state.createGeometry = (
  145. newProps.sizeFactor !== currentProps.sizeFactor ||
  146. newProps.detail !== currentProps.detail ||
  147. newProps.linearSegments !== currentProps.linearSegments ||
  148. newProps.radialSegments !== currentProps.radialSegments ||
  149. newProps.aspectRatio !== currentProps.aspectRatio ||
  150. newProps.arrowFactor !== currentProps.arrowFactor
  151. )
  152. const secondaryStructureHash = SecondaryStructureProvider.get(newStructureGroup.structure).version
  153. if ((state.info.secondaryStructureHash as number) !== secondaryStructureHash) {
  154. state.createGeometry = true;
  155. state.info.secondaryStructureHash = secondaryStructureHash
  156. }
  157. }
  158. }, materialId)
  159. }