Box3.js 8.9 KB

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