Box3.js 4.9 KB

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