Box3.js 12 KB

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