Box3.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. clampPoint: function ( point, optionalTarget ) {
  218. var result = optionalTarget || new Vector3();
  219. return result.copy( point ).clamp( this.min, this.max );
  220. },
  221. distanceToPoint: function () {
  222. var v1 = new Vector3();
  223. return function distanceToPoint( point ) {
  224. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  225. return clampedPoint.sub( point ).length();
  226. };
  227. }(),
  228. getBoundingSphere: function () {
  229. var v1 = new Vector3();
  230. return function getBoundingSphere( optionalTarget ) {
  231. var result = optionalTarget || new Sphere();
  232. this.getCenter( result.center );
  233. result.radius = this.getSize( v1 ).length() * 0.5;
  234. return result;
  235. };
  236. }(),
  237. intersect: function ( box ) {
  238. this.min.max( box.min );
  239. this.max.min( box.max );
  240. // 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.
  241. if ( this.isEmpty() ) this.makeEmpty();
  242. return this;
  243. },
  244. union: function ( box ) {
  245. this.min.min( box.min );
  246. this.max.max( box.max );
  247. return this;
  248. },
  249. applyMatrix4: function () {
  250. var points = [
  251. new Vector3(),
  252. new Vector3(),
  253. new Vector3(),
  254. new Vector3(),
  255. new Vector3(),
  256. new Vector3(),
  257. new Vector3(),
  258. new Vector3()
  259. ];
  260. return function applyMatrix4( matrix ) {
  261. // transform of empty box is an empty box.
  262. if ( this.isEmpty() ) return this;
  263. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  264. points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  265. points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  266. points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  267. points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  268. points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  269. points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  270. points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  271. points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  272. this.setFromPoints( points );
  273. return this;
  274. };
  275. }(),
  276. translate: function ( offset ) {
  277. this.min.add( offset );
  278. this.max.add( offset );
  279. return this;
  280. },
  281. equals: function ( box ) {
  282. return box.min.equals( this.min ) && box.max.equals( this.max );
  283. }
  284. } );
  285. export { Box3 };