Box3.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. setFromArray: function ( array ) {
  17. this.makeEmpty();
  18. var minX = + Infinity;
  19. var minY = + Infinity;
  20. var minZ = + Infinity;
  21. var maxX = - Infinity;
  22. var maxY = - Infinity;
  23. var maxZ = - Infinity;
  24. for ( var i = 0, il = array.length; i < il; i += 3 ) {
  25. var x = array[ i ];
  26. var y = array[ i + 1 ];
  27. var z = array[ i + 2 ];
  28. if ( x < minX ) minX = x;
  29. if ( y < minY ) minY = y;
  30. if ( z < minZ ) minZ = z;
  31. if ( x > maxX ) maxX = x;
  32. if ( y > maxY ) maxY = y;
  33. if ( z > maxZ ) maxZ = z;
  34. }
  35. this.min.set( minX, minY, minZ );
  36. this.max.set( maxX, maxY, maxZ );
  37. },
  38. setFromPoints: function ( points ) {
  39. this.makeEmpty();
  40. for ( var i = 0, il = points.length; i < il; i ++ ) {
  41. this.expandByPoint( points[ i ] );
  42. }
  43. return this;
  44. },
  45. setFromCenterAndSize: function () {
  46. var v1 = new THREE.Vector3();
  47. return function ( center, size ) {
  48. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  49. this.min.copy( center ).sub( halfSize );
  50. this.max.copy( center ).add( halfSize );
  51. return this;
  52. };
  53. }(),
  54. setFromObject: function () {
  55. // Computes the world-axis-aligned bounding box of an object (including its children),
  56. // accounting for both the object's, and children's, world transforms
  57. var box;
  58. return function ( object ) {
  59. if ( box === undefined ) box = new THREE.Box3();
  60. var scope = this;
  61. this.makeEmpty();
  62. object.updateMatrixWorld( true );
  63. object.traverse( function ( node ) {
  64. var geometry = node.geometry;
  65. if ( geometry !== undefined ) {
  66. if ( geometry.boundingBox === null ) {
  67. geometry.computeBoundingBox();
  68. }
  69. box.copy( geometry.boundingBox );
  70. box.applyMatrix4( node.matrixWorld );
  71. scope.union( box );
  72. }
  73. } );
  74. return this;
  75. };
  76. }(),
  77. clone: function () {
  78. return new this.constructor().copy( this );
  79. },
  80. copy: function ( box ) {
  81. this.min.copy( box.min );
  82. this.max.copy( box.max );
  83. return this;
  84. },
  85. makeEmpty: function () {
  86. this.min.x = this.min.y = this.min.z = + Infinity;
  87. this.max.x = this.max.y = this.max.z = - Infinity;
  88. return this;
  89. },
  90. isEmpty: function () {
  91. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  92. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  93. },
  94. center: function ( optionalTarget ) {
  95. var result = optionalTarget || new THREE.Vector3();
  96. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  97. },
  98. size: function ( optionalTarget ) {
  99. var result = optionalTarget || new THREE.Vector3();
  100. return result.subVectors( this.max, this.min );
  101. },
  102. expandByPoint: function ( point ) {
  103. this.min.min( point );
  104. this.max.max( point );
  105. return this;
  106. },
  107. expandByVector: function ( vector ) {
  108. this.min.sub( vector );
  109. this.max.add( vector );
  110. return this;
  111. },
  112. expandByScalar: function ( scalar ) {
  113. this.min.addScalar( - scalar );
  114. this.max.addScalar( scalar );
  115. return this;
  116. },
  117. containsPoint: function ( point ) {
  118. if ( point.x < this.min.x || point.x > this.max.x ||
  119. point.y < this.min.y || point.y > this.max.y ||
  120. point.z < this.min.z || point.z > this.max.z ) {
  121. return false;
  122. }
  123. return true;
  124. },
  125. containsBox: function ( box ) {
  126. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  127. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  128. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
  129. return true;
  130. }
  131. return false;
  132. },
  133. getParameter: function ( point, optionalTarget ) {
  134. // This can potentially have a divide by zero if the box
  135. // has a size dimension of 0.
  136. var result = optionalTarget || new THREE.Vector3();
  137. return result.set(
  138. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  139. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  140. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  141. );
  142. },
  143. intersectsBox: function ( box ) {
  144. // using 6 splitting planes to rule out intersections.
  145. if ( box.max.x < this.min.x || box.min.x > this.max.x ||
  146. box.max.y < this.min.y || box.min.y > this.max.y ||
  147. box.max.z < this.min.z || box.min.z > this.max.z ) {
  148. return false;
  149. }
  150. return true;
  151. },
  152. intersectsSphere: ( function () {
  153. var closestPoint;
  154. return function intersectsSphere( sphere ) {
  155. if ( closestPoint === undefined ) closestPoint = new THREE.Vector3();
  156. // Find the point on the AABB closest to the sphere center.
  157. this.clampPoint( sphere.center, closestPoint );
  158. // If that point is inside the sphere, the AABB and sphere intersect.
  159. return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  160. };
  161. } )(),
  162. intersectsPlane: function ( plane ) {
  163. // We compute the minimum and maximum dot product values. If those values
  164. // are on the same side (back or front) of the plane, then there is no intersection.
  165. var min, max;
  166. if ( plane.normal.x > 0 ) {
  167. min = plane.normal.x * this.min.x;
  168. max = plane.normal.x * this.max.x;
  169. } else {
  170. min = plane.normal.x * this.max.x;
  171. max = plane.normal.x * this.min.x;
  172. }
  173. if ( plane.normal.y > 0 ) {
  174. min += plane.normal.y * this.min.y;
  175. max += plane.normal.y * this.max.y;
  176. } else {
  177. min += plane.normal.y * this.max.y;
  178. max += plane.normal.y * this.min.y;
  179. }
  180. if ( plane.normal.z > 0 ) {
  181. min += plane.normal.z * this.min.z;
  182. max += plane.normal.z * this.max.z;
  183. } else {
  184. min += plane.normal.z * this.max.z;
  185. max += plane.normal.z * this.min.z;
  186. }
  187. return ( min <= plane.constant && max >= plane.constant );
  188. },
  189. clampPoint: function ( point, optionalTarget ) {
  190. var result = optionalTarget || new THREE.Vector3();
  191. return result.copy( point ).clamp( this.min, this.max );
  192. },
  193. distanceToPoint: function () {
  194. var v1 = new THREE.Vector3();
  195. return function ( point ) {
  196. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  197. return clampedPoint.sub( point ).length();
  198. };
  199. }(),
  200. getBoundingSphere: function () {
  201. var v1 = new THREE.Vector3();
  202. return function ( optionalTarget ) {
  203. var result = optionalTarget || new THREE.Sphere();
  204. result.center = this.center();
  205. result.radius = this.size( v1 ).length() * 0.5;
  206. return result;
  207. };
  208. }(),
  209. intersect: function ( box ) {
  210. this.min.max( box.min );
  211. this.max.min( box.max );
  212. return this;
  213. },
  214. union: function ( box ) {
  215. this.min.min( box.min );
  216. this.max.max( box.max );
  217. return this;
  218. },
  219. applyMatrix4: function () {
  220. var points = [
  221. new THREE.Vector3(),
  222. new THREE.Vector3(),
  223. new THREE.Vector3(),
  224. new THREE.Vector3(),
  225. new THREE.Vector3(),
  226. new THREE.Vector3(),
  227. new THREE.Vector3(),
  228. new THREE.Vector3()
  229. ];
  230. return function ( matrix ) {
  231. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  232. points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  233. points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  234. points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  235. points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  236. points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  237. points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  238. points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  239. points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  240. this.makeEmpty();
  241. this.setFromPoints( points );
  242. return this;
  243. };
  244. }(),
  245. translate: function ( offset ) {
  246. this.min.add( offset );
  247. this.max.add( offset );
  248. return this;
  249. },
  250. equals: function ( box ) {
  251. return box.min.equals( this.min ) && box.max.equals( this.max );
  252. }
  253. };