AxisAlignedBoundingBox.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 (
  124. left === right ||
  125. (defined(left) &&
  126. defined(right) &&
  127. Cartesian3.equals(left.center, right.center) &&
  128. Cartesian3.equals(left.minimum, right.minimum) &&
  129. Cartesian3.equals(left.maximum, right.maximum))
  130. );
  131. };
  132. var intersectScratch = new Cartesian3();
  133. /**
  134. * Determines which side of a plane a box is located.
  135. *
  136. * @param {AxisAlignedBoundingBox} box The bounding box to test.
  137. * @param {Plane} plane The plane to test against.
  138. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  139. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  140. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  141. * intersects the plane.
  142. */
  143. AxisAlignedBoundingBox.intersectPlane = function (box, plane) {
  144. //>>includeStart('debug', pragmas.debug);
  145. Check.defined("box", box);
  146. Check.defined("plane", plane);
  147. //>>includeEnd('debug');
  148. intersectScratch = Cartesian3.subtract(
  149. box.maximum,
  150. box.minimum,
  151. intersectScratch
  152. );
  153. var h = Cartesian3.multiplyByScalar(intersectScratch, 0.5, intersectScratch); //The positive half diagonal
  154. var normal = plane.normal;
  155. var e =
  156. h.x * Math.abs(normal.x) +
  157. h.y * Math.abs(normal.y) +
  158. h.z * Math.abs(normal.z);
  159. var s = Cartesian3.dot(box.center, normal) + plane.distance; //signed distance from center
  160. if (s - e > 0) {
  161. return Intersect.INSIDE;
  162. }
  163. if (s + e < 0) {
  164. //Not in front because normals point inward
  165. return Intersect.OUTSIDE;
  166. }
  167. return Intersect.INTERSECTING;
  168. };
  169. /**
  170. * Duplicates this AxisAlignedBoundingBox instance.
  171. *
  172. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  173. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  174. */
  175. AxisAlignedBoundingBox.prototype.clone = function (result) {
  176. return AxisAlignedBoundingBox.clone(this, result);
  177. };
  178. /**
  179. * Determines which side of a plane this box is located.
  180. *
  181. * @param {Plane} plane The plane to test against.
  182. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  183. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  184. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  185. * intersects the plane.
  186. */
  187. AxisAlignedBoundingBox.prototype.intersectPlane = function (plane) {
  188. return AxisAlignedBoundingBox.intersectPlane(this, plane);
  189. };
  190. /**
  191. * Compares this AxisAlignedBoundingBox against the provided AxisAlignedBoundingBox componentwise and returns
  192. * <code>true</code> if they are equal, <code>false</code> otherwise.
  193. *
  194. * @param {AxisAlignedBoundingBox} [right] The right hand side AxisAlignedBoundingBox.
  195. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  196. */
  197. AxisAlignedBoundingBox.prototype.equals = function (right) {
  198. return AxisAlignedBoundingBox.equals(this, right);
  199. };
  200. export default AxisAlignedBoundingBox;