Box3.js 13 KB

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