lines-builder.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 { ChunkedArray } from '../../../mol-data/util';
  7. import { Lines } from './lines';
  8. import { Mat4, Vec3 } from '../../../mol-math/linear-algebra';
  9. import { Cage } from '../../../mol-geo/primitive/cage';
  10. export interface LinesBuilder {
  11. add(startX: number, startY: number, startZ: number, endX: number, endY: number, endZ: number, group: number): void
  12. addFixedCountDashes(start: Vec3, end: Vec3, segmentCount: number, group: number): void
  13. addFixedLengthDashes(start: Vec3, end: Vec3, segmentLength: number, group: number): void
  14. addCage(t: Mat4, cage: Cage, group: number): void
  15. getLines(): Lines
  16. }
  17. const tmpVecA = Vec3();
  18. const tmpVecB = Vec3();
  19. const tmpDir = Vec3();
  20. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  21. const caAdd = ChunkedArray.add;
  22. const caAdd3 = ChunkedArray.add3;
  23. export namespace LinesBuilder {
  24. export function create(initialCount = 2048, chunkSize = 1024, lines?: Lines): LinesBuilder {
  25. const groups = ChunkedArray.create(Float32Array, 1, chunkSize, lines ? lines.groupBuffer.ref.value : initialCount);
  26. const starts = ChunkedArray.create(Float32Array, 3, chunkSize, lines ? lines.startBuffer.ref.value : initialCount);
  27. const ends = ChunkedArray.create(Float32Array, 3, chunkSize, lines ? lines.endBuffer.ref.value : initialCount);
  28. const add = (startX: number, startY: number, startZ: number, endX: number, endY: number, endZ: number, group: number) => {
  29. for (let i = 0; i < 4; ++i) {
  30. caAdd3(starts, startX, startY, startZ);
  31. caAdd3(ends, endX, endY, endZ);
  32. caAdd(groups, group);
  33. }
  34. };
  35. const addFixedCountDashes = (start: Vec3, end: Vec3, segmentCount: number, group: number) => {
  36. const d = Vec3.distance(start, end);
  37. const s = Math.floor(segmentCount / 2);
  38. const step = 1 / segmentCount;
  39. Vec3.sub(tmpDir, end, start);
  40. for (let j = 0; j < s; ++j) {
  41. const f = step * (j * 2 + 1);
  42. Vec3.setMagnitude(tmpDir, tmpDir, d * f);
  43. Vec3.add(tmpVecA, start, tmpDir);
  44. Vec3.setMagnitude(tmpDir, tmpDir, d * step * ((j + 1) * 2));
  45. Vec3.add(tmpVecB, start, tmpDir);
  46. add(tmpVecA[0], tmpVecA[1], tmpVecA[2], tmpVecB[0], tmpVecB[1], tmpVecB[2], group);
  47. }
  48. };
  49. return {
  50. add,
  51. addFixedCountDashes,
  52. addFixedLengthDashes: (start: Vec3, end: Vec3, segmentLength: number, group: number) => {
  53. const d = Vec3.distance(start, end);
  54. addFixedCountDashes(start, end, d / segmentLength, group);
  55. },
  56. addCage: (t: Mat4, cage: Cage, group: number) => {
  57. const { vertices, edges } = cage;
  58. for (let i = 0, il = edges.length; i < il; i += 2) {
  59. Vec3.fromArray(tmpVecA, vertices, edges[i] * 3);
  60. Vec3.fromArray(tmpVecB, vertices, edges[i + 1] * 3);
  61. Vec3.transformMat4(tmpVecA, tmpVecA, t);
  62. Vec3.transformMat4(tmpVecB, tmpVecB, t);
  63. add(tmpVecA[0], tmpVecA[1], tmpVecA[2], tmpVecB[0], tmpVecB[1], tmpVecB[2], group);
  64. }
  65. },
  66. getLines: () => {
  67. const lineCount = groups.elementCount / 4;
  68. const gb = ChunkedArray.compact(groups, true) as Float32Array;
  69. const sb = ChunkedArray.compact(starts, true) as Float32Array;
  70. const eb = ChunkedArray.compact(ends, true) as Float32Array;
  71. const mb = lines && lineCount <= lines.lineCount ? lines.mappingBuffer.ref.value : new Float32Array(lineCount * 8);
  72. const ib = lines && lineCount <= lines.lineCount ? lines.indexBuffer.ref.value : new Uint32Array(lineCount * 6);
  73. if (!lines || lineCount > lines.lineCount) fillMappingAndIndices(lineCount, mb, ib);
  74. return Lines.create(mb, ib, gb, sb, eb, lineCount, lines);
  75. }
  76. };
  77. }
  78. }
  79. function fillMappingAndIndices(n: number, mb: Float32Array, ib: Uint32Array) {
  80. for (let i = 0; i < n; ++i) {
  81. const mo = i * 8;
  82. mb[mo] = -1; mb[mo + 1] = 1;
  83. mb[mo + 2] = -1; mb[mo + 3] = -1;
  84. mb[mo + 4] = 1; mb[mo + 5] = 1;
  85. mb[mo + 6] = 1; mb[mo + 7] = -1;
  86. }
  87. for (let i = 0; i < n; ++i) {
  88. const o = i * 4;
  89. const io = i * 6;
  90. ib[io] = o; ib[io + 1] = o + 1; ib[io + 2] = o + 2;
  91. ib[io + 3] = o + 1; ib[io + 4] = o + 3; ib[io + 5] = o + 2;
  92. }
  93. }