Box3.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Box3 = function ( min, max ) {
  5. this.min = min !== undefined ? min.clone() : new THREE.Vector3( Infinity, Infinity, Infinity );
  6. this.max = max !== undefined ? max.clone() : new THREE.Vector3( -Infinity, -Infinity, -Infinity );
  7. };
  8. THREE.Box3.prototype = {
  9. constructor: THREE.Box3,
  10. set: function ( min, max ) {
  11. this.min.copy( min );
  12. this.max.copy( max );
  13. return this;
  14. },
  15. setFromPoints: function ( points ) {
  16. if ( points.length > 0 ) {
  17. var p = points[ 0 ];
  18. this.min.copy( p );
  19. this.max.copy( p );
  20. for ( var i = 1, il = points.length; i < il; i ++ ) {
  21. p = points[ i ];
  22. if ( p.x < this.min.x ) {
  23. this.min.x = p.x;
  24. } else if ( p.x > this.max.x ) {
  25. this.max.x = p.x;
  26. }
  27. if ( p.y < this.min.y ) {
  28. this.min.y = p.y;
  29. } else if ( p.y > this.max.y ) {
  30. this.max.y = p.y;
  31. }
  32. if ( p.z < this.min.z ) {
  33. this.min.z = p.z;
  34. } else if ( p.z > this.max.z ) {
  35. this.max.z = p.z;
  36. }
  37. }
  38. } else {
  39. this.makeEmpty();
  40. }
  41. return this;
  42. },
  43. setFromCenterAndSize: function ( center, size ) {
  44. var halfSize = THREE.Box3.__v1.copy( size ).multiplyScalar( 0.5 );
  45. this.min.copy( center ).subSelf( halfSize );
  46. this.max.copy( center ).addSelf( halfSize );
  47. return this;
  48. },
  49. copy: function ( box ) {
  50. this.min.copy( box.min );
  51. this.max.copy( box.max );
  52. return this;
  53. },
  54. makeEmpty: function () {
  55. this.min.x = this.min.y = this.min.z = Infinity;
  56. this.max.x = this.max.y = this.max.z = -Infinity;
  57. return this;
  58. },
  59. empty: function () {
  60. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  61. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  62. },
  63. center: function ( optionalTarget ) {
  64. var result = optionalTarget || new THREE.Vector3();
  65. return result.add( this.min, this.max ).multiplyScalar( 0.5 );
  66. },
  67. size: function ( optionalTarget ) {
  68. var result = optionalTarget || new THREE.Vector3();
  69. return result.sub( this.max, this.min );
  70. },
  71. expandByPoint: function ( point ) {
  72. this.min.minSelf( point );
  73. this.max.maxSelf( point );
  74. return this;
  75. },
  76. expandByVector: function ( vector ) {
  77. this.min.subSelf( vector );
  78. this.max.addSelf( vector );
  79. return this;
  80. },
  81. expandByScalar: function ( scalar ) {
  82. this.min.addScalar( -scalar );
  83. this.max.addScalar( scalar );
  84. return this;
  85. },
  86. containsPoint: function ( point ) {
  87. if ( ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
  88. ( this.min.y <= point.y ) && ( point.y <= this.max.y ) &&
  89. ( this.min.z <= point.z ) && ( point.z <= this.max.z ) ) {
  90. return true;
  91. }
  92. return false;
  93. },
  94. containsBox: function ( box ) {
  95. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  96. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  97. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
  98. return true;
  99. }
  100. return false;
  101. },
  102. getParameter: function ( point ) {
  103. // This can potentially have a divide by zero if the box
  104. // has a size dimension of 0.
  105. return new THREE.Vector3(
  106. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  107. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  108. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  109. );
  110. },
  111. isIntersectionBox: function ( box ) {
  112. // using 6 splitting planes to rule out intersections.
  113. if ( ( box.max.x < this.min.x ) || ( box.min.x > this.max.x ) ||
  114. ( box.max.y < this.min.y ) || ( box.min.y > this.max.y ) ||
  115. ( box.max.z < this.min.z ) || ( box.min.z > this.max.z ) ) {
  116. return false;
  117. }
  118. return true;
  119. },
  120. clampPoint: function ( point, optionalTarget ) {
  121. var result = optionalTarget || new THREE.Vector3();
  122. return new THREE.Vector3().copy( point ).clampSelf( this.min, this.max );
  123. },
  124. distanceToPoint: function ( point ) {
  125. var clampedPoint = THREE.Box3.__v1.copy( point ).clampSelf( this.min, this.max );
  126. return clampedPoint.subSelf( point ).length();
  127. },
  128. intersect: function ( box ) {
  129. this.min.maxSelf( box.min );
  130. this.max.minSelf( box.max );
  131. return this;
  132. },
  133. union: function ( box ) {
  134. this.min.minSelf( box.min );
  135. this.max.maxSelf( box.max );
  136. return this;
  137. },
  138. transform: function ( matrix ) {
  139. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  140. var newPoints = [
  141. matrix.multiplyVector3( THREE.Box3.__v0.set( this.min.x, this.min.y, this.min.z ) ), // 000
  142. matrix.multiplyVector3( THREE.Box3.__v1.set( this.min.x, this.min.y, this.max.z ) ), // 001
  143. matrix.multiplyVector3( THREE.Box3.__v2.set( this.min.x, this.max.y, this.min.z ) ), // 010
  144. matrix.multiplyVector3( THREE.Box3.__v3.set( this.min.x, this.max.y, this.max.z ) ), // 011
  145. matrix.multiplyVector3( THREE.Box3.__v4.set( this.max.x, this.min.y, this.min.z ) ), // 100
  146. matrix.multiplyVector3( THREE.Box3.__v5.set( this.max.x, this.min.y, this.max.z ) ), // 101
  147. matrix.multiplyVector3( THREE.Box3.__v6.set( this.max.x, this.max.y, this.min.z ) ), // 110
  148. matrix.multiplyVector3( THREE.Box3.__v7.set( this.max.x, this.max.y, this.max.z ) ) // 111
  149. ];
  150. this.makeEmpty();
  151. this.setFromPoints( newPoints );
  152. return this;
  153. },
  154. translate: function ( offset ) {
  155. this.min.addSelf( offset );
  156. this.max.addSelf( offset );
  157. return this;
  158. },
  159. equals: function ( box ) {
  160. return box.min.equals( this.min ) && box.max.equals( this.max );
  161. },
  162. clone: function () {
  163. return new THREE.Box3().copy( this );
  164. }
  165. };
  166. THREE.Box3.__v0 = new THREE.Vector3();
  167. THREE.Box3.__v1 = new THREE.Vector3();
  168. THREE.Box3.__v2 = new THREE.Vector3();
  169. THREE.Box3.__v3 = new THREE.Vector3();
  170. THREE.Box3.__v4 = new THREE.Vector3();
  171. THREE.Box3.__v5 = new THREE.Vector3();
  172. THREE.Box3.__v6 = new THREE.Vector3();
  173. THREE.Box3.__v7 = new THREE.Vector3();