polymer-trace-mesh.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { Unit, Structure } from 'mol-model/structure';
  7. import { UnitsVisual } from '../representation';
  8. import { VisualUpdateState } from '../../util';
  9. import { PolymerTraceIterator, createCurveSegmentState, interpolateCurveSegment, PolymerLocationIterator, getPolymerElementLoci, eachPolymerElement, interpolateSizes } from './util/polymer';
  10. import { SecondaryStructureType, isNucleic } from 'mol-model/structure/model/types';
  11. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  12. import { ParamDefinition as PD } from 'mol-util/param-definition';
  13. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  14. import { MeshBuilder } from 'mol-geo/geometry/mesh/mesh-builder';
  15. import { addSheet } from 'mol-geo/geometry/mesh/builder/sheet';
  16. import { addTube } from 'mol-geo/geometry/mesh/builder/tube';
  17. import { VisualContext } from 'mol-repr/visual';
  18. import { Theme } from 'mol-theme/theme';
  19. export const PolymerTraceMeshParams = {
  20. sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
  21. linearSegments: PD.Numeric(8, { min: 1, max: 48, step: 1 }),
  22. radialSegments: PD.Numeric(16, { min: 3, max: 56, step: 1 }),
  23. aspectRatio: PD.Numeric(5, { min: 0.1, max: 5, step: 0.1 }),
  24. arrowFactor: PD.Numeric(1.5, { min: 0.1, max: 5, step: 0.1 }),
  25. }
  26. export const DefaultPolymerTraceMeshProps = PD.getDefaultValues(PolymerTraceMeshParams)
  27. export type PolymerTraceMeshProps = typeof DefaultPolymerTraceMeshProps
  28. // TODO handle polymer ends properly
  29. function createPolymerTraceMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerTraceMeshProps, mesh?: Mesh) {
  30. const polymerElementCount = unit.polymerElements.length
  31. if (!polymerElementCount) return Mesh.createEmpty(mesh)
  32. const { sizeFactor, linearSegments, radialSegments, aspectRatio, arrowFactor } = props
  33. const vertexCount = linearSegments * radialSegments * polymerElementCount + (radialSegments + 1) * polymerElementCount * 2
  34. const builderState = MeshBuilder.createState(vertexCount, vertexCount / 10, mesh)
  35. const isCoarse = Unit.isCoarse(unit)
  36. const state = createCurveSegmentState(linearSegments)
  37. const { curvePoints, normalVectors, binormalVectors, widthValues, heightValues } = state
  38. let i = 0
  39. const polymerTraceIt = PolymerTraceIterator(unit)
  40. while (polymerTraceIt.hasNext) {
  41. const v = polymerTraceIt.move()
  42. builderState.currentGroup = i
  43. const isNucleicType = isNucleic(v.moleculeType)
  44. const isSheet = SecondaryStructureType.is(v.secStrucType, SecondaryStructureType.Flag.Beta)
  45. const isHelix = SecondaryStructureType.is(v.secStrucType, SecondaryStructureType.Flag.Helix)
  46. const tension = isNucleicType ? 0.5 : 0.9
  47. const shift = isNucleicType ? 0.3 : 0.5
  48. interpolateCurveSegment(state, v, tension, shift)
  49. let w0 = theme.size.size(v.centerPrev) * sizeFactor
  50. let w1 = theme.size.size(v.center) * sizeFactor
  51. let w2 = theme.size.size(v.centerNext) * sizeFactor
  52. if (isCoarse) {
  53. w0 *= aspectRatio / 2
  54. w1 *= aspectRatio / 2
  55. w2 *= aspectRatio / 2
  56. }
  57. if (isSheet) {
  58. const h1 = w1 * aspectRatio
  59. const arrowHeight = v.secStrucLast ? h1 * arrowFactor : 0
  60. addSheet(builderState, curvePoints, normalVectors, binormalVectors, linearSegments, w1, h1, arrowHeight, v.secStrucFirst, v.secStrucLast)
  61. } else {
  62. let h0: number, h1: number, h2: number
  63. if (isHelix && !v.isCoarseBackbone) {
  64. h0 = w0 * aspectRatio
  65. h1 = w1 * aspectRatio
  66. h2 = w2 * aspectRatio
  67. } else if (isNucleicType && !v.isCoarseBackbone) {
  68. h0 = w0 * aspectRatio;
  69. [w0, h0] = [h0, w0]
  70. h1 = w1 * aspectRatio;
  71. [w1, h1] = [h1, w1]
  72. h2 = w2 * aspectRatio;
  73. [w2, h2] = [h2, w2]
  74. } else {
  75. h0 = w0
  76. h1 = w1
  77. h2 = w2
  78. }
  79. interpolateSizes(state, w0, w1, w2, h0, h1, h2, shift)
  80. addTube(builderState, curvePoints, normalVectors, binormalVectors, linearSegments, radialSegments, widthValues, heightValues, 1, v.secStrucFirst || v.coarseBackboneFirst, v.secStrucLast || v.coarseBackboneLast)
  81. }
  82. ++i
  83. }
  84. return MeshBuilder.getMesh(builderState)
  85. }
  86. export const PolymerTraceParams = {
  87. ...UnitsMeshParams,
  88. ...PolymerTraceMeshParams
  89. }
  90. export type PolymerTraceParams = typeof PolymerTraceParams
  91. export function PolymerTraceVisual(): UnitsVisual<PolymerTraceParams> {
  92. return UnitsMeshVisual<PolymerTraceParams>({
  93. defaultProps: PD.getDefaultValues(PolymerTraceParams),
  94. createGeometry: createPolymerTraceMesh,
  95. createLocationIterator: PolymerLocationIterator.fromGroup,
  96. getLoci: getPolymerElementLoci,
  97. eachLocation: eachPolymerElement,
  98. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerTraceParams>, currentProps: PD.Values<PolymerTraceParams>) => {
  99. state.createGeometry = (
  100. newProps.sizeFactor !== currentProps.sizeFactor ||
  101. newProps.linearSegments !== currentProps.linearSegments ||
  102. newProps.radialSegments !== currentProps.radialSegments ||
  103. newProps.aspectRatio !== currentProps.aspectRatio ||
  104. newProps.arrowFactor !== currentProps.arrowFactor
  105. )
  106. }
  107. })
  108. }