OrientedBoundingBox.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. import BoundingSphere from "./BoundingSphere.js";
  2. import Cartesian2 from "./Cartesian2.js";
  3. import Cartesian3 from "./Cartesian3.js";
  4. import Cartographic from "./Cartographic.js";
  5. import Check from "./Check.js";
  6. import defaultValue from "./defaultValue.js";
  7. import defined from "./defined.js";
  8. import DeveloperError from "./DeveloperError.js";
  9. import Ellipsoid from "./Ellipsoid.js";
  10. import EllipsoidTangentPlane from "./EllipsoidTangentPlane.js";
  11. import Intersect from "./Intersect.js";
  12. import Interval from "./Interval.js";
  13. import CesiumMath from "./Math.js";
  14. import Matrix3 from "./Matrix3.js";
  15. import Matrix4 from "./Matrix4.js";
  16. import Plane from "./Plane.js";
  17. import Rectangle from "./Rectangle.js";
  18. /**
  19. * Creates an instance of an OrientedBoundingBox.
  20. * An OrientedBoundingBox of some object is a closed and convex rectangular cuboid. It can provide a tighter bounding volume than {@link BoundingSphere} or {@link AxisAlignedBoundingBox} in many cases.
  21. * @alias OrientedBoundingBox
  22. * @constructor
  23. *
  24. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the box.
  25. * @param {Matrix3} [halfAxes=Matrix3.ZERO] The three orthogonal half-axes of the bounding box.
  26. * Equivalently, the transformation matrix, to rotate and scale a 2x2x2
  27. * cube centered at the origin.
  28. *
  29. *
  30. * @example
  31. * // Create an OrientedBoundingBox using a transformation matrix, a position where the box will be translated, and a scale.
  32. * const center = new Cesium.Cartesian3(1.0, 0.0, 0.0);
  33. * const halfAxes = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(1.0, 3.0, 2.0), new Cesium.Matrix3());
  34. *
  35. * const obb = new Cesium.OrientedBoundingBox(center, halfAxes);
  36. *
  37. * @see BoundingSphere
  38. * @see BoundingRectangle
  39. */
  40. function OrientedBoundingBox(center, halfAxes) {
  41. /**
  42. * The center of the box.
  43. * @type {Cartesian3}
  44. * @default {@link Cartesian3.ZERO}
  45. */
  46. this.center = Cartesian3.clone(defaultValue(center, Cartesian3.ZERO));
  47. /**
  48. * The three orthogonal half-axes of the bounding box. Equivalently, the
  49. * transformation matrix, to rotate and scale a 2x2x2 cube centered at the
  50. * origin.
  51. * @type {Matrix3}
  52. * @default {@link Matrix3.ZERO}
  53. */
  54. this.halfAxes = Matrix3.clone(defaultValue(halfAxes, Matrix3.ZERO));
  55. }
  56. /**
  57. * The number of elements used to pack the object into an array.
  58. * @type {number}
  59. */
  60. OrientedBoundingBox.packedLength =
  61. Cartesian3.packedLength + Matrix3.packedLength;
  62. /**
  63. * Stores the provided instance into the provided array.
  64. *
  65. * @param {OrientedBoundingBox} value The value to pack.
  66. * @param {number[]} array The array to pack into.
  67. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  68. *
  69. * @returns {number[]} The array that was packed into
  70. */
  71. OrientedBoundingBox.pack = function (value, array, startingIndex) {
  72. //>>includeStart('debug', pragmas.debug);
  73. Check.typeOf.object("value", value);
  74. Check.defined("array", array);
  75. //>>includeEnd('debug');
  76. startingIndex = defaultValue(startingIndex, 0);
  77. Cartesian3.pack(value.center, array, startingIndex);
  78. Matrix3.pack(value.halfAxes, array, startingIndex + Cartesian3.packedLength);
  79. return array;
  80. };
  81. /**
  82. * Retrieves an instance from a packed array.
  83. *
  84. * @param {number[]} array The packed array.
  85. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  86. * @param {OrientedBoundingBox} [result] The object into which to store the result.
  87. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if one was not provided.
  88. */
  89. OrientedBoundingBox.unpack = function (array, startingIndex, result) {
  90. //>>includeStart('debug', pragmas.debug);
  91. Check.defined("array", array);
  92. //>>includeEnd('debug');
  93. startingIndex = defaultValue(startingIndex, 0);
  94. if (!defined(result)) {
  95. result = new OrientedBoundingBox();
  96. }
  97. Cartesian3.unpack(array, startingIndex, result.center);
  98. Matrix3.unpack(
  99. array,
  100. startingIndex + Cartesian3.packedLength,
  101. result.halfAxes
  102. );
  103. return result;
  104. };
  105. const scratchCartesian1 = new Cartesian3();
  106. const scratchCartesian2 = new Cartesian3();
  107. const scratchCartesian3 = new Cartesian3();
  108. const scratchCartesian4 = new Cartesian3();
  109. const scratchCartesian5 = new Cartesian3();
  110. const scratchCartesian6 = new Cartesian3();
  111. const scratchCovarianceResult = new Matrix3();
  112. const scratchEigenResult = {
  113. unitary: new Matrix3(),
  114. diagonal: new Matrix3(),
  115. };
  116. /**
  117. * Computes an instance of an OrientedBoundingBox of the given positions.
  118. * This is an implementation of Stefan Gottschalk's Collision Queries using Oriented Bounding Boxes solution (PHD thesis).
  119. * Reference: http://gamma.cs.unc.edu/users/gottschalk/main.pdf
  120. *
  121. * @param {Cartesian3[]} [positions] List of {@link Cartesian3} points that the bounding box will enclose.
  122. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  123. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if one was not provided.
  124. *
  125. * @example
  126. * // Compute an object oriented bounding box enclosing two points.
  127. * const box = Cesium.OrientedBoundingBox.fromPoints([new Cesium.Cartesian3(2, 0, 0), new Cesium.Cartesian3(-2, 0, 0)]);
  128. */
  129. OrientedBoundingBox.fromPoints = function (positions, result) {
  130. if (!defined(result)) {
  131. result = new OrientedBoundingBox();
  132. }
  133. if (!defined(positions) || positions.length === 0) {
  134. result.halfAxes = Matrix3.ZERO;
  135. result.center = Cartesian3.ZERO;
  136. return result;
  137. }
  138. let i;
  139. const length = positions.length;
  140. const meanPoint = Cartesian3.clone(positions[0], scratchCartesian1);
  141. for (i = 1; i < length; i++) {
  142. Cartesian3.add(meanPoint, positions[i], meanPoint);
  143. }
  144. const invLength = 1.0 / length;
  145. Cartesian3.multiplyByScalar(meanPoint, invLength, meanPoint);
  146. let exx = 0.0;
  147. let exy = 0.0;
  148. let exz = 0.0;
  149. let eyy = 0.0;
  150. let eyz = 0.0;
  151. let ezz = 0.0;
  152. let p;
  153. for (i = 0; i < length; i++) {
  154. p = Cartesian3.subtract(positions[i], meanPoint, scratchCartesian2);
  155. exx += p.x * p.x;
  156. exy += p.x * p.y;
  157. exz += p.x * p.z;
  158. eyy += p.y * p.y;
  159. eyz += p.y * p.z;
  160. ezz += p.z * p.z;
  161. }
  162. exx *= invLength;
  163. exy *= invLength;
  164. exz *= invLength;
  165. eyy *= invLength;
  166. eyz *= invLength;
  167. ezz *= invLength;
  168. const covarianceMatrix = scratchCovarianceResult;
  169. covarianceMatrix[0] = exx;
  170. covarianceMatrix[1] = exy;
  171. covarianceMatrix[2] = exz;
  172. covarianceMatrix[3] = exy;
  173. covarianceMatrix[4] = eyy;
  174. covarianceMatrix[5] = eyz;
  175. covarianceMatrix[6] = exz;
  176. covarianceMatrix[7] = eyz;
  177. covarianceMatrix[8] = ezz;
  178. const eigenDecomposition = Matrix3.computeEigenDecomposition(
  179. covarianceMatrix,
  180. scratchEigenResult
  181. );
  182. const rotation = Matrix3.clone(eigenDecomposition.unitary, result.halfAxes);
  183. let v1 = Matrix3.getColumn(rotation, 0, scratchCartesian4);
  184. let v2 = Matrix3.getColumn(rotation, 1, scratchCartesian5);
  185. let v3 = Matrix3.getColumn(rotation, 2, scratchCartesian6);
  186. let u1 = -Number.MAX_VALUE;
  187. let u2 = -Number.MAX_VALUE;
  188. let u3 = -Number.MAX_VALUE;
  189. let l1 = Number.MAX_VALUE;
  190. let l2 = Number.MAX_VALUE;
  191. let l3 = Number.MAX_VALUE;
  192. for (i = 0; i < length; i++) {
  193. p = positions[i];
  194. u1 = Math.max(Cartesian3.dot(v1, p), u1);
  195. u2 = Math.max(Cartesian3.dot(v2, p), u2);
  196. u3 = Math.max(Cartesian3.dot(v3, p), u3);
  197. l1 = Math.min(Cartesian3.dot(v1, p), l1);
  198. l2 = Math.min(Cartesian3.dot(v2, p), l2);
  199. l3 = Math.min(Cartesian3.dot(v3, p), l3);
  200. }
  201. v1 = Cartesian3.multiplyByScalar(v1, 0.5 * (l1 + u1), v1);
  202. v2 = Cartesian3.multiplyByScalar(v2, 0.5 * (l2 + u2), v2);
  203. v3 = Cartesian3.multiplyByScalar(v3, 0.5 * (l3 + u3), v3);
  204. const center = Cartesian3.add(v1, v2, result.center);
  205. Cartesian3.add(center, v3, center);
  206. const scale = scratchCartesian3;
  207. scale.x = u1 - l1;
  208. scale.y = u2 - l2;
  209. scale.z = u3 - l3;
  210. Cartesian3.multiplyByScalar(scale, 0.5, scale);
  211. Matrix3.multiplyByScale(result.halfAxes, scale, result.halfAxes);
  212. return result;
  213. };
  214. const scratchOffset = new Cartesian3();
  215. const scratchScale = new Cartesian3();
  216. function fromPlaneExtents(
  217. planeOrigin,
  218. planeXAxis,
  219. planeYAxis,
  220. planeZAxis,
  221. minimumX,
  222. maximumX,
  223. minimumY,
  224. maximumY,
  225. minimumZ,
  226. maximumZ,
  227. result
  228. ) {
  229. //>>includeStart('debug', pragmas.debug);
  230. if (
  231. !defined(minimumX) ||
  232. !defined(maximumX) ||
  233. !defined(minimumY) ||
  234. !defined(maximumY) ||
  235. !defined(minimumZ) ||
  236. !defined(maximumZ)
  237. ) {
  238. throw new DeveloperError(
  239. "all extents (minimum/maximum X/Y/Z) are required."
  240. );
  241. }
  242. //>>includeEnd('debug');
  243. if (!defined(result)) {
  244. result = new OrientedBoundingBox();
  245. }
  246. const halfAxes = result.halfAxes;
  247. Matrix3.setColumn(halfAxes, 0, planeXAxis, halfAxes);
  248. Matrix3.setColumn(halfAxes, 1, planeYAxis, halfAxes);
  249. Matrix3.setColumn(halfAxes, 2, planeZAxis, halfAxes);
  250. let centerOffset = scratchOffset;
  251. centerOffset.x = (minimumX + maximumX) / 2.0;
  252. centerOffset.y = (minimumY + maximumY) / 2.0;
  253. centerOffset.z = (minimumZ + maximumZ) / 2.0;
  254. const scale = scratchScale;
  255. scale.x = (maximumX - minimumX) / 2.0;
  256. scale.y = (maximumY - minimumY) / 2.0;
  257. scale.z = (maximumZ - minimumZ) / 2.0;
  258. const center = result.center;
  259. centerOffset = Matrix3.multiplyByVector(halfAxes, centerOffset, centerOffset);
  260. Cartesian3.add(planeOrigin, centerOffset, center);
  261. Matrix3.multiplyByScale(halfAxes, scale, halfAxes);
  262. return result;
  263. }
  264. const scratchRectangleCenterCartographic = new Cartographic();
  265. const scratchRectangleCenter = new Cartesian3();
  266. const scratchPerimeterCartographicNC = new Cartographic();
  267. const scratchPerimeterCartographicNW = new Cartographic();
  268. const scratchPerimeterCartographicCW = new Cartographic();
  269. const scratchPerimeterCartographicSW = new Cartographic();
  270. const scratchPerimeterCartographicSC = new Cartographic();
  271. const scratchPerimeterCartesianNC = new Cartesian3();
  272. const scratchPerimeterCartesianNW = new Cartesian3();
  273. const scratchPerimeterCartesianCW = new Cartesian3();
  274. const scratchPerimeterCartesianSW = new Cartesian3();
  275. const scratchPerimeterCartesianSC = new Cartesian3();
  276. const scratchPerimeterProjectedNC = new Cartesian2();
  277. const scratchPerimeterProjectedNW = new Cartesian2();
  278. const scratchPerimeterProjectedCW = new Cartesian2();
  279. const scratchPerimeterProjectedSW = new Cartesian2();
  280. const scratchPerimeterProjectedSC = new Cartesian2();
  281. const scratchPlaneOrigin = new Cartesian3();
  282. const scratchPlaneNormal = new Cartesian3();
  283. const scratchPlaneXAxis = new Cartesian3();
  284. const scratchHorizonCartesian = new Cartesian3();
  285. const scratchHorizonProjected = new Cartesian2();
  286. const scratchMaxY = new Cartesian3();
  287. const scratchMinY = new Cartesian3();
  288. const scratchZ = new Cartesian3();
  289. const scratchPlane = new Plane(Cartesian3.UNIT_X, 0.0);
  290. /**
  291. * Computes an OrientedBoundingBox that bounds a {@link Rectangle} on the surface of an {@link Ellipsoid}.
  292. * There are no guarantees about the orientation of the bounding box.
  293. *
  294. * @param {Rectangle} rectangle The cartographic rectangle on the surface of the ellipsoid.
  295. * @param {number} [minimumHeight=0.0] The minimum height (elevation) within the tile.
  296. * @param {number} [maximumHeight=0.0] The maximum height (elevation) within the tile.
  297. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle is defined.
  298. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  299. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if none was provided.
  300. *
  301. * @exception {DeveloperError} rectangle.width must be between 0 and 2 * pi.
  302. * @exception {DeveloperError} rectangle.height must be between 0 and pi.
  303. * @exception {DeveloperError} ellipsoid must be an ellipsoid of revolution (<code>radii.x == radii.y</code>)
  304. */
  305. OrientedBoundingBox.fromRectangle = function (
  306. rectangle,
  307. minimumHeight,
  308. maximumHeight,
  309. ellipsoid,
  310. result
  311. ) {
  312. //>>includeStart('debug', pragmas.debug);
  313. if (!defined(rectangle)) {
  314. throw new DeveloperError("rectangle is required");
  315. }
  316. if (rectangle.width < 0.0 || rectangle.width > CesiumMath.TWO_PI) {
  317. throw new DeveloperError("Rectangle width must be between 0 and 2 * pi");
  318. }
  319. if (rectangle.height < 0.0 || rectangle.height > CesiumMath.PI) {
  320. throw new DeveloperError("Rectangle height must be between 0 and pi");
  321. }
  322. if (
  323. defined(ellipsoid) &&
  324. !CesiumMath.equalsEpsilon(
  325. ellipsoid.radii.x,
  326. ellipsoid.radii.y,
  327. CesiumMath.EPSILON15
  328. )
  329. ) {
  330. throw new DeveloperError(
  331. "Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)"
  332. );
  333. }
  334. //>>includeEnd('debug');
  335. minimumHeight = defaultValue(minimumHeight, 0.0);
  336. maximumHeight = defaultValue(maximumHeight, 0.0);
  337. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  338. let minX, maxX, minY, maxY, minZ, maxZ, plane;
  339. if (rectangle.width <= CesiumMath.PI) {
  340. // The bounding box will be aligned with the tangent plane at the center of the rectangle.
  341. const tangentPointCartographic = Rectangle.center(
  342. rectangle,
  343. scratchRectangleCenterCartographic
  344. );
  345. const tangentPoint = ellipsoid.cartographicToCartesian(
  346. tangentPointCartographic,
  347. scratchRectangleCenter
  348. );
  349. const tangentPlane = new EllipsoidTangentPlane(tangentPoint, ellipsoid);
  350. plane = tangentPlane.plane;
  351. // If the rectangle spans the equator, CW is instead aligned with the equator (because it sticks out the farthest at the equator).
  352. const lonCenter = tangentPointCartographic.longitude;
  353. const latCenter =
  354. rectangle.south < 0.0 && rectangle.north > 0.0
  355. ? 0.0
  356. : tangentPointCartographic.latitude;
  357. // Compute XY extents using the rectangle at maximum height
  358. const perimeterCartographicNC = Cartographic.fromRadians(
  359. lonCenter,
  360. rectangle.north,
  361. maximumHeight,
  362. scratchPerimeterCartographicNC
  363. );
  364. const perimeterCartographicNW = Cartographic.fromRadians(
  365. rectangle.west,
  366. rectangle.north,
  367. maximumHeight,
  368. scratchPerimeterCartographicNW
  369. );
  370. const perimeterCartographicCW = Cartographic.fromRadians(
  371. rectangle.west,
  372. latCenter,
  373. maximumHeight,
  374. scratchPerimeterCartographicCW
  375. );
  376. const perimeterCartographicSW = Cartographic.fromRadians(
  377. rectangle.west,
  378. rectangle.south,
  379. maximumHeight,
  380. scratchPerimeterCartographicSW
  381. );
  382. const perimeterCartographicSC = Cartographic.fromRadians(
  383. lonCenter,
  384. rectangle.south,
  385. maximumHeight,
  386. scratchPerimeterCartographicSC
  387. );
  388. const perimeterCartesianNC = ellipsoid.cartographicToCartesian(
  389. perimeterCartographicNC,
  390. scratchPerimeterCartesianNC
  391. );
  392. let perimeterCartesianNW = ellipsoid.cartographicToCartesian(
  393. perimeterCartographicNW,
  394. scratchPerimeterCartesianNW
  395. );
  396. const perimeterCartesianCW = ellipsoid.cartographicToCartesian(
  397. perimeterCartographicCW,
  398. scratchPerimeterCartesianCW
  399. );
  400. let perimeterCartesianSW = ellipsoid.cartographicToCartesian(
  401. perimeterCartographicSW,
  402. scratchPerimeterCartesianSW
  403. );
  404. const perimeterCartesianSC = ellipsoid.cartographicToCartesian(
  405. perimeterCartographicSC,
  406. scratchPerimeterCartesianSC
  407. );
  408. const perimeterProjectedNC = tangentPlane.projectPointToNearestOnPlane(
  409. perimeterCartesianNC,
  410. scratchPerimeterProjectedNC
  411. );
  412. const perimeterProjectedNW = tangentPlane.projectPointToNearestOnPlane(
  413. perimeterCartesianNW,
  414. scratchPerimeterProjectedNW
  415. );
  416. const perimeterProjectedCW = tangentPlane.projectPointToNearestOnPlane(
  417. perimeterCartesianCW,
  418. scratchPerimeterProjectedCW
  419. );
  420. const perimeterProjectedSW = tangentPlane.projectPointToNearestOnPlane(
  421. perimeterCartesianSW,
  422. scratchPerimeterProjectedSW
  423. );
  424. const perimeterProjectedSC = tangentPlane.projectPointToNearestOnPlane(
  425. perimeterCartesianSC,
  426. scratchPerimeterProjectedSC
  427. );
  428. minX = Math.min(
  429. perimeterProjectedNW.x,
  430. perimeterProjectedCW.x,
  431. perimeterProjectedSW.x
  432. );
  433. maxX = -minX; // symmetrical
  434. maxY = Math.max(perimeterProjectedNW.y, perimeterProjectedNC.y);
  435. minY = Math.min(perimeterProjectedSW.y, perimeterProjectedSC.y);
  436. // Compute minimum Z using the rectangle at minimum height, since it will be deeper than the maximum height
  437. perimeterCartographicNW.height = perimeterCartographicSW.height = minimumHeight;
  438. perimeterCartesianNW = ellipsoid.cartographicToCartesian(
  439. perimeterCartographicNW,
  440. scratchPerimeterCartesianNW
  441. );
  442. perimeterCartesianSW = ellipsoid.cartographicToCartesian(
  443. perimeterCartographicSW,
  444. scratchPerimeterCartesianSW
  445. );
  446. minZ = Math.min(
  447. Plane.getPointDistance(plane, perimeterCartesianNW),
  448. Plane.getPointDistance(plane, perimeterCartesianSW)
  449. );
  450. maxZ = maximumHeight; // Since the tangent plane touches the surface at height = 0, this is okay
  451. return fromPlaneExtents(
  452. tangentPlane.origin,
  453. tangentPlane.xAxis,
  454. tangentPlane.yAxis,
  455. tangentPlane.zAxis,
  456. minX,
  457. maxX,
  458. minY,
  459. maxY,
  460. minZ,
  461. maxZ,
  462. result
  463. );
  464. }
  465. // Handle the case where rectangle width is greater than PI (wraps around more than half the ellipsoid).
  466. const fullyAboveEquator = rectangle.south > 0.0;
  467. const fullyBelowEquator = rectangle.north < 0.0;
  468. const latitudeNearestToEquator = fullyAboveEquator
  469. ? rectangle.south
  470. : fullyBelowEquator
  471. ? rectangle.north
  472. : 0.0;
  473. const centerLongitude = Rectangle.center(
  474. rectangle,
  475. scratchRectangleCenterCartographic
  476. ).longitude;
  477. // Plane is located at the rectangle's center longitude and the rectangle's latitude that is closest to the equator. It rotates around the Z axis.
  478. // This results in a better fit than the obb approach for smaller rectangles, which orients with the rectangle's center normal.
  479. const planeOrigin = Cartesian3.fromRadians(
  480. centerLongitude,
  481. latitudeNearestToEquator,
  482. maximumHeight,
  483. ellipsoid,
  484. scratchPlaneOrigin
  485. );
  486. planeOrigin.z = 0.0; // center the plane on the equator to simpify plane normal calculation
  487. const isPole =
  488. Math.abs(planeOrigin.x) < CesiumMath.EPSILON10 &&
  489. Math.abs(planeOrigin.y) < CesiumMath.EPSILON10;
  490. const planeNormal = !isPole
  491. ? Cartesian3.normalize(planeOrigin, scratchPlaneNormal)
  492. : Cartesian3.UNIT_X;
  493. const planeYAxis = Cartesian3.UNIT_Z;
  494. const planeXAxis = Cartesian3.cross(
  495. planeNormal,
  496. planeYAxis,
  497. scratchPlaneXAxis
  498. );
  499. plane = Plane.fromPointNormal(planeOrigin, planeNormal, scratchPlane);
  500. // Get the horizon point relative to the center. This will be the farthest extent in the plane's X dimension.
  501. const horizonCartesian = Cartesian3.fromRadians(
  502. centerLongitude + CesiumMath.PI_OVER_TWO,
  503. latitudeNearestToEquator,
  504. maximumHeight,
  505. ellipsoid,
  506. scratchHorizonCartesian
  507. );
  508. maxX = Cartesian3.dot(
  509. Plane.projectPointOntoPlane(
  510. plane,
  511. horizonCartesian,
  512. scratchHorizonProjected
  513. ),
  514. planeXAxis
  515. );
  516. minX = -maxX; // symmetrical
  517. // Get the min and max Y, using the height that will give the largest extent
  518. maxY = Cartesian3.fromRadians(
  519. 0.0,
  520. rectangle.north,
  521. fullyBelowEquator ? minimumHeight : maximumHeight,
  522. ellipsoid,
  523. scratchMaxY
  524. ).z;
  525. minY = Cartesian3.fromRadians(
  526. 0.0,
  527. rectangle.south,
  528. fullyAboveEquator ? minimumHeight : maximumHeight,
  529. ellipsoid,
  530. scratchMinY
  531. ).z;
  532. const farZ = Cartesian3.fromRadians(
  533. rectangle.east,
  534. latitudeNearestToEquator,
  535. maximumHeight,
  536. ellipsoid,
  537. scratchZ
  538. );
  539. minZ = Plane.getPointDistance(plane, farZ);
  540. maxZ = 0.0; // plane origin starts at maxZ already
  541. // min and max are local to the plane axes
  542. return fromPlaneExtents(
  543. planeOrigin,
  544. planeXAxis,
  545. planeYAxis,
  546. planeNormal,
  547. minX,
  548. maxX,
  549. minY,
  550. maxY,
  551. minZ,
  552. maxZ,
  553. result
  554. );
  555. };
  556. /**
  557. * Computes an OrientedBoundingBox that bounds an affine transformation.
  558. *
  559. * @param {Matrix4} transformation The affine transformation.
  560. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  561. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if none was provided.
  562. */
  563. OrientedBoundingBox.fromTransformation = function (transformation, result) {
  564. //>>includeStart('debug', pragmas.debug);
  565. Check.typeOf.object("transformation", transformation);
  566. //>>includeEnd('debug');
  567. if (!defined(result)) {
  568. result = new OrientedBoundingBox();
  569. }
  570. result.center = Matrix4.getTranslation(transformation, result.center);
  571. result.halfAxes = Matrix4.getMatrix3(transformation, result.halfAxes);
  572. result.halfAxes = Matrix3.multiplyByScalar(
  573. result.halfAxes,
  574. 0.5,
  575. result.halfAxes
  576. );
  577. return result;
  578. };
  579. /**
  580. * Duplicates a OrientedBoundingBox instance.
  581. *
  582. * @param {OrientedBoundingBox} box The bounding box to duplicate.
  583. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  584. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if none was provided. (Returns undefined if box is undefined)
  585. */
  586. OrientedBoundingBox.clone = function (box, result) {
  587. if (!defined(box)) {
  588. return undefined;
  589. }
  590. if (!defined(result)) {
  591. return new OrientedBoundingBox(box.center, box.halfAxes);
  592. }
  593. Cartesian3.clone(box.center, result.center);
  594. Matrix3.clone(box.halfAxes, result.halfAxes);
  595. return result;
  596. };
  597. /**
  598. * Determines which side of a plane the oriented bounding box is located.
  599. *
  600. * @param {OrientedBoundingBox} box The oriented bounding box to test.
  601. * @param {Plane} plane The plane to test against.
  602. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  603. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  604. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  605. * intersects the plane.
  606. */
  607. OrientedBoundingBox.intersectPlane = function (box, plane) {
  608. //>>includeStart('debug', pragmas.debug);
  609. if (!defined(box)) {
  610. throw new DeveloperError("box is required.");
  611. }
  612. if (!defined(plane)) {
  613. throw new DeveloperError("plane is required.");
  614. }
  615. //>>includeEnd('debug');
  616. const center = box.center;
  617. const normal = plane.normal;
  618. const halfAxes = box.halfAxes;
  619. const normalX = normal.x,
  620. normalY = normal.y,
  621. normalZ = normal.z;
  622. // plane is used as if it is its normal; the first three components are assumed to be normalized
  623. const radEffective =
  624. Math.abs(
  625. normalX * halfAxes[Matrix3.COLUMN0ROW0] +
  626. normalY * halfAxes[Matrix3.COLUMN0ROW1] +
  627. normalZ * halfAxes[Matrix3.COLUMN0ROW2]
  628. ) +
  629. Math.abs(
  630. normalX * halfAxes[Matrix3.COLUMN1ROW0] +
  631. normalY * halfAxes[Matrix3.COLUMN1ROW1] +
  632. normalZ * halfAxes[Matrix3.COLUMN1ROW2]
  633. ) +
  634. Math.abs(
  635. normalX * halfAxes[Matrix3.COLUMN2ROW0] +
  636. normalY * halfAxes[Matrix3.COLUMN2ROW1] +
  637. normalZ * halfAxes[Matrix3.COLUMN2ROW2]
  638. );
  639. const distanceToPlane = Cartesian3.dot(normal, center) + plane.distance;
  640. if (distanceToPlane <= -radEffective) {
  641. // The entire box is on the negative side of the plane normal
  642. return Intersect.OUTSIDE;
  643. } else if (distanceToPlane >= radEffective) {
  644. // The entire box is on the positive side of the plane normal
  645. return Intersect.INSIDE;
  646. }
  647. return Intersect.INTERSECTING;
  648. };
  649. const scratchCartesianU = new Cartesian3();
  650. const scratchCartesianV = new Cartesian3();
  651. const scratchCartesianW = new Cartesian3();
  652. const scratchValidAxis2 = new Cartesian3();
  653. const scratchValidAxis3 = new Cartesian3();
  654. const scratchPPrime = new Cartesian3();
  655. /**
  656. * Computes the estimated distance squared from the closest point on a bounding box to a point.
  657. *
  658. * @param {OrientedBoundingBox} box The box.
  659. * @param {Cartesian3} cartesian The point
  660. * @returns {number} The distance squared from the oriented bounding box to the point. Returns 0 if the point is inside the box.
  661. *
  662. * @example
  663. * // Sort bounding boxes from back to front
  664. * boxes.sort(function(a, b) {
  665. * return Cesium.OrientedBoundingBox.distanceSquaredTo(b, camera.positionWC) - Cesium.OrientedBoundingBox.distanceSquaredTo(a, camera.positionWC);
  666. * });
  667. */
  668. OrientedBoundingBox.distanceSquaredTo = function (box, cartesian) {
  669. // See Geometric Tools for Computer Graphics 10.4.2
  670. //>>includeStart('debug', pragmas.debug);
  671. if (!defined(box)) {
  672. throw new DeveloperError("box is required.");
  673. }
  674. if (!defined(cartesian)) {
  675. throw new DeveloperError("cartesian is required.");
  676. }
  677. //>>includeEnd('debug');
  678. const offset = Cartesian3.subtract(cartesian, box.center, scratchOffset);
  679. const halfAxes = box.halfAxes;
  680. let u = Matrix3.getColumn(halfAxes, 0, scratchCartesianU);
  681. let v = Matrix3.getColumn(halfAxes, 1, scratchCartesianV);
  682. let w = Matrix3.getColumn(halfAxes, 2, scratchCartesianW);
  683. const uHalf = Cartesian3.magnitude(u);
  684. const vHalf = Cartesian3.magnitude(v);
  685. const wHalf = Cartesian3.magnitude(w);
  686. let uValid = true;
  687. let vValid = true;
  688. let wValid = true;
  689. if (uHalf > 0) {
  690. Cartesian3.divideByScalar(u, uHalf, u);
  691. } else {
  692. uValid = false;
  693. }
  694. if (vHalf > 0) {
  695. Cartesian3.divideByScalar(v, vHalf, v);
  696. } else {
  697. vValid = false;
  698. }
  699. if (wHalf > 0) {
  700. Cartesian3.divideByScalar(w, wHalf, w);
  701. } else {
  702. wValid = false;
  703. }
  704. const numberOfDegenerateAxes = !uValid + !vValid + !wValid;
  705. let validAxis1;
  706. let validAxis2;
  707. let validAxis3;
  708. if (numberOfDegenerateAxes === 1) {
  709. let degenerateAxis = u;
  710. validAxis1 = v;
  711. validAxis2 = w;
  712. if (!vValid) {
  713. degenerateAxis = v;
  714. validAxis1 = u;
  715. } else if (!wValid) {
  716. degenerateAxis = w;
  717. validAxis2 = u;
  718. }
  719. validAxis3 = Cartesian3.cross(validAxis1, validAxis2, scratchValidAxis3);
  720. if (degenerateAxis === u) {
  721. u = validAxis3;
  722. } else if (degenerateAxis === v) {
  723. v = validAxis3;
  724. } else if (degenerateAxis === w) {
  725. w = validAxis3;
  726. }
  727. } else if (numberOfDegenerateAxes === 2) {
  728. validAxis1 = u;
  729. if (vValid) {
  730. validAxis1 = v;
  731. } else if (wValid) {
  732. validAxis1 = w;
  733. }
  734. let crossVector = Cartesian3.UNIT_Y;
  735. if (crossVector.equalsEpsilon(validAxis1, CesiumMath.EPSILON3)) {
  736. crossVector = Cartesian3.UNIT_X;
  737. }
  738. validAxis2 = Cartesian3.cross(validAxis1, crossVector, scratchValidAxis2);
  739. Cartesian3.normalize(validAxis2, validAxis2);
  740. validAxis3 = Cartesian3.cross(validAxis1, validAxis2, scratchValidAxis3);
  741. Cartesian3.normalize(validAxis3, validAxis3);
  742. if (validAxis1 === u) {
  743. v = validAxis2;
  744. w = validAxis3;
  745. } else if (validAxis1 === v) {
  746. w = validAxis2;
  747. u = validAxis3;
  748. } else if (validAxis1 === w) {
  749. u = validAxis2;
  750. v = validAxis3;
  751. }
  752. } else if (numberOfDegenerateAxes === 3) {
  753. u = Cartesian3.UNIT_X;
  754. v = Cartesian3.UNIT_Y;
  755. w = Cartesian3.UNIT_Z;
  756. }
  757. const pPrime = scratchPPrime;
  758. pPrime.x = Cartesian3.dot(offset, u);
  759. pPrime.y = Cartesian3.dot(offset, v);
  760. pPrime.z = Cartesian3.dot(offset, w);
  761. let distanceSquared = 0.0;
  762. let d;
  763. if (pPrime.x < -uHalf) {
  764. d = pPrime.x + uHalf;
  765. distanceSquared += d * d;
  766. } else if (pPrime.x > uHalf) {
  767. d = pPrime.x - uHalf;
  768. distanceSquared += d * d;
  769. }
  770. if (pPrime.y < -vHalf) {
  771. d = pPrime.y + vHalf;
  772. distanceSquared += d * d;
  773. } else if (pPrime.y > vHalf) {
  774. d = pPrime.y - vHalf;
  775. distanceSquared += d * d;
  776. }
  777. if (pPrime.z < -wHalf) {
  778. d = pPrime.z + wHalf;
  779. distanceSquared += d * d;
  780. } else if (pPrime.z > wHalf) {
  781. d = pPrime.z - wHalf;
  782. distanceSquared += d * d;
  783. }
  784. return distanceSquared;
  785. };
  786. const scratchCorner = new Cartesian3();
  787. const scratchToCenter = new Cartesian3();
  788. /**
  789. * The distances calculated by the vector from the center of the bounding box to position projected onto direction.
  790. * <br>
  791. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  792. * closest and farthest planes from position that intersect the bounding box.
  793. *
  794. * @param {OrientedBoundingBox} box The bounding box to calculate the distance to.
  795. * @param {Cartesian3} position The position to calculate the distance from.
  796. * @param {Cartesian3} direction The direction from position.
  797. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  798. * @returns {Interval} The nearest and farthest distances on the bounding box from position in direction.
  799. */
  800. OrientedBoundingBox.computePlaneDistances = function (
  801. box,
  802. position,
  803. direction,
  804. result
  805. ) {
  806. //>>includeStart('debug', pragmas.debug);
  807. if (!defined(box)) {
  808. throw new DeveloperError("box is required.");
  809. }
  810. if (!defined(position)) {
  811. throw new DeveloperError("position is required.");
  812. }
  813. if (!defined(direction)) {
  814. throw new DeveloperError("direction is required.");
  815. }
  816. //>>includeEnd('debug');
  817. if (!defined(result)) {
  818. result = new Interval();
  819. }
  820. let minDist = Number.POSITIVE_INFINITY;
  821. let maxDist = Number.NEGATIVE_INFINITY;
  822. const center = box.center;
  823. const halfAxes = box.halfAxes;
  824. const u = Matrix3.getColumn(halfAxes, 0, scratchCartesianU);
  825. const v = Matrix3.getColumn(halfAxes, 1, scratchCartesianV);
  826. const w = Matrix3.getColumn(halfAxes, 2, scratchCartesianW);
  827. // project first corner
  828. const corner = Cartesian3.add(u, v, scratchCorner);
  829. Cartesian3.add(corner, w, corner);
  830. Cartesian3.add(corner, center, corner);
  831. const toCenter = Cartesian3.subtract(corner, position, scratchToCenter);
  832. let mag = Cartesian3.dot(direction, toCenter);
  833. minDist = Math.min(mag, minDist);
  834. maxDist = Math.max(mag, maxDist);
  835. // project second corner
  836. Cartesian3.add(center, u, corner);
  837. Cartesian3.add(corner, v, corner);
  838. Cartesian3.subtract(corner, w, corner);
  839. Cartesian3.subtract(corner, position, toCenter);
  840. mag = Cartesian3.dot(direction, toCenter);
  841. minDist = Math.min(mag, minDist);
  842. maxDist = Math.max(mag, maxDist);
  843. // project third corner
  844. Cartesian3.add(center, u, corner);
  845. Cartesian3.subtract(corner, v, corner);
  846. Cartesian3.add(corner, w, corner);
  847. Cartesian3.subtract(corner, position, toCenter);
  848. mag = Cartesian3.dot(direction, toCenter);
  849. minDist = Math.min(mag, minDist);
  850. maxDist = Math.max(mag, maxDist);
  851. // project fourth corner
  852. Cartesian3.add(center, u, corner);
  853. Cartesian3.subtract(corner, v, corner);
  854. Cartesian3.subtract(corner, w, corner);
  855. Cartesian3.subtract(corner, position, toCenter);
  856. mag = Cartesian3.dot(direction, toCenter);
  857. minDist = Math.min(mag, minDist);
  858. maxDist = Math.max(mag, maxDist);
  859. // project fifth corner
  860. Cartesian3.subtract(center, u, corner);
  861. Cartesian3.add(corner, v, corner);
  862. Cartesian3.add(corner, w, corner);
  863. Cartesian3.subtract(corner, position, toCenter);
  864. mag = Cartesian3.dot(direction, toCenter);
  865. minDist = Math.min(mag, minDist);
  866. maxDist = Math.max(mag, maxDist);
  867. // project sixth corner
  868. Cartesian3.subtract(center, u, corner);
  869. Cartesian3.add(corner, v, corner);
  870. Cartesian3.subtract(corner, w, corner);
  871. Cartesian3.subtract(corner, position, toCenter);
  872. mag = Cartesian3.dot(direction, toCenter);
  873. minDist = Math.min(mag, minDist);
  874. maxDist = Math.max(mag, maxDist);
  875. // project seventh corner
  876. Cartesian3.subtract(center, u, corner);
  877. Cartesian3.subtract(corner, v, corner);
  878. Cartesian3.add(corner, w, corner);
  879. Cartesian3.subtract(corner, position, toCenter);
  880. mag = Cartesian3.dot(direction, toCenter);
  881. minDist = Math.min(mag, minDist);
  882. maxDist = Math.max(mag, maxDist);
  883. // project eighth corner
  884. Cartesian3.subtract(center, u, corner);
  885. Cartesian3.subtract(corner, v, corner);
  886. Cartesian3.subtract(corner, w, corner);
  887. Cartesian3.subtract(corner, position, toCenter);
  888. mag = Cartesian3.dot(direction, toCenter);
  889. minDist = Math.min(mag, minDist);
  890. maxDist = Math.max(mag, maxDist);
  891. result.start = minDist;
  892. result.stop = maxDist;
  893. return result;
  894. };
  895. const scratchXAxis = new Cartesian3();
  896. const scratchYAxis = new Cartesian3();
  897. const scratchZAxis = new Cartesian3();
  898. /**
  899. * Computes the eight corners of an oriented bounding box. The corners are ordered by (-X, -Y, -Z), (-X, -Y, +Z), (-X, +Y, -Z), (-X, +Y, +Z), (+X, -Y, -Z), (+X, -Y, +Z), (+X, +Y, -Z), (+X, +Y, +Z).
  900. *
  901. * @param {OrientedBoundingBox} box The oriented bounding box.
  902. * @param {Cartesian3[]} [result] An array of eight {@link Cartesian3} instances onto which to store the corners.
  903. * @returns {Cartesian3[]} The modified result parameter or a new array if none was provided.
  904. */
  905. OrientedBoundingBox.computeCorners = function (box, result) {
  906. //>>includeStart('debug', pragmas.debug);
  907. Check.typeOf.object("box", box);
  908. //>>includeEnd('debug');
  909. if (!defined(result)) {
  910. result = [
  911. new Cartesian3(),
  912. new Cartesian3(),
  913. new Cartesian3(),
  914. new Cartesian3(),
  915. new Cartesian3(),
  916. new Cartesian3(),
  917. new Cartesian3(),
  918. new Cartesian3(),
  919. ];
  920. }
  921. const center = box.center;
  922. const halfAxes = box.halfAxes;
  923. const xAxis = Matrix3.getColumn(halfAxes, 0, scratchXAxis);
  924. const yAxis = Matrix3.getColumn(halfAxes, 1, scratchYAxis);
  925. const zAxis = Matrix3.getColumn(halfAxes, 2, scratchZAxis);
  926. Cartesian3.clone(center, result[0]);
  927. Cartesian3.subtract(result[0], xAxis, result[0]);
  928. Cartesian3.subtract(result[0], yAxis, result[0]);
  929. Cartesian3.subtract(result[0], zAxis, result[0]);
  930. Cartesian3.clone(center, result[1]);
  931. Cartesian3.subtract(result[1], xAxis, result[1]);
  932. Cartesian3.subtract(result[1], yAxis, result[1]);
  933. Cartesian3.add(result[1], zAxis, result[1]);
  934. Cartesian3.clone(center, result[2]);
  935. Cartesian3.subtract(result[2], xAxis, result[2]);
  936. Cartesian3.add(result[2], yAxis, result[2]);
  937. Cartesian3.subtract(result[2], zAxis, result[2]);
  938. Cartesian3.clone(center, result[3]);
  939. Cartesian3.subtract(result[3], xAxis, result[3]);
  940. Cartesian3.add(result[3], yAxis, result[3]);
  941. Cartesian3.add(result[3], zAxis, result[3]);
  942. Cartesian3.clone(center, result[4]);
  943. Cartesian3.add(result[4], xAxis, result[4]);
  944. Cartesian3.subtract(result[4], yAxis, result[4]);
  945. Cartesian3.subtract(result[4], zAxis, result[4]);
  946. Cartesian3.clone(center, result[5]);
  947. Cartesian3.add(result[5], xAxis, result[5]);
  948. Cartesian3.subtract(result[5], yAxis, result[5]);
  949. Cartesian3.add(result[5], zAxis, result[5]);
  950. Cartesian3.clone(center, result[6]);
  951. Cartesian3.add(result[6], xAxis, result[6]);
  952. Cartesian3.add(result[6], yAxis, result[6]);
  953. Cartesian3.subtract(result[6], zAxis, result[6]);
  954. Cartesian3.clone(center, result[7]);
  955. Cartesian3.add(result[7], xAxis, result[7]);
  956. Cartesian3.add(result[7], yAxis, result[7]);
  957. Cartesian3.add(result[7], zAxis, result[7]);
  958. return result;
  959. };
  960. const scratchRotationScale = new Matrix3();
  961. /**
  962. * Computes a transformation matrix from an oriented bounding box.
  963. *
  964. * @param {OrientedBoundingBox} box The oriented bounding box.
  965. * @param {Matrix4} result The object onto which to store the result.
  966. * @returns {Matrix4} The modified result parameter or a new {@link Matrix4} instance if none was provided.
  967. */
  968. OrientedBoundingBox.computeTransformation = function (box, result) {
  969. //>>includeStart('debug', pragmas.debug);
  970. Check.typeOf.object("box", box);
  971. //>>includeEnd('debug');
  972. if (!defined(result)) {
  973. result = new Matrix4();
  974. }
  975. const translation = box.center;
  976. const rotationScale = Matrix3.multiplyByUniformScale(
  977. box.halfAxes,
  978. 2.0,
  979. scratchRotationScale
  980. );
  981. return Matrix4.fromRotationTranslation(rotationScale, translation, result);
  982. };
  983. const scratchBoundingSphere = new BoundingSphere();
  984. /**
  985. * Determines whether or not a bounding box is hidden from view by the occluder.
  986. *
  987. * @param {OrientedBoundingBox} box The bounding box surrounding the occludee object.
  988. * @param {Occluder} occluder The occluder.
  989. * @returns {boolean} <code>true</code> if the box is not visible; otherwise <code>false</code>.
  990. */
  991. OrientedBoundingBox.isOccluded = function (box, occluder) {
  992. //>>includeStart('debug', pragmas.debug);
  993. if (!defined(box)) {
  994. throw new DeveloperError("box is required.");
  995. }
  996. if (!defined(occluder)) {
  997. throw new DeveloperError("occluder is required.");
  998. }
  999. //>>includeEnd('debug');
  1000. const sphere = BoundingSphere.fromOrientedBoundingBox(
  1001. box,
  1002. scratchBoundingSphere
  1003. );
  1004. return !occluder.isBoundingSphereVisible(sphere);
  1005. };
  1006. /**
  1007. * Determines which side of a plane the oriented bounding box is located.
  1008. *
  1009. * @param {Plane} plane The plane to test against.
  1010. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  1011. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  1012. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  1013. * intersects the plane.
  1014. */
  1015. OrientedBoundingBox.prototype.intersectPlane = function (plane) {
  1016. return OrientedBoundingBox.intersectPlane(this, plane);
  1017. };
  1018. /**
  1019. * Computes the estimated distance squared from the closest point on a bounding box to a point.
  1020. *
  1021. * @param {Cartesian3} cartesian The point
  1022. * @returns {number} The estimated distance squared from the bounding sphere to the point.
  1023. *
  1024. * @example
  1025. * // Sort bounding boxes from back to front
  1026. * boxes.sort(function(a, b) {
  1027. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  1028. * });
  1029. */
  1030. OrientedBoundingBox.prototype.distanceSquaredTo = function (cartesian) {
  1031. return OrientedBoundingBox.distanceSquaredTo(this, cartesian);
  1032. };
  1033. /**
  1034. * The distances calculated by the vector from the center of the bounding box to position projected onto direction.
  1035. * <br>
  1036. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  1037. * closest and farthest planes from position that intersect the bounding box.
  1038. *
  1039. * @param {Cartesian3} position The position to calculate the distance from.
  1040. * @param {Cartesian3} direction The direction from position.
  1041. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  1042. * @returns {Interval} The nearest and farthest distances on the bounding box from position in direction.
  1043. */
  1044. OrientedBoundingBox.prototype.computePlaneDistances = function (
  1045. position,
  1046. direction,
  1047. result
  1048. ) {
  1049. return OrientedBoundingBox.computePlaneDistances(
  1050. this,
  1051. position,
  1052. direction,
  1053. result
  1054. );
  1055. };
  1056. /**
  1057. * Computes the eight corners of an oriented bounding box. The corners are ordered by (-X, -Y, -Z), (-X, -Y, +Z), (-X, +Y, -Z), (-X, +Y, +Z), (+X, -Y, -Z), (+X, -Y, +Z), (+X, +Y, -Z), (+X, +Y, +Z).
  1058. *
  1059. * @param {Cartesian3[]} [result] An array of eight {@link Cartesian3} instances onto which to store the corners.
  1060. * @returns {Cartesian3[]} The modified result parameter or a new array if none was provided.
  1061. */
  1062. OrientedBoundingBox.prototype.computeCorners = function (result) {
  1063. return OrientedBoundingBox.computeCorners(this, result);
  1064. };
  1065. /**
  1066. * Computes a transformation matrix from an oriented bounding box.
  1067. *
  1068. * @param {Matrix4} result The object onto which to store the result.
  1069. * @returns {Matrix4} The modified result parameter or a new {@link Matrix4} instance if none was provided.
  1070. */
  1071. OrientedBoundingBox.prototype.computeTransformation = function (result) {
  1072. return OrientedBoundingBox.computeTransformation(this, result);
  1073. };
  1074. /**
  1075. * Determines whether or not a bounding box is hidden from view by the occluder.
  1076. *
  1077. * @param {Occluder} occluder The occluder.
  1078. * @returns {boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  1079. */
  1080. OrientedBoundingBox.prototype.isOccluded = function (occluder) {
  1081. return OrientedBoundingBox.isOccluded(this, occluder);
  1082. };
  1083. /**
  1084. * Compares the provided OrientedBoundingBox componentwise and returns
  1085. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1086. *
  1087. * @param {OrientedBoundingBox} left The first OrientedBoundingBox.
  1088. * @param {OrientedBoundingBox} right The second OrientedBoundingBox.
  1089. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1090. */
  1091. OrientedBoundingBox.equals = function (left, right) {
  1092. return (
  1093. left === right ||
  1094. (defined(left) &&
  1095. defined(right) &&
  1096. Cartesian3.equals(left.center, right.center) &&
  1097. Matrix3.equals(left.halfAxes, right.halfAxes))
  1098. );
  1099. };
  1100. /**
  1101. * Duplicates this OrientedBoundingBox instance.
  1102. *
  1103. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  1104. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if one was not provided.
  1105. */
  1106. OrientedBoundingBox.prototype.clone = function (result) {
  1107. return OrientedBoundingBox.clone(this, result);
  1108. };
  1109. /**
  1110. * Compares this OrientedBoundingBox against the provided OrientedBoundingBox componentwise and returns
  1111. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1112. *
  1113. * @param {OrientedBoundingBox} [right] The right hand side OrientedBoundingBox.
  1114. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1115. */
  1116. OrientedBoundingBox.prototype.equals = function (right) {
  1117. return OrientedBoundingBox.equals(this, right);
  1118. };
  1119. export default OrientedBoundingBox;