Box2.js 3.8 KB

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