Box3.js 6.0 KB

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