gaussian-surface-wireframe.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 { 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, getUnitExtraRadius } 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 } = await computeUnitGaussianDensity(structure, unit, props, ctx.webgl).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, props.radiusOffset + getUnitExtraRadius(unit));
  28. wireframe.setBoundingSphere(sphere);
  29. return wireframe;
  30. }
  31. export const GaussianWireframeParams = {
  32. ...UnitsLinesParams,
  33. ...GaussianDensityParams,
  34. sizeFactor: PD.Numeric(1.5, { min: 0, max: 10, step: 0.1 }),
  35. lineSizeAttenuation: PD.Boolean(false),
  36. ignoreHydrogens: PD.Boolean(false),
  37. };
  38. export type GaussianWireframeParams = typeof GaussianWireframeParams
  39. export function GaussianWireframeVisual(materialId: number): UnitsVisual<GaussianWireframeParams> {
  40. return UnitsLinesVisual<GaussianWireframeParams>({
  41. defaultProps: PD.getDefaultValues(GaussianWireframeParams),
  42. createGeometry: createGaussianWireframe,
  43. createLocationIterator: ElementIterator.fromGroup,
  44. getLoci: getElementLoci,
  45. eachLocation: eachElement,
  46. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<GaussianWireframeParams>, currentProps: PD.Values<GaussianWireframeParams>) => {
  47. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  48. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true;
  49. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true;
  50. if (newProps.useGpu !== currentProps.useGpu) state.createGeometry = true;
  51. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  52. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  53. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  54. }
  55. }, materialId);
  56. }