Box3.js 6.7 KB

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