Box3.js 4.7 KB

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