Box2.js 4.5 KB

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