EllipsoidGeodesic.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 CesiumMath from "./Math.js";
  8. function setConstants(ellipsoidGeodesic) {
  9. var uSquared = ellipsoidGeodesic._uSquared;
  10. var a = ellipsoidGeodesic._ellipsoid.maximumRadius;
  11. var b = ellipsoidGeodesic._ellipsoid.minimumRadius;
  12. var f = (a - b) / a;
  13. var cosineHeading = Math.cos(ellipsoidGeodesic._startHeading);
  14. var sineHeading = Math.sin(ellipsoidGeodesic._startHeading);
  15. var tanU = (1 - f) * Math.tan(ellipsoidGeodesic._start.latitude);
  16. var cosineU = 1.0 / Math.sqrt(1.0 + tanU * tanU);
  17. var sineU = cosineU * tanU;
  18. var sigma = Math.atan2(tanU, cosineHeading);
  19. var sineAlpha = cosineU * sineHeading;
  20. var sineSquaredAlpha = sineAlpha * sineAlpha;
  21. var cosineSquaredAlpha = 1.0 - sineSquaredAlpha;
  22. var cosineAlpha = Math.sqrt(cosineSquaredAlpha);
  23. var u2Over4 = uSquared / 4.0;
  24. var u4Over16 = u2Over4 * u2Over4;
  25. var u6Over64 = u4Over16 * u2Over4;
  26. var u8Over256 = u4Over16 * u4Over16;
  27. var a0 =
  28. 1.0 +
  29. u2Over4 -
  30. (3.0 * u4Over16) / 4.0 +
  31. (5.0 * u6Over64) / 4.0 -
  32. (175.0 * u8Over256) / 64.0;
  33. var a1 = 1.0 - u2Over4 + (15.0 * u4Over16) / 8.0 - (35.0 * u6Over64) / 8.0;
  34. var a2 = 1.0 - 3.0 * u2Over4 + (35.0 * u4Over16) / 4.0;
  35. var a3 = 1.0 - 5.0 * u2Over4;
  36. var distanceRatio =
  37. a0 * sigma -
  38. (a1 * Math.sin(2.0 * sigma) * u2Over4) / 2.0 -
  39. (a2 * Math.sin(4.0 * sigma) * u4Over16) / 16.0 -
  40. (a3 * Math.sin(6.0 * sigma) * u6Over64) / 48.0 -
  41. (Math.sin(8.0 * sigma) * 5.0 * u8Over256) / 512;
  42. var constants = ellipsoidGeodesic._constants;
  43. constants.a = a;
  44. constants.b = b;
  45. constants.f = f;
  46. constants.cosineHeading = cosineHeading;
  47. constants.sineHeading = sineHeading;
  48. constants.tanU = tanU;
  49. constants.cosineU = cosineU;
  50. constants.sineU = sineU;
  51. constants.sigma = sigma;
  52. constants.sineAlpha = sineAlpha;
  53. constants.sineSquaredAlpha = sineSquaredAlpha;
  54. constants.cosineSquaredAlpha = cosineSquaredAlpha;
  55. constants.cosineAlpha = cosineAlpha;
  56. constants.u2Over4 = u2Over4;
  57. constants.u4Over16 = u4Over16;
  58. constants.u6Over64 = u6Over64;
  59. constants.u8Over256 = u8Over256;
  60. constants.a0 = a0;
  61. constants.a1 = a1;
  62. constants.a2 = a2;
  63. constants.a3 = a3;
  64. constants.distanceRatio = distanceRatio;
  65. }
  66. function computeC(f, cosineSquaredAlpha) {
  67. return (
  68. (f * cosineSquaredAlpha * (4.0 + f * (4.0 - 3.0 * cosineSquaredAlpha))) /
  69. 16.0
  70. );
  71. }
  72. function computeDeltaLambda(
  73. f,
  74. sineAlpha,
  75. cosineSquaredAlpha,
  76. sigma,
  77. sineSigma,
  78. cosineSigma,
  79. cosineTwiceSigmaMidpoint
  80. ) {
  81. var C = computeC(f, cosineSquaredAlpha);
  82. return (
  83. (1.0 - C) *
  84. f *
  85. sineAlpha *
  86. (sigma +
  87. C *
  88. sineSigma *
  89. (cosineTwiceSigmaMidpoint +
  90. C *
  91. cosineSigma *
  92. (2.0 * cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint - 1.0)))
  93. );
  94. }
  95. function vincentyInverseFormula(
  96. ellipsoidGeodesic,
  97. major,
  98. minor,
  99. firstLongitude,
  100. firstLatitude,
  101. secondLongitude,
  102. secondLatitude
  103. ) {
  104. var eff = (major - minor) / major;
  105. var l = secondLongitude - firstLongitude;
  106. var u1 = Math.atan((1 - eff) * Math.tan(firstLatitude));
  107. var u2 = Math.atan((1 - eff) * Math.tan(secondLatitude));
  108. var cosineU1 = Math.cos(u1);
  109. var sineU1 = Math.sin(u1);
  110. var cosineU2 = Math.cos(u2);
  111. var sineU2 = Math.sin(u2);
  112. var cc = cosineU1 * cosineU2;
  113. var cs = cosineU1 * sineU2;
  114. var ss = sineU1 * sineU2;
  115. var sc = sineU1 * cosineU2;
  116. var lambda = l;
  117. var lambdaDot = CesiumMath.TWO_PI;
  118. var cosineLambda = Math.cos(lambda);
  119. var sineLambda = Math.sin(lambda);
  120. var sigma;
  121. var cosineSigma;
  122. var sineSigma;
  123. var cosineSquaredAlpha;
  124. var cosineTwiceSigmaMidpoint;
  125. do {
  126. cosineLambda = Math.cos(lambda);
  127. sineLambda = Math.sin(lambda);
  128. var temp = cs - sc * cosineLambda;
  129. sineSigma = Math.sqrt(
  130. cosineU2 * cosineU2 * sineLambda * sineLambda + temp * temp
  131. );
  132. cosineSigma = ss + cc * cosineLambda;
  133. sigma = Math.atan2(sineSigma, cosineSigma);
  134. var sineAlpha;
  135. if (sineSigma === 0.0) {
  136. sineAlpha = 0.0;
  137. cosineSquaredAlpha = 1.0;
  138. } else {
  139. sineAlpha = (cc * sineLambda) / sineSigma;
  140. cosineSquaredAlpha = 1.0 - sineAlpha * sineAlpha;
  141. }
  142. lambdaDot = lambda;
  143. cosineTwiceSigmaMidpoint = cosineSigma - (2.0 * ss) / cosineSquaredAlpha;
  144. if (isNaN(cosineTwiceSigmaMidpoint)) {
  145. cosineTwiceSigmaMidpoint = 0.0;
  146. }
  147. lambda =
  148. l +
  149. computeDeltaLambda(
  150. eff,
  151. sineAlpha,
  152. cosineSquaredAlpha,
  153. sigma,
  154. sineSigma,
  155. cosineSigma,
  156. cosineTwiceSigmaMidpoint
  157. );
  158. } while (Math.abs(lambda - lambdaDot) > CesiumMath.EPSILON12);
  159. var uSquared =
  160. (cosineSquaredAlpha * (major * major - minor * minor)) / (minor * minor);
  161. var A =
  162. 1.0 +
  163. (uSquared *
  164. (4096.0 + uSquared * (uSquared * (320.0 - 175.0 * uSquared) - 768.0))) /
  165. 16384.0;
  166. var B =
  167. (uSquared *
  168. (256.0 + uSquared * (uSquared * (74.0 - 47.0 * uSquared) - 128.0))) /
  169. 1024.0;
  170. var cosineSquaredTwiceSigmaMidpoint =
  171. cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint;
  172. var deltaSigma =
  173. B *
  174. sineSigma *
  175. (cosineTwiceSigmaMidpoint +
  176. (B *
  177. (cosineSigma * (2.0 * cosineSquaredTwiceSigmaMidpoint - 1.0) -
  178. (B *
  179. cosineTwiceSigmaMidpoint *
  180. (4.0 * sineSigma * sineSigma - 3.0) *
  181. (4.0 * cosineSquaredTwiceSigmaMidpoint - 3.0)) /
  182. 6.0)) /
  183. 4.0);
  184. var distance = minor * A * (sigma - deltaSigma);
  185. var startHeading = Math.atan2(cosineU2 * sineLambda, cs - sc * cosineLambda);
  186. var endHeading = Math.atan2(cosineU1 * sineLambda, cs * cosineLambda - sc);
  187. ellipsoidGeodesic._distance = distance;
  188. ellipsoidGeodesic._startHeading = startHeading;
  189. ellipsoidGeodesic._endHeading = endHeading;
  190. ellipsoidGeodesic._uSquared = uSquared;
  191. }
  192. var scratchCart1 = new Cartesian3();
  193. var scratchCart2 = new Cartesian3();
  194. function computeProperties(ellipsoidGeodesic, start, end, ellipsoid) {
  195. var firstCartesian = Cartesian3.normalize(
  196. ellipsoid.cartographicToCartesian(start, scratchCart2),
  197. scratchCart1
  198. );
  199. var lastCartesian = Cartesian3.normalize(
  200. ellipsoid.cartographicToCartesian(end, scratchCart2),
  201. scratchCart2
  202. );
  203. //>>includeStart('debug', pragmas.debug);
  204. Check.typeOf.number.greaterThanOrEquals(
  205. "value",
  206. Math.abs(
  207. Math.abs(Cartesian3.angleBetween(firstCartesian, lastCartesian)) - Math.PI
  208. ),
  209. 0.0125
  210. );
  211. //>>includeEnd('debug');
  212. vincentyInverseFormula(
  213. ellipsoidGeodesic,
  214. ellipsoid.maximumRadius,
  215. ellipsoid.minimumRadius,
  216. start.longitude,
  217. start.latitude,
  218. end.longitude,
  219. end.latitude
  220. );
  221. ellipsoidGeodesic._start = Cartographic.clone(
  222. start,
  223. ellipsoidGeodesic._start
  224. );
  225. ellipsoidGeodesic._end = Cartographic.clone(end, ellipsoidGeodesic._end);
  226. ellipsoidGeodesic._start.height = 0;
  227. ellipsoidGeodesic._end.height = 0;
  228. setConstants(ellipsoidGeodesic);
  229. }
  230. /**
  231. * Initializes a geodesic on the ellipsoid connecting the two provided planetodetic points.
  232. *
  233. * @alias EllipsoidGeodesic
  234. * @constructor
  235. *
  236. * @param {Cartographic} [start] The initial planetodetic point on the path.
  237. * @param {Cartographic} [end] The final planetodetic point on the path.
  238. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the geodesic lies.
  239. */
  240. function EllipsoidGeodesic(start, end, ellipsoid) {
  241. var e = defaultValue(ellipsoid, Ellipsoid.WGS84);
  242. this._ellipsoid = e;
  243. this._start = new Cartographic();
  244. this._end = new Cartographic();
  245. this._constants = {};
  246. this._startHeading = undefined;
  247. this._endHeading = undefined;
  248. this._distance = undefined;
  249. this._uSquared = undefined;
  250. if (defined(start) && defined(end)) {
  251. computeProperties(this, start, end, e);
  252. }
  253. }
  254. Object.defineProperties(EllipsoidGeodesic.prototype, {
  255. /**
  256. * Gets the ellipsoid.
  257. * @memberof EllipsoidGeodesic.prototype
  258. * @type {Ellipsoid}
  259. * @readonly
  260. */
  261. ellipsoid: {
  262. get: function () {
  263. return this._ellipsoid;
  264. },
  265. },
  266. /**
  267. * Gets the surface distance between the start and end point
  268. * @memberof EllipsoidGeodesic.prototype
  269. * @type {Number}
  270. * @readonly
  271. */
  272. surfaceDistance: {
  273. get: function () {
  274. //>>includeStart('debug', pragmas.debug);
  275. Check.defined("distance", this._distance);
  276. //>>includeEnd('debug');
  277. return this._distance;
  278. },
  279. },
  280. /**
  281. * Gets the initial planetodetic point on the path.
  282. * @memberof EllipsoidGeodesic.prototype
  283. * @type {Cartographic}
  284. * @readonly
  285. */
  286. start: {
  287. get: function () {
  288. return this._start;
  289. },
  290. },
  291. /**
  292. * Gets the final planetodetic point on the path.
  293. * @memberof EllipsoidGeodesic.prototype
  294. * @type {Cartographic}
  295. * @readonly
  296. */
  297. end: {
  298. get: function () {
  299. return this._end;
  300. },
  301. },
  302. /**
  303. * Gets the heading at the initial point.
  304. * @memberof EllipsoidGeodesic.prototype
  305. * @type {Number}
  306. * @readonly
  307. */
  308. startHeading: {
  309. get: function () {
  310. //>>includeStart('debug', pragmas.debug);
  311. Check.defined("distance", this._distance);
  312. //>>includeEnd('debug');
  313. return this._startHeading;
  314. },
  315. },
  316. /**
  317. * Gets the heading at the final point.
  318. * @memberof EllipsoidGeodesic.prototype
  319. * @type {Number}
  320. * @readonly
  321. */
  322. endHeading: {
  323. get: function () {
  324. //>>includeStart('debug', pragmas.debug);
  325. Check.defined("distance", this._distance);
  326. //>>includeEnd('debug');
  327. return this._endHeading;
  328. },
  329. },
  330. });
  331. /**
  332. * Sets the start and end points of the geodesic
  333. *
  334. * @param {Cartographic} start The initial planetodetic point on the path.
  335. * @param {Cartographic} end The final planetodetic point on the path.
  336. */
  337. EllipsoidGeodesic.prototype.setEndPoints = function (start, end) {
  338. //>>includeStart('debug', pragmas.debug);
  339. Check.defined("start", start);
  340. Check.defined("end", end);
  341. //>>includeEnd('debug');
  342. computeProperties(this, start, end, this._ellipsoid);
  343. };
  344. /**
  345. * Provides the location of a point at the indicated portion along the geodesic.
  346. *
  347. * @param {Number} fraction The portion of the distance between the initial and final points.
  348. * @param {Cartographic} [result] The object in which to store the result.
  349. * @returns {Cartographic} The location of the point along the geodesic.
  350. */
  351. EllipsoidGeodesic.prototype.interpolateUsingFraction = function (
  352. fraction,
  353. result
  354. ) {
  355. return this.interpolateUsingSurfaceDistance(
  356. this._distance * fraction,
  357. result
  358. );
  359. };
  360. /**
  361. * Provides the location of a point at the indicated distance along the geodesic.
  362. *
  363. * @param {Number} distance The distance from the inital point to the point of interest along the geodesic
  364. * @param {Cartographic} [result] The object in which to store the result.
  365. * @returns {Cartographic} The location of the point along the geodesic.
  366. *
  367. * @exception {DeveloperError} start and end must be set before calling function interpolateUsingSurfaceDistance
  368. */
  369. EllipsoidGeodesic.prototype.interpolateUsingSurfaceDistance = function (
  370. distance,
  371. result
  372. ) {
  373. //>>includeStart('debug', pragmas.debug);
  374. Check.defined("distance", this._distance);
  375. //>>includeEnd('debug');
  376. var constants = this._constants;
  377. var s = constants.distanceRatio + distance / constants.b;
  378. var cosine2S = Math.cos(2.0 * s);
  379. var cosine4S = Math.cos(4.0 * s);
  380. var cosine6S = Math.cos(6.0 * s);
  381. var sine2S = Math.sin(2.0 * s);
  382. var sine4S = Math.sin(4.0 * s);
  383. var sine6S = Math.sin(6.0 * s);
  384. var sine8S = Math.sin(8.0 * s);
  385. var s2 = s * s;
  386. var s3 = s * s2;
  387. var u8Over256 = constants.u8Over256;
  388. var u2Over4 = constants.u2Over4;
  389. var u6Over64 = constants.u6Over64;
  390. var u4Over16 = constants.u4Over16;
  391. var sigma =
  392. (2.0 * s3 * u8Over256 * cosine2S) / 3.0 +
  393. s *
  394. (1.0 -
  395. u2Over4 +
  396. (7.0 * u4Over16) / 4.0 -
  397. (15.0 * u6Over64) / 4.0 +
  398. (579.0 * u8Over256) / 64.0 -
  399. (u4Over16 - (15.0 * u6Over64) / 4.0 + (187.0 * u8Over256) / 16.0) *
  400. cosine2S -
  401. ((5.0 * u6Over64) / 4.0 - (115.0 * u8Over256) / 16.0) * cosine4S -
  402. (29.0 * u8Over256 * cosine6S) / 16.0) +
  403. (u2Over4 / 2.0 -
  404. u4Over16 +
  405. (71.0 * u6Over64) / 32.0 -
  406. (85.0 * u8Over256) / 16.0) *
  407. sine2S +
  408. ((5.0 * u4Over16) / 16.0 -
  409. (5.0 * u6Over64) / 4.0 +
  410. (383.0 * u8Over256) / 96.0) *
  411. sine4S -
  412. s2 *
  413. ((u6Over64 - (11.0 * u8Over256) / 2.0) * sine2S +
  414. (5.0 * u8Over256 * sine4S) / 2.0) +
  415. ((29.0 * u6Over64) / 96.0 - (29.0 * u8Over256) / 16.0) * sine6S +
  416. (539.0 * u8Over256 * sine8S) / 1536.0;
  417. var theta = Math.asin(Math.sin(sigma) * constants.cosineAlpha);
  418. var latitude = Math.atan((constants.a / constants.b) * Math.tan(theta));
  419. // Redefine in terms of relative argument of latitude.
  420. sigma = sigma - constants.sigma;
  421. var cosineTwiceSigmaMidpoint = Math.cos(2.0 * constants.sigma + sigma);
  422. var sineSigma = Math.sin(sigma);
  423. var cosineSigma = Math.cos(sigma);
  424. var cc = constants.cosineU * cosineSigma;
  425. var ss = constants.sineU * sineSigma;
  426. var lambda = Math.atan2(
  427. sineSigma * constants.sineHeading,
  428. cc - ss * constants.cosineHeading
  429. );
  430. var l =
  431. lambda -
  432. computeDeltaLambda(
  433. constants.f,
  434. constants.sineAlpha,
  435. constants.cosineSquaredAlpha,
  436. sigma,
  437. sineSigma,
  438. cosineSigma,
  439. cosineTwiceSigmaMidpoint
  440. );
  441. if (defined(result)) {
  442. result.longitude = this._start.longitude + l;
  443. result.latitude = latitude;
  444. result.height = 0.0;
  445. return result;
  446. }
  447. return new Cartographic(this._start.longitude + l, latitude, 0.0);
  448. };
  449. export default EllipsoidGeodesic;