Box3.js 9.5 KB

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