Home Reference Source

src/volume/csg/Operation.js

  1. import { Box3 } from "three";
  2.  
  3. /**
  4. * A CSG operation.
  5. */
  6.  
  7. export class Operation {
  8.  
  9. /**
  10. * Constructs a new operation.
  11. *
  12. * @param {OperationType} type - The type of this operation.
  13. * @param {Operation} ...children - Child operations.
  14. */
  15.  
  16. constructor(type, ...children) {
  17.  
  18. /**
  19. * The type of this operation.
  20. *
  21. * @type {OperationType}
  22. */
  23.  
  24. this.type = type;
  25.  
  26. /**
  27. * A list of operations.
  28. *
  29. * Right-hand side operands have precedence, meaning that the result of the
  30. * first item in the list will be dominated by the result of the second one,
  31. * etc.
  32. *
  33. * @type {Operation[]}
  34. * @private
  35. */
  36.  
  37. this.children = children;
  38.  
  39. /**
  40. * The bounding box of this operation.
  41. *
  42. * @type {Box3}
  43. * @private
  44. */
  45.  
  46. this.boundingBox = null;
  47.  
  48. }
  49.  
  50. /**
  51. * Calculates the complete bounding box of this CSG operation if it doesn't
  52. * exist yet and returns it.
  53. *
  54. * @return {Box3} The bounding box.
  55. */
  56.  
  57. getBoundingBox() {
  58.  
  59. if(this.boundingBox === null) {
  60.  
  61. this.boundingBox = this.computeBoundingBox();
  62.  
  63. }
  64.  
  65. return this.boundingBox;
  66.  
  67. }
  68.  
  69. /**
  70. * Calculates the bounding box of this CSG operation while taking all child
  71. * operations into account.
  72. *
  73. * @return {Box3} The bounding box.
  74. */
  75.  
  76. computeBoundingBox() {
  77.  
  78. const children = this.children;
  79. const boundingBox = new Box3();
  80.  
  81. let i, l;
  82.  
  83. for(i = 0, l = children.length; i < l; ++i) {
  84.  
  85. boundingBox.union(children[i].getBoundingBox());
  86.  
  87. }
  88.  
  89. return boundingBox;
  90.  
  91. }
  92.  
  93. }