gaussian-surface-mesh.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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, VisualUpdateState } from '..';
  8. import { RuntimeContext } from 'mol-task'
  9. import { Mesh } from '../../../geometry/mesh/mesh';
  10. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  11. import { StructureElementIterator, getElementLoci, markElement } from './util/element';
  12. import { computeMarchingCubesMesh } from '../../../util/marching-cubes/algorithm';
  13. import { GaussianDensityProps, GaussianDensityParams } from 'mol-model/structure/structure/unit/gaussian-density';
  14. import { paramDefaultValues } from 'mol-util/parameter';
  15. async function createGaussianSurfaceMesh(ctx: RuntimeContext, unit: Unit, structure: Structure, props: GaussianDensityProps, mesh?: Mesh): Promise<Mesh> {
  16. const { smoothness } = props
  17. const { transform, field, idField } = await unit.computeGaussianDensity(props, ctx)
  18. const params = {
  19. isoLevel: Math.exp(-smoothness),
  20. scalarField: field,
  21. idField
  22. }
  23. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx)
  24. Mesh.transformImmediate(surface, transform)
  25. Mesh.computeNormalsImmediate(surface)
  26. Mesh.uniformTriangleGroup(surface)
  27. return surface;
  28. }
  29. export const GaussianSurfaceParams = {
  30. ...UnitsMeshParams,
  31. ...GaussianDensityParams,
  32. }
  33. export const DefaultGaussianSurfaceProps = paramDefaultValues(GaussianSurfaceParams)
  34. export type GaussianSurfaceProps = typeof DefaultGaussianSurfaceProps
  35. export function GaussianSurfaceVisual(): UnitsVisual<GaussianSurfaceProps> {
  36. return UnitsMeshVisual<GaussianSurfaceProps>({
  37. defaultProps: DefaultGaussianSurfaceProps,
  38. createGeometry: createGaussianSurfaceMesh,
  39. createLocationIterator: StructureElementIterator.fromGroup,
  40. getLoci: getElementLoci,
  41. mark: markElement,
  42. setUpdateState: (state: VisualUpdateState, newProps: GaussianSurfaceProps, currentProps: GaussianSurfaceProps) => {
  43. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true
  44. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true
  45. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true
  46. if (newProps.useGpu !== currentProps.useGpu) state.createGeometry = true
  47. if (newProps.ignoreCache !== currentProps.ignoreCache) state.createGeometry = true
  48. }
  49. })
  50. }