AxisAlignedBoundingBox-31fadcf0.js 9.8 KB

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