Box3.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Box3 = function ( min, max ) {
  5. // TODO: Is this valid JavaScript to check if the parameters are specified?
  6. if( ! min && ! max ) {
  7. this.makeEmpty();
  8. }
  9. else {
  10. this.min = min || new THREE.Vector3();
  11. this.max = max || this.min; // This is done on purpose so you can make a box using a single point and then expand it.
  12. }
  13. };
  14. THREE.Box3.prototype = {
  15. constructor: THREE.Box3,
  16. set: function ( min, max ) {
  17. this.min = min;
  18. this.max = max;
  19. return this;
  20. },
  21. setFromPoints: function ( points ) {
  22. this.makeEmpty();
  23. for( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
  24. this.expandByPoint( points[i] );
  25. }
  26. return this;
  27. };
  28. setFromCenterAndSize: function ( center, size ) {
  29. var halfSize = new THREE.Vector3().copy( size ).multiplyScalar( 0.5 );
  30. this.min.copy( center ).subSelf( halfSize );
  31. this.max.copy( center ).addSelf( halfSize );
  32. return box;
  33. },
  34. copy: function ( box ) {
  35. this.min = box.min;
  36. this.max = box.max;
  37. return this;
  38. },
  39. makeEmpty: function () {
  40. this.min.x = this.min.y = this.min.z = Number.MAX_VALUE;
  41. this.max.x = this.max.y = this.max.z = -Number.MAX_VALUE;
  42. return this;
  43. },
  44. empty: function () {
  45. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  46. return
  47. ( this.max.x < this.min.x ) ||
  48. ( this.max.y < this.min.y ) ||
  49. ( this.max.z < this.min.z );
  50. },
  51. volume: function () {
  52. return
  53. ( this.max.x - this.min.x ) *
  54. ( this.max.y - this.min.y ) *
  55. ( this.max.z - this.min.z );
  56. },
  57. center: function () {
  58. return new THREE.Vector3().add( this.min, this.max ).multiplyScalar( 0.5 );
  59. },
  60. size: function () {
  61. return new THREE.Vector3().sub( this.max, this.min );
  62. },
  63. expandByPoint: function ( point ) {
  64. this.min.minSelf( point );
  65. this.max.maxSelf( point );
  66. return this;
  67. },
  68. expandByVector: function ( vector ) {
  69. this.min.subSelf( vector );
  70. this.max.addSelf( vector );
  71. return this;
  72. },
  73. expandByScalar: function ( scalar ) {
  74. this.min.addScalar( -scalar );
  75. this.max.addScalar( scalar );
  76. return this;
  77. },
  78. containsPoint: function ( point ) {
  79. if(
  80. ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
  81. ( this.min.y <= point.y ) && ( point.y <= this.max.y ) &&
  82. ( this.min.z <= point.z ) && ( point.z <= this.max.z )
  83. ) {
  84. return true;
  85. }
  86. return false;
  87. },
  88. containsBox: function ( box ) {
  89. if(
  90. ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  91. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  92. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z )
  93. ) {
  94. return true;
  95. }
  96. return false;
  97. },
  98. isIntersection: function ( box ) {
  99. // using 6 splitting planes to rule out intersections.
  100. if(
  101. ( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
  102. ( this.max.y < box.min.y ) || ( box.min.y > this.max.y ) ||
  103. ( this.max.z < box.min.z ) || ( box.min.z > this.max.z )
  104. ) {
  105. return false;
  106. }
  107. return true;
  108. },
  109. getParameter: function ( point ) {
  110. // This can potentially have a divide by zero if the box
  111. // has a size dimension of 0.
  112. return new THREE.Vector3(
  113. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  114. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  115. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  116. );
  117. },
  118. clampPoint: function ( point ) {
  119. return new THREE.Vector3().copy( point ).maxSelf( this.min ).minSelf( this.max );
  120. },
  121. distanceToPoint: function ( point ) {
  122. return this.clampPoint( point ).subSelf( point ).length();
  123. },
  124. intersect: function ( box ) {
  125. this.min.maxSelf( box.min );
  126. this.max.minSelf( box.max );
  127. return this;
  128. },
  129. union: function ( box ) {
  130. this.min.minSelf( box.min );
  131. this.max.maxSelf( box.max );
  132. return this;
  133. },
  134. translate: function ( offset ) {
  135. this.min.addSelf( offset );
  136. this.max.addSelf( offset );
  137. return this;
  138. },
  139. scale: function ( factor ) {
  140. var sizeDeltaHalf = this.size().multiplyScalar( ( 1 - factor ) * 0.5 );
  141. this.expandByVector( sizeDeltaHalf );
  142. return this;
  143. }
  144. };