AxisAlignedBoundingBox-7b93960a.js 11 KB

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