points.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { ValueCell } from 'mol-util'
  7. import { Mat4 } from 'mol-math/linear-algebra'
  8. import { transformPositionArray/* , transformDirectionArray, getNormalMatrix */ } from '../../util';
  9. import { Geometry } from '../geometry';
  10. import { RuntimeContext } from 'mol-task';
  11. import { createColors } from '../color-data';
  12. import { createMarkers } from '../marker-data';
  13. import { createSizes } from '../size-data';
  14. import { TransformData } from '../transform-data';
  15. import { LocationIterator } from '../../util/location-iterator';
  16. import { ParamDefinition as PD } from 'mol-util/param-definition';
  17. import { calculateBoundingSphere } from 'mol-gl/renderable/util';
  18. import { Sphere3D } from 'mol-math/geometry';
  19. import { Theme } from 'mol-theme/theme';
  20. import { PointsValues } from 'mol-gl/renderable/points';
  21. /** Point cloud */
  22. export interface Points {
  23. readonly kind: 'points',
  24. /** Number of vertices in the point cloud */
  25. pointCount: number,
  26. /** Vertex buffer as array of xyz values wrapped in a value cell */
  27. readonly centerBuffer: ValueCell<Float32Array>,
  28. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  29. readonly groupBuffer: ValueCell<Float32Array>,
  30. }
  31. export namespace Points {
  32. export function createEmpty(points?: Points): Points {
  33. const cb = points ? points.centerBuffer.ref.value : new Float32Array(0)
  34. const gb = points ? points.groupBuffer.ref.value : new Float32Array(0)
  35. return {
  36. kind: 'points',
  37. pointCount: 0,
  38. centerBuffer: points ? ValueCell.update(points.centerBuffer, cb) : ValueCell.create(cb),
  39. groupBuffer: points ? ValueCell.update(points.groupBuffer, gb) : ValueCell.create(gb),
  40. }
  41. }
  42. export function transformImmediate(points: Points, t: Mat4) {
  43. transformRangeImmediate(points, t, 0, points.pointCount)
  44. }
  45. export function transformRangeImmediate(points: Points, t: Mat4, offset: number, count: number) {
  46. const c = points.centerBuffer.ref.value
  47. transformPositionArray(t, c, offset, count)
  48. ValueCell.update(points.centerBuffer, c);
  49. }
  50. //
  51. export const Params = {
  52. ...Geometry.Params,
  53. pointSizeAttenuation: PD.Boolean('Point Size Attenuation', '', false),
  54. pointFilledCircle: PD.Boolean('Point Filled Circle', '', false),
  55. pointEdgeBleach: PD.Numeric('Point Edge Bleach', '', 0.2, 0, 1, 0.05),
  56. }
  57. export const DefaultProps = PD.getDefaultValues(Params)
  58. export type Props = typeof DefaultProps
  59. export async function createValues(ctx: RuntimeContext, points: Points, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: Props): Promise<PointsValues> {
  60. const { instanceCount, groupCount } = locationIt
  61. const color = await createColors(ctx, locationIt, theme.color)
  62. const size = await createSizes(ctx, locationIt, theme.size)
  63. const marker = createMarkers(instanceCount * groupCount)
  64. const counts = { drawCount: points.pointCount, groupCount, instanceCount }
  65. const boundingSphere = calculateBoundingSphere(
  66. points.centerBuffer.ref.value, points.pointCount,
  67. transform.aTransform.ref.value, transform.instanceCount.ref.value
  68. )
  69. return {
  70. aPosition: points.centerBuffer,
  71. aGroup: points.groupBuffer,
  72. boundingSphere: ValueCell.create(boundingSphere),
  73. ...color,
  74. ...size,
  75. ...marker,
  76. ...transform,
  77. ...Geometry.createValues(props, counts),
  78. dPointSizeAttenuation: ValueCell.create(props.pointSizeAttenuation),
  79. dPointFilledCircle: ValueCell.create(props.pointFilledCircle),
  80. uPointEdgeBleach: ValueCell.create(props.pointEdgeBleach),
  81. }
  82. }
  83. export function updateValues(values: PointsValues, props: Props) {
  84. const boundingSphere = calculateBoundingSphere(
  85. values.aPosition.ref.value, Math.floor(values.aPosition.ref.value.length / 3),
  86. values.aTransform.ref.value, values.instanceCount.ref.value
  87. )
  88. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  89. ValueCell.update(values.boundingSphere, boundingSphere)
  90. }
  91. Geometry.updateValues(values, props)
  92. ValueCell.updateIfChanged(values.dPointSizeAttenuation, props.pointSizeAttenuation)
  93. ValueCell.updateIfChanged(values.dPointFilledCircle, props.pointFilledCircle)
  94. ValueCell.updateIfChanged(values.uPointEdgeBleach, props.pointEdgeBleach)
  95. }
  96. }