cylinders-builder.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2020-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Gianluca Tomasello <giagitom@gmail.com>
  6. */
  7. import { ChunkedArray } from '../../../mol-data/util';
  8. import { Cylinders } from './cylinders';
  9. import { Vec3 } from '../../../mol-math/linear-algebra';
  10. export interface CylindersBuilder {
  11. add(startX: number, startY: number, startZ: number, endX: number, endY: number, endZ: number, radiusScale: number, topCap: boolean, bottomCap: boolean, group: number): void
  12. addFixedCountDashes(start: Vec3, end: Vec3, segmentCount: number, radiusScale: number, topCap: boolean, bottomCap: boolean, stubCap: boolean, group: number): void
  13. addFixedLengthDashes(start: Vec3, end: Vec3, segmentLength: number, radiusScale: number, topCap: boolean, bottomCap: boolean, group: number): void
  14. getCylinders(): Cylinders
  15. }
  16. const tmpVecA = Vec3();
  17. const tmpVecB = Vec3();
  18. const tmpDir = Vec3();
  19. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  20. const caAdd = ChunkedArray.add;
  21. const caAdd3 = ChunkedArray.add3;
  22. export namespace CylindersBuilder {
  23. export function create(initialCount = 2048, chunkSize = 1024, cylinders?: Cylinders): CylindersBuilder {
  24. const groups = ChunkedArray.create(Float32Array, 1, chunkSize, cylinders ? cylinders.groupBuffer.ref.value : initialCount);
  25. const starts = ChunkedArray.create(Float32Array, 3, chunkSize, cylinders ? cylinders.startBuffer.ref.value : initialCount);
  26. const ends = ChunkedArray.create(Float32Array, 3, chunkSize, cylinders ? cylinders.endBuffer.ref.value : initialCount);
  27. const scales = ChunkedArray.create(Float32Array, 1, chunkSize, cylinders ? cylinders.scaleBuffer.ref.value : initialCount);
  28. const caps = ChunkedArray.create(Float32Array, 1, chunkSize, cylinders ? cylinders.capBuffer.ref.value : initialCount);
  29. const add = (startX: number, startY: number, startZ: number, endX: number, endY: number, endZ: number, radiusScale: number, topCap: boolean, bottomCap: boolean, group: number) => {
  30. for (let i = 0; i < 6; ++i) {
  31. caAdd3(starts, startX, startY, startZ);
  32. caAdd3(ends, endX, endY, endZ);
  33. caAdd(groups, group);
  34. caAdd(scales, radiusScale);
  35. caAdd(caps, (topCap ? 1 : 0) + (bottomCap ? 2 : 0));
  36. }
  37. };
  38. const addFixedCountDashes = (start: Vec3, end: Vec3, segmentCount: number, radiusScale: number, topCap: boolean, bottomCap: boolean, stubCap: boolean, group: number) => {
  39. const d = Vec3.distance(start, end);
  40. const isOdd = segmentCount % 2 !== 0;
  41. const s = Math.floor((segmentCount + 1) / 2);
  42. const step = d / (segmentCount + 0.5);
  43. Vec3.setMagnitude(tmpDir, Vec3.sub(tmpDir, end, start), step);
  44. Vec3.copy(tmpVecA, start);
  45. for (let j = 0; j < s; ++j) {
  46. Vec3.add(tmpVecA, tmpVecA, tmpDir);
  47. if (isOdd && j === s - 1) {
  48. Vec3.copy(tmpVecB, end);
  49. if (!stubCap) bottomCap = false;
  50. } else {
  51. Vec3.add(tmpVecB, tmpVecA, tmpDir);
  52. }
  53. add(tmpVecA[0], tmpVecA[1], tmpVecA[2], tmpVecB[0], tmpVecB[1], tmpVecB[2], radiusScale, topCap, bottomCap, group);
  54. Vec3.add(tmpVecA, tmpVecA, tmpDir);
  55. }
  56. };
  57. return {
  58. add,
  59. addFixedCountDashes,
  60. addFixedLengthDashes: (start: Vec3, end: Vec3, segmentLength: number, radiusScale: number, topCap: boolean, bottomCap: boolean, group: number) => {
  61. const d = Vec3.distance(start, end);
  62. addFixedCountDashes(start, end, d / segmentLength, radiusScale, topCap, bottomCap, true, group);
  63. },
  64. getCylinders: () => {
  65. const cylinderCount = groups.elementCount / 6;
  66. const gb = ChunkedArray.compact(groups, true) as Float32Array;
  67. const sb = ChunkedArray.compact(starts, true) as Float32Array;
  68. const eb = ChunkedArray.compact(ends, true) as Float32Array;
  69. const ab = ChunkedArray.compact(scales, true) as Float32Array;
  70. const cb = ChunkedArray.compact(caps, true) as Float32Array;
  71. const mb = cylinders && cylinderCount <= cylinders.cylinderCount ? cylinders.mappingBuffer.ref.value : new Float32Array(cylinderCount * 18);
  72. const ib = cylinders && cylinderCount <= cylinders.cylinderCount ? cylinders.indexBuffer.ref.value : new Uint32Array(cylinderCount * 12);
  73. if (!cylinders || cylinderCount > cylinders.cylinderCount) fillMappingAndIndices(cylinderCount, mb, ib);
  74. return Cylinders.create(mb, ib, gb, sb, eb, ab, cb, cylinderCount, cylinders);
  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 * 18;
  82. mb[mo] = -1; mb[mo + 1] = 1; mb[mo + 2] = -1;
  83. mb[mo + 3] = -1; mb[mo + 4] = -1; mb[mo + 5] = -1;
  84. mb[mo + 6] = 1; mb[mo + 7] = 1; mb[mo + 8] = -1;
  85. mb[mo + 9] = 1; mb[mo + 10] = 1; mb[mo + 11] = 1;
  86. mb[mo + 12] = 1; mb[mo + 13] = -1; mb[mo + 14] = -1;
  87. mb[mo + 15] = 1; mb[mo + 16] = -1; mb[mo + 17] = 1;
  88. }
  89. for (let i = 0; i < n; ++i) {
  90. const o = i * 6;
  91. const io = i * 12;
  92. ib[io] = o; ib[io + 1] = o + 1; ib[io + 2] = o + 2;
  93. ib[io + 3] = o + 1; ib[io + 4] = o + 4; ib[io + 5] = o + 2;
  94. ib[io + 6] = o + 2; ib[io + 7] = o + 4; ib[io + 8] = o + 3;
  95. ib[io + 9] = o + 4; ib[io + 10] = o + 5; ib[io + 11] = o + 3;
  96. }
  97. }