AxisAlignedBoundingBox-8103739f.js 9.9 KB

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