OrientedBoundingBox-417528c0.js 45 KB

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