Box3.js 12 KB

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