Box3.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * @author bhouston / http://clara.io
  3. * @author WestLangley / http://github.com/WestLangley
  4. */
  5. THREE.Box3 = function ( min, max ) {
  6. this.min = ( min !== undefined ) ? min : new THREE.Vector3( Infinity, Infinity, Infinity );
  7. this.max = ( max !== undefined ) ? max : new THREE.Vector3( - Infinity, - Infinity, - Infinity );
  8. };
  9. THREE.Box3.prototype = {
  10. constructor: THREE.Box3,
  11. set: function ( min, max ) {
  12. this.min.copy( min );
  13. this.max.copy( max );
  14. return this;
  15. },
  16. setFromPoints: function ( points ) {
  17. this.makeEmpty();
  18. for ( var i = 0, il = points.length; i < il; i ++ ) {
  19. this.expandByPoint( points[ i ] );
  20. }
  21. return this;
  22. },
  23. setFromCenterAndSize: function () {
  24. var v1 = new THREE.Vector3();
  25. return function ( center, size ) {
  26. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  27. this.min.copy( center ).sub( halfSize );
  28. this.max.copy( center ).add( halfSize );
  29. return this;
  30. };
  31. }(),
  32. setFromObject: function () {
  33. // Computes the world-axis-aligned bounding box of an object (including its children),
  34. // accounting for both the object's, and children's, world transforms
  35. var v1 = new THREE.Vector3();
  36. return function ( object ) {
  37. var scope = this;
  38. object.updateMatrixWorld( true );
  39. this.makeEmpty();
  40. object.traverse( function ( node ) {
  41. var geometry = node.geometry;
  42. if ( geometry !== undefined ) {
  43. if ( geometry instanceof THREE.Geometry ) {
  44. var vertices = geometry.vertices;
  45. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  46. v1.copy( vertices[ i ] );
  47. v1.applyMatrix4( node.matrixWorld );
  48. scope.expandByPoint( v1 );
  49. }
  50. } else if ( geometry instanceof THREE.BufferGeometry && geometry.attributes[ 'position' ] !== undefined ) {
  51. var positions = geometry.attributes[ 'position' ].array;
  52. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  53. v1.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  54. v1.applyMatrix4( node.matrixWorld );
  55. scope.expandByPoint( v1 );
  56. }
  57. }
  58. }
  59. } );
  60. return this;
  61. };
  62. }(),
  63. clone: function () {
  64. return new this.constructor().copy( this );
  65. },
  66. copy: function ( box ) {
  67. this.min.copy( box.min );
  68. this.max.copy( box.max );
  69. return this;
  70. },
  71. makeEmpty: function () {
  72. this.min.x = this.min.y = this.min.z = Infinity;
  73. this.max.x = this.max.y = this.max.z = - Infinity;
  74. return this;
  75. },
  76. empty: function () {
  77. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  78. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  79. },
  80. center: function ( optionalTarget ) {
  81. var result = optionalTarget || new THREE.Vector3();
  82. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  83. },
  84. size: function ( optionalTarget ) {
  85. var result = optionalTarget || new THREE.Vector3();
  86. return result.subVectors( this.max, this.min );
  87. },
  88. expandByPoint: function ( point ) {
  89. this.min.min( point );
  90. this.max.max( point );
  91. return this;
  92. },
  93. expandByVector: function ( vector ) {
  94. this.min.sub( vector );
  95. this.max.add( vector );
  96. return this;
  97. },
  98. expandByScalar: function ( scalar ) {
  99. this.min.addScalar( - scalar );
  100. this.max.addScalar( scalar );
  101. return this;
  102. },
  103. containsPoint: function ( point ) {
  104. if ( point.x < this.min.x || point.x > this.max.x ||
  105. point.y < this.min.y || point.y > this.max.y ||
  106. point.z < this.min.z || point.z > this.max.z ) {
  107. return false;
  108. }
  109. return true;
  110. },
  111. containsBox: function ( box ) {
  112. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  113. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  114. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
  115. return true;
  116. }
  117. return false;
  118. },
  119. getParameter: function ( point, optionalTarget ) {
  120. // This can potentially have a divide by zero if the box
  121. // has a size dimension of 0.
  122. var result = optionalTarget || new THREE.Vector3();
  123. return result.set(
  124. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  125. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  126. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  127. );
  128. },
  129. isIntersectionBox: function ( box ) {
  130. // using 6 splitting planes to rule out intersections.
  131. if ( box.max.x < this.min.x || box.min.x > this.max.x ||
  132. box.max.y < this.min.y || box.min.y > this.max.y ||
  133. box.max.z < this.min.z || box.min.z > this.max.z ) {
  134. return false;
  135. }
  136. return true;
  137. },
  138. isIntersectionSphere: ( function () {
  139. var closestPoint;
  140. return function isIntersectionSphere( sphere ) {
  141. if ( closestPoint === undefined ) closestPoint = new THREE.Vector3();
  142. // Find the point on the AABB closest to the sphere center.
  143. this.clampPoint( sphere.center, closestPoint );
  144. // If that point is inside the sphere, the AABB and sphere intersect.
  145. return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  146. };
  147. } )(),
  148. clampPoint: function ( point, optionalTarget ) {
  149. var result = optionalTarget || new THREE.Vector3();
  150. return result.copy( point ).clamp( this.min, this.max );
  151. },
  152. distanceToPoint: function () {
  153. var v1 = new THREE.Vector3();
  154. return function ( point ) {
  155. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  156. return clampedPoint.sub( point ).length();
  157. };
  158. }(),
  159. getBoundingSphere: function () {
  160. var v1 = new THREE.Vector3();
  161. return function ( optionalTarget ) {
  162. var result = optionalTarget || new THREE.Sphere();
  163. result.center = this.center();
  164. result.radius = this.size( v1 ).length() * 0.5;
  165. return result;
  166. };
  167. }(),
  168. intersect: function ( box ) {
  169. this.min.max( box.min );
  170. this.max.min( box.max );
  171. return this;
  172. },
  173. union: function ( box ) {
  174. this.min.min( box.min );
  175. this.max.max( box.max );
  176. return this;
  177. },
  178. applyMatrix4: function () {
  179. var points = [
  180. new THREE.Vector3(),
  181. new THREE.Vector3(),
  182. new THREE.Vector3(),
  183. new THREE.Vector3(),
  184. new THREE.Vector3(),
  185. new THREE.Vector3(),
  186. new THREE.Vector3(),
  187. new THREE.Vector3()
  188. ];
  189. return function ( matrix ) {
  190. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  191. points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  192. points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  193. points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  194. points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  195. points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  196. points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  197. points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  198. points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  199. this.makeEmpty();
  200. this.setFromPoints( points );
  201. return this;
  202. };
  203. }(),
  204. translate: function ( offset ) {
  205. this.min.add( offset );
  206. this.max.add( offset );
  207. return this;
  208. },
  209. equals: function ( box ) {
  210. return box.min.equals( this.min ) && box.max.equals( this.max );
  211. }
  212. };