OrientedBoundingBox-635e6e10.js 42 KB

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