gaussian-density-point.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 { StructureElementIterator } from './util/element';
  10. import { EmptyLoci } from 'mol-model/loci';
  11. import { Vec3 } from 'mol-math/linear-algebra';
  12. import { UnitsPointsVisual, UnitsPointsParams } from '../units-visual';
  13. import { ParamDefinition as PD } from 'mol-util/param-definition';
  14. import { Points } from 'mol-geo/geometry/points/points';
  15. import { PointsBuilder } from 'mol-geo/geometry/points/points-builder';
  16. import { VisualContext } from 'mol-repr/visual';
  17. import { Theme } from 'mol-theme/theme';
  18. import { computeUnitGaussianDensity, GaussianDensityParams, GaussianDensityProps } from './util/gaussian';
  19. export const GaussianDensityPointParams = {
  20. ...UnitsPointsParams,
  21. ...GaussianDensityParams,
  22. pointSizeAttenuation: PD.Boolean(false),
  23. }
  24. export type GaussianDensityPointParams = typeof GaussianDensityPointParams
  25. export async function createGaussianDensityPoint(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: GaussianDensityProps, points?: Points) {
  26. const { transform, field: { space, data } } = await computeUnitGaussianDensity(unit, props, ctx.webgl).runInContext(ctx.runtime)
  27. const { dimensions, get } = space
  28. const [ xn, yn, zn ] = dimensions
  29. const n = xn * yn * zn * 3
  30. const builder = PointsBuilder.create(n, n / 10, points)
  31. const p = Vec3.zero()
  32. let i = 0
  33. for (let x = 0; x < xn; ++x) {
  34. for (let y = 0; y < yn; ++y) {
  35. for (let z = 0; z < zn; ++z) {
  36. if (get(data, x, y, z) > 0.001) {
  37. Vec3.set(p, x, y, z)
  38. Vec3.transformMat4(p, p, transform)
  39. builder.add(p[0], p[1], p[2], i)
  40. }
  41. if (i % 100000 === 0 && ctx.runtime.shouldUpdate) {
  42. await ctx.runtime.update({ message: 'Creating density points', current: i, max: n });
  43. }
  44. ++i
  45. }
  46. }
  47. }
  48. return builder.getPoints()
  49. }
  50. export function GaussianDensityPointVisual(materialId: number): UnitsVisual<GaussianDensityPointParams> {
  51. return UnitsPointsVisual<GaussianDensityPointParams>({
  52. defaultProps: PD.getDefaultValues(GaussianDensityPointParams),
  53. createGeometry: createGaussianDensityPoint,
  54. createLocationIterator: StructureElementIterator.fromGroup,
  55. getLoci: () => EmptyLoci,
  56. eachLocation: () => false,
  57. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<GaussianDensityPointParams>, currentProps: PD.Values<GaussianDensityPointParams>) => {
  58. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true
  59. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true
  60. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true
  61. if (newProps.useGpu !== currentProps.useGpu) state.createGeometry = true
  62. }
  63. }, materialId)
  64. }