BoundingSphere.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Cartographic from "./Cartographic.js";
  3. import Check from "./Check.js";
  4. import defaultValue from "./defaultValue.js";
  5. import defined from "./defined.js";
  6. import Ellipsoid from "./Ellipsoid.js";
  7. import GeographicProjection from "./GeographicProjection.js";
  8. import Intersect from "./Intersect.js";
  9. import Interval from "./Interval.js";
  10. import CesiumMath from "./Math.js";
  11. import Matrix3 from "./Matrix3.js";
  12. import Matrix4 from "./Matrix4.js";
  13. import Rectangle from "./Rectangle.js";
  14. /**
  15. * A bounding sphere with a center and a radius.
  16. * @alias BoundingSphere
  17. * @constructor
  18. *
  19. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  20. * @param {Number} [radius=0.0] The radius of the bounding sphere.
  21. *
  22. * @see AxisAlignedBoundingBox
  23. * @see BoundingRectangle
  24. * @see Packable
  25. */
  26. function BoundingSphere(center, radius) {
  27. /**
  28. * The center point of the sphere.
  29. * @type {Cartesian3}
  30. * @default {@link Cartesian3.ZERO}
  31. */
  32. this.center = Cartesian3.clone(defaultValue(center, Cartesian3.ZERO));
  33. /**
  34. * The radius of the sphere.
  35. * @type {Number}
  36. * @default 0.0
  37. */
  38. this.radius = defaultValue(radius, 0.0);
  39. }
  40. var fromPointsXMin = new Cartesian3();
  41. var fromPointsYMin = new Cartesian3();
  42. var fromPointsZMin = new Cartesian3();
  43. var fromPointsXMax = new Cartesian3();
  44. var fromPointsYMax = new Cartesian3();
  45. var fromPointsZMax = new Cartesian3();
  46. var fromPointsCurrentPos = new Cartesian3();
  47. var fromPointsScratch = new Cartesian3();
  48. var fromPointsRitterCenter = new Cartesian3();
  49. var fromPointsMinBoxPt = new Cartesian3();
  50. var fromPointsMaxBoxPt = new Cartesian3();
  51. var fromPointsNaiveCenterScratch = new Cartesian3();
  52. var volumeConstant = (4.0 / 3.0) * CesiumMath.PI;
  53. /**
  54. * Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
  55. * The bounding sphere is computed by running two algorithms, a naive algorithm and
  56. * Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
  57. *
  58. * @param {Cartesian3[]} [positions] An array of points that the bounding sphere will enclose. Each point must have <code>x</code>, <code>y</code>, and <code>z</code> properties.
  59. * @param {BoundingSphere} [result] The object onto which to store the result.
  60. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  61. *
  62. * @see {@link http://help.agi.com/AGIComponents/html/BlogBoundingSphere.htm|Bounding Sphere computation article}
  63. */
  64. BoundingSphere.fromPoints = function (positions, result) {
  65. if (!defined(result)) {
  66. result = new BoundingSphere();
  67. }
  68. if (!defined(positions) || positions.length === 0) {
  69. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  70. result.radius = 0.0;
  71. return result;
  72. }
  73. var currentPos = Cartesian3.clone(positions[0], fromPointsCurrentPos);
  74. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  75. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  76. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  77. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  78. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  79. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  80. var numPositions = positions.length;
  81. var i;
  82. for (i = 1; i < numPositions; i++) {
  83. Cartesian3.clone(positions[i], currentPos);
  84. var x = currentPos.x;
  85. var y = currentPos.y;
  86. var z = currentPos.z;
  87. // Store points containing the the smallest and largest components
  88. if (x < xMin.x) {
  89. Cartesian3.clone(currentPos, xMin);
  90. }
  91. if (x > xMax.x) {
  92. Cartesian3.clone(currentPos, xMax);
  93. }
  94. if (y < yMin.y) {
  95. Cartesian3.clone(currentPos, yMin);
  96. }
  97. if (y > yMax.y) {
  98. Cartesian3.clone(currentPos, yMax);
  99. }
  100. if (z < zMin.z) {
  101. Cartesian3.clone(currentPos, zMin);
  102. }
  103. if (z > zMax.z) {
  104. Cartesian3.clone(currentPos, zMax);
  105. }
  106. }
  107. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  108. var xSpan = Cartesian3.magnitudeSquared(
  109. Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  110. );
  111. var ySpan = Cartesian3.magnitudeSquared(
  112. Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  113. );
  114. var zSpan = Cartesian3.magnitudeSquared(
  115. Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  116. );
  117. // Set the diameter endpoints to the largest span.
  118. var diameter1 = xMin;
  119. var diameter2 = xMax;
  120. var maxSpan = xSpan;
  121. if (ySpan > maxSpan) {
  122. maxSpan = ySpan;
  123. diameter1 = yMin;
  124. diameter2 = yMax;
  125. }
  126. if (zSpan > maxSpan) {
  127. maxSpan = zSpan;
  128. diameter1 = zMin;
  129. diameter2 = zMax;
  130. }
  131. // Calculate the center of the initial sphere found by Ritter's algorithm
  132. var ritterCenter = fromPointsRitterCenter;
  133. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  134. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  135. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  136. // Calculate the radius of the initial sphere found by Ritter's algorithm
  137. var radiusSquared = Cartesian3.magnitudeSquared(
  138. Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  139. );
  140. var ritterRadius = Math.sqrt(radiusSquared);
  141. // Find the center of the sphere found using the Naive method.
  142. var minBoxPt = fromPointsMinBoxPt;
  143. minBoxPt.x = xMin.x;
  144. minBoxPt.y = yMin.y;
  145. minBoxPt.z = zMin.z;
  146. var maxBoxPt = fromPointsMaxBoxPt;
  147. maxBoxPt.x = xMax.x;
  148. maxBoxPt.y = yMax.y;
  149. maxBoxPt.z = zMax.z;
  150. var naiveCenter = Cartesian3.midpoint(
  151. minBoxPt,
  152. maxBoxPt,
  153. fromPointsNaiveCenterScratch
  154. );
  155. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  156. var naiveRadius = 0;
  157. for (i = 0; i < numPositions; i++) {
  158. Cartesian3.clone(positions[i], currentPos);
  159. // Find the furthest point from the naive center to calculate the naive radius.
  160. var r = Cartesian3.magnitude(
  161. Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  162. );
  163. if (r > naiveRadius) {
  164. naiveRadius = r;
  165. }
  166. // Make adjustments to the Ritter Sphere to include all points.
  167. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(
  168. Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  169. );
  170. if (oldCenterToPointSquared > radiusSquared) {
  171. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  172. // Calculate new radius to include the point that lies outside
  173. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  174. radiusSquared = ritterRadius * ritterRadius;
  175. // Calculate center of new Ritter sphere
  176. var oldToNew = oldCenterToPoint - ritterRadius;
  177. ritterCenter.x =
  178. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  179. oldCenterToPoint;
  180. ritterCenter.y =
  181. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  182. oldCenterToPoint;
  183. ritterCenter.z =
  184. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  185. oldCenterToPoint;
  186. }
  187. }
  188. if (ritterRadius < naiveRadius) {
  189. Cartesian3.clone(ritterCenter, result.center);
  190. result.radius = ritterRadius;
  191. } else {
  192. Cartesian3.clone(naiveCenter, result.center);
  193. result.radius = naiveRadius;
  194. }
  195. return result;
  196. };
  197. var defaultProjection = new GeographicProjection();
  198. var fromRectangle2DLowerLeft = new Cartesian3();
  199. var fromRectangle2DUpperRight = new Cartesian3();
  200. var fromRectangle2DSouthwest = new Cartographic();
  201. var fromRectangle2DNortheast = new Cartographic();
  202. /**
  203. * Computes a bounding sphere from a rectangle projected in 2D.
  204. *
  205. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  206. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  207. * @param {BoundingSphere} [result] The object onto which to store the result.
  208. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  209. */
  210. BoundingSphere.fromRectangle2D = function (rectangle, projection, result) {
  211. return BoundingSphere.fromRectangleWithHeights2D(
  212. rectangle,
  213. projection,
  214. 0.0,
  215. 0.0,
  216. result
  217. );
  218. };
  219. /**
  220. * Computes a bounding sphere from a rectangle projected in 2D. The bounding sphere accounts for the
  221. * object's minimum and maximum heights over the rectangle.
  222. *
  223. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  224. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  225. * @param {Number} [minimumHeight=0.0] The minimum height over the rectangle.
  226. * @param {Number} [maximumHeight=0.0] The maximum height over the rectangle.
  227. * @param {BoundingSphere} [result] The object onto which to store the result.
  228. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  229. */
  230. BoundingSphere.fromRectangleWithHeights2D = function (
  231. rectangle,
  232. projection,
  233. minimumHeight,
  234. maximumHeight,
  235. result
  236. ) {
  237. if (!defined(result)) {
  238. result = new BoundingSphere();
  239. }
  240. if (!defined(rectangle)) {
  241. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  242. result.radius = 0.0;
  243. return result;
  244. }
  245. projection = defaultValue(projection, defaultProjection);
  246. Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
  247. fromRectangle2DSouthwest.height = minimumHeight;
  248. Rectangle.northeast(rectangle, fromRectangle2DNortheast);
  249. fromRectangle2DNortheast.height = maximumHeight;
  250. var lowerLeft = projection.project(
  251. fromRectangle2DSouthwest,
  252. fromRectangle2DLowerLeft
  253. );
  254. var upperRight = projection.project(
  255. fromRectangle2DNortheast,
  256. fromRectangle2DUpperRight
  257. );
  258. var width = upperRight.x - lowerLeft.x;
  259. var height = upperRight.y - lowerLeft.y;
  260. var elevation = upperRight.z - lowerLeft.z;
  261. result.radius =
  262. Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
  263. var center = result.center;
  264. center.x = lowerLeft.x + width * 0.5;
  265. center.y = lowerLeft.y + height * 0.5;
  266. center.z = lowerLeft.z + elevation * 0.5;
  267. return result;
  268. };
  269. var fromRectangle3DScratch = [];
  270. /**
  271. * Computes a bounding sphere from a rectangle in 3D. The bounding sphere is created using a subsample of points
  272. * on the ellipsoid and contained in the rectangle. It may not be accurate for all rectangles on all types of ellipsoids.
  273. *
  274. * @param {Rectangle} [rectangle] The valid rectangle used to create a bounding sphere.
  275. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine positions of the rectangle.
  276. * @param {Number} [surfaceHeight=0.0] The height above the surface of the ellipsoid.
  277. * @param {BoundingSphere} [result] The object onto which to store the result.
  278. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  279. */
  280. BoundingSphere.fromRectangle3D = function (
  281. rectangle,
  282. ellipsoid,
  283. surfaceHeight,
  284. result
  285. ) {
  286. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  287. surfaceHeight = defaultValue(surfaceHeight, 0.0);
  288. if (!defined(result)) {
  289. result = new BoundingSphere();
  290. }
  291. if (!defined(rectangle)) {
  292. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  293. result.radius = 0.0;
  294. return result;
  295. }
  296. var positions = Rectangle.subsample(
  297. rectangle,
  298. ellipsoid,
  299. surfaceHeight,
  300. fromRectangle3DScratch
  301. );
  302. return BoundingSphere.fromPoints(positions, result);
  303. };
  304. /**
  305. * Computes a tight-fitting bounding sphere enclosing a list of 3D points, where the points are
  306. * stored in a flat array in X, Y, Z, order. The bounding sphere is computed by running two
  307. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  308. * ensure a tight fit.
  309. *
  310. * @param {Number[]} [positions] An array of points that the bounding sphere will enclose. Each point
  311. * is formed from three elements in the array in the order X, Y, Z.
  312. * @param {Cartesian3} [center=Cartesian3.ZERO] The position to which the positions are relative, which need not be the
  313. * origin of the coordinate system. This is useful when the positions are to be used for
  314. * relative-to-center (RTC) rendering.
  315. * @param {Number} [stride=3] The number of array elements per vertex. It must be at least 3, but it may
  316. * be higher. Regardless of the value of this parameter, the X coordinate of the first position
  317. * is at array index 0, the Y coordinate is at array index 1, and the Z coordinate is at array index
  318. * 2. When stride is 3, the X coordinate of the next position then begins at array index 3. If
  319. * the stride is 5, however, two array elements are skipped and the next position begins at array
  320. * index 5.
  321. * @param {BoundingSphere} [result] The object onto which to store the result.
  322. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  323. *
  324. * @example
  325. * // Compute the bounding sphere from 3 positions, each specified relative to a center.
  326. * // In addition to the X, Y, and Z coordinates, the points array contains two additional
  327. * // elements per point which are ignored for the purpose of computing the bounding sphere.
  328. * var center = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  329. * var points = [1.0, 2.0, 3.0, 0.1, 0.2,
  330. * 4.0, 5.0, 6.0, 0.1, 0.2,
  331. * 7.0, 8.0, 9.0, 0.1, 0.2];
  332. * var sphere = Cesium.BoundingSphere.fromVertices(points, center, 5);
  333. *
  334. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  335. */
  336. BoundingSphere.fromVertices = function (positions, center, stride, result) {
  337. if (!defined(result)) {
  338. result = new BoundingSphere();
  339. }
  340. if (!defined(positions) || positions.length === 0) {
  341. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  342. result.radius = 0.0;
  343. return result;
  344. }
  345. center = defaultValue(center, Cartesian3.ZERO);
  346. stride = defaultValue(stride, 3);
  347. //>>includeStart('debug', pragmas.debug);
  348. Check.typeOf.number.greaterThanOrEquals("stride", stride, 3);
  349. //>>includeEnd('debug');
  350. var currentPos = fromPointsCurrentPos;
  351. currentPos.x = positions[0] + center.x;
  352. currentPos.y = positions[1] + center.y;
  353. currentPos.z = positions[2] + center.z;
  354. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  355. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  356. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  357. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  358. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  359. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  360. var numElements = positions.length;
  361. var i;
  362. for (i = 0; i < numElements; i += stride) {
  363. var x = positions[i] + center.x;
  364. var y = positions[i + 1] + center.y;
  365. var z = positions[i + 2] + center.z;
  366. currentPos.x = x;
  367. currentPos.y = y;
  368. currentPos.z = z;
  369. // Store points containing the the smallest and largest components
  370. if (x < xMin.x) {
  371. Cartesian3.clone(currentPos, xMin);
  372. }
  373. if (x > xMax.x) {
  374. Cartesian3.clone(currentPos, xMax);
  375. }
  376. if (y < yMin.y) {
  377. Cartesian3.clone(currentPos, yMin);
  378. }
  379. if (y > yMax.y) {
  380. Cartesian3.clone(currentPos, yMax);
  381. }
  382. if (z < zMin.z) {
  383. Cartesian3.clone(currentPos, zMin);
  384. }
  385. if (z > zMax.z) {
  386. Cartesian3.clone(currentPos, zMax);
  387. }
  388. }
  389. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  390. var xSpan = Cartesian3.magnitudeSquared(
  391. Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  392. );
  393. var ySpan = Cartesian3.magnitudeSquared(
  394. Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  395. );
  396. var zSpan = Cartesian3.magnitudeSquared(
  397. Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  398. );
  399. // Set the diameter endpoints to the largest span.
  400. var diameter1 = xMin;
  401. var diameter2 = xMax;
  402. var maxSpan = xSpan;
  403. if (ySpan > maxSpan) {
  404. maxSpan = ySpan;
  405. diameter1 = yMin;
  406. diameter2 = yMax;
  407. }
  408. if (zSpan > maxSpan) {
  409. maxSpan = zSpan;
  410. diameter1 = zMin;
  411. diameter2 = zMax;
  412. }
  413. // Calculate the center of the initial sphere found by Ritter's algorithm
  414. var ritterCenter = fromPointsRitterCenter;
  415. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  416. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  417. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  418. // Calculate the radius of the initial sphere found by Ritter's algorithm
  419. var radiusSquared = Cartesian3.magnitudeSquared(
  420. Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  421. );
  422. var ritterRadius = Math.sqrt(radiusSquared);
  423. // Find the center of the sphere found using the Naive method.
  424. var minBoxPt = fromPointsMinBoxPt;
  425. minBoxPt.x = xMin.x;
  426. minBoxPt.y = yMin.y;
  427. minBoxPt.z = zMin.z;
  428. var maxBoxPt = fromPointsMaxBoxPt;
  429. maxBoxPt.x = xMax.x;
  430. maxBoxPt.y = yMax.y;
  431. maxBoxPt.z = zMax.z;
  432. var naiveCenter = Cartesian3.midpoint(
  433. minBoxPt,
  434. maxBoxPt,
  435. fromPointsNaiveCenterScratch
  436. );
  437. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  438. var naiveRadius = 0;
  439. for (i = 0; i < numElements; i += stride) {
  440. currentPos.x = positions[i] + center.x;
  441. currentPos.y = positions[i + 1] + center.y;
  442. currentPos.z = positions[i + 2] + center.z;
  443. // Find the furthest point from the naive center to calculate the naive radius.
  444. var r = Cartesian3.magnitude(
  445. Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  446. );
  447. if (r > naiveRadius) {
  448. naiveRadius = r;
  449. }
  450. // Make adjustments to the Ritter Sphere to include all points.
  451. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(
  452. Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  453. );
  454. if (oldCenterToPointSquared > radiusSquared) {
  455. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  456. // Calculate new radius to include the point that lies outside
  457. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  458. radiusSquared = ritterRadius * ritterRadius;
  459. // Calculate center of new Ritter sphere
  460. var oldToNew = oldCenterToPoint - ritterRadius;
  461. ritterCenter.x =
  462. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  463. oldCenterToPoint;
  464. ritterCenter.y =
  465. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  466. oldCenterToPoint;
  467. ritterCenter.z =
  468. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  469. oldCenterToPoint;
  470. }
  471. }
  472. if (ritterRadius < naiveRadius) {
  473. Cartesian3.clone(ritterCenter, result.center);
  474. result.radius = ritterRadius;
  475. } else {
  476. Cartesian3.clone(naiveCenter, result.center);
  477. result.radius = naiveRadius;
  478. }
  479. return result;
  480. };
  481. /**
  482. * Computes a tight-fitting bounding sphere enclosing a list of EncodedCartesian3s, where the points are
  483. * stored in parallel flat arrays in X, Y, Z, order. The bounding sphere is computed by running two
  484. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  485. * ensure a tight fit.
  486. *
  487. * @param {Number[]} [positionsHigh] An array of high bits of the encoded cartesians that the bounding sphere will enclose. Each point
  488. * is formed from three elements in the array in the order X, Y, Z.
  489. * @param {Number[]} [positionsLow] An array of low bits of the encoded cartesians that the bounding sphere will enclose. Each point
  490. * is formed from three elements in the array in the order X, Y, Z.
  491. * @param {BoundingSphere} [result] The object onto which to store the result.
  492. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  493. *
  494. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  495. */
  496. BoundingSphere.fromEncodedCartesianVertices = function (
  497. positionsHigh,
  498. positionsLow,
  499. result
  500. ) {
  501. if (!defined(result)) {
  502. result = new BoundingSphere();
  503. }
  504. if (
  505. !defined(positionsHigh) ||
  506. !defined(positionsLow) ||
  507. positionsHigh.length !== positionsLow.length ||
  508. positionsHigh.length === 0
  509. ) {
  510. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  511. result.radius = 0.0;
  512. return result;
  513. }
  514. var currentPos = fromPointsCurrentPos;
  515. currentPos.x = positionsHigh[0] + positionsLow[0];
  516. currentPos.y = positionsHigh[1] + positionsLow[1];
  517. currentPos.z = positionsHigh[2] + positionsLow[2];
  518. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  519. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  520. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  521. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  522. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  523. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  524. var numElements = positionsHigh.length;
  525. var i;
  526. for (i = 0; i < numElements; i += 3) {
  527. var x = positionsHigh[i] + positionsLow[i];
  528. var y = positionsHigh[i + 1] + positionsLow[i + 1];
  529. var z = positionsHigh[i + 2] + positionsLow[i + 2];
  530. currentPos.x = x;
  531. currentPos.y = y;
  532. currentPos.z = z;
  533. // Store points containing the the smallest and largest components
  534. if (x < xMin.x) {
  535. Cartesian3.clone(currentPos, xMin);
  536. }
  537. if (x > xMax.x) {
  538. Cartesian3.clone(currentPos, xMax);
  539. }
  540. if (y < yMin.y) {
  541. Cartesian3.clone(currentPos, yMin);
  542. }
  543. if (y > yMax.y) {
  544. Cartesian3.clone(currentPos, yMax);
  545. }
  546. if (z < zMin.z) {
  547. Cartesian3.clone(currentPos, zMin);
  548. }
  549. if (z > zMax.z) {
  550. Cartesian3.clone(currentPos, zMax);
  551. }
  552. }
  553. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  554. var xSpan = Cartesian3.magnitudeSquared(
  555. Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  556. );
  557. var ySpan = Cartesian3.magnitudeSquared(
  558. Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  559. );
  560. var zSpan = Cartesian3.magnitudeSquared(
  561. Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  562. );
  563. // Set the diameter endpoints to the largest span.
  564. var diameter1 = xMin;
  565. var diameter2 = xMax;
  566. var maxSpan = xSpan;
  567. if (ySpan > maxSpan) {
  568. maxSpan = ySpan;
  569. diameter1 = yMin;
  570. diameter2 = yMax;
  571. }
  572. if (zSpan > maxSpan) {
  573. maxSpan = zSpan;
  574. diameter1 = zMin;
  575. diameter2 = zMax;
  576. }
  577. // Calculate the center of the initial sphere found by Ritter's algorithm
  578. var ritterCenter = fromPointsRitterCenter;
  579. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  580. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  581. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  582. // Calculate the radius of the initial sphere found by Ritter's algorithm
  583. var radiusSquared = Cartesian3.magnitudeSquared(
  584. Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  585. );
  586. var ritterRadius = Math.sqrt(radiusSquared);
  587. // Find the center of the sphere found using the Naive method.
  588. var minBoxPt = fromPointsMinBoxPt;
  589. minBoxPt.x = xMin.x;
  590. minBoxPt.y = yMin.y;
  591. minBoxPt.z = zMin.z;
  592. var maxBoxPt = fromPointsMaxBoxPt;
  593. maxBoxPt.x = xMax.x;
  594. maxBoxPt.y = yMax.y;
  595. maxBoxPt.z = zMax.z;
  596. var naiveCenter = Cartesian3.midpoint(
  597. minBoxPt,
  598. maxBoxPt,
  599. fromPointsNaiveCenterScratch
  600. );
  601. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  602. var naiveRadius = 0;
  603. for (i = 0; i < numElements; i += 3) {
  604. currentPos.x = positionsHigh[i] + positionsLow[i];
  605. currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
  606. currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
  607. // Find the furthest point from the naive center to calculate the naive radius.
  608. var r = Cartesian3.magnitude(
  609. Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  610. );
  611. if (r > naiveRadius) {
  612. naiveRadius = r;
  613. }
  614. // Make adjustments to the Ritter Sphere to include all points.
  615. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(
  616. Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  617. );
  618. if (oldCenterToPointSquared > radiusSquared) {
  619. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  620. // Calculate new radius to include the point that lies outside
  621. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  622. radiusSquared = ritterRadius * ritterRadius;
  623. // Calculate center of new Ritter sphere
  624. var oldToNew = oldCenterToPoint - ritterRadius;
  625. ritterCenter.x =
  626. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  627. oldCenterToPoint;
  628. ritterCenter.y =
  629. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  630. oldCenterToPoint;
  631. ritterCenter.z =
  632. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  633. oldCenterToPoint;
  634. }
  635. }
  636. if (ritterRadius < naiveRadius) {
  637. Cartesian3.clone(ritterCenter, result.center);
  638. result.radius = ritterRadius;
  639. } else {
  640. Cartesian3.clone(naiveCenter, result.center);
  641. result.radius = naiveRadius;
  642. }
  643. return result;
  644. };
  645. /**
  646. * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
  647. * tighly and fully encompases the box.
  648. *
  649. * @param {Cartesian3} [corner] The minimum height over the rectangle.
  650. * @param {Cartesian3} [oppositeCorner] The maximum height over the rectangle.
  651. * @param {BoundingSphere} [result] The object onto which to store the result.
  652. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  653. *
  654. * @example
  655. * // Create a bounding sphere around the unit cube
  656. * var sphere = Cesium.BoundingSphere.fromCornerPoints(new Cesium.Cartesian3(-0.5, -0.5, -0.5), new Cesium.Cartesian3(0.5, 0.5, 0.5));
  657. */
  658. BoundingSphere.fromCornerPoints = function (corner, oppositeCorner, result) {
  659. //>>includeStart('debug', pragmas.debug);
  660. Check.typeOf.object("corner", corner);
  661. Check.typeOf.object("oppositeCorner", oppositeCorner);
  662. //>>includeEnd('debug');
  663. if (!defined(result)) {
  664. result = new BoundingSphere();
  665. }
  666. var center = Cartesian3.midpoint(corner, oppositeCorner, result.center);
  667. result.radius = Cartesian3.distance(center, oppositeCorner);
  668. return result;
  669. };
  670. /**
  671. * Creates a bounding sphere encompassing an ellipsoid.
  672. *
  673. * @param {Ellipsoid} ellipsoid The ellipsoid around which to create a bounding sphere.
  674. * @param {BoundingSphere} [result] The object onto which to store the result.
  675. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  676. *
  677. * @example
  678. * var boundingSphere = Cesium.BoundingSphere.fromEllipsoid(ellipsoid);
  679. */
  680. BoundingSphere.fromEllipsoid = function (ellipsoid, result) {
  681. //>>includeStart('debug', pragmas.debug);
  682. Check.typeOf.object("ellipsoid", ellipsoid);
  683. //>>includeEnd('debug');
  684. if (!defined(result)) {
  685. result = new BoundingSphere();
  686. }
  687. Cartesian3.clone(Cartesian3.ZERO, result.center);
  688. result.radius = ellipsoid.maximumRadius;
  689. return result;
  690. };
  691. var fromBoundingSpheresScratch = new Cartesian3();
  692. /**
  693. * Computes a tight-fitting bounding sphere enclosing the provided array of bounding spheres.
  694. *
  695. * @param {BoundingSphere[]} [boundingSpheres] The array of bounding spheres.
  696. * @param {BoundingSphere} [result] The object onto which to store the result.
  697. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  698. */
  699. BoundingSphere.fromBoundingSpheres = function (boundingSpheres, result) {
  700. if (!defined(result)) {
  701. result = new BoundingSphere();
  702. }
  703. if (!defined(boundingSpheres) || boundingSpheres.length === 0) {
  704. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  705. result.radius = 0.0;
  706. return result;
  707. }
  708. var length = boundingSpheres.length;
  709. if (length === 1) {
  710. return BoundingSphere.clone(boundingSpheres[0], result);
  711. }
  712. if (length === 2) {
  713. return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
  714. }
  715. var positions = [];
  716. var i;
  717. for (i = 0; i < length; i++) {
  718. positions.push(boundingSpheres[i].center);
  719. }
  720. result = BoundingSphere.fromPoints(positions, result);
  721. var center = result.center;
  722. var radius = result.radius;
  723. for (i = 0; i < length; i++) {
  724. var tmp = boundingSpheres[i];
  725. radius = Math.max(
  726. radius,
  727. Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) +
  728. tmp.radius
  729. );
  730. }
  731. result.radius = radius;
  732. return result;
  733. };
  734. var fromOrientedBoundingBoxScratchU = new Cartesian3();
  735. var fromOrientedBoundingBoxScratchV = new Cartesian3();
  736. var fromOrientedBoundingBoxScratchW = new Cartesian3();
  737. /**
  738. * Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
  739. *
  740. * @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
  741. * @param {BoundingSphere} [result] The object onto which to store the result.
  742. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  743. */
  744. BoundingSphere.fromOrientedBoundingBox = function (
  745. orientedBoundingBox,
  746. result
  747. ) {
  748. //>>includeStart('debug', pragmas.debug);
  749. Check.defined("orientedBoundingBox", orientedBoundingBox);
  750. //>>includeEnd('debug');
  751. if (!defined(result)) {
  752. result = new BoundingSphere();
  753. }
  754. var halfAxes = orientedBoundingBox.halfAxes;
  755. var u = Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
  756. var v = Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
  757. var w = Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
  758. Cartesian3.add(u, v, u);
  759. Cartesian3.add(u, w, u);
  760. result.center = Cartesian3.clone(orientedBoundingBox.center, result.center);
  761. result.radius = Cartesian3.magnitude(u);
  762. return result;
  763. };
  764. /**
  765. * Duplicates a BoundingSphere instance.
  766. *
  767. * @param {BoundingSphere} sphere The bounding sphere to duplicate.
  768. * @param {BoundingSphere} [result] The object onto which to store the result.
  769. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided. (Returns undefined if sphere is undefined)
  770. */
  771. BoundingSphere.clone = function (sphere, result) {
  772. if (!defined(sphere)) {
  773. return undefined;
  774. }
  775. if (!defined(result)) {
  776. return new BoundingSphere(sphere.center, sphere.radius);
  777. }
  778. result.center = Cartesian3.clone(sphere.center, result.center);
  779. result.radius = sphere.radius;
  780. return result;
  781. };
  782. /**
  783. * The number of elements used to pack the object into an array.
  784. * @type {Number}
  785. */
  786. BoundingSphere.packedLength = 4;
  787. /**
  788. * Stores the provided instance into the provided array.
  789. *
  790. * @param {BoundingSphere} value The value to pack.
  791. * @param {Number[]} array The array to pack into.
  792. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  793. *
  794. * @returns {Number[]} The array that was packed into
  795. */
  796. BoundingSphere.pack = function (value, array, startingIndex) {
  797. //>>includeStart('debug', pragmas.debug);
  798. Check.typeOf.object("value", value);
  799. Check.defined("array", array);
  800. //>>includeEnd('debug');
  801. startingIndex = defaultValue(startingIndex, 0);
  802. var center = value.center;
  803. array[startingIndex++] = center.x;
  804. array[startingIndex++] = center.y;
  805. array[startingIndex++] = center.z;
  806. array[startingIndex] = value.radius;
  807. return array;
  808. };
  809. /**
  810. * Retrieves an instance from a packed array.
  811. *
  812. * @param {Number[]} array The packed array.
  813. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  814. * @param {BoundingSphere} [result] The object into which to store the result.
  815. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  816. */
  817. BoundingSphere.unpack = function (array, startingIndex, result) {
  818. //>>includeStart('debug', pragmas.debug);
  819. Check.defined("array", array);
  820. //>>includeEnd('debug');
  821. startingIndex = defaultValue(startingIndex, 0);
  822. if (!defined(result)) {
  823. result = new BoundingSphere();
  824. }
  825. var center = result.center;
  826. center.x = array[startingIndex++];
  827. center.y = array[startingIndex++];
  828. center.z = array[startingIndex++];
  829. result.radius = array[startingIndex];
  830. return result;
  831. };
  832. var unionScratch = new Cartesian3();
  833. var unionScratchCenter = new Cartesian3();
  834. /**
  835. * Computes a bounding sphere that contains both the left and right bounding spheres.
  836. *
  837. * @param {BoundingSphere} left A sphere to enclose in a bounding sphere.
  838. * @param {BoundingSphere} right A sphere to enclose in a bounding sphere.
  839. * @param {BoundingSphere} [result] The object onto which to store the result.
  840. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  841. */
  842. BoundingSphere.union = function (left, right, result) {
  843. //>>includeStart('debug', pragmas.debug);
  844. Check.typeOf.object("left", left);
  845. Check.typeOf.object("right", right);
  846. //>>includeEnd('debug');
  847. if (!defined(result)) {
  848. result = new BoundingSphere();
  849. }
  850. var leftCenter = left.center;
  851. var leftRadius = left.radius;
  852. var rightCenter = right.center;
  853. var rightRadius = right.radius;
  854. var toRightCenter = Cartesian3.subtract(
  855. rightCenter,
  856. leftCenter,
  857. unionScratch
  858. );
  859. var centerSeparation = Cartesian3.magnitude(toRightCenter);
  860. if (leftRadius >= centerSeparation + rightRadius) {
  861. // Left sphere wins.
  862. left.clone(result);
  863. return result;
  864. }
  865. if (rightRadius >= centerSeparation + leftRadius) {
  866. // Right sphere wins.
  867. right.clone(result);
  868. return result;
  869. }
  870. // There are two tangent points, one on far side of each sphere.
  871. var halfDistanceBetweenTangentPoints =
  872. (leftRadius + centerSeparation + rightRadius) * 0.5;
  873. // Compute the center point halfway between the two tangent points.
  874. var center = Cartesian3.multiplyByScalar(
  875. toRightCenter,
  876. (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation,
  877. unionScratchCenter
  878. );
  879. Cartesian3.add(center, leftCenter, center);
  880. Cartesian3.clone(center, result.center);
  881. result.radius = halfDistanceBetweenTangentPoints;
  882. return result;
  883. };
  884. var expandScratch = new Cartesian3();
  885. /**
  886. * Computes a bounding sphere by enlarging the provided sphere to contain the provided point.
  887. *
  888. * @param {BoundingSphere} sphere A sphere to expand.
  889. * @param {Cartesian3} point A point to enclose in a bounding sphere.
  890. * @param {BoundingSphere} [result] The object onto which to store the result.
  891. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  892. */
  893. BoundingSphere.expand = function (sphere, point, result) {
  894. //>>includeStart('debug', pragmas.debug);
  895. Check.typeOf.object("sphere", sphere);
  896. Check.typeOf.object("point", point);
  897. //>>includeEnd('debug');
  898. result = BoundingSphere.clone(sphere, result);
  899. var radius = Cartesian3.magnitude(
  900. Cartesian3.subtract(point, result.center, expandScratch)
  901. );
  902. if (radius > result.radius) {
  903. result.radius = radius;
  904. }
  905. return result;
  906. };
  907. /**
  908. * Determines which side of a plane a sphere is located.
  909. *
  910. * @param {BoundingSphere} sphere The bounding sphere to test.
  911. * @param {Plane} plane The plane to test against.
  912. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  913. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  914. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  915. * intersects the plane.
  916. */
  917. BoundingSphere.intersectPlane = function (sphere, plane) {
  918. //>>includeStart('debug', pragmas.debug);
  919. Check.typeOf.object("sphere", sphere);
  920. Check.typeOf.object("plane", plane);
  921. //>>includeEnd('debug');
  922. var center = sphere.center;
  923. var radius = sphere.radius;
  924. var normal = plane.normal;
  925. var distanceToPlane = Cartesian3.dot(normal, center) + plane.distance;
  926. if (distanceToPlane < -radius) {
  927. // The center point is negative side of the plane normal
  928. return Intersect.OUTSIDE;
  929. } else if (distanceToPlane < radius) {
  930. // The center point is positive side of the plane, but radius extends beyond it; partial overlap
  931. return Intersect.INTERSECTING;
  932. }
  933. return Intersect.INSIDE;
  934. };
  935. /**
  936. * Applies a 4x4 affine transformation matrix to a bounding sphere.
  937. *
  938. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  939. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  940. * @param {BoundingSphere} [result] The object onto which to store the result.
  941. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  942. */
  943. BoundingSphere.transform = function (sphere, transform, result) {
  944. //>>includeStart('debug', pragmas.debug);
  945. Check.typeOf.object("sphere", sphere);
  946. Check.typeOf.object("transform", transform);
  947. //>>includeEnd('debug');
  948. if (!defined(result)) {
  949. result = new BoundingSphere();
  950. }
  951. result.center = Matrix4.multiplyByPoint(
  952. transform,
  953. sphere.center,
  954. result.center
  955. );
  956. result.radius = Matrix4.getMaximumScale(transform) * sphere.radius;
  957. return result;
  958. };
  959. var distanceSquaredToScratch = new Cartesian3();
  960. /**
  961. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  962. *
  963. * @param {BoundingSphere} sphere The sphere.
  964. * @param {Cartesian3} cartesian The point
  965. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  966. *
  967. * @example
  968. * // Sort bounding spheres from back to front
  969. * spheres.sort(function(a, b) {
  970. * return Cesium.BoundingSphere.distanceSquaredTo(b, camera.positionWC) - Cesium.BoundingSphere.distanceSquaredTo(a, camera.positionWC);
  971. * });
  972. */
  973. BoundingSphere.distanceSquaredTo = function (sphere, cartesian) {
  974. //>>includeStart('debug', pragmas.debug);
  975. Check.typeOf.object("sphere", sphere);
  976. Check.typeOf.object("cartesian", cartesian);
  977. //>>includeEnd('debug');
  978. var diff = Cartesian3.subtract(
  979. sphere.center,
  980. cartesian,
  981. distanceSquaredToScratch
  982. );
  983. return Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius;
  984. };
  985. /**
  986. * Applies a 4x4 affine transformation matrix to a bounding sphere where there is no scale
  987. * The transformation matrix is not verified to have a uniform scale of 1.
  988. * This method is faster than computing the general bounding sphere transform using {@link BoundingSphere.transform}.
  989. *
  990. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  991. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  992. * @param {BoundingSphere} [result] The object onto which to store the result.
  993. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  994. *
  995. * @example
  996. * var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid);
  997. * var boundingSphere = new Cesium.BoundingSphere();
  998. * var newBoundingSphere = Cesium.BoundingSphere.transformWithoutScale(boundingSphere, modelMatrix);
  999. */
  1000. BoundingSphere.transformWithoutScale = function (sphere, transform, result) {
  1001. //>>includeStart('debug', pragmas.debug);
  1002. Check.typeOf.object("sphere", sphere);
  1003. Check.typeOf.object("transform", transform);
  1004. //>>includeEnd('debug');
  1005. if (!defined(result)) {
  1006. result = new BoundingSphere();
  1007. }
  1008. result.center = Matrix4.multiplyByPoint(
  1009. transform,
  1010. sphere.center,
  1011. result.center
  1012. );
  1013. result.radius = sphere.radius;
  1014. return result;
  1015. };
  1016. var scratchCartesian3 = new Cartesian3();
  1017. /**
  1018. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  1019. * plus/minus the radius of the bounding sphere.
  1020. * <br>
  1021. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  1022. * closest and farthest planes from position that intersect the bounding sphere.
  1023. *
  1024. * @param {BoundingSphere} sphere The bounding sphere to calculate the distance to.
  1025. * @param {Cartesian3} position The position to calculate the distance from.
  1026. * @param {Cartesian3} direction The direction from position.
  1027. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  1028. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  1029. */
  1030. BoundingSphere.computePlaneDistances = function (
  1031. sphere,
  1032. position,
  1033. direction,
  1034. result
  1035. ) {
  1036. //>>includeStart('debug', pragmas.debug);
  1037. Check.typeOf.object("sphere", sphere);
  1038. Check.typeOf.object("position", position);
  1039. Check.typeOf.object("direction", direction);
  1040. //>>includeEnd('debug');
  1041. if (!defined(result)) {
  1042. result = new Interval();
  1043. }
  1044. var toCenter = Cartesian3.subtract(
  1045. sphere.center,
  1046. position,
  1047. scratchCartesian3
  1048. );
  1049. var mag = Cartesian3.dot(direction, toCenter);
  1050. result.start = mag - sphere.radius;
  1051. result.stop = mag + sphere.radius;
  1052. return result;
  1053. };
  1054. var projectTo2DNormalScratch = new Cartesian3();
  1055. var projectTo2DEastScratch = new Cartesian3();
  1056. var projectTo2DNorthScratch = new Cartesian3();
  1057. var projectTo2DWestScratch = new Cartesian3();
  1058. var projectTo2DSouthScratch = new Cartesian3();
  1059. var projectTo2DCartographicScratch = new Cartographic();
  1060. var projectTo2DPositionsScratch = new Array(8);
  1061. for (var n = 0; n < 8; ++n) {
  1062. projectTo2DPositionsScratch[n] = new Cartesian3();
  1063. }
  1064. var projectTo2DProjection = new GeographicProjection();
  1065. /**
  1066. * Creates a bounding sphere in 2D from a bounding sphere in 3D world coordinates.
  1067. *
  1068. * @param {BoundingSphere} sphere The bounding sphere to transform to 2D.
  1069. * @param {Object} [projection=GeographicProjection] The projection to 2D.
  1070. * @param {BoundingSphere} [result] The object onto which to store the result.
  1071. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1072. */
  1073. BoundingSphere.projectTo2D = function (sphere, projection, result) {
  1074. //>>includeStart('debug', pragmas.debug);
  1075. Check.typeOf.object("sphere", sphere);
  1076. //>>includeEnd('debug');
  1077. projection = defaultValue(projection, projectTo2DProjection);
  1078. var ellipsoid = projection.ellipsoid;
  1079. var center = sphere.center;
  1080. var radius = sphere.radius;
  1081. var normal;
  1082. if (Cartesian3.equals(center, Cartesian3.ZERO)) {
  1083. // Bounding sphere is at the center. The geodetic surface normal is not
  1084. // defined here so pick the x-axis as a fallback.
  1085. normal = Cartesian3.clone(Cartesian3.UNIT_X, projectTo2DNormalScratch);
  1086. } else {
  1087. normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
  1088. }
  1089. var east = Cartesian3.cross(
  1090. Cartesian3.UNIT_Z,
  1091. normal,
  1092. projectTo2DEastScratch
  1093. );
  1094. Cartesian3.normalize(east, east);
  1095. var north = Cartesian3.cross(normal, east, projectTo2DNorthScratch);
  1096. Cartesian3.normalize(north, north);
  1097. Cartesian3.multiplyByScalar(normal, radius, normal);
  1098. Cartesian3.multiplyByScalar(north, radius, north);
  1099. Cartesian3.multiplyByScalar(east, radius, east);
  1100. var south = Cartesian3.negate(north, projectTo2DSouthScratch);
  1101. var west = Cartesian3.negate(east, projectTo2DWestScratch);
  1102. var positions = projectTo2DPositionsScratch;
  1103. // top NE corner
  1104. var corner = positions[0];
  1105. Cartesian3.add(normal, north, corner);
  1106. Cartesian3.add(corner, east, corner);
  1107. // top NW corner
  1108. corner = positions[1];
  1109. Cartesian3.add(normal, north, corner);
  1110. Cartesian3.add(corner, west, corner);
  1111. // top SW corner
  1112. corner = positions[2];
  1113. Cartesian3.add(normal, south, corner);
  1114. Cartesian3.add(corner, west, corner);
  1115. // top SE corner
  1116. corner = positions[3];
  1117. Cartesian3.add(normal, south, corner);
  1118. Cartesian3.add(corner, east, corner);
  1119. Cartesian3.negate(normal, normal);
  1120. // bottom NE corner
  1121. corner = positions[4];
  1122. Cartesian3.add(normal, north, corner);
  1123. Cartesian3.add(corner, east, corner);
  1124. // bottom NW corner
  1125. corner = positions[5];
  1126. Cartesian3.add(normal, north, corner);
  1127. Cartesian3.add(corner, west, corner);
  1128. // bottom SW corner
  1129. corner = positions[6];
  1130. Cartesian3.add(normal, south, corner);
  1131. Cartesian3.add(corner, west, corner);
  1132. // bottom SE corner
  1133. corner = positions[7];
  1134. Cartesian3.add(normal, south, corner);
  1135. Cartesian3.add(corner, east, corner);
  1136. var length = positions.length;
  1137. for (var i = 0; i < length; ++i) {
  1138. var position = positions[i];
  1139. Cartesian3.add(center, position, position);
  1140. var cartographic = ellipsoid.cartesianToCartographic(
  1141. position,
  1142. projectTo2DCartographicScratch
  1143. );
  1144. projection.project(cartographic, position);
  1145. }
  1146. result = BoundingSphere.fromPoints(positions, result);
  1147. // swizzle center components
  1148. center = result.center;
  1149. var x = center.x;
  1150. var y = center.y;
  1151. var z = center.z;
  1152. center.x = z;
  1153. center.y = x;
  1154. center.z = y;
  1155. return result;
  1156. };
  1157. /**
  1158. * Determines whether or not a sphere is hidden from view by the occluder.
  1159. *
  1160. * @param {BoundingSphere} sphere The bounding sphere surrounding the occludee object.
  1161. * @param {Occluder} occluder The occluder.
  1162. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  1163. */
  1164. BoundingSphere.isOccluded = function (sphere, occluder) {
  1165. //>>includeStart('debug', pragmas.debug);
  1166. Check.typeOf.object("sphere", sphere);
  1167. Check.typeOf.object("occluder", occluder);
  1168. //>>includeEnd('debug');
  1169. return !occluder.isBoundingSphereVisible(sphere);
  1170. };
  1171. /**
  1172. * Compares the provided BoundingSphere componentwise and returns
  1173. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1174. *
  1175. * @param {BoundingSphere} [left] The first BoundingSphere.
  1176. * @param {BoundingSphere} [right] The second BoundingSphere.
  1177. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1178. */
  1179. BoundingSphere.equals = function (left, right) {
  1180. return (
  1181. left === right ||
  1182. (defined(left) &&
  1183. defined(right) &&
  1184. Cartesian3.equals(left.center, right.center) &&
  1185. left.radius === right.radius)
  1186. );
  1187. };
  1188. /**
  1189. * Determines which side of a plane the sphere is located.
  1190. *
  1191. * @param {Plane} plane The plane to test against.
  1192. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  1193. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  1194. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  1195. * intersects the plane.
  1196. */
  1197. BoundingSphere.prototype.intersectPlane = function (plane) {
  1198. return BoundingSphere.intersectPlane(this, plane);
  1199. };
  1200. /**
  1201. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  1202. *
  1203. * @param {Cartesian3} cartesian The point
  1204. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  1205. *
  1206. * @example
  1207. * // Sort bounding spheres from back to front
  1208. * spheres.sort(function(a, b) {
  1209. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  1210. * });
  1211. */
  1212. BoundingSphere.prototype.distanceSquaredTo = function (cartesian) {
  1213. return BoundingSphere.distanceSquaredTo(this, cartesian);
  1214. };
  1215. /**
  1216. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  1217. * plus/minus the radius of the bounding sphere.
  1218. * <br>
  1219. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  1220. * closest and farthest planes from position that intersect the bounding sphere.
  1221. *
  1222. * @param {Cartesian3} position The position to calculate the distance from.
  1223. * @param {Cartesian3} direction The direction from position.
  1224. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  1225. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  1226. */
  1227. BoundingSphere.prototype.computePlaneDistances = function (
  1228. position,
  1229. direction,
  1230. result
  1231. ) {
  1232. return BoundingSphere.computePlaneDistances(
  1233. this,
  1234. position,
  1235. direction,
  1236. result
  1237. );
  1238. };
  1239. /**
  1240. * Determines whether or not a sphere is hidden from view by the occluder.
  1241. *
  1242. * @param {Occluder} occluder The occluder.
  1243. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  1244. */
  1245. BoundingSphere.prototype.isOccluded = function (occluder) {
  1246. return BoundingSphere.isOccluded(this, occluder);
  1247. };
  1248. /**
  1249. * Compares this BoundingSphere against the provided BoundingSphere componentwise and returns
  1250. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1251. *
  1252. * @param {BoundingSphere} [right] The right hand side BoundingSphere.
  1253. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1254. */
  1255. BoundingSphere.prototype.equals = function (right) {
  1256. return BoundingSphere.equals(this, right);
  1257. };
  1258. /**
  1259. * Duplicates this BoundingSphere instance.
  1260. *
  1261. * @param {BoundingSphere} [result] The object onto which to store the result.
  1262. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1263. */
  1264. BoundingSphere.prototype.clone = function (result) {
  1265. return BoundingSphere.clone(this, result);
  1266. };
  1267. /**
  1268. * Computes the radius of the BoundingSphere.
  1269. * @returns {Number} The radius of the BoundingSphere.
  1270. */
  1271. BoundingSphere.prototype.volume = function () {
  1272. var radius = this.radius;
  1273. return volumeConstant * radius * radius * radius;
  1274. };
  1275. export default BoundingSphere;