Box3.js 11 KB

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