Box3.js 6.7 KB

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