Box3.js 7.6 KB

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