points.ts 10 KB

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