Box3.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import { Vector3 } from './Vector3.js';
  2. class Box3 {
  3. constructor( min, max ) {
  4. Object.defineProperty( this, 'isBox3', { value: true } );
  5. this.min = ( min !== undefined ) ? min : new Vector3( + Infinity, + Infinity, + Infinity );
  6. this.max = ( max !== undefined ) ? max : new Vector3( - Infinity, - Infinity, - Infinity );
  7. }
  8. set( min, max ) {
  9. this.min.copy( min );
  10. this.max.copy( max );
  11. return this;
  12. }
  13. setFromArray( array ) {
  14. let minX = + Infinity;
  15. let minY = + Infinity;
  16. let minZ = + Infinity;
  17. let maxX = - Infinity;
  18. let maxY = - Infinity;
  19. let maxZ = - Infinity;
  20. for ( let i = 0, l = array.length; i < l; i += 3 ) {
  21. const x = array[ i ];
  22. const y = array[ i + 1 ];
  23. const z = array[ i + 2 ];
  24. if ( x < minX ) minX = x;
  25. if ( y < minY ) minY = y;
  26. if ( z < minZ ) minZ = z;
  27. if ( x > maxX ) maxX = x;
  28. if ( y > maxY ) maxY = y;
  29. if ( z > maxZ ) maxZ = z;
  30. }
  31. this.min.set( minX, minY, minZ );
  32. this.max.set( maxX, maxY, maxZ );
  33. return this;
  34. }
  35. setFromBufferAttribute( attribute ) {
  36. let minX = + Infinity;
  37. let minY = + Infinity;
  38. let minZ = + Infinity;
  39. let maxX = - Infinity;
  40. let maxY = - Infinity;
  41. let maxZ = - Infinity;
  42. for ( let i = 0, l = attribute.count; i < l; i ++ ) {
  43. const x = attribute.getX( i );
  44. const y = attribute.getY( i );
  45. const z = attribute.getZ( i );
  46. if ( x < minX ) minX = x;
  47. if ( y < minY ) minY = y;
  48. if ( z < minZ ) minZ = z;
  49. if ( x > maxX ) maxX = x;
  50. if ( y > maxY ) maxY = y;
  51. if ( z > maxZ ) maxZ = z;
  52. }
  53. this.min.set( minX, minY, minZ );
  54. this.max.set( maxX, maxY, maxZ );
  55. return this;
  56. }
  57. setFromPoints( points ) {
  58. this.makeEmpty();
  59. for ( let i = 0, il = points.length; i < il; i ++ ) {
  60. this.expandByPoint( points[ i ] );
  61. }
  62. return this;
  63. }
  64. setFromCenterAndSize( center, size ) {
  65. const halfSize = _vector.copy( size ).multiplyScalar( 0.5 );
  66. this.min.copy( center ).sub( halfSize );
  67. this.max.copy( center ).add( halfSize );
  68. return this;
  69. }
  70. setFromObject( object ) {
  71. this.makeEmpty();
  72. return this.expandByObject( object );
  73. }
  74. clone() {
  75. return new this.constructor().copy( this );
  76. }
  77. copy( box ) {
  78. this.min.copy( box.min );
  79. this.max.copy( box.max );
  80. return this;
  81. }
  82. makeEmpty() {
  83. this.min.x = this.min.y = this.min.z = + Infinity;
  84. this.max.x = this.max.y = this.max.z = - Infinity;
  85. return this;
  86. }
  87. isEmpty() {
  88. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  89. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  90. }
  91. getCenter( target ) {
  92. if ( target === undefined ) {
  93. console.warn( 'THREE.Box3: .getCenter() target is now required' );
  94. target = new Vector3();
  95. }
  96. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  97. }
  98. getSize( target ) {
  99. if ( target === undefined ) {
  100. console.warn( 'THREE.Box3: .getSize() target is now required' );
  101. target = new Vector3();
  102. }
  103. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min );
  104. }
  105. expandByPoint( point ) {
  106. this.min.min( point );
  107. this.max.max( point );
  108. return this;
  109. }
  110. expandByVector( vector ) {
  111. this.min.sub( vector );
  112. this.max.add( vector );
  113. return this;
  114. }
  115. expandByScalar( scalar ) {
  116. this.min.addScalar( - scalar );
  117. this.max.addScalar( scalar );
  118. return this;
  119. }
  120. expandByObject( object ) {
  121. // Computes the world-axis-aligned bounding box of an object (including its children),
  122. // accounting for both the object's, and children's, world transforms
  123. object.updateWorldMatrix( false, false );
  124. const geometry = object.geometry;
  125. if ( geometry !== undefined ) {
  126. if ( geometry.boundingBox === null ) {
  127. geometry.computeBoundingBox();
  128. }
  129. _box.copy( geometry.boundingBox );
  130. _box.applyMatrix4( object.matrixWorld );
  131. this.union( _box );
  132. }
  133. const children = object.children;
  134. for ( let i = 0, l = children.length; i < l; i ++ ) {
  135. this.expandByObject( children[ i ] );
  136. }
  137. return this;
  138. }
  139. containsPoint( point ) {
  140. return point.x < this.min.x || point.x > this.max.x ||
  141. point.y < this.min.y || point.y > this.max.y ||
  142. point.z < this.min.z || point.z > this.max.z ? false : true;
  143. }
  144. containsBox( box ) {
  145. return 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. }
  149. getParameter( point, target ) {
  150. // This can potentially have a divide by zero if the box
  151. // has a size dimension of 0.
  152. if ( target === undefined ) {
  153. console.warn( 'THREE.Box3: .getParameter() target is now required' );
  154. target = new Vector3();
  155. }
  156. return target.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( box ) {
  163. // using 6 splitting planes to rule out intersections.
  164. return 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 ? false : true;
  167. }
  168. intersectsSphere( sphere ) {
  169. // Find the point on the AABB closest to the sphere center.
  170. this.clampPoint( sphere.center, _vector );
  171. // If that point is inside the sphere, the AABB and sphere intersect.
  172. return _vector.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  173. }
  174. intersectsPlane( plane ) {
  175. // We compute the minimum and maximum dot product values. If those values
  176. // are on the same side (back or front) of the plane, then there is no intersection.
  177. let min, max;
  178. if ( plane.normal.x > 0 ) {
  179. min = plane.normal.x * this.min.x;
  180. max = plane.normal.x * this.max.x;
  181. } else {
  182. min = plane.normal.x * this.max.x;
  183. max = plane.normal.x * this.min.x;
  184. }
  185. if ( plane.normal.y > 0 ) {
  186. min += plane.normal.y * this.min.y;
  187. max += plane.normal.y * this.max.y;
  188. } else {
  189. min += plane.normal.y * this.max.y;
  190. max += plane.normal.y * this.min.y;
  191. }
  192. if ( plane.normal.z > 0 ) {
  193. min += plane.normal.z * this.min.z;
  194. max += plane.normal.z * this.max.z;
  195. } else {
  196. min += plane.normal.z * this.max.z;
  197. max += plane.normal.z * this.min.z;
  198. }
  199. return ( min <= - plane.constant && max >= - plane.constant );
  200. }
  201. intersectsTriangle( triangle ) {
  202. if ( this.isEmpty() ) {
  203. return false;
  204. }
  205. // compute box center and extents
  206. this.getCenter( _center );
  207. _extents.subVectors( this.max, _center );
  208. // translate triangle to aabb origin
  209. _v0.subVectors( triangle.a, _center );
  210. _v1.subVectors( triangle.b, _center );
  211. _v2.subVectors( triangle.c, _center );
  212. // compute edge vectors for triangle
  213. _f0.subVectors( _v1, _v0 );
  214. _f1.subVectors( _v2, _v1 );
  215. _f2.subVectors( _v0, _v2 );
  216. // test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
  217. // make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
  218. // axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
  219. let axes = [
  220. 0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y,
  221. _f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x,
  222. - _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0
  223. ];
  224. if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) {
  225. return false;
  226. }
  227. // test 3 face normals from the aabb
  228. axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
  229. if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) {
  230. return false;
  231. }
  232. // finally testing the face normal of the triangle
  233. // use already existing triangle edge vectors here
  234. _triangleNormal.crossVectors( _f0, _f1 );
  235. axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ];
  236. return satForAxes( axes, _v0, _v1, _v2, _extents );
  237. }
  238. clampPoint( point, target ) {
  239. if ( target === undefined ) {
  240. console.warn( 'THREE.Box3: .clampPoint() target is now required' );
  241. target = new Vector3();
  242. }
  243. return target.copy( point ).clamp( this.min, this.max );
  244. }
  245. distanceToPoint( point ) {
  246. const clampedPoint = _vector.copy( point ).clamp( this.min, this.max );
  247. return clampedPoint.sub( point ).length();
  248. }
  249. getBoundingSphere( target ) {
  250. if ( target === undefined ) {
  251. console.error( 'THREE.Box3: .getBoundingSphere() target is now required' );
  252. //target = new Sphere(); // removed to avoid cyclic dependency
  253. }
  254. this.getCenter( target.center );
  255. target.radius = this.getSize( _vector ).length() * 0.5;
  256. return target;
  257. }
  258. intersect( box ) {
  259. this.min.max( box.min );
  260. this.max.min( box.max );
  261. // 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.
  262. if ( this.isEmpty() ) this.makeEmpty();
  263. return this;
  264. }
  265. union( box ) {
  266. this.min.min( box.min );
  267. this.max.max( box.max );
  268. return this;
  269. }
  270. applyMatrix4( matrix ) {
  271. // transform of empty box is an empty box.
  272. if ( this.isEmpty() ) return this;
  273. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  274. _points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  275. _points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  276. _points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  277. _points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  278. _points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  279. _points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  280. _points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  281. _points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  282. this.setFromPoints( _points );
  283. return this;
  284. }
  285. translate( offset ) {
  286. this.min.add( offset );
  287. this.max.add( offset );
  288. return this;
  289. }
  290. equals( box ) {
  291. return box.min.equals( this.min ) && box.max.equals( this.max );
  292. }
  293. }
  294. function satForAxes( axes, v0, v1, v2, extents ) {
  295. for ( let i = 0, j = axes.length - 3; i <= j; i += 3 ) {
  296. _testAxis.fromArray( axes, i );
  297. // project the aabb onto the seperating axis
  298. const r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z );
  299. // project all 3 vertices of the triangle onto the seperating axis
  300. const p0 = v0.dot( _testAxis );
  301. const p1 = v1.dot( _testAxis );
  302. const p2 = v2.dot( _testAxis );
  303. // actual test, basically see if either of the most extreme of the triangle points intersects r
  304. if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
  305. // points of the projected triangle are outside the projected half-length of the aabb
  306. // the axis is seperating and we can exit
  307. return false;
  308. }
  309. }
  310. return true;
  311. }
  312. const _points = [
  313. new Vector3(),
  314. new Vector3(),
  315. new Vector3(),
  316. new Vector3(),
  317. new Vector3(),
  318. new Vector3(),
  319. new Vector3(),
  320. new Vector3()
  321. ];
  322. const _vector = new Vector3();
  323. const _box = new Box3();
  324. // triangle centered vertices
  325. const _v0 = new Vector3();
  326. const _v1 = new Vector3();
  327. const _v2 = new Vector3();
  328. // triangle edge vectors
  329. const _f0 = new Vector3();
  330. const _f1 = new Vector3();
  331. const _f2 = new Vector3();
  332. const _center = new Vector3();
  333. const _extents = new Vector3();
  334. const _triangleNormal = new Vector3();
  335. const _testAxis = new Vector3();
  336. export { Box3 };