Points.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import * as THREE from "../libs/three.js/build/three.module.js";
  2. export class Points {
  3. constructor () {
  4. this.boundingBox = new THREE.Box3();
  5. this.numPoints = 0;
  6. this.data = {};
  7. }
  8. add (points) {
  9. let currentSize = this.numPoints;
  10. let additionalSize = points.numPoints;
  11. let newSize = currentSize + additionalSize;
  12. let thisAttributes = Object.keys(this.data);
  13. let otherAttributes = Object.keys(points.data);
  14. let attributes = new Set([...thisAttributes, ...otherAttributes]);
  15. for (let attribute of attributes) {
  16. if (thisAttributes.includes(attribute) && otherAttributes.includes(attribute)) {
  17. // attribute in both, merge
  18. let Type = this.data[attribute].constructor;
  19. let merged = new Type(this.data[attribute].length + points.data[attribute].length);
  20. merged.set(this.data[attribute], 0);
  21. merged.set(points.data[attribute], this.data[attribute].length);
  22. this.data[attribute] = merged;
  23. } else if (thisAttributes.includes(attribute) && !otherAttributes.includes(attribute)) {
  24. // attribute only in this; take over this and expand to new size
  25. let elementsPerPoint = this.data[attribute].length / this.numPoints;
  26. let Type = this.data[attribute].constructor;
  27. let expanded = new Type(elementsPerPoint * newSize);
  28. expanded.set(this.data[attribute], 0);
  29. this.data[attribute] = expanded;
  30. } else if (!thisAttributes.includes(attribute) && otherAttributes.includes(attribute)) {
  31. // attribute only in points to be added; take over new points and expand to new size
  32. let elementsPerPoint = points.data[attribute].length / points.numPoints;
  33. let Type = points.data[attribute].constructor;
  34. let expanded = new Type(elementsPerPoint * newSize);
  35. expanded.set(points.data[attribute], elementsPerPoint * currentSize);
  36. this.data[attribute] = expanded;
  37. }
  38. }
  39. this.numPoints = newSize;
  40. this.boundingBox.union(points.boundingBox);
  41. }
  42. }