Box2.js 4.2 KB

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