2
0

Box3.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. * @author WestLangley / http://github.com/WestLangley
  4. */
  5. THREE.Box3 = function ( min, max ) {
  6. this.min = ( min !== undefined ) ? min : new THREE.Vector3( Infinity, Infinity, Infinity );
  7. this.max = ( max !== undefined ) ? max : new THREE.Vector3( -Infinity, -Infinity, -Infinity );
  8. };
  9. THREE.Box3.prototype = {
  10. constructor: THREE.Box3,
  11. set: function ( min, max ) {
  12. this.min.copy( min );
  13. this.max.copy( max );
  14. return this;
  15. },
  16. addPoint: function ( point ) {
  17. if ( point.x < this.min.x ) {
  18. this.min.x = point.x;
  19. } else if ( point.x > this.max.x ) {
  20. this.max.x = point.x;
  21. }
  22. if ( point.y < this.min.y ) {
  23. this.min.y = point.y;
  24. } else if ( point.y > this.max.y ) {
  25. this.max.y = point.y;
  26. }
  27. if ( point.z < this.min.z ) {
  28. this.min.z = point.z;
  29. } else if ( point.z > this.max.z ) {
  30. this.max.z = point.z;
  31. }
  32. },
  33. setFromPoints: function ( points ) {
  34. if ( points.length > 0 ) {
  35. var point = points[ 0 ];
  36. this.min.copy( point );
  37. this.max.copy( point );
  38. for ( var i = 1, il = points.length; i < il; i ++ ) {
  39. this.addPoint( points[ i ] )
  40. }
  41. } else {
  42. this.makeEmpty();
  43. }
  44. return this;
  45. },
  46. setFromCenterAndSize: function() {
  47. var v1 = new THREE.Vector3();
  48. return function ( center, size ) {
  49. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  50. this.min.copy( center ).sub( halfSize );
  51. this.max.copy( center ).add( halfSize );
  52. return this;
  53. };
  54. }(),
  55. setFromObject: function() {
  56. // Computes the world-axis-aligned bounding box of an object (including its children),
  57. // accounting for both the object's, and childrens', world transforms
  58. var v1 = new THREE.Vector3();
  59. return function( object ) {
  60. var scope = this;
  61. object.updateMatrixWorld( true );
  62. this.makeEmpty();
  63. object.traverse( function ( node ) {
  64. if ( node.geometry !== undefined && node.geometry.vertices !== undefined ) {
  65. var vertices = node.geometry.vertices;
  66. for ( var i = 0, il = vertices.length; i < il; i++ ) {
  67. v1.copy( vertices[ i ] );
  68. v1.applyMatrix4( node.matrixWorld );
  69. scope.expandByPoint( v1 );
  70. }
  71. }
  72. } );
  73. return this;
  74. };
  75. }(),
  76. copy: function ( box ) {
  77. this.min.copy( box.min );
  78. this.max.copy( box.max );
  79. return this;
  80. },
  81. makeEmpty: function () {
  82. this.min.x = this.min.y = this.min.z = Infinity;
  83. this.max.x = this.max.y = this.max.z = -Infinity;
  84. return this;
  85. },
  86. empty: function () {
  87. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  88. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  89. },
  90. center: function ( optionalTarget ) {
  91. var result = optionalTarget || new THREE.Vector3();
  92. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  93. },
  94. size: function ( optionalTarget ) {
  95. var result = optionalTarget || new THREE.Vector3();
  96. return result.subVectors( this.max, this.min );
  97. },
  98. expandByPoint: function ( point ) {
  99. this.min.min( point );
  100. this.max.max( point );
  101. return this;
  102. },
  103. expandByVector: function ( vector ) {
  104. this.min.sub( vector );
  105. this.max.add( vector );
  106. return this;
  107. },
  108. expandByScalar: function ( scalar ) {
  109. this.min.addScalar( -scalar );
  110. this.max.addScalar( scalar );
  111. return this;
  112. },
  113. containsPoint: function ( point ) {
  114. if ( point.x < this.min.x || point.x > this.max.x ||
  115. point.y < this.min.y || point.y > this.max.y ||
  116. point.z < this.min.z || point.z > this.max.z ) {
  117. return false;
  118. }
  119. return true;
  120. },
  121. containsBox: function ( box ) {
  122. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  123. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  124. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
  125. return true;
  126. }
  127. return false;
  128. },
  129. getParameter: function ( point, optionalTarget ) {
  130. // This can potentially have a divide by zero if the box
  131. // has a size dimension of 0.
  132. var result = optionalTarget || new THREE.Vector3();
  133. return result.set(
  134. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  135. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  136. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  137. );
  138. },
  139. isIntersectionBox: function ( box ) {
  140. // using 6 splitting planes to rule out intersections.
  141. if ( box.max.x < this.min.x || box.min.x > this.max.x ||
  142. box.max.y < this.min.y || box.min.y > this.max.y ||
  143. box.max.z < this.min.z || box.min.z > this.max.z ) {
  144. return false;
  145. }
  146. return true;
  147. },
  148. clampPoint: function ( point, optionalTarget ) {
  149. var result = optionalTarget || new THREE.Vector3();
  150. return result.copy( point ).clamp( this.min, this.max );
  151. },
  152. distanceToPoint: function() {
  153. var v1 = new THREE.Vector3();
  154. return function ( point ) {
  155. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  156. return clampedPoint.sub( point ).length();
  157. };
  158. }(),
  159. getBoundingSphere: function() {
  160. var v1 = new THREE.Vector3();
  161. return function ( optionalTarget ) {
  162. var result = optionalTarget || new THREE.Sphere();
  163. result.center = this.center();
  164. result.radius = this.size( v1 ).length() * 0.5;
  165. return result;
  166. };
  167. }(),
  168. intersect: function ( box ) {
  169. this.min.max( box.min );
  170. this.max.min( box.max );
  171. return this;
  172. },
  173. union: function ( box ) {
  174. this.min.min( box.min );
  175. this.max.max( box.max );
  176. return this;
  177. },
  178. applyMatrix4: function() {
  179. var points = [
  180. new THREE.Vector3(),
  181. new THREE.Vector3(),
  182. new THREE.Vector3(),
  183. new THREE.Vector3(),
  184. new THREE.Vector3(),
  185. new THREE.Vector3(),
  186. new THREE.Vector3(),
  187. new THREE.Vector3()
  188. ];
  189. return function ( matrix ) {
  190. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  191. points[0].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  192. points[1].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  193. points[2].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  194. points[3].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  195. points[4].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  196. points[5].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  197. points[6].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  198. points[7].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  199. this.makeEmpty();
  200. this.setFromPoints( points );
  201. return this;
  202. };
  203. }(),
  204. translate: function ( offset ) {
  205. this.min.add( offset );
  206. this.max.add( offset );
  207. return this;
  208. },
  209. equals: function ( box ) {
  210. return box.min.equals( this.min ) && box.max.equals( this.max );
  211. },
  212. clone: function () {
  213. return new THREE.Box3().copy( this );
  214. }
  215. };