Box3.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @author Ben Houston / ben@exocortex.com / http://github.com/bhouston
  3. */
  4. ( function ( THREE ) {
  5. THREE.Box3 = function ( min, max ) {
  6. // TODO: Is this valid JavaScript to check if the parameters are specified?
  7. if( ! min && ! max ) {
  8. this.makeEmpty();
  9. }
  10. else {
  11. this.min = min || new THREE.Vector3();
  12. this.max = max || this.min; // This is done on purpose so you can make a box using a single point and then expand it.
  13. }
  14. };
  15. THREE.Box3.fromPoints = function ( points ) {
  16. var boundingBox = new THREE.Box3();
  17. for( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
  18. boundingBox.extendByPoint( points[i] );
  19. }
  20. return boundingBox;
  21. };
  22. THREE.Box3.fromCenterAndSize = function ( center, size ) {
  23. var halfSize = new THREE.Vector3().copy( size ).multiplyScalar( 0.5 );
  24. var box = new THREE.Box3( center, center );
  25. box.min.subSelf( halfSize );
  26. box.min.addSelf( halfSize );
  27. return box;
  28. };
  29. THREE.Box3.prototype.set = function ( min, max ) {
  30. this.min = min;
  31. this.max = max;
  32. return this;
  33. };
  34. THREE.Box3.prototype.copy = function ( box ) {
  35. this.min = box.min;
  36. this.max = box.max;
  37. return this;
  38. };
  39. THREE.Box3.prototype.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. THREE.Box3.prototype.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. THREE.Box3.prototype.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. THREE.Box3.prototype.center = function () {
  58. return new THREE.Vector3().add( this.min, this.max ).multiplyScalar( 0.5 );
  59. };
  60. THREE.Box3.prototype.size = function () {
  61. return new THREE.Vector3().sub( this.max, this.min );
  62. };
  63. THREE.Box3.prototype.expandByPoint = function ( point ) {
  64. this.min.minSelf( point );
  65. this.max.maxSelf( point );
  66. return this;
  67. };
  68. THREE.Box3.prototype.expandByVector = function ( vector ) {
  69. this.min.subSelf( vector );
  70. this.max.addSelf( vector );
  71. return this;
  72. };
  73. THREE.Box3.prototype.expandByScalar = function ( scalar ) {
  74. this.min.addScalar( -scalar );
  75. this.max.addScalar( scalar );
  76. return this;
  77. };
  78. THREE.Box3.prototype.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. THREE.Box3.prototype.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. THREE.Box3.prototype.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. THREE.Box3.prototype.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. THREE.Box3.prototype.clampPoint = function ( point ) {
  119. return new THREE.Vector3().copy( point ).maxSelf( this.min ).minSelf( this.max );
  120. };
  121. THREE.Box3.prototype.distanceToPoint = function ( point ) {
  122. return this.clampPoint( point ).subSelf( point ).length();
  123. };
  124. THREE.Box3.prototype.intersect = function ( box ) {
  125. this.min.maxSelf( box.min );
  126. this.max.minSelf( box.max );
  127. return this;
  128. };
  129. THREE.Box3.prototype.union = function ( box ) {
  130. this.min.minSelf( box.min );
  131. this.max.maxSelf( box.max );
  132. return this;
  133. };
  134. THREE.Box3.prototype.translate = function ( offset ) {
  135. this.min.addSelf( offset );
  136. this.max.addSelf( offset );
  137. return this;
  138. };
  139. THREE.Box3.prototype.scale = function ( factor ) {
  140. var sizeDeltaHalf = this.size().multiplyScalar( ( 1 - factor ) * 0.5 );
  141. this.expandByVector( sizeDeltaHalf );
  142. return this;
  143. };
  144. }( THREE ) );
粤ICP备19079148号