polymer-trace-mesh.ts 9.9 KB

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