EllipsoidGeodesic.js 14 KB

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