Box2.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 p = points[ 0 ];
  29. this.min.copy( p );
  30. this.max.copy( p );
  31. for ( var i = 1, il = points.length; i < il; i ++ ) {
  32. p = points[ i ];
  33. if ( p.x < this.min.x ) {
  34. this.min.x = p.x;
  35. } else if ( p.x > this.max.x ) {
  36. this.max.x = p.x;
  37. }
  38. if ( p.y < this.min.y ) {
  39. this.min.y = p.y;
  40. } else if ( p.y > this.max.y ) {
  41. this.max.y = p.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. volume: function () {
  70. return ( this.max.x - this.min.x ) * ( this.max.y - this.min.y );
  71. },
  72. center: function () {
  73. return new THREE.Vector2().add( this.min, this.max ).multiplyScalar( 0.5 );
  74. },
  75. size: function () {
  76. return new THREE.Vector2().sub( this.max, this.min );
  77. },
  78. expandByPoint: function ( point ) {
  79. this.min.minSelf( point );
  80. this.max.maxSelf( point );
  81. return this;
  82. },
  83. expandByVector: function ( vector ) {
  84. this.min.subSelf( vector );
  85. this.max.addSelf( vector );
  86. return this;
  87. },
  88. expandByScalar: function ( scalar ) {
  89. this.min.addScalar( -scalar );
  90. this.max.addScalar( scalar );
  91. return this;
  92. },
  93. containsPoint: function ( point ) {
  94. if ( ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
  95. ( this.min.y <= point.y ) && ( point.y <= this.max.y ) ) {
  96. return true;
  97. }
  98. return false;
  99. },
  100. containsBox: function ( box ) {
  101. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  102. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) ) {
  103. return true;
  104. }
  105. return false;
  106. },
  107. getParameter: function ( point ) {
  108. // This can potentially have a divide by zero if the box
  109. // has a size dimension of 0.
  110. return new THREE.Vector2(
  111. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  112. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  113. );
  114. },
  115. isIntersection: function ( box ) {
  116. // using 6 splitting planes to rule out intersections.
  117. if ( ( box.max.x < this.min.x ) || ( box.min.x > this.max.x ) ||
  118. ( box.max.y < this.min.y ) || ( box.min.y > this.max.y ) ) {
  119. return false;
  120. }
  121. return true;
  122. },
  123. clampPoint: function ( point ) {
  124. return new THREE.Vector2().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();