OrientedBoundingBox-2dd47921.js 45 KB

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