gaussian-surface-mesh.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Unit, Structure } from 'mol-model/structure';
  7. import { UnitsVisual } from '../index';
  8. import { VisualUpdateState } from '../../util';
  9. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  10. import { StructureElementIterator, getElementLoci, markElement } from './util/element';
  11. import { GaussianDensityProps, GaussianDensityParams } from 'mol-model/structure/structure/unit/gaussian-density';
  12. import { paramDefaultValues } from 'mol-util/parameter';
  13. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  14. import { computeMarchingCubesMesh } from 'mol-geo/util/marching-cubes/algorithm';
  15. import { VisualContext } from 'mol-repr';
  16. async function createGaussianSurfaceMesh(ctx: VisualContext, unit: Unit, structure: Structure, props: GaussianDensityProps, mesh?: Mesh): Promise<Mesh> {
  17. const { smoothness } = props
  18. const { transform, field, idField } = await unit.computeGaussianDensity(props, ctx.runtime, ctx.webgl)
  19. const params = {
  20. isoLevel: Math.exp(-smoothness),
  21. scalarField: field,
  22. idField
  23. }
  24. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime)
  25. Mesh.transformImmediate(surface, transform)
  26. Mesh.computeNormalsImmediate(surface)
  27. Mesh.uniformTriangleGroup(surface)
  28. return surface;
  29. }
  30. export const GaussianSurfaceParams = {
  31. ...UnitsMeshParams,
  32. ...GaussianDensityParams,
  33. }
  34. export const DefaultGaussianSurfaceProps = paramDefaultValues(GaussianSurfaceParams)
  35. export type GaussianSurfaceProps = typeof DefaultGaussianSurfaceProps
  36. export function GaussianSurfaceVisual(): UnitsVisual<GaussianSurfaceProps> {
  37. return UnitsMeshVisual<GaussianSurfaceProps>({
  38. defaultProps: DefaultGaussianSurfaceProps,
  39. createGeometry: createGaussianSurfaceMesh,
  40. createLocationIterator: StructureElementIterator.fromGroup,
  41. getLoci: getElementLoci,
  42. mark: markElement,
  43. setUpdateState: (state: VisualUpdateState, newProps: GaussianSurfaceProps, currentProps: GaussianSurfaceProps) => {
  44. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true
  45. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true
  46. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true
  47. if (newProps.useGpu !== currentProps.useGpu) state.createGeometry = true
  48. if (newProps.ignoreCache !== currentProps.ignoreCache) state.createGeometry = true
  49. }
  50. })
  51. }