Box3.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import { Vector3 } from './Vector3';
  2. import { Sphere } from './Sphere';
  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 v1 = new Vector3();
  126. return function expandByObject( object ) {
  127. var scope = this;
  128. object.updateMatrixWorld( true );
  129. object.traverse( function ( node ) {
  130. var i, l;
  131. var geometry = node.geometry;
  132. if ( geometry !== undefined ) {
  133. if ( geometry.isGeometry ) {
  134. var vertices = geometry.vertices;
  135. for ( i = 0, l = vertices.length; i < l; i ++ ) {
  136. v1.copy( vertices[ i ] );
  137. v1.applyMatrix4( node.matrixWorld );
  138. scope.expandByPoint( v1 );
  139. }
  140. } else if ( geometry.isBufferGeometry ) {
  141. var attribute = geometry.attributes.position;
  142. if ( attribute !== undefined ) {
  143. for ( i = 0, l = attribute.count; i < l; i ++ ) {
  144. v1.fromBufferAttribute( attribute, i ).applyMatrix4( node.matrixWorld );
  145. scope.expandByPoint( v1 );
  146. }
  147. }
  148. }
  149. }
  150. } );
  151. return this;
  152. };
  153. }(),
  154. containsPoint: function ( point ) {
  155. return point.x < this.min.x || point.x > this.max.x ||
  156. point.y < this.min.y || point.y > this.max.y ||
  157. point.z < this.min.z || point.z > this.max.z ? false : true;
  158. },
  159. containsBox: function ( box ) {
  160. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  161. this.min.y <= box.min.y && box.max.y <= this.max.y &&
  162. this.min.z <= box.min.z && box.max.z <= this.max.z;
  163. },
  164. getParameter: function ( point, optionalTarget ) {
  165. // This can potentially have a divide by zero if the box
  166. // has a size dimension of 0.
  167. var result = optionalTarget || new Vector3();
  168. return result.set(
  169. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  170. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  171. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  172. );
  173. },
  174. intersectsBox: function ( box ) {
  175. // using 6 splitting planes to rule out intersections.
  176. return box.max.x < this.min.x || box.min.x > this.max.x ||
  177. box.max.y < this.min.y || box.min.y > this.max.y ||
  178. box.max.z < this.min.z || box.min.z > this.max.z ? false : true;
  179. },
  180. intersectsSphere: ( function () {
  181. var closestPoint = new Vector3();
  182. return function intersectsSphere( sphere ) {
  183. // Find the point on the AABB closest to the sphere center.
  184. this.clampPoint( sphere.center, closestPoint );
  185. // If that point is inside the sphere, the AABB and sphere intersect.
  186. return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  187. };
  188. } )(),
  189. intersectsPlane: function ( plane ) {
  190. // We compute the minimum and maximum dot product values. If those values
  191. // are on the same side (back or front) of the plane, then there is no intersection.
  192. var min, max;
  193. if ( plane.normal.x > 0 ) {
  194. min = plane.normal.x * this.min.x;
  195. max = plane.normal.x * this.max.x;
  196. } else {
  197. min = plane.normal.x * this.max.x;
  198. max = plane.normal.x * this.min.x;
  199. }
  200. if ( plane.normal.y > 0 ) {
  201. min += plane.normal.y * this.min.y;
  202. max += plane.normal.y * this.max.y;
  203. } else {
  204. min += plane.normal.y * this.max.y;
  205. max += plane.normal.y * this.min.y;
  206. }
  207. if ( plane.normal.z > 0 ) {
  208. min += plane.normal.z * this.min.z;
  209. max += plane.normal.z * this.max.z;
  210. } else {
  211. min += plane.normal.z * this.max.z;
  212. max += plane.normal.z * this.min.z;
  213. }
  214. return ( min <= plane.constant && max >= plane.constant );
  215. },
  216. clampPoint: function ( point, optionalTarget ) {
  217. var result = optionalTarget || new Vector3();
  218. return result.copy( point ).clamp( this.min, this.max );
  219. },
  220. distanceToPoint: function () {
  221. var v1 = new Vector3();
  222. return function distanceToPoint( point ) {
  223. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  224. return clampedPoint.sub( point ).length();
  225. };
  226. }(),
  227. getBoundingSphere: function () {
  228. var v1 = new Vector3();
  229. return function getBoundingSphere( optionalTarget ) {
  230. var result = optionalTarget || new Sphere();
  231. this.getCenter( result.center );
  232. result.radius = this.getSize( v1 ).length() * 0.5;
  233. return result;
  234. };
  235. }(),
  236. intersect: function ( box ) {
  237. this.min.max( box.min );
  238. this.max.min( box.max );
  239. // 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.
  240. if( this.isEmpty() ) this.makeEmpty();
  241. return this;
  242. },
  243. union: function ( box ) {
  244. this.min.min( box.min );
  245. this.max.max( box.max );
  246. return this;
  247. },
  248. applyMatrix4: function () {
  249. var points = [
  250. new Vector3(),
  251. new Vector3(),
  252. new Vector3(),
  253. new Vector3(),
  254. new Vector3(),
  255. new Vector3(),
  256. new Vector3(),
  257. new Vector3()
  258. ];
  259. return function applyMatrix4( matrix ) {
  260. // transform of empty box is an empty box.
  261. if( this.isEmpty() ) return this;
  262. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  263. points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  264. points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  265. points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  266. points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  267. points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  268. points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  269. points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  270. points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  271. this.setFromPoints( points );
  272. return this;
  273. };
  274. }(),
  275. translate: function ( offset ) {
  276. this.min.add( offset );
  277. this.max.add( offset );
  278. return this;
  279. },
  280. equals: function ( box ) {
  281. return box.min.equals( this.min ) && box.max.equals( this.max );
  282. }
  283. } );
  284. export { Box3 };