OrientedBoundingBox-0b41570b.js 46 KB

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