molecular-surface-mesh.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright (c) 2019-2022 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 { UnitsMeshParams, UnitsVisual, UnitsMeshVisual } from '../units-visual';
  8. import { MolecularSurfaceCalculationParams } from '../../../mol-math/geometry/molecular-surface';
  9. import { VisualContext } from '../../visual';
  10. import { Unit, Structure } from '../../../mol-model/structure';
  11. import { Theme } from '../../../mol-theme/theme';
  12. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  13. import { computeStructureMolecularSurface, computeUnitMolecularSurface } from './util/molecular-surface';
  14. import { computeMarchingCubesMesh } from '../../../mol-geo/util/marching-cubes/algorithm';
  15. import { ElementIterator, getElementLoci, eachElement, getSerialElementLoci, eachSerialElement } from './util/element';
  16. import { VisualUpdateState } from '../../util';
  17. import { CommonSurfaceParams } from './util/common';
  18. import { Sphere3D } from '../../../mol-math/geometry';
  19. import { MeshValues } from '../../../mol-gl/renderable/mesh';
  20. import { Texture } from '../../../mol-gl/webgl/texture';
  21. import { WebGLContext } from '../../../mol-gl/webgl/context';
  22. import { applyMeshColorSmoothing } from '../../../mol-geo/geometry/mesh/color-smoothing';
  23. import { ColorSmoothingParams, getColorSmoothingProps } from '../../../mol-geo/geometry/base';
  24. import { ValueCell } from '../../../mol-util';
  25. import { ComplexMeshVisual, ComplexVisual } from '../complex-visual';
  26. export const MolecularSurfaceMeshParams = {
  27. ...UnitsMeshParams,
  28. ...MolecularSurfaceCalculationParams,
  29. ...CommonSurfaceParams,
  30. ...ColorSmoothingParams,
  31. };
  32. export type MolecularSurfaceMeshParams = typeof MolecularSurfaceMeshParams
  33. export type MolecularSurfaceMeshProps = PD.Values<MolecularSurfaceMeshParams>
  34. type MolecularSurfaceMeta = {
  35. resolution?: number
  36. colorTexture?: Texture
  37. }
  38. //
  39. async function createMolecularSurfaceMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: MolecularSurfaceMeshProps, mesh?: Mesh): Promise<Mesh> {
  40. const { transform, field, idField, resolution, maxRadius } = await computeUnitMolecularSurface(structure, unit, theme.size, props).runInContext(ctx.runtime);
  41. const params = {
  42. isoLevel: props.probeRadius,
  43. scalarField: field,
  44. idField
  45. };
  46. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime);
  47. if (props.includeParent) {
  48. const iterations = Math.ceil(2 / props.resolution);
  49. Mesh.smoothEdges(surface, { iterations, maxNewEdgeLength: Math.sqrt(2) });
  50. }
  51. Mesh.transform(surface, transform);
  52. if (ctx.webgl && !ctx.webgl.isWebGL2) {
  53. Mesh.uniformTriangleGroup(surface);
  54. ValueCell.updateIfChanged(surface.varyingGroup, false);
  55. } else {
  56. ValueCell.updateIfChanged(surface.varyingGroup, true);
  57. }
  58. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, maxRadius);
  59. surface.setBoundingSphere(sphere);
  60. (surface.meta as MolecularSurfaceMeta).resolution = resolution;
  61. return surface;
  62. }
  63. export function MolecularSurfaceMeshVisual(materialId: number): UnitsVisual<MolecularSurfaceMeshParams> {
  64. return UnitsMeshVisual<MolecularSurfaceMeshParams>({
  65. defaultProps: PD.getDefaultValues(MolecularSurfaceMeshParams),
  66. createGeometry: createMolecularSurfaceMesh,
  67. createLocationIterator: ElementIterator.fromGroup,
  68. getLoci: getElementLoci,
  69. eachLocation: eachElement,
  70. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<MolecularSurfaceMeshParams>, currentProps: PD.Values<MolecularSurfaceMeshParams>) => {
  71. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  72. if (newProps.probeRadius !== currentProps.probeRadius) state.createGeometry = true;
  73. if (newProps.probePositions !== currentProps.probePositions) state.createGeometry = true;
  74. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  75. if (newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant) state.createGeometry = true;
  76. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  77. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  78. if (newProps.smoothColors.name !== currentProps.smoothColors.name) {
  79. state.updateColor = true;
  80. } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') {
  81. if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true;
  82. if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true;
  83. }
  84. },
  85. processValues: (values: MeshValues, geometry: Mesh, props: PD.Values<MolecularSurfaceMeshParams>, theme: Theme, webgl?: WebGLContext) => {
  86. const { resolution, colorTexture } = geometry.meta as MolecularSurfaceMeta;
  87. const csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution);
  88. if (csp) {
  89. applyMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture);
  90. (geometry.meta as MolecularSurfaceMeta).colorTexture = values.tColorGrid.ref.value;
  91. }
  92. },
  93. dispose: (geometry: Mesh) => {
  94. (geometry.meta as MolecularSurfaceMeta).colorTexture?.destroy();
  95. }
  96. }, materialId);
  97. }
  98. //
  99. async function createStructureMolecularSurfaceMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: MolecularSurfaceMeshProps, mesh?: Mesh): Promise<Mesh> {
  100. const { transform, field, idField, resolution, maxRadius } = await computeStructureMolecularSurface(structure, theme.size, props).runInContext(ctx.runtime);
  101. const params = {
  102. isoLevel: props.probeRadius,
  103. scalarField: field,
  104. idField
  105. };
  106. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime);
  107. if (props.includeParent) {
  108. const iterations = Math.ceil(2 / props.resolution);
  109. Mesh.smoothEdges(surface, { iterations, maxNewEdgeLength: Math.sqrt(2) });
  110. }
  111. Mesh.transform(surface, transform);
  112. if (ctx.webgl && !ctx.webgl.isWebGL2) {
  113. Mesh.uniformTriangleGroup(surface);
  114. ValueCell.updateIfChanged(surface.varyingGroup, false);
  115. } else {
  116. ValueCell.updateIfChanged(surface.varyingGroup, true);
  117. }
  118. const sphere = Sphere3D.expand(Sphere3D(), structure.boundary.sphere, maxRadius);
  119. surface.setBoundingSphere(sphere);
  120. (surface.meta as MolecularSurfaceMeta).resolution = resolution;
  121. return surface;
  122. }
  123. export function StructureMolecularSurfaceMeshVisual(materialId: number): ComplexVisual<MolecularSurfaceMeshParams> {
  124. return ComplexMeshVisual<MolecularSurfaceMeshParams>({
  125. defaultProps: PD.getDefaultValues(MolecularSurfaceMeshParams),
  126. createGeometry: createStructureMolecularSurfaceMesh,
  127. createLocationIterator: ElementIterator.fromStructure,
  128. getLoci: getSerialElementLoci,
  129. eachLocation: eachSerialElement,
  130. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<MolecularSurfaceMeshParams>, currentProps: PD.Values<MolecularSurfaceMeshParams>) => {
  131. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  132. if (newProps.probeRadius !== currentProps.probeRadius) state.createGeometry = true;
  133. if (newProps.probePositions !== currentProps.probePositions) state.createGeometry = true;
  134. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  135. if (newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant) state.createGeometry = true;
  136. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  137. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  138. if (newProps.smoothColors.name !== currentProps.smoothColors.name) {
  139. state.updateColor = true;
  140. } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') {
  141. if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true;
  142. if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true;
  143. }
  144. },
  145. processValues: (values: MeshValues, geometry: Mesh, props: PD.Values<MolecularSurfaceMeshParams>, theme: Theme, webgl?: WebGLContext) => {
  146. const { resolution, colorTexture } = geometry.meta as MolecularSurfaceMeta;
  147. const csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution);
  148. if (csp) {
  149. applyMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture);
  150. (geometry.meta as MolecularSurfaceMeta).colorTexture = values.tColorGrid.ref.value;
  151. }
  152. },
  153. dispose: (geometry: Mesh) => {
  154. (geometry.meta as MolecularSurfaceMeta).colorTexture?.destroy();
  155. }
  156. }, materialId);
  157. }