gaussian-surface-mesh.ts 2.7 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 '../representation';
  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 { ParamDefinition as PD } from 'mol-util/param-definition';
  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/representation';
  16. import { Theme } from 'mol-theme/theme';
  17. async function createGaussianSurfaceMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: GaussianDensityProps, mesh?: Mesh): Promise<Mesh> {
  18. const { smoothness } = props
  19. const { transform, field, idField } = await unit.computeGaussianDensity(props, ctx.runtime, ctx.webgl)
  20. const params = {
  21. isoLevel: Math.exp(-smoothness),
  22. scalarField: field,
  23. idField
  24. }
  25. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime)
  26. Mesh.transformImmediate(surface, transform)
  27. Mesh.computeNormalsImmediate(surface)
  28. Mesh.uniformTriangleGroup(surface)
  29. return surface;
  30. }
  31. export const GaussianSurfaceParams = {
  32. ...UnitsMeshParams,
  33. ...GaussianDensityParams,
  34. }
  35. export type GaussianSurfaceParams = typeof GaussianSurfaceParams
  36. export function GaussianSurfaceVisual(): UnitsVisual<GaussianSurfaceParams> {
  37. return UnitsMeshVisual<GaussianSurfaceParams>({
  38. defaultProps: PD.getDefaultValues(GaussianSurfaceParams),
  39. createGeometry: createGaussianSurfaceMesh,
  40. createLocationIterator: StructureElementIterator.fromGroup,
  41. getLoci: getElementLoci,
  42. mark: markElement,
  43. setUpdateState: (state: VisualUpdateState, newProps: PD.DefaultValues<GaussianSurfaceParams>, currentProps: PD.DefaultValues<GaussianSurfaceParams>) => {
  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. }