gaussian-density-point.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { RuntimeContext } from 'mol-task'
  8. import { UnitsVisual } from '../index';
  9. import { VisualUpdateState } from '../../util';
  10. import { StructureElementIterator } from './util/element';
  11. import { EmptyLoci } from 'mol-model/loci';
  12. import { Vec3 } from 'mol-math/linear-algebra';
  13. import { UnitsPointsVisual, UnitsPointsParams } from '../units-visual';
  14. import { SizeThemeOptions, SizeThemeName } from 'mol-theme/size';
  15. import { GaussianDensityProps, GaussianDensityParams } from 'mol-model/structure/structure/unit/gaussian-density';
  16. import { paramDefaultValues, SelectParam, NumberParam, BooleanParam } from 'mol-util/parameter';
  17. import { Points } from 'mol-geo/geometry/points/points';
  18. import { PointsBuilder } from 'mol-geo/geometry/points/points-builder';
  19. export const GaussianDensityPointParams = {
  20. ...UnitsPointsParams,
  21. ...GaussianDensityParams,
  22. sizeTheme: SelectParam<SizeThemeName>('Size Theme', '', 'uniform', SizeThemeOptions),
  23. sizeValue: NumberParam('Size Value', '', 1, 0, 20, 0.1),
  24. pointSizeAttenuation: BooleanParam('Point Size Attenuation', '', false),
  25. }
  26. export const DefaultGaussianDensityPointProps = paramDefaultValues(GaussianDensityPointParams)
  27. export type GaussianDensityPointProps = typeof DefaultGaussianDensityPointProps
  28. export async function createGaussianDensityPoint(ctx: RuntimeContext, unit: Unit, structure: Structure, props: GaussianDensityProps, points?: Points) {
  29. const { transform, field: { space, data } } = await unit.computeGaussianDensity(props, ctx)
  30. const { dimensions, get } = space
  31. const [ xn, yn, zn ] = dimensions
  32. const n = xn * yn * zn * 3
  33. const builder = PointsBuilder.create(n, n / 10, points)
  34. const p = Vec3.zero()
  35. let i = 0
  36. for (let x = 0; x < xn; ++x) {
  37. for (let y = 0; y < yn; ++y) {
  38. for (let z = 0; z < zn; ++z) {
  39. if (get(data, x, y, z) > 0.001) {
  40. Vec3.set(p, x, y, z)
  41. Vec3.transformMat4(p, p, transform)
  42. builder.add(p[0], p[1], p[2], i)
  43. }
  44. if (i % 100000 === 0 && ctx.shouldUpdate) {
  45. await ctx.update({ message: 'Creating density points', current: i, max: n });
  46. }
  47. ++i
  48. }
  49. }
  50. }
  51. return builder.getPoints()
  52. }
  53. export function GaussianDensityPointVisual(): UnitsVisual<GaussianDensityPointProps> {
  54. return UnitsPointsVisual<GaussianDensityPointProps>({
  55. defaultProps: DefaultGaussianDensityPointProps,
  56. createGeometry: createGaussianDensityPoint,
  57. createLocationIterator: StructureElementIterator.fromGroup,
  58. getLoci: () => EmptyLoci,
  59. mark: () => false,
  60. setUpdateState: (state: VisualUpdateState, newProps: GaussianDensityPointProps, currentProps: GaussianDensityPointProps) => {
  61. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true
  62. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true
  63. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true
  64. if (newProps.useGpu !== currentProps.useGpu) state.createGeometry = true
  65. if (newProps.ignoreCache !== currentProps.ignoreCache) state.createGeometry = true
  66. }
  67. })
  68. }