polymer-backbone-cylinder.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, StructureElement, ElementIndex } 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 { Vec3 } from '../../../mol-math/linear-algebra';
  13. import { CylinderProps } from '../../../mol-geo/primitive/cylinder';
  14. import { eachPolymerElement, getPolymerElementLoci, NucleicShift, PolymerLocationIterator, StandardShift } from './util/polymer';
  15. import { addCylinder } from '../../../mol-geo/geometry/mesh/builder/cylinder';
  16. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual, UnitsCylindersVisual, UnitsCylindersParams } from '../units-visual';
  17. import { VisualUpdateState } from '../../util';
  18. import { BaseGeometry } from '../../../mol-geo/geometry/base';
  19. import { Sphere3D } from '../../../mol-math/geometry';
  20. import { isNucleic, MoleculeType } from '../../../mol-model/structure/model/types';
  21. import { WebGLContext } from '../../../mol-gl/webgl/context';
  22. import { Cylinders } from '../../../mol-geo/geometry/cylinders/cylinders';
  23. import { CylindersBuilder } from '../../../mol-geo/geometry/cylinders/cylinders-builder';
  24. import { eachPolymerBackboneLink } from './util/polymer/backbone';
  25. import { StructureGroup } from './util/common';
  26. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  27. const v3scale = Vec3.scale;
  28. const v3add = Vec3.add;
  29. const v3sub = Vec3.sub;
  30. export const PolymerBackboneCylinderParams = {
  31. ...UnitsMeshParams,
  32. ...UnitsCylindersParams,
  33. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  34. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }, BaseGeometry.CustomQualityParamInfo),
  35. tryUseImpostor: PD.Boolean(true),
  36. };
  37. export type PolymerBackboneCylinderParams = typeof PolymerBackboneCylinderParams
  38. export function PolymerBackboneCylinderVisual(materialId: number, structure: Structure, props: PD.Values<PolymerBackboneCylinderParams>, webgl?: WebGLContext) {
  39. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
  40. ? PolymerBackboneCylinderImpostorVisual(materialId)
  41. : PolymerBackboneCylinderMeshVisual(materialId);
  42. }
  43. interface PolymerBackboneCylinderProps {
  44. radialSegments: number,
  45. sizeFactor: number,
  46. }
  47. function createPolymerBackboneCylinderImpostor(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerBackboneCylinderProps, cylinders?: Cylinders) {
  48. const polymerElementCount = unit.polymerElements.length;
  49. if (!polymerElementCount) return Cylinders.createEmpty(cylinders);
  50. const cylindersCountEstimate = polymerElementCount * 2;
  51. const builder = CylindersBuilder.create(cylindersCountEstimate, cylindersCountEstimate / 4, cylinders);
  52. const pos = unit.conformation.invariantPosition;
  53. const pA = Vec3();
  54. const pB = Vec3();
  55. const pM = Vec3();
  56. const add = function (indexA: ElementIndex, indexB: ElementIndex, groupA: number, groupB: number, moleculeType: MoleculeType) {
  57. pos(indexA, pA);
  58. pos(indexB, pB);
  59. const isNucleicType = isNucleic(moleculeType);
  60. const shift = isNucleicType ? NucleicShift : StandardShift;
  61. v3add(pM, pA, v3scale(pM, v3sub(pM, pB, pA), shift));
  62. builder.add(pA[0], pA[1], pA[2], pM[0], pM[1], pM[2], 1, false, false, groupA);
  63. builder.add(pM[0], pM[1], pM[2], pB[0], pB[1], pB[2], 1, false, false, groupB);
  64. };
  65. eachPolymerBackboneLink(unit, add);
  66. const c = builder.getCylinders();
  67. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  68. c.setBoundingSphere(sphere);
  69. return c;
  70. }
  71. export function PolymerBackboneCylinderImpostorVisual(materialId: number): UnitsVisual<PolymerBackboneCylinderParams> {
  72. return UnitsCylindersVisual<PolymerBackboneCylinderParams>({
  73. defaultProps: PD.getDefaultValues(PolymerBackboneCylinderParams),
  74. createGeometry: createPolymerBackboneCylinderImpostor,
  75. createLocationIterator: PolymerLocationIterator.fromGroup,
  76. getLoci: getPolymerElementLoci,
  77. eachLocation: eachPolymerElement,
  78. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerBackboneCylinderParams>, currentProps: PD.Values<PolymerBackboneCylinderParams>) => { },
  79. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<PolymerBackboneCylinderParams>, webgl?: WebGLContext) => {
  80. return !props.tryUseImpostor || !webgl;
  81. }
  82. }, materialId);
  83. }
  84. function createPolymerBackboneCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerBackboneCylinderProps, mesh?: Mesh) {
  85. const polymerElementCount = unit.polymerElements.length;
  86. if (!polymerElementCount) return Mesh.createEmpty(mesh);
  87. const { radialSegments, sizeFactor } = props;
  88. const vertexCountEstimate = radialSegments * 2 * polymerElementCount * 2;
  89. const builderState = MeshBuilder.createState(vertexCountEstimate, vertexCountEstimate / 10, mesh);
  90. const pos = unit.conformation.invariantPosition;
  91. const pA = Vec3();
  92. const pB = Vec3();
  93. const cylinderProps: CylinderProps = { radiusTop: 1, radiusBottom: 1, radialSegments };
  94. const centerA = StructureElement.Location.create(structure, unit);
  95. const centerB = StructureElement.Location.create(structure, unit);
  96. const add = function (indexA: ElementIndex, indexB: ElementIndex, groupA: number, groupB: number, moleculeType: MoleculeType) {
  97. centerA.element = indexA;
  98. centerB.element = indexB;
  99. pos(centerA.element, pA);
  100. pos(centerB.element, pB);
  101. const isNucleicType = isNucleic(moleculeType);
  102. const shift = isNucleicType ? NucleicShift : StandardShift;
  103. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerA) * sizeFactor;
  104. builderState.currentGroup = groupA;
  105. addCylinder(builderState, pA, pB, shift, cylinderProps);
  106. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerB) * sizeFactor;
  107. builderState.currentGroup = groupB;
  108. addCylinder(builderState, pB, pA, 1 - shift, cylinderProps);
  109. };
  110. eachPolymerBackboneLink(unit, add);
  111. const m = MeshBuilder.getMesh(builderState);
  112. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  113. m.setBoundingSphere(sphere);
  114. return m;
  115. }
  116. export function PolymerBackboneCylinderMeshVisual(materialId: number): UnitsVisual<PolymerBackboneCylinderParams> {
  117. return UnitsMeshVisual<PolymerBackboneCylinderParams>({
  118. defaultProps: PD.getDefaultValues(PolymerBackboneCylinderParams),
  119. createGeometry: createPolymerBackboneCylinderMesh,
  120. createLocationIterator: PolymerLocationIterator.fromGroup,
  121. getLoci: getPolymerElementLoci,
  122. eachLocation: eachPolymerElement,
  123. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerBackboneCylinderParams>, currentProps: PD.Values<PolymerBackboneCylinderParams>) => {
  124. state.createGeometry = (
  125. newProps.sizeFactor !== currentProps.sizeFactor ||
  126. newProps.radialSegments !== currentProps.radialSegments
  127. );
  128. },
  129. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<PolymerBackboneCylinderParams>, webgl?: WebGLContext) => {
  130. return props.tryUseImpostor && !!webgl;
  131. }
  132. }, materialId);
  133. }