Box3.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Box3 = function ( min, max ) {
  5. if( ! min && ! max ) {
  6. this.min = new THREE.Vector3();
  7. this.max = new THREE.Vector3();
  8. this.min.x = this.min.y = this.min.z = Infinity;
  9. this.max.x = this.max.y = this.max.z = -Infinity;
  10. console.log( this );
  11. }
  12. else {
  13. this.min = min || new THREE.Vector3();
  14. this.max = max || new THREE.Vector3().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
  15. }
  16. };
  17. THREE.Box3.prototype = {
  18. constructor: THREE.Box3,
  19. set: function ( min, max ) {
  20. this.min = min;
  21. this.max = max;
  22. return this;
  23. },
  24. setFromPoints: function ( points ) {
  25. if( points.length > 0 ) {
  26. var p = points[0];
  27. this.min.copy( p );
  28. this.max.copy( p );
  29. for( var i = 1, numPoints = points.length; i < numPoints; i ++ ) {
  30. p = points[ v ];
  31. if ( p.x < this.min.x ) {
  32. this.min.x = p.x;
  33. }
  34. else if ( p.x > this.max.x ) {
  35. this.max.x = p.x;
  36. }
  37. if ( p.y < this.min.y ) {
  38. this.min.y = p.y;
  39. }
  40. else if ( p.y > this.max.y ) {
  41. this.max.y = p.y;
  42. }
  43. if ( p.z < this.min.z ) {
  44. this.min.z = p.z;
  45. }
  46. else if ( p.z > this.max.z ) {
  47. this.max.z = p.z;
  48. }
  49. }
  50. }
  51. else {
  52. this.makeEmpty();
  53. }
  54. return this;
  55. },
  56. setFromCenterAndSize: function ( center, size ) {
  57. var halfSize = THREE.Box3.__v1.copy( size ).multiplyScalar( 0.5 );
  58. this.min.copy( center ).subSelf( halfSize );
  59. this.max.copy( center ).addSelf( halfSize );
  60. return box;
  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
  75. ( this.max.x < this.min.x ) ||
  76. ( this.max.y < this.min.y ) ||
  77. ( this.max.z < this.min.z );
  78. },
  79. volume: function () {
  80. return
  81. ( this.max.x - this.min.x ) *
  82. ( this.max.y - this.min.y ) *
  83. ( this.max.z - this.min.z );
  84. },
  85. center: function () {
  86. return new THREE.Vector3().add( this.min, this.max ).multiplyScalar( 0.5 );
  87. },
  88. size: function () {
  89. return new THREE.Vector3().sub( this.max, this.min );
  90. },
  91. expandByPoint: function ( point ) {
  92. this.min.minSelf( point );
  93. this.max.maxSelf( point );
  94. return this;
  95. },
  96. expandByVector: function ( vector ) {
  97. this.min.subSelf( vector );
  98. this.max.addSelf( vector );
  99. return this;
  100. },
  101. expandByScalar: function ( scalar ) {
  102. this.min.addScalar( -scalar );
  103. this.max.addScalar( scalar );
  104. return this;
  105. },
  106. containsPoint: function ( point ) {
  107. if(
  108. ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
  109. ( this.min.y <= point.y ) && ( point.y <= this.max.y ) &&
  110. ( this.min.z <= point.z ) && ( point.z <= this.max.z )
  111. ) {
  112. return true;
  113. }
  114. return false;
  115. },
  116. containsBox: function ( box ) {
  117. if(
  118. ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  119. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  120. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z )
  121. ) {
  122. return true;
  123. }
  124. return false;
  125. },
  126. getParameter: function ( point ) {
  127. // This can potentially have a divide by zero if the box
  128. // has a size dimension of 0.
  129. return new THREE.Vector3(
  130. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  131. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  132. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  133. );
  134. },
  135. clampPoint: function ( point ) {
  136. return new THREE.Vector3().copy( point ).clampSelf( this.min, this.max );
  137. },
  138. distanceToPoint: function ( point ) {
  139. return this.clampPoint( point ).subSelf( point ).length();
  140. },
  141. isIntersection: function ( box ) {
  142. // using 6 splitting planes to rule out intersections.
  143. if(
  144. ( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
  145. ( this.max.y < box.min.y ) || ( box.min.y > this.max.y ) ||
  146. ( this.max.z < box.min.z ) || ( box.min.z > this.max.z )
  147. ) {
  148. return false;
  149. }
  150. return true;
  151. },
  152. intersect: function ( box ) {
  153. this.min.maxSelf( box.min );
  154. this.max.minSelf( box.max );
  155. return this;
  156. },
  157. union: function ( box ) {
  158. this.min.minSelf( box.min );
  159. this.max.maxSelf( box.max );
  160. return this;
  161. },
  162. translate: function ( offset ) {
  163. this.min.addSelf( offset );
  164. this.max.addSelf( offset );
  165. return this;
  166. },
  167. scale: function ( factor ) {
  168. var sizeDeltaHalf = this.size().multiplyScalar( ( 1 - factor ) * 0.5 );
  169. this.expandByVector( sizeDeltaHalf );
  170. return this;
  171. },
  172. equals: function ( box ) {
  173. return box.min.equals( this.min ) && box.max.equals( this.max );
  174. },
  175. clone: function () {
  176. return new THREE.Box3().copy( this );
  177. }
  178. };
  179. THREE.Box3.__v1 = new THREE.Vector3();
粤ICP备19079148号