BoundingSphere.js 54 KB

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