molecular-surface-mesh.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 { 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 { computeUnitMolecularSurface, MolecularSurfaceProps } from './util/molecular-surface';
  14. import { computeMarchingCubesMesh } from '../../../mol-geo/util/marching-cubes/algorithm';
  15. import { ElementIterator, getElementLoci, eachElement } from './util/element';
  16. import { VisualUpdateState } from '../../util';
  17. import { CommonSurfaceParams } from './util/common';
  18. import { Sphere3D } from '../../../mol-math/geometry';
  19. export const MolecularSurfaceMeshParams = {
  20. ...UnitsMeshParams,
  21. ...MolecularSurfaceCalculationParams,
  22. ...CommonSurfaceParams
  23. }
  24. export type MolecularSurfaceMeshParams = typeof MolecularSurfaceMeshParams
  25. //
  26. async function createMolecularSurfaceMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: MolecularSurfaceProps, mesh?: Mesh): Promise<Mesh> {
  27. const { transform, field, idField } = await computeUnitMolecularSurface(structure, unit, props).runInContext(ctx.runtime)
  28. const params = {
  29. isoLevel: props.probeRadius,
  30. scalarField: field,
  31. idField
  32. }
  33. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime)
  34. Mesh.transform(surface, transform)
  35. if (ctx.webgl && !ctx.webgl.isWebGL2) Mesh.uniformTriangleGroup(surface)
  36. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, props.probeRadius)
  37. surface.setBoundingSphere(sphere)
  38. return surface
  39. }
  40. export function MolecularSurfaceMeshVisual(materialId: number): UnitsVisual<MolecularSurfaceMeshParams> {
  41. return UnitsMeshVisual<MolecularSurfaceMeshParams>({
  42. defaultProps: PD.getDefaultValues(MolecularSurfaceMeshParams),
  43. createGeometry: createMolecularSurfaceMesh,
  44. createLocationIterator: ElementIterator.fromGroup,
  45. getLoci: getElementLoci,
  46. eachLocation: eachElement,
  47. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<MolecularSurfaceMeshParams>, currentProps: PD.Values<MolecularSurfaceMeshParams>) => {
  48. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true
  49. if (newProps.probeRadius !== currentProps.probeRadius) state.createGeometry = true
  50. if (newProps.probePositions !== currentProps.probePositions) state.createGeometry = true
  51. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true
  52. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true
  53. }
  54. }, materialId)
  55. }