Box3.js 12 KB

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