points.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Copyright (c) 2018-2023 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, Vec3, Vec4 } 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, PositionLocation } 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. import { createEmptyClipping } from '../clipping-data';
  27. import { createEmptySubstance } from '../substance-data';
  28. /** Point cloud */
  29. export interface Points {
  30. readonly kind: 'points',
  31. /** Number of vertices in the point cloud */
  32. pointCount: number,
  33. /** Center buffer as array of xyz values wrapped in a value cell */
  34. readonly centerBuffer: ValueCell<Float32Array>,
  35. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  36. readonly groupBuffer: ValueCell<Float32Array>,
  37. /** Bounding sphere of the points */
  38. readonly boundingSphere: Sphere3D
  39. /** Maps group ids to point indices */
  40. readonly groupMapping: GroupMapping
  41. setBoundingSphere(boundingSphere: Sphere3D): void
  42. }
  43. export namespace Points {
  44. export function create(centers: Float32Array, groups: Float32Array, pointCount: number, points?: Points): Points {
  45. return points ?
  46. update(centers, groups, pointCount, points) :
  47. fromArrays(centers, groups, pointCount);
  48. }
  49. export function createEmpty(points?: Points): Points {
  50. const cb = points ? points.centerBuffer.ref.value : new Float32Array(0);
  51. const gb = points ? points.groupBuffer.ref.value : new Float32Array(0);
  52. return create(cb, gb, 0, points);
  53. }
  54. function hashCode(points: Points) {
  55. return hashFnv32a([
  56. points.pointCount, points.centerBuffer.ref.version, points.groupBuffer.ref.version,
  57. ]);
  58. }
  59. function fromArrays(centers: Float32Array, groups: Float32Array, pointCount: number): Points {
  60. const boundingSphere = Sphere3D();
  61. let groupMapping: GroupMapping;
  62. let currentHash = -1;
  63. let currentGroup = -1;
  64. const points = {
  65. kind: 'points' as const,
  66. pointCount,
  67. centerBuffer: ValueCell.create(centers),
  68. groupBuffer: ValueCell.create(groups),
  69. get boundingSphere() {
  70. const newHash = hashCode(points);
  71. if (newHash !== currentHash) {
  72. const b = calculateInvariantBoundingSphere(points.centerBuffer.ref.value, points.pointCount, 1);
  73. Sphere3D.copy(boundingSphere, b);
  74. currentHash = newHash;
  75. }
  76. return boundingSphere;
  77. },
  78. get groupMapping() {
  79. if (points.groupBuffer.ref.version !== currentGroup) {
  80. groupMapping = createGroupMapping(points.groupBuffer.ref.value, points.pointCount);
  81. currentGroup = points.groupBuffer.ref.version;
  82. }
  83. return groupMapping;
  84. },
  85. setBoundingSphere(sphere: Sphere3D) {
  86. Sphere3D.copy(boundingSphere, sphere);
  87. currentHash = hashCode(points);
  88. }
  89. };
  90. return points;
  91. }
  92. function update(centers: Float32Array, groups: Float32Array, pointCount: number, points: Points) {
  93. points.pointCount = pointCount;
  94. ValueCell.update(points.centerBuffer, centers);
  95. ValueCell.update(points.groupBuffer, groups);
  96. return points;
  97. }
  98. export function transform(points: Points, t: Mat4) {
  99. const c = points.centerBuffer.ref.value;
  100. transformPositionArray(t, c, 0, points.pointCount);
  101. ValueCell.update(points.centerBuffer, c);
  102. }
  103. //
  104. export const StyleTypes = {
  105. 'square': 'Square',
  106. 'circle': 'Circle',
  107. 'fuzzy': 'Fuzzy',
  108. };
  109. export type StyleTypes = keyof typeof StyleTypes;
  110. export const StyleTypeNames = Object.keys(StyleTypes) as StyleTypes[];
  111. export const Params = {
  112. ...BaseGeometry.Params,
  113. sizeFactor: PD.Numeric(3, { min: 0, max: 10, step: 0.1 }),
  114. pointSizeAttenuation: PD.Boolean(false),
  115. pointStyle: PD.Select('square', PD.objectToOptions(StyleTypes)),
  116. };
  117. export type Params = typeof Params
  118. export const Utils: GeometryUtils<Points, Params> = {
  119. Params,
  120. createEmpty,
  121. createValues,
  122. createValuesSimple,
  123. updateValues,
  124. updateBoundingSphere,
  125. createRenderableState,
  126. updateRenderableState,
  127. createPositionIterator
  128. };
  129. function createPositionIterator(points: Points, transform: TransformData): LocationIterator {
  130. const groupCount = points.pointCount;
  131. const instanceCount = transform.instanceCount.ref.value;
  132. const location = PositionLocation();
  133. const p = location.position;
  134. const v = points.centerBuffer.ref.value;
  135. const m = transform.aTransform.ref.value;
  136. const getLocation = (groupIndex: number, instanceIndex: number) => {
  137. if (instanceIndex < 0) {
  138. Vec3.fromArray(p, v, groupIndex * 3);
  139. } else {
  140. Vec3.transformMat4Offset(p, v, m, 0, groupIndex * 3, instanceIndex * 16);
  141. }
  142. return location;
  143. };
  144. return LocationIterator(groupCount, instanceCount, 1, getLocation);
  145. }
  146. function createValues(points: Points, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): PointsValues {
  147. const { instanceCount, groupCount } = locationIt;
  148. const positionIt = createPositionIterator(points, transform);
  149. const color = createColors(locationIt, positionIt, theme.color);
  150. const size = createSizes(locationIt, theme.size);
  151. const marker = props.instanceGranularity
  152. ? createMarkers(instanceCount, 'instance')
  153. : createMarkers(instanceCount * groupCount, 'groupInstance');
  154. const overpaint = createEmptyOverpaint();
  155. const transparency = createEmptyTransparency();
  156. const material = createEmptySubstance();
  157. const clipping = createEmptyClipping();
  158. const counts = { drawCount: points.pointCount, vertexCount: points.pointCount, groupCount, instanceCount };
  159. const invariantBoundingSphere = Sphere3D.clone(points.boundingSphere);
  160. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount, 0);
  161. return {
  162. dGeometryType: ValueCell.create('points'),
  163. aPosition: points.centerBuffer,
  164. aGroup: points.groupBuffer,
  165. boundingSphere: ValueCell.create(boundingSphere),
  166. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  167. uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)),
  168. ...color,
  169. ...size,
  170. ...marker,
  171. ...overpaint,
  172. ...transparency,
  173. ...material,
  174. ...clipping,
  175. ...transform,
  176. ...BaseGeometry.createValues(props, counts),
  177. uSizeFactor: ValueCell.create(props.sizeFactor),
  178. dPointSizeAttenuation: ValueCell.create(props.pointSizeAttenuation),
  179. dPointStyle: ValueCell.create(props.pointStyle),
  180. };
  181. }
  182. function createValuesSimple(points: Points, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  183. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  184. const p = { ...PD.getDefaultValues(Params), ...props };
  185. return createValues(points, s.transform, s.locationIterator, s.theme, p);
  186. }
  187. function updateValues(values: PointsValues, props: PD.Values<Params>) {
  188. BaseGeometry.updateValues(values, props);
  189. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor);
  190. ValueCell.updateIfChanged(values.dPointSizeAttenuation, props.pointSizeAttenuation);
  191. ValueCell.updateIfChanged(values.dPointStyle, props.pointStyle);
  192. }
  193. function updateBoundingSphere(values: PointsValues, points: Points) {
  194. const invariantBoundingSphere = Sphere3D.clone(points.boundingSphere);
  195. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value, 0);
  196. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  197. ValueCell.update(values.boundingSphere, boundingSphere);
  198. }
  199. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  200. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  201. ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
  202. }
  203. }
  204. function createRenderableState(props: PD.Values<Params>): RenderableState {
  205. const state = BaseGeometry.createRenderableState(props);
  206. updateRenderableState(state, props);
  207. return state;
  208. }
  209. function updateRenderableState(state: RenderableState, props: PD.Values<Params>) {
  210. BaseGeometry.updateRenderableState(state, props);
  211. state.opaque = state.opaque && props.pointStyle !== 'fuzzy';
  212. state.writeDepth = state.opaque;
  213. }
  214. }