gaussian-density-volume.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 { Structure } from 'mol-model/structure';
  7. import { VisualUpdateState } from '../../util';
  8. import { ParamDefinition as PD } from 'mol-util/param-definition';
  9. import { DirectVolume } from 'mol-geo/geometry/direct-volume/direct-volume';
  10. import { VisualContext } from 'mol-repr/visual';
  11. import { Theme } from 'mol-theme/theme';
  12. import { ComplexVisual, ComplexDirectVolumeVisual, ComplexDirectVolumeParams } from '../complex-visual';
  13. import { EmptyLoci } from 'mol-model/loci';
  14. import { NullLocation } from 'mol-model/location';
  15. import { LocationIterator } from 'mol-geo/util/location-iterator';
  16. import { computeStructureGaussianDensityTexture, GaussianDensityTextureProps, GaussianDensityTextureParams } from './util/gaussian';
  17. async function createGaussianDensityVolume(ctx: VisualContext, structure: Structure, theme: Theme, props: GaussianDensityTextureProps, directVolume?: DirectVolume): Promise<DirectVolume> {
  18. const { runtime, webgl } = ctx
  19. if (webgl === undefined) throw new Error('createGaussianDensityVolume requires `webgl` object in VisualContext')
  20. const p = { ...props, useGpu: true }
  21. const oldTexture = directVolume ? directVolume.gridTexture.ref.value : undefined
  22. const densityTextureData = await computeStructureGaussianDensityTexture(structure, p, webgl, oldTexture).runInContext(runtime)
  23. const { transform, texture, bbox, gridDimension } = densityTextureData
  24. return DirectVolume.create(bbox, gridDimension, transform, texture, directVolume)
  25. }
  26. export const GaussianDensityVolumeParams = {
  27. ...ComplexDirectVolumeParams,
  28. ...GaussianDensityTextureParams
  29. }
  30. export type GaussianDensityVolumeParams = typeof GaussianDensityVolumeParams
  31. export function GaussianDensityVolumeVisual(): ComplexVisual<GaussianDensityVolumeParams> {
  32. return ComplexDirectVolumeVisual<GaussianDensityVolumeParams>({
  33. defaultProps: PD.getDefaultValues(GaussianDensityVolumeParams),
  34. createGeometry: createGaussianDensityVolume,
  35. createLocationIterator: (structure: Structure) => LocationIterator(structure.elementCount, 1, () => NullLocation),
  36. getLoci: () => EmptyLoci, // TODO
  37. mark: () => false, // TODO
  38. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<GaussianDensityVolumeParams>, currentProps: PD.Values<GaussianDensityVolumeParams>) => {
  39. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true
  40. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true
  41. if (newProps.smoothness !== currentProps.smoothness) {
  42. state.createGeometry = true
  43. newProps.isoValueNorm = Math.exp(-newProps.smoothness)
  44. }
  45. }
  46. })
  47. }