Box2.js 3.8 KB

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