AxisAlignedBoundingBox-1aaf78c2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Matrix2-9e1c22e2', './RuntimeError-4f8ec8a2', './defaultValue-97284df2', './Transforms-273eeb44'], (function (exports, Matrix2, RuntimeError, defaultValue, Transforms) { 'use strict';
  26. /**
  27. * Creates an instance of an AxisAlignedBoundingBox from the minimum and maximum points along the x, y, and z axes.
  28. * @alias AxisAlignedBoundingBox
  29. * @constructor
  30. *
  31. * @param {Cartesian3} [minimum=Cartesian3.ZERO] The minimum point along the x, y, and z axes.
  32. * @param {Cartesian3} [maximum=Cartesian3.ZERO] The maximum point along the x, y, and z axes.
  33. * @param {Cartesian3} [center] The center of the box; automatically computed if not supplied.
  34. *
  35. * @see BoundingSphere
  36. * @see BoundingRectangle
  37. */
  38. function AxisAlignedBoundingBox(minimum, maximum, center) {
  39. /**
  40. * The minimum point defining the bounding box.
  41. * @type {Cartesian3}
  42. * @default {@link Cartesian3.ZERO}
  43. */
  44. this.minimum = Matrix2.Cartesian3.clone(defaultValue.defaultValue(minimum, Matrix2.Cartesian3.ZERO));
  45. /**
  46. * The maximum point defining the bounding box.
  47. * @type {Cartesian3}
  48. * @default {@link Cartesian3.ZERO}
  49. */
  50. this.maximum = Matrix2.Cartesian3.clone(defaultValue.defaultValue(maximum, Matrix2.Cartesian3.ZERO));
  51. // If center was not defined, compute it.
  52. if (!defaultValue.defined(center)) {
  53. center = Matrix2.Cartesian3.midpoint(this.minimum, this.maximum, new Matrix2.Cartesian3());
  54. } else {
  55. center = Matrix2.Cartesian3.clone(center);
  56. }
  57. /**
  58. * The center point of the bounding box.
  59. * @type {Cartesian3}
  60. */
  61. this.center = center;
  62. }
  63. /**
  64. * Creates an instance of an AxisAlignedBoundingBox from its corners.
  65. *
  66. * @param {Cartesian3} minimum The minimum point along the x, y, and z axes.
  67. * @param {Cartesian3} maximum The maximum point along the x, y, and z axes.
  68. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  69. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  70. *
  71. * @example
  72. * // Compute an axis aligned bounding box from the two corners.
  73. * const box = Cesium.AxisAlignedBoundingBox.fromCorners(new Cesium.Cartesian3(-1, -1, -1), new Cesium.Cartesian3(1, 1, 1));
  74. */
  75. AxisAlignedBoundingBox.fromCorners = function (minimum, maximum, result) {
  76. //>>includeStart('debug', pragmas.debug);
  77. RuntimeError.Check.defined("minimum", minimum);
  78. RuntimeError.Check.defined("maximum", maximum);
  79. //>>includeEnd('debug');
  80. if (!defaultValue.defined(result)) {
  81. result = new AxisAlignedBoundingBox();
  82. }
  83. result.minimum = Matrix2.Cartesian3.clone(minimum, result.minimum);
  84. result.maximum = Matrix2.Cartesian3.clone(maximum, result.maximum);
  85. result.center = Matrix2.Cartesian3.midpoint(minimum, maximum, result.center);
  86. return result;
  87. };
  88. /**
  89. * Computes an instance of an AxisAlignedBoundingBox. The box is determined by
  90. * finding the points spaced the farthest apart on the x, y, and z axes.
  91. *
  92. * @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.
  93. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  94. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  95. *
  96. * @example
  97. * // Compute an axis aligned bounding box enclosing two points.
  98. * const box = Cesium.AxisAlignedBoundingBox.fromPoints([new Cesium.Cartesian3(2, 0, 0), new Cesium.Cartesian3(-2, 0, 0)]);
  99. */
  100. AxisAlignedBoundingBox.fromPoints = function (positions, result) {
  101. if (!defaultValue.defined(result)) {
  102. result = new AxisAlignedBoundingBox();
  103. }
  104. if (!defaultValue.defined(positions) || positions.length === 0) {
  105. result.minimum = Matrix2.Cartesian3.clone(Matrix2.Cartesian3.ZERO, result.minimum);
  106. result.maximum = Matrix2.Cartesian3.clone(Matrix2.Cartesian3.ZERO, result.maximum);
  107. result.center = Matrix2.Cartesian3.clone(Matrix2.Cartesian3.ZERO, result.center);
  108. return result;
  109. }
  110. let minimumX = positions[0].x;
  111. let minimumY = positions[0].y;
  112. let minimumZ = positions[0].z;
  113. let maximumX = positions[0].x;
  114. let maximumY = positions[0].y;
  115. let maximumZ = positions[0].z;
  116. const length = positions.length;
  117. for (let i = 1; i < length; i++) {
  118. const p = positions[i];
  119. const x = p.x;
  120. const y = p.y;
  121. const z = p.z;
  122. minimumX = Math.min(x, minimumX);
  123. maximumX = Math.max(x, maximumX);
  124. minimumY = Math.min(y, minimumY);
  125. maximumY = Math.max(y, maximumY);
  126. minimumZ = Math.min(z, minimumZ);
  127. maximumZ = Math.max(z, maximumZ);
  128. }
  129. const minimum = result.minimum;
  130. minimum.x = minimumX;
  131. minimum.y = minimumY;
  132. minimum.z = minimumZ;
  133. const maximum = result.maximum;
  134. maximum.x = maximumX;
  135. maximum.y = maximumY;
  136. maximum.z = maximumZ;
  137. result.center = Matrix2.Cartesian3.midpoint(minimum, maximum, result.center);
  138. return result;
  139. };
  140. /**
  141. * Duplicates a AxisAlignedBoundingBox instance.
  142. *
  143. * @param {AxisAlignedBoundingBox} box The bounding box to duplicate.
  144. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  145. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if none was provided. (Returns undefined if box is undefined)
  146. */
  147. AxisAlignedBoundingBox.clone = function (box, result) {
  148. if (!defaultValue.defined(box)) {
  149. return undefined;
  150. }
  151. if (!defaultValue.defined(result)) {
  152. return new AxisAlignedBoundingBox(box.minimum, box.maximum, box.center);
  153. }
  154. result.minimum = Matrix2.Cartesian3.clone(box.minimum, result.minimum);
  155. result.maximum = Matrix2.Cartesian3.clone(box.maximum, result.maximum);
  156. result.center = Matrix2.Cartesian3.clone(box.center, result.center);
  157. return result;
  158. };
  159. /**
  160. * Compares the provided AxisAlignedBoundingBox componentwise and returns
  161. * <code>true</code> if they are equal, <code>false</code> otherwise.
  162. *
  163. * @param {AxisAlignedBoundingBox} [left] The first AxisAlignedBoundingBox.
  164. * @param {AxisAlignedBoundingBox} [right] The second AxisAlignedBoundingBox.
  165. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  166. */
  167. AxisAlignedBoundingBox.equals = function (left, right) {
  168. return (
  169. left === right ||
  170. (defaultValue.defined(left) &&
  171. defaultValue.defined(right) &&
  172. Matrix2.Cartesian3.equals(left.center, right.center) &&
  173. Matrix2.Cartesian3.equals(left.minimum, right.minimum) &&
  174. Matrix2.Cartesian3.equals(left.maximum, right.maximum))
  175. );
  176. };
  177. let intersectScratch = new Matrix2.Cartesian3();
  178. /**
  179. * Determines which side of a plane a box is located.
  180. *
  181. * @param {AxisAlignedBoundingBox} box The bounding box to test.
  182. * @param {Plane} plane The plane to test against.
  183. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  184. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  185. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  186. * intersects the plane.
  187. */
  188. AxisAlignedBoundingBox.intersectPlane = function (box, plane) {
  189. //>>includeStart('debug', pragmas.debug);
  190. RuntimeError.Check.defined("box", box);
  191. RuntimeError.Check.defined("plane", plane);
  192. //>>includeEnd('debug');
  193. intersectScratch = Matrix2.Cartesian3.subtract(
  194. box.maximum,
  195. box.minimum,
  196. intersectScratch
  197. );
  198. const h = Matrix2.Cartesian3.multiplyByScalar(
  199. intersectScratch,
  200. 0.5,
  201. intersectScratch
  202. ); //The positive half diagonal
  203. const normal = plane.normal;
  204. const e =
  205. h.x * Math.abs(normal.x) +
  206. h.y * Math.abs(normal.y) +
  207. h.z * Math.abs(normal.z);
  208. const s = Matrix2.Cartesian3.dot(box.center, normal) + plane.distance; //signed distance from center
  209. if (s - e > 0) {
  210. return Transforms.Intersect.INSIDE;
  211. }
  212. if (s + e < 0) {
  213. //Not in front because normals point inward
  214. return Transforms.Intersect.OUTSIDE;
  215. }
  216. return Transforms.Intersect.INTERSECTING;
  217. };
  218. /**
  219. * Duplicates this AxisAlignedBoundingBox instance.
  220. *
  221. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  222. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  223. */
  224. AxisAlignedBoundingBox.prototype.clone = function (result) {
  225. return AxisAlignedBoundingBox.clone(this, result);
  226. };
  227. /**
  228. * Determines which side of a plane this box is located.
  229. *
  230. * @param {Plane} plane The plane to test against.
  231. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  232. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  233. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  234. * intersects the plane.
  235. */
  236. AxisAlignedBoundingBox.prototype.intersectPlane = function (plane) {
  237. return AxisAlignedBoundingBox.intersectPlane(this, plane);
  238. };
  239. /**
  240. * Compares this AxisAlignedBoundingBox against the provided AxisAlignedBoundingBox componentwise and returns
  241. * <code>true</code> if they are equal, <code>false</code> otherwise.
  242. *
  243. * @param {AxisAlignedBoundingBox} [right] The right hand side AxisAlignedBoundingBox.
  244. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  245. */
  246. AxisAlignedBoundingBox.prototype.equals = function (right) {
  247. return AxisAlignedBoundingBox.equals(this, right);
  248. };
  249. exports.AxisAlignedBoundingBox = AxisAlignedBoundingBox;
  250. }));
  251. //# sourceMappingURL=AxisAlignedBoundingBox-1aaf78c2.js.map