AxisAlignedBoundingBox.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import Cartesian3 from './Cartesian3.js';
  2. import Check from './Check.js';
  3. import defaultValue from './defaultValue.js';
  4. import defined from './defined.js';
  5. import Intersect from './Intersect.js';
  6. /**
  7. * Creates an instance of an AxisAlignedBoundingBox from the minimum and maximum points along the x, y, and z axes.
  8. * @alias AxisAlignedBoundingBox
  9. * @constructor
  10. *
  11. * @param {Cartesian3} [minimum=Cartesian3.ZERO] The minimum point along the x, y, and z axes.
  12. * @param {Cartesian3} [maximum=Cartesian3.ZERO] The maximum point along the x, y, and z axes.
  13. * @param {Cartesian3} [center] The center of the box; automatically computed if not supplied.
  14. *
  15. * @see BoundingSphere
  16. * @see BoundingRectangle
  17. */
  18. function AxisAlignedBoundingBox(minimum, maximum, center) {
  19. /**
  20. * The minimum point defining the bounding box.
  21. * @type {Cartesian3}
  22. * @default {@link Cartesian3.ZERO}
  23. */
  24. this.minimum = Cartesian3.clone(defaultValue(minimum, Cartesian3.ZERO));
  25. /**
  26. * The maximum point defining the bounding box.
  27. * @type {Cartesian3}
  28. * @default {@link Cartesian3.ZERO}
  29. */
  30. this.maximum = Cartesian3.clone(defaultValue(maximum, Cartesian3.ZERO));
  31. //If center was not defined, compute it.
  32. if (!defined(center)) {
  33. center = Cartesian3.midpoint(this.minimum, this.maximum, new Cartesian3());
  34. } else {
  35. center = Cartesian3.clone(center);
  36. }
  37. /**
  38. * The center point of the bounding box.
  39. * @type {Cartesian3}
  40. */
  41. this.center = center;
  42. }
  43. /**
  44. * Computes an instance of an AxisAlignedBoundingBox. The box is determined by
  45. * finding the points spaced the farthest apart on the x, y, and z axes.
  46. *
  47. * @param {Cartesian3[]} positions List of points that the bounding box will enclose. Each point must have a <code>x</code>, <code>y</code>, and <code>z</code> properties.
  48. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  49. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  50. *
  51. * @example
  52. * // Compute an axis aligned bounding box enclosing two points.
  53. * var box = Cesium.AxisAlignedBoundingBox.fromPoints([new Cesium.Cartesian3(2, 0, 0), new Cesium.Cartesian3(-2, 0, 0)]);
  54. */
  55. AxisAlignedBoundingBox.fromPoints = function(positions, result) {
  56. if (!defined(result)) {
  57. result = new AxisAlignedBoundingBox();
  58. }
  59. if (!defined(positions) || positions.length === 0) {
  60. result.minimum = Cartesian3.clone(Cartesian3.ZERO, result.minimum);
  61. result.maximum = Cartesian3.clone(Cartesian3.ZERO, result.maximum);
  62. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  63. return result;
  64. }
  65. var minimumX = positions[0].x;
  66. var minimumY = positions[0].y;
  67. var minimumZ = positions[0].z;
  68. var maximumX = positions[0].x;
  69. var maximumY = positions[0].y;
  70. var maximumZ = positions[0].z;
  71. var length = positions.length;
  72. for ( var i = 1; i < length; i++) {
  73. var p = positions[i];
  74. var x = p.x;
  75. var y = p.y;
  76. var z = p.z;
  77. minimumX = Math.min(x, minimumX);
  78. maximumX = Math.max(x, maximumX);
  79. minimumY = Math.min(y, minimumY);
  80. maximumY = Math.max(y, maximumY);
  81. minimumZ = Math.min(z, minimumZ);
  82. maximumZ = Math.max(z, maximumZ);
  83. }
  84. var minimum = result.minimum;
  85. minimum.x = minimumX;
  86. minimum.y = minimumY;
  87. minimum.z = minimumZ;
  88. var maximum = result.maximum;
  89. maximum.x = maximumX;
  90. maximum.y = maximumY;
  91. maximum.z = maximumZ;
  92. result.center = Cartesian3.midpoint(minimum, maximum, result.center);
  93. return result;
  94. };
  95. /**
  96. * Duplicates a AxisAlignedBoundingBox instance.
  97. *
  98. * @param {AxisAlignedBoundingBox} box The bounding box to duplicate.
  99. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  100. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if none was provided. (Returns undefined if box is undefined)
  101. */
  102. AxisAlignedBoundingBox.clone = function(box, result) {
  103. if (!defined(box)) {
  104. return undefined;
  105. }
  106. if (!defined(result)) {
  107. return new AxisAlignedBoundingBox(box.minimum, box.maximum, box.center);
  108. }
  109. result.minimum = Cartesian3.clone(box.minimum, result.minimum);
  110. result.maximum = Cartesian3.clone(box.maximum, result.maximum);
  111. result.center = Cartesian3.clone(box.center, result.center);
  112. return result;
  113. };
  114. /**
  115. * Compares the provided AxisAlignedBoundingBox componentwise and returns
  116. * <code>true</code> if they are equal, <code>false</code> otherwise.
  117. *
  118. * @param {AxisAlignedBoundingBox} [left] The first AxisAlignedBoundingBox.
  119. * @param {AxisAlignedBoundingBox} [right] The second AxisAlignedBoundingBox.
  120. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  121. */
  122. AxisAlignedBoundingBox.equals = function(left, right) {
  123. return (left === right) ||
  124. ((defined(left)) &&
  125. (defined(right)) &&
  126. Cartesian3.equals(left.center, right.center) &&
  127. Cartesian3.equals(left.minimum, right.minimum) &&
  128. Cartesian3.equals(left.maximum, right.maximum));
  129. };
  130. var intersectScratch = new Cartesian3();
  131. /**
  132. * Determines which side of a plane a box is located.
  133. *
  134. * @param {AxisAlignedBoundingBox} box The bounding box to test.
  135. * @param {Plane} plane The plane to test against.
  136. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  137. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  138. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  139. * intersects the plane.
  140. */
  141. AxisAlignedBoundingBox.intersectPlane = function(box, plane) {
  142. //>>includeStart('debug', pragmas.debug);
  143. Check.defined('box', box);
  144. Check.defined('plane', plane);
  145. //>>includeEnd('debug');
  146. intersectScratch = Cartesian3.subtract(box.maximum, box.minimum, intersectScratch);
  147. var h = Cartesian3.multiplyByScalar(intersectScratch, 0.5, intersectScratch); //The positive half diagonal
  148. var normal = plane.normal;
  149. var e = h.x * Math.abs(normal.x) + h.y * Math.abs(normal.y) + h.z * Math.abs(normal.z);
  150. var s = Cartesian3.dot(box.center, normal) + plane.distance; //signed distance from center
  151. if (s - e > 0) {
  152. return Intersect.INSIDE;
  153. }
  154. if (s + e < 0) {
  155. //Not in front because normals point inward
  156. return Intersect.OUTSIDE;
  157. }
  158. return Intersect.INTERSECTING;
  159. };
  160. /**
  161. * Duplicates this AxisAlignedBoundingBox instance.
  162. *
  163. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  164. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  165. */
  166. AxisAlignedBoundingBox.prototype.clone = function(result) {
  167. return AxisAlignedBoundingBox.clone(this, result);
  168. };
  169. /**
  170. * Determines which side of a plane this box is located.
  171. *
  172. * @param {Plane} plane The plane to test against.
  173. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  174. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  175. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  176. * intersects the plane.
  177. */
  178. AxisAlignedBoundingBox.prototype.intersectPlane = function(plane) {
  179. return AxisAlignedBoundingBox.intersectPlane(this, plane);
  180. };
  181. /**
  182. * Compares this AxisAlignedBoundingBox against the provided AxisAlignedBoundingBox componentwise and returns
  183. * <code>true</code> if they are equal, <code>false</code> otherwise.
  184. *
  185. * @param {AxisAlignedBoundingBox} [right] The right hand side AxisAlignedBoundingBox.
  186. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  187. */
  188. AxisAlignedBoundingBox.prototype.equals = function(right) {
  189. return AxisAlignedBoundingBox.equals(this, right);
  190. };
  191. export default AxisAlignedBoundingBox;