polymer-tube-mesh.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Copyright (c) 2019-2020 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, StandardTension, StandardShift, NucleicShift, OverhangFactor } from './util/polymer';
  13. import { isNucleic } from '../../../mol-model/structure/model/types';
  14. import { addTube } from '../../../mol-geo/geometry/mesh/builder/tube';
  15. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual } from '../units-visual';
  16. import { VisualUpdateState } from '../../util';
  17. import { addSheet } from '../../../mol-geo/geometry/mesh/builder/sheet';
  18. import { addRibbon } from '../../../mol-geo/geometry/mesh/builder/ribbon';
  19. import { Vec3 } from '../../../mol-math/linear-algebra';
  20. import { addSphere } from '../../../mol-geo/geometry/mesh/builder/sphere';
  21. import { BaseGeometry } from '../../../mol-geo/geometry/base';
  22. import { Sphere3D } from '../../../mol-math/geometry';
  23. export const PolymerTubeMeshParams = {
  24. sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
  25. detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }, BaseGeometry.CustomQualityParamInfo),
  26. linearSegments: PD.Numeric(8, { min: 1, max: 48, step: 1 }, BaseGeometry.CustomQualityParamInfo),
  27. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }, BaseGeometry.CustomQualityParamInfo),
  28. };
  29. export const DefaultPolymerTubeMeshProps = PD.getDefaultValues(PolymerTubeMeshParams);
  30. export type PolymerTubeMeshProps = typeof DefaultPolymerTubeMeshProps
  31. const tmpV1 = Vec3();
  32. function createPolymerTubeMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerTubeMeshProps, mesh?: Mesh) {
  33. const polymerElementCount = unit.polymerElements.length;
  34. if (!polymerElementCount) return Mesh.createEmpty(mesh);
  35. const { sizeFactor, detail, linearSegments, radialSegments } = props;
  36. const vertexCount = linearSegments * radialSegments * polymerElementCount + (radialSegments + 1) * polymerElementCount * 2;
  37. const builderState = MeshBuilder.createState(vertexCount, vertexCount / 10, mesh);
  38. const state = createCurveSegmentState(linearSegments);
  39. const { curvePoints, normalVectors, binormalVectors, widthValues, heightValues } = state;
  40. let i = 0;
  41. const polymerTraceIt = PolymerTraceIterator(unit, structure, { ignoreSecondaryStructure: true });
  42. while (polymerTraceIt.hasNext) {
  43. const v = polymerTraceIt.move();
  44. builderState.currentGroup = i;
  45. const isNucleicType = isNucleic(v.moleculeType);
  46. const shift = isNucleicType ? NucleicShift : StandardShift;
  47. interpolateCurveSegment(state, v, StandardTension, shift);
  48. const startCap = v.coarseBackboneFirst || v.first;
  49. const endCap = v.coarseBackboneLast || v.last;
  50. const s0 = theme.size.size(v.centerPrev) * sizeFactor;
  51. const s1 = theme.size.size(v.center) * sizeFactor;
  52. const s2 = theme.size.size(v.centerNext) * sizeFactor;
  53. interpolateSizes(state, s0, s1, s2, s0, s1, s2, shift);
  54. let segmentCount = linearSegments;
  55. if (v.initial) {
  56. segmentCount = Math.max(Math.round(linearSegments * shift), 1);
  57. const offset = linearSegments - segmentCount;
  58. curvePoints.copyWithin(0, offset * 3);
  59. binormalVectors.copyWithin(0, offset * 3);
  60. normalVectors.copyWithin(0, offset * 3);
  61. widthValues.copyWithin(0, offset * 3);
  62. heightValues.copyWithin(0, offset * 3);
  63. Vec3.fromArray(tmpV1, curvePoints, 3);
  64. Vec3.normalize(tmpV1, Vec3.sub(tmpV1, v.p2, tmpV1));
  65. Vec3.scaleAndAdd(tmpV1, v.p2, tmpV1, s1 * OverhangFactor);
  66. Vec3.toArray(tmpV1, curvePoints, 0);
  67. } else if (v.final) {
  68. segmentCount = Math.max(Math.round(linearSegments * (1 - shift)), 1);
  69. Vec3.fromArray(tmpV1, curvePoints, segmentCount * 3 - 3);
  70. Vec3.normalize(tmpV1, Vec3.sub(tmpV1, v.p2, tmpV1));
  71. Vec3.scaleAndAdd(tmpV1, v.p2, tmpV1, s1 * OverhangFactor);
  72. Vec3.toArray(tmpV1, curvePoints, segmentCount * 3);
  73. }
  74. if (v.initial === true && v.final === true) {
  75. addSphere(builderState, v.p2, s1 * 2, detail);
  76. } else if (radialSegments === 2) {
  77. addRibbon(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, 0);
  78. } else if (radialSegments === 4) {
  79. addSheet(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, widthValues, heightValues, 0, startCap, endCap);
  80. } else {
  81. addTube(builderState, curvePoints, normalVectors, binormalVectors, segmentCount, radialSegments, widthValues, heightValues, startCap, endCap, 'elliptical');
  82. }
  83. ++i;
  84. }
  85. const m = MeshBuilder.getMesh(builderState);
  86. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  87. m.setBoundingSphere(sphere);
  88. return m;
  89. }
  90. export const PolymerTubeParams = {
  91. ...UnitsMeshParams,
  92. ...PolymerTubeMeshParams
  93. };
  94. export type PolymerTubeParams = typeof PolymerTubeParams
  95. export function PolymerTubeVisual(materialId: number): UnitsVisual<PolymerTubeParams> {
  96. return UnitsMeshVisual<PolymerTubeParams>({
  97. defaultProps: PD.getDefaultValues(PolymerTubeParams),
  98. createGeometry: createPolymerTubeMesh,
  99. createLocationIterator: PolymerLocationIterator.fromGroup,
  100. getLoci: getPolymerElementLoci,
  101. eachLocation: eachPolymerElement,
  102. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerTubeParams>, currentProps: PD.Values<PolymerTubeParams>) => {
  103. state.createGeometry = (
  104. newProps.sizeFactor !== currentProps.sizeFactor ||
  105. newProps.detail !== currentProps.detail ||
  106. newProps.linearSegments !== currentProps.linearSegments ||
  107. newProps.radialSegments !== currentProps.radialSegments
  108. );
  109. }
  110. }, materialId);
  111. }