Box3.js 9.3 KB

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