Box2.js 4.1 KB

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