Box3.js 13 KB

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