Box3.js 13 KB

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