Box2.js 3.7 KB

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