gaussian-surface-wireframe.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (c) 2018-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 { VisualContext } from '../../visual';
  8. import { Unit, Structure } from '../../../mol-model/structure';
  9. import { Theme } from '../../../mol-theme/theme';
  10. import { Lines } from '../../../mol-geo/geometry/lines/lines';
  11. import { computeUnitGaussianDensity, GaussianDensityParams, GaussianDensityProps } from './util/gaussian';
  12. import { computeMarchingCubesLines } from '../../../mol-geo/util/marching-cubes/algorithm';
  13. import { UnitsLinesParams, UnitsVisual, UnitsLinesVisual } from '../units-visual';
  14. import { ElementIterator, getElementLoci, eachElement } from './util/element';
  15. import { VisualUpdateState } from '../../util';
  16. import { Sphere3D } from '../../../mol-math/geometry';
  17. async function createGaussianWireframe(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: GaussianDensityProps, lines?: Lines): Promise<Lines> {
  18. const { smoothness } = props;
  19. const { transform, field, idField, maxRadius } = await computeUnitGaussianDensity(structure, unit, theme.size, props).runInContext(ctx.runtime);
  20. const params = {
  21. isoLevel: Math.exp(-smoothness),
  22. scalarField: field,
  23. idField
  24. };
  25. const wireframe = await computeMarchingCubesLines(params, lines).runAsChild(ctx.runtime);
  26. Lines.transform(wireframe, transform);
  27. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, maxRadius);
  28. wireframe.setBoundingSphere(sphere);
  29. return wireframe;
  30. }
  31. export const GaussianWireframeParams = {
  32. ...UnitsLinesParams,
  33. ...GaussianDensityParams,
  34. sizeFactor: PD.Numeric(3, { min: 0, max: 10, step: 0.1 }),
  35. lineSizeAttenuation: PD.Boolean(false),
  36. ignoreHydrogens: PD.Boolean(false),
  37. onlyPolarHydrogens: PD.Boolean(false),
  38. includeParent: PD.Boolean(false, { isHidden: true }),
  39. };
  40. export type GaussianWireframeParams = typeof GaussianWireframeParams
  41. export function GaussianWireframeVisual(materialId: number): UnitsVisual<GaussianWireframeParams> {
  42. return UnitsLinesVisual<GaussianWireframeParams>({
  43. defaultProps: PD.getDefaultValues(GaussianWireframeParams),
  44. createGeometry: createGaussianWireframe,
  45. createLocationIterator: ElementIterator.fromGroup,
  46. getLoci: getElementLoci,
  47. eachLocation: eachElement,
  48. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<GaussianWireframeParams>, currentProps: PD.Values<GaussianWireframeParams>) => {
  49. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  50. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true;
  51. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true;
  52. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  53. if (newProps.onlyPolarHydrogens !== currentProps.onlyPolarHydrogens) state.createGeometry = true;
  54. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  55. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  56. }
  57. }, materialId);
  58. }