OrientedBoundingBox.js 33 KB

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