OrientedBoundingBox.js 41 KB

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