Box3.js 6.0 KB

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