lines.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Copyright (c) 2018-2022 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 { LinesValues } from '../../../mol-gl/renderable/lines';
  16. import { Mesh } from '../mesh/mesh';
  17. import { LinesBuilder } from './lines-builder';
  18. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  19. import { calculateInvariantBoundingSphere, calculateTransformBoundingSphere } from '../../../mol-gl/renderable/util';
  20. import { Sphere3D } from '../../../mol-math/geometry';
  21. import { Theme } from '../../../mol-theme/theme';
  22. import { Color } from '../../../mol-util/color';
  23. import { BaseGeometry } from '../base';
  24. import { createEmptyOverpaint } from '../overpaint-data';
  25. import { createEmptyTransparency } from '../transparency-data';
  26. import { hashFnv32a } from '../../../mol-data/util';
  27. import { createEmptyClipping } from '../clipping-data';
  28. import { createEmptySubstance } from '../substance-data';
  29. /** Wide line */
  30. export interface Lines {
  31. readonly kind: 'lines',
  32. /** Number of lines */
  33. lineCount: number,
  34. /** Mapping buffer as array of xy values wrapped in a value cell */
  35. readonly mappingBuffer: ValueCell<Float32Array>,
  36. /** Index buffer as array of vertex index triplets wrapped in a value cell */
  37. readonly indexBuffer: ValueCell<Uint32Array>,
  38. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  39. readonly groupBuffer: ValueCell<Float32Array>,
  40. /** Line start buffer as array of xyz values wrapped in a value cell */
  41. readonly startBuffer: ValueCell<Float32Array>,
  42. /** Line end buffer as array of xyz values wrapped in a value cell */
  43. readonly endBuffer: ValueCell<Float32Array>,
  44. /** Bounding sphere of the lines */
  45. readonly boundingSphere: Sphere3D
  46. /** Maps group ids to line indices */
  47. readonly groupMapping: GroupMapping
  48. setBoundingSphere(boundingSphere: Sphere3D): void
  49. }
  50. export namespace Lines {
  51. export function create(mappings: Float32Array, indices: Uint32Array, groups: Float32Array, starts: Float32Array, ends: Float32Array, lineCount: number, lines?: Lines): Lines {
  52. return lines ?
  53. update(mappings, indices, groups, starts, ends, lineCount, lines) :
  54. fromArrays(mappings, indices, groups, starts, ends, lineCount);
  55. }
  56. export function createEmpty(lines?: Lines): Lines {
  57. const mb = lines ? lines.mappingBuffer.ref.value : new Float32Array(0);
  58. const ib = lines ? lines.indexBuffer.ref.value : new Uint32Array(0);
  59. const gb = lines ? lines.groupBuffer.ref.value : new Float32Array(0);
  60. const sb = lines ? lines.startBuffer.ref.value : new Float32Array(0);
  61. const eb = lines ? lines.endBuffer.ref.value : new Float32Array(0);
  62. return create(mb, ib, gb, sb, eb, 0, lines);
  63. }
  64. export function fromMesh(mesh: Mesh, lines?: Lines) {
  65. const vb = mesh.vertexBuffer.ref.value;
  66. const ib = mesh.indexBuffer.ref.value;
  67. const gb = mesh.groupBuffer.ref.value;
  68. const builder = LinesBuilder.create(mesh.triangleCount * 3, mesh.triangleCount / 10, lines);
  69. // TODO avoid duplicate lines
  70. for (let i = 0, il = mesh.triangleCount * 3; i < il; i += 3) {
  71. const i0 = ib[i], i1 = ib[i + 1], i2 = ib[i + 2];
  72. const x0 = vb[i0 * 3], y0 = vb[i0 * 3 + 1], z0 = vb[i0 * 3 + 2];
  73. const x1 = vb[i1 * 3], y1 = vb[i1 * 3 + 1], z1 = vb[i1 * 3 + 2];
  74. const x2 = vb[i2 * 3], y2 = vb[i2 * 3 + 1], z2 = vb[i2 * 3 + 2];
  75. builder.add(x0, y0, z0, x1, y1, z1, gb[i0]);
  76. builder.add(x0, y0, z0, x2, y2, z2, gb[i0]);
  77. builder.add(x1, y1, z1, x2, y2, z2, gb[i1]);
  78. }
  79. return builder.getLines();
  80. }
  81. function hashCode(lines: Lines) {
  82. return hashFnv32a([
  83. lines.lineCount, lines.mappingBuffer.ref.version, lines.indexBuffer.ref.version,
  84. lines.groupBuffer.ref.version, lines.startBuffer.ref.version, lines.endBuffer.ref.version
  85. ]);
  86. }
  87. function fromArrays(mappings: Float32Array, indices: Uint32Array, groups: Float32Array, starts: Float32Array, ends: Float32Array, lineCount: number): Lines {
  88. const boundingSphere = Sphere3D();
  89. let groupMapping: GroupMapping;
  90. let currentHash = -1;
  91. let currentGroup = -1;
  92. const lines = {
  93. kind: 'lines' as const,
  94. lineCount,
  95. mappingBuffer: ValueCell.create(mappings),
  96. indexBuffer: ValueCell.create(indices),
  97. groupBuffer: ValueCell.create(groups),
  98. startBuffer: ValueCell.create(starts),
  99. endBuffer: ValueCell.create(ends),
  100. get boundingSphere() {
  101. const newHash = hashCode(lines);
  102. if (newHash !== currentHash) {
  103. const s = calculateInvariantBoundingSphere(lines.startBuffer.ref.value, lines.lineCount * 4, 4);
  104. const e = calculateInvariantBoundingSphere(lines.endBuffer.ref.value, lines.lineCount * 4, 4);
  105. Sphere3D.expandBySphere(boundingSphere, s, e);
  106. currentHash = newHash;
  107. }
  108. return boundingSphere;
  109. },
  110. get groupMapping() {
  111. if (lines.groupBuffer.ref.version !== currentGroup) {
  112. groupMapping = createGroupMapping(lines.groupBuffer.ref.value, lines.lineCount, 4);
  113. currentGroup = lines.groupBuffer.ref.version;
  114. }
  115. return groupMapping;
  116. },
  117. setBoundingSphere(sphere: Sphere3D) {
  118. Sphere3D.copy(boundingSphere, sphere);
  119. currentHash = hashCode(lines);
  120. }
  121. };
  122. return lines;
  123. }
  124. function update(mappings: Float32Array, indices: Uint32Array, groups: Float32Array, starts: Float32Array, ends: Float32Array, lineCount: number, lines: Lines) {
  125. if (lineCount > lines.lineCount) {
  126. ValueCell.update(lines.mappingBuffer, mappings);
  127. ValueCell.update(lines.indexBuffer, indices);
  128. }
  129. lines.lineCount = lineCount;
  130. ValueCell.update(lines.groupBuffer, groups);
  131. ValueCell.update(lines.startBuffer, starts);
  132. ValueCell.update(lines.endBuffer, ends);
  133. return lines;
  134. }
  135. export function transform(lines: Lines, t: Mat4) {
  136. const start = lines.startBuffer.ref.value;
  137. transformPositionArray(t, start, 0, lines.lineCount * 4);
  138. ValueCell.update(lines.startBuffer, start);
  139. const end = lines.endBuffer.ref.value;
  140. transformPositionArray(t, end, 0, lines.lineCount * 4);
  141. ValueCell.update(lines.endBuffer, end);
  142. }
  143. //
  144. export const Params = {
  145. ...BaseGeometry.Params,
  146. sizeFactor: PD.Numeric(2, { min: 0, max: 10, step: 0.1 }),
  147. lineSizeAttenuation: PD.Boolean(false),
  148. };
  149. export type Params = typeof Params
  150. export const Utils: GeometryUtils<Lines, Params> = {
  151. Params,
  152. createEmpty,
  153. createValues,
  154. createValuesSimple,
  155. updateValues,
  156. updateBoundingSphere,
  157. createRenderableState: BaseGeometry.createRenderableState,
  158. updateRenderableState: BaseGeometry.updateRenderableState,
  159. createPositionIterator
  160. };
  161. function createPositionIterator(lines: Lines, transform: TransformData): LocationIterator {
  162. const groupCount = lines.lineCount * 4;
  163. const instanceCount = transform.instanceCount.ref.value;
  164. const location = PositionLocation();
  165. const p = location.position;
  166. const s = lines.startBuffer.ref.value;
  167. const e = lines.endBuffer.ref.value;
  168. const m = transform.aTransform.ref.value;
  169. const getLocation = (groupIndex: number, instanceIndex: number) => {
  170. const v = groupIndex % 4 === 0 ? s : e;
  171. if (instanceIndex < 0) {
  172. Vec3.fromArray(p, v, groupIndex * 3);
  173. } else {
  174. Vec3.transformMat4Offset(p, v, m, 0, groupIndex * 3, instanceIndex * 16);
  175. }
  176. return location;
  177. };
  178. return LocationIterator(groupCount, instanceCount, 2, getLocation);
  179. }
  180. function createValues(lines: Lines, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): LinesValues {
  181. const { instanceCount, groupCount } = locationIt;
  182. const positionIt = createPositionIterator(lines, transform);
  183. const color = createColors(locationIt, positionIt, theme.color);
  184. const size = createSizes(locationIt, theme.size);
  185. const marker = props.instanceGranularity
  186. ? createMarkers(instanceCount, 'instance')
  187. : createMarkers(instanceCount * groupCount, 'groupInstance');
  188. const overpaint = createEmptyOverpaint();
  189. const transparency = createEmptyTransparency();
  190. const material = createEmptySubstance();
  191. const clipping = createEmptyClipping();
  192. const counts = { drawCount: lines.lineCount * 2 * 3, vertexCount: lines.lineCount * 4, groupCount, instanceCount };
  193. const invariantBoundingSphere = Sphere3D.clone(lines.boundingSphere);
  194. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
  195. return {
  196. dGeometryType: ValueCell.create('lines'),
  197. aMapping: lines.mappingBuffer,
  198. aGroup: lines.groupBuffer,
  199. aStart: lines.startBuffer,
  200. aEnd: lines.endBuffer,
  201. elements: lines.indexBuffer,
  202. boundingSphere: ValueCell.create(boundingSphere),
  203. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  204. uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)),
  205. ...color,
  206. ...size,
  207. ...marker,
  208. ...overpaint,
  209. ...transparency,
  210. ...material,
  211. ...clipping,
  212. ...transform,
  213. ...BaseGeometry.createValues(props, counts),
  214. uSizeFactor: ValueCell.create(props.sizeFactor),
  215. dLineSizeAttenuation: ValueCell.create(props.lineSizeAttenuation),
  216. uDoubleSided: ValueCell.create(true),
  217. dFlipSided: ValueCell.create(false),
  218. };
  219. }
  220. function createValuesSimple(lines: Lines, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  221. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  222. const p = { ...PD.getDefaultValues(Params), ...props };
  223. return createValues(lines, s.transform, s.locationIterator, s.theme, p);
  224. }
  225. function updateValues(values: LinesValues, props: PD.Values<Params>) {
  226. BaseGeometry.updateValues(values, props);
  227. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor);
  228. ValueCell.updateIfChanged(values.dLineSizeAttenuation, props.lineSizeAttenuation);
  229. }
  230. function updateBoundingSphere(values: LinesValues, lines: Lines) {
  231. const invariantBoundingSphere = Sphere3D.clone(lines.boundingSphere);
  232. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
  233. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  234. ValueCell.update(values.boundingSphere, boundingSphere);
  235. }
  236. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  237. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  238. ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
  239. }
  240. }
  241. }