Box2.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Box2 = function ( min, max ) {
  5. this.min = min !== undefined ? min.clone() : new THREE.Vector2( Infinity, Infinity );
  6. this.max = max !== undefined ? max.clone() : 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 ( center, size ) {
  39. var halfSize = THREE.Box2.__v1.copy( size ).multiplyScalar( 0.5 );
  40. this.min.copy( center ).sub( halfSize );
  41. this.max.copy( center ).add( halfSize );
  42. return this;
  43. },
  44. copy: function ( box ) {
  45. this.min.copy( box.min );
  46. this.max.copy( box.max );
  47. return this;
  48. },
  49. makeEmpty: function () {
  50. this.min.x = this.min.y = Infinity;
  51. this.max.x = this.max.y = -Infinity;
  52. return this;
  53. },
  54. empty: function () {
  55. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  56. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  57. },
  58. center: function ( optionalTarget ) {
  59. var result = optionalTarget || new THREE.Vector2();
  60. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  61. },
  62. size: function ( optionalTarget ) {
  63. var result = optionalTarget || new THREE.Vector2();
  64. return result.subVectors( this.max, this.min );
  65. },
  66. expandByPoint: function ( point ) {
  67. this.min.min( point );
  68. this.max.max( point );
  69. return this;
  70. },
  71. expandByVector: function ( vector ) {
  72. this.min.sub( vector );
  73. this.max.add( vector );
  74. return this;
  75. },
  76. expandByScalar: function ( scalar ) {
  77. this.min.addScalar( -scalar );
  78. this.max.addScalar( scalar );
  79. return this;
  80. },
  81. containsPoint: function ( point ) {
  82. if ( ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
  83. ( this.min.y <= point.y ) && ( point.y <= this.max.y ) ) {
  84. return true;
  85. }
  86. return false;
  87. },
  88. containsBox: function ( box ) {
  89. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  90. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) ) {
  91. return true;
  92. }
  93. return false;
  94. },
  95. getParameter: function ( point ) {
  96. // This can potentially have a divide by zero if the box
  97. // has a size dimension of 0.
  98. return new THREE.Vector2(
  99. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  100. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  101. );
  102. },
  103. isIntersectionBox: function ( box ) {
  104. // using 6 splitting planes to rule out intersections.
  105. if ( ( box.max.x < this.min.x ) ||
  106. ( box.min.x > this.max.x ) ||
  107. ( box.max.y < this.min.y ) ||
  108. ( 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 ( point ) {
  118. var clampedPoint = THREE.Box2.__v1.copy( point ).clamp( this.min, this.max );
  119. return clampedPoint.sub( point ).length();
  120. },
  121. intersect: function ( box ) {
  122. this.min.max( box.min );
  123. this.max.min( box.max );
  124. return this;
  125. },
  126. union: function ( box ) {
  127. this.min.min( box.min );
  128. this.max.max( box.max );
  129. return this;
  130. },
  131. translate: function ( offset ) {
  132. this.min.add( offset );
  133. this.max.add( offset );
  134. return this;
  135. },
  136. equals: function ( box ) {
  137. return box.min.equals( this.min ) && box.max.equals( this.max );
  138. },
  139. clone: function () {
  140. return new THREE.Box2().copy( this );
  141. }
  142. };
  143. THREE.Box2.__v1 = new THREE.Vector2();