Box3.js 6.7 KB

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