lines.ts 12 KB

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