polymer-trace-mesh.ts 11 KB

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