points.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Copyright (c) 2018-2020 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, GroupMapping, createGroupMapping} from '../../util';
  9. import { GeometryUtils } from '../geometry';
  10. import { createColors } from '../color-data';
  11. import { createMarkers } from '../marker-data';
  12. import { createSizes } from '../size-data';
  13. import { TransformData } from '../transform-data';
  14. import { LocationIterator } from '../../util/location-iterator';
  15. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  16. import { calculateInvariantBoundingSphere, calculateTransformBoundingSphere } from '../../../mol-gl/renderable/util';
  17. import { Sphere3D } from '../../../mol-math/geometry';
  18. import { Theme } from '../../../mol-theme/theme';
  19. import { PointsValues } from '../../../mol-gl/renderable/points';
  20. import { RenderableState } from '../../../mol-gl/renderable';
  21. import { Color } from '../../../mol-util/color';
  22. import { BaseGeometry } from '../base';
  23. import { createEmptyOverpaint } from '../overpaint-data';
  24. import { createEmptyTransparency } from '../transparency-data';
  25. import { hashFnv32a } from '../../../mol-data/util';
  26. /** Point cloud */
  27. export interface Points {
  28. readonly kind: 'points',
  29. /** Number of vertices in the point cloud */
  30. pointCount: number,
  31. /** Center buffer as array of xyz values wrapped in a value cell */
  32. readonly centerBuffer: ValueCell<Float32Array>,
  33. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  34. readonly groupBuffer: ValueCell<Float32Array>,
  35. /** Bounding sphere of the points */
  36. readonly boundingSphere: Sphere3D
  37. /** Maps group ids to point indices */
  38. readonly groupMapping: GroupMapping
  39. setBoundingSphere(boundingSphere: Sphere3D): void
  40. }
  41. export namespace Points {
  42. export function create(centers: Float32Array, groups: Float32Array, pointCount: number, points?: Points): Points {
  43. return points ?
  44. update(centers, groups, pointCount, points) :
  45. fromArrays(centers, groups, pointCount);
  46. }
  47. export function createEmpty(points?: Points): Points {
  48. const cb = points ? points.centerBuffer.ref.value : new Float32Array(0);
  49. const gb = points ? points.groupBuffer.ref.value : new Float32Array(0);
  50. return create(cb, gb, 0, points);
  51. }
  52. function hashCode(points: Points) {
  53. return hashFnv32a([
  54. points.pointCount, points.centerBuffer.ref.version, points.groupBuffer.ref.version,
  55. ]);
  56. }
  57. function fromArrays(centers: Float32Array, groups: Float32Array, pointCount: number): Points {
  58. const boundingSphere = Sphere3D();
  59. let groupMapping: GroupMapping;
  60. let currentHash = -1;
  61. let currentGroup = -1;
  62. const points = {
  63. kind: 'points' as const,
  64. pointCount,
  65. centerBuffer: ValueCell.create(centers),
  66. groupBuffer: ValueCell.create(groups),
  67. get boundingSphere() {
  68. const newHash = hashCode(points);
  69. if (newHash !== currentHash) {
  70. const b = calculateInvariantBoundingSphere(points.centerBuffer.ref.value, points.pointCount, 1);
  71. Sphere3D.copy(boundingSphere, b);
  72. currentHash = newHash;
  73. }
  74. return boundingSphere;
  75. },
  76. get groupMapping() {
  77. if (points.groupBuffer.ref.version !== currentGroup) {
  78. groupMapping = createGroupMapping(points.groupBuffer.ref.value, points.pointCount);
  79. currentGroup = points.groupBuffer.ref.version;
  80. }
  81. return groupMapping;
  82. },
  83. setBoundingSphere(sphere: Sphere3D) {
  84. Sphere3D.copy(boundingSphere, sphere);
  85. currentHash = hashCode(points);
  86. }
  87. };
  88. return points;
  89. }
  90. function update(centers: Float32Array, groups: Float32Array, pointCount: number, points: Points) {
  91. points.pointCount = pointCount;
  92. ValueCell.update(points.centerBuffer, centers);
  93. ValueCell.update(points.groupBuffer, groups);
  94. return points;
  95. }
  96. export function transform(points: Points, t: Mat4) {
  97. const c = points.centerBuffer.ref.value;
  98. transformPositionArray(t, c, 0, points.pointCount);
  99. ValueCell.update(points.centerBuffer, c);
  100. }
  101. //
  102. export const Params = {
  103. ...BaseGeometry.Params,
  104. sizeFactor: PD.Numeric(1, { min: 0, max: 10, step: 0.1 }),
  105. pointSizeAttenuation: PD.Boolean(false),
  106. pointFilledCircle: PD.Boolean(false),
  107. pointEdgeBleach: PD.Numeric(0.2, { min: 0, max: 1, step: 0.05 }),
  108. };
  109. export type Params = typeof Params
  110. export const Utils: GeometryUtils<Points, Params> = {
  111. Params,
  112. createEmpty,
  113. createValues,
  114. createValuesSimple,
  115. updateValues,
  116. updateBoundingSphere,
  117. createRenderableState,
  118. updateRenderableState
  119. };
  120. function createValues(points: Points, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): PointsValues {
  121. const { instanceCount, groupCount } = locationIt;
  122. const color = createColors(locationIt, theme.color);
  123. const size = createSizes(locationIt, theme.size);
  124. const marker = createMarkers(instanceCount * groupCount);
  125. const overpaint = createEmptyOverpaint();
  126. const transparency = createEmptyTransparency();
  127. const counts = { drawCount: points.pointCount, groupCount, instanceCount };
  128. const invariantBoundingSphere = Sphere3D.clone(points.boundingSphere);
  129. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
  130. return {
  131. aPosition: points.centerBuffer,
  132. aGroup: points.groupBuffer,
  133. boundingSphere: ValueCell.create(boundingSphere),
  134. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  135. ...color,
  136. ...size,
  137. ...marker,
  138. ...overpaint,
  139. ...transparency,
  140. ...transform,
  141. ...BaseGeometry.createValues(props, counts),
  142. uSizeFactor: ValueCell.create(props.sizeFactor),
  143. dPointSizeAttenuation: ValueCell.create(props.pointSizeAttenuation),
  144. dPointFilledCircle: ValueCell.create(props.pointFilledCircle),
  145. uPointEdgeBleach: ValueCell.create(props.pointEdgeBleach),
  146. };
  147. }
  148. function createValuesSimple(points: Points, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  149. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  150. const p = { ...PD.getDefaultValues(Params), ...props };
  151. return createValues(points, s.transform, s.locationIterator, s.theme, p);
  152. }
  153. function updateValues(values: PointsValues, props: PD.Values<Params>) {
  154. BaseGeometry.updateValues(values, props);
  155. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor);
  156. ValueCell.updateIfChanged(values.dPointSizeAttenuation, props.pointSizeAttenuation);
  157. ValueCell.updateIfChanged(values.dPointFilledCircle, props.pointFilledCircle);
  158. ValueCell.updateIfChanged(values.uPointEdgeBleach, props.pointEdgeBleach);
  159. }
  160. function updateBoundingSphere(values: PointsValues, points: Points) {
  161. const invariantBoundingSphere = Sphere3D.clone(points.boundingSphere);
  162. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
  163. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  164. ValueCell.update(values.boundingSphere, boundingSphere);
  165. }
  166. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  167. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  168. }
  169. }
  170. function createRenderableState(props: PD.Values<Params>): RenderableState {
  171. const state = BaseGeometry.createRenderableState(props);
  172. updateRenderableState(state, props);
  173. return state;
  174. }
  175. function updateRenderableState(state: RenderableState, props: PD.Values<Params>) {
  176. BaseGeometry.updateRenderableState(state, props);
  177. state.opaque = state.opaque && (
  178. !props.pointFilledCircle ||
  179. (props.pointFilledCircle && props.pointEdgeBleach === 0)
  180. );
  181. state.writeDepth = state.opaque;
  182. }
  183. }