Box3.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @author Ben Houston / ben@exocortex.com / http://github.com/bhouston
  3. */
  4. ( function ( THREE ) {
  5. THREE.Box3 = function ( min, max ) {
  6. this.min = min || new THREE.Vector3();
  7. this.max = max || this.min; // This is done on purpose so you can make a box using a single point and then expand it.
  8. };
  9. THREE.Box3.prototype.set = function ( min, max ) {
  10. this.min = min;
  11. this.max = max;
  12. return this;
  13. };
  14. THREE.Box3.prototype.copy = function ( box ) {
  15. this.min = box.min;
  16. this.max = box.max;
  17. return this;
  18. };
  19. THREE.Box3.prototype.empty = function () {
  20. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  21. return
  22. (( this.max.x - this.min.x ) <= 0 ) ||
  23. (( this.max.y - this.min.y ) <= 0 ) ||
  24. (( this.max.z - this.min.z ) <= 0 );
  25. };
  26. THREE.Box3.prototype.volume = function () {
  27. return
  28. ( this.max.x - this.min.x ) *
  29. ( this.max.y - this.min.y ) *
  30. ( this.max.z - this.min.z );
  31. };
  32. THREE.Box3.prototype.center = function () {
  33. return new THREE.Vector3().add( this.min, this.max ).multiplyScalar( 0.5 );
  34. };
  35. THREE.Box3.prototype.size = function () {
  36. return new THREE.Vector3().sub( this.max, this.min );
  37. };
  38. THREE.Box3.prototype.extendByPoint = function ( point ) {
  39. this.min.minSelf( point );
  40. this.max.maxSelf( point );
  41. return this;
  42. };
  43. THREE.Box3.prototype.extendByBox = function ( box ) {
  44. // Assumption: for speed purposes, we assume that the box is not empty, e.g. box.min < box.max
  45. this.min.minSelf( box.min );
  46. this.max.maxSelf( box.max );
  47. return this;
  48. };
  49. THREE.Box3.prototype.expandByVector = function ( vector ) {
  50. this.min.subSelf( vector );
  51. this.max.addSelf( vector );
  52. return this;
  53. };
  54. THREE.Box3.prototype.expandByScalar = function ( scalar ) {
  55. this.min.addScalar( -scalar );
  56. this.max.addScalar( scalar );
  57. return this;
  58. };
  59. THREE.Box3.prototype.containsPoint = function ( point ) {
  60. if(
  61. ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
  62. ( this.min.y <= point.y ) && ( point.y <= this.max.y ) &&
  63. ( this.min.z <= point.z ) && ( point.z <= this.max.z )
  64. ) {
  65. return true;
  66. }
  67. return false;
  68. };
  69. THREE.Box3.prototype.containsBox = function ( box ) {
  70. // Assumption: for speed purposes, we assume that the box is not empty, e.g. box.min < box.max
  71. if(
  72. ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  73. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  74. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z )
  75. ) {
  76. return true;
  77. }
  78. return false;
  79. };
  80. THREE.Box3.prototype.isIntersection = function ( box ) {
  81. // Assumption: for speed purposes, we assume that the box is not empty, e.g. box.min < box.max
  82. // using 6 splitting planes to rule out intersections.
  83. if(
  84. ( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
  85. ( this.max.y < box.min.y ) || ( box.min.y > this.max.y ) ||
  86. ( this.max.z < box.min.z ) || ( box.min.z > this.max.z )
  87. ) {
  88. return false;
  89. }
  90. return true;
  91. };
  92. THREE.Box3.prototype.getParameter = function ( point ) {
  93. // Assumption: for speed purposes, we assume that the box is not empty, e.g. box.min < box.max
  94. // This assumption can lead to a divide by zero if the box is actually empty.
  95. // Suggestions? I don't want to check for empty as it is a speed hit, but maybe
  96. // it is necessary.
  97. return new THREE.Vector3(
  98. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  99. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  100. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  101. );
  102. };
  103. THREE.Box3.prototype.clampPoint = function ( point ) {
  104. return point.maxSelf( this.min ).minSelf( this.max );
  105. };
  106. THREE.Box3.prototype.distanceToPoint = function ( point ) {
  107. return this.clampPoint( point ).subSelf( point ).length();
  108. };
  109. THREE.Box3.prototype.intersect = function ( box ) {
  110. // Assumption: for speed purposes, we assume that the box is not empty, e.g. box.min < box.max
  111. this.min = this.clampPoint( box.min );
  112. this.max = this.clampPoint( box.max );
  113. return this;
  114. };
  115. THREE.Box3.prototype.union = function ( box ) {
  116. // Assumption: for speed purposes, we assume that the box is not empty, e.g. box.min < box.max
  117. this.extendByPoint( box.min );
  118. this.extendByPoint( box.max );
  119. return this;
  120. };
  121. THREE.Box3.prototype.translate = function ( offset ) {
  122. this.min.addSelf( offset );
  123. this.max.addSelf( offset );
  124. return this;
  125. };
  126. }( THREE ) );
粤ICP备19079148号