EllipsoidRhumbLine.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 DeveloperError from "./DeveloperError.js";
  7. import Ellipsoid from "./Ellipsoid.js";
  8. import CesiumMath from "./Math.js";
  9. function calculateM(ellipticity, major, latitude) {
  10. if (ellipticity === 0.0) {
  11. // sphere
  12. return major * latitude;
  13. }
  14. const e2 = ellipticity * ellipticity;
  15. const e4 = e2 * e2;
  16. const e6 = e4 * e2;
  17. const e8 = e6 * e2;
  18. const e10 = e8 * e2;
  19. const e12 = e10 * e2;
  20. const phi = latitude;
  21. const sin2Phi = Math.sin(2 * phi);
  22. const sin4Phi = Math.sin(4 * phi);
  23. const sin6Phi = Math.sin(6 * phi);
  24. const sin8Phi = Math.sin(8 * phi);
  25. const sin10Phi = Math.sin(10 * phi);
  26. const sin12Phi = Math.sin(12 * phi);
  27. return (
  28. major *
  29. ((1 -
  30. e2 / 4 -
  31. (3 * e4) / 64 -
  32. (5 * e6) / 256 -
  33. (175 * e8) / 16384 -
  34. (441 * e10) / 65536 -
  35. (4851 * e12) / 1048576) *
  36. phi -
  37. ((3 * e2) / 8 +
  38. (3 * e4) / 32 +
  39. (45 * e6) / 1024 +
  40. (105 * e8) / 4096 +
  41. (2205 * e10) / 131072 +
  42. (6237 * e12) / 524288) *
  43. sin2Phi +
  44. ((15 * e4) / 256 +
  45. (45 * e6) / 1024 +
  46. (525 * e8) / 16384 +
  47. (1575 * e10) / 65536 +
  48. (155925 * e12) / 8388608) *
  49. sin4Phi -
  50. ((35 * e6) / 3072 +
  51. (175 * e8) / 12288 +
  52. (3675 * e10) / 262144 +
  53. (13475 * e12) / 1048576) *
  54. sin6Phi +
  55. ((315 * e8) / 131072 + (2205 * e10) / 524288 + (43659 * e12) / 8388608) *
  56. sin8Phi -
  57. ((693 * e10) / 1310720 + (6237 * e12) / 5242880) * sin10Phi +
  58. ((1001 * e12) / 8388608) * sin12Phi)
  59. );
  60. }
  61. function calculateInverseM(M, ellipticity, major) {
  62. const d = M / major;
  63. if (ellipticity === 0.0) {
  64. // sphere
  65. return d;
  66. }
  67. const d2 = d * d;
  68. const d3 = d2 * d;
  69. const d4 = d3 * d;
  70. const e = ellipticity;
  71. const e2 = e * e;
  72. const e4 = e2 * e2;
  73. const e6 = e4 * e2;
  74. const e8 = e6 * e2;
  75. const e10 = e8 * e2;
  76. const e12 = e10 * e2;
  77. const sin2D = Math.sin(2 * d);
  78. const cos2D = Math.cos(2 * d);
  79. const sin4D = Math.sin(4 * d);
  80. const cos4D = Math.cos(4 * d);
  81. const sin6D = Math.sin(6 * d);
  82. const cos6D = Math.cos(6 * d);
  83. const sin8D = Math.sin(8 * d);
  84. const cos8D = Math.cos(8 * d);
  85. const sin10D = Math.sin(10 * d);
  86. const cos10D = Math.cos(10 * d);
  87. const sin12D = Math.sin(12 * d);
  88. return (
  89. d +
  90. (d * e2) / 4 +
  91. (7 * d * e4) / 64 +
  92. (15 * d * e6) / 256 +
  93. (579 * d * e8) / 16384 +
  94. (1515 * d * e10) / 65536 +
  95. (16837 * d * e12) / 1048576 +
  96. ((3 * d * e4) / 16 +
  97. (45 * d * e6) / 256 -
  98. (d * (32 * d2 - 561) * e8) / 4096 -
  99. (d * (232 * d2 - 1677) * e10) / 16384 +
  100. (d * (399985 - 90560 * d2 + 512 * d4) * e12) / 5242880) *
  101. cos2D +
  102. ((21 * d * e6) / 256 +
  103. (483 * d * e8) / 4096 -
  104. (d * (224 * d2 - 1969) * e10) / 16384 -
  105. (d * (33152 * d2 - 112599) * e12) / 1048576) *
  106. cos4D +
  107. ((151 * d * e8) / 4096 +
  108. (4681 * d * e10) / 65536 +
  109. (1479 * d * e12) / 16384 -
  110. (453 * d3 * e12) / 32768) *
  111. cos6D +
  112. ((1097 * d * e10) / 65536 + (42783 * d * e12) / 1048576) * cos8D +
  113. ((8011 * d * e12) / 1048576) * cos10D +
  114. ((3 * e2) / 8 +
  115. (3 * e4) / 16 +
  116. (213 * e6) / 2048 -
  117. (3 * d2 * e6) / 64 +
  118. (255 * e8) / 4096 -
  119. (33 * d2 * e8) / 512 +
  120. (20861 * e10) / 524288 -
  121. (33 * d2 * e10) / 512 +
  122. (d4 * e10) / 1024 +
  123. (28273 * e12) / 1048576 -
  124. (471 * d2 * e12) / 8192 +
  125. (9 * d4 * e12) / 4096) *
  126. sin2D +
  127. ((21 * e4) / 256 +
  128. (21 * e6) / 256 +
  129. (533 * e8) / 8192 -
  130. (21 * d2 * e8) / 512 +
  131. (197 * e10) / 4096 -
  132. (315 * d2 * e10) / 4096 +
  133. (584039 * e12) / 16777216 -
  134. (12517 * d2 * e12) / 131072 +
  135. (7 * d4 * e12) / 2048) *
  136. sin4D +
  137. ((151 * e6) / 6144 +
  138. (151 * e8) / 4096 +
  139. (5019 * e10) / 131072 -
  140. (453 * d2 * e10) / 16384 +
  141. (26965 * e12) / 786432 -
  142. (8607 * d2 * e12) / 131072) *
  143. sin6D +
  144. ((1097 * e8) / 131072 +
  145. (1097 * e10) / 65536 +
  146. (225797 * e12) / 10485760 -
  147. (1097 * d2 * e12) / 65536) *
  148. sin8D +
  149. ((8011 * e10) / 2621440 + (8011 * e12) / 1048576) * sin10D +
  150. ((293393 * e12) / 251658240) * sin12D
  151. );
  152. }
  153. function calculateSigma(ellipticity, latitude) {
  154. if (ellipticity === 0.0) {
  155. // sphere
  156. return Math.log(Math.tan(0.5 * (CesiumMath.PI_OVER_TWO + latitude)));
  157. }
  158. const eSinL = ellipticity * Math.sin(latitude);
  159. return (
  160. Math.log(Math.tan(0.5 * (CesiumMath.PI_OVER_TWO + latitude))) -
  161. (ellipticity / 2.0) * Math.log((1 + eSinL) / (1 - eSinL))
  162. );
  163. }
  164. function calculateHeading(
  165. ellipsoidRhumbLine,
  166. firstLongitude,
  167. firstLatitude,
  168. secondLongitude,
  169. secondLatitude
  170. ) {
  171. const sigma1 = calculateSigma(ellipsoidRhumbLine._ellipticity, firstLatitude);
  172. const sigma2 = calculateSigma(
  173. ellipsoidRhumbLine._ellipticity,
  174. secondLatitude
  175. );
  176. return Math.atan2(
  177. CesiumMath.negativePiToPi(secondLongitude - firstLongitude),
  178. sigma2 - sigma1
  179. );
  180. }
  181. function calculateArcLength(
  182. ellipsoidRhumbLine,
  183. major,
  184. minor,
  185. firstLongitude,
  186. firstLatitude,
  187. secondLongitude,
  188. secondLatitude
  189. ) {
  190. const heading = ellipsoidRhumbLine._heading;
  191. const deltaLongitude = secondLongitude - firstLongitude;
  192. let distance = 0.0;
  193. //Check to see if the rhumb line has constant latitude
  194. //This equation will diverge if heading gets close to 90 degrees
  195. if (
  196. CesiumMath.equalsEpsilon(
  197. Math.abs(heading),
  198. CesiumMath.PI_OVER_TWO,
  199. CesiumMath.EPSILON8
  200. )
  201. ) {
  202. //If heading is close to 90 degrees
  203. if (major === minor) {
  204. distance =
  205. major *
  206. Math.cos(firstLatitude) *
  207. CesiumMath.negativePiToPi(deltaLongitude);
  208. } else {
  209. const sinPhi = Math.sin(firstLatitude);
  210. distance =
  211. (major *
  212. Math.cos(firstLatitude) *
  213. CesiumMath.negativePiToPi(deltaLongitude)) /
  214. Math.sqrt(1 - ellipsoidRhumbLine._ellipticitySquared * sinPhi * sinPhi);
  215. }
  216. } else {
  217. const M1 = calculateM(
  218. ellipsoidRhumbLine._ellipticity,
  219. major,
  220. firstLatitude
  221. );
  222. const M2 = calculateM(
  223. ellipsoidRhumbLine._ellipticity,
  224. major,
  225. secondLatitude
  226. );
  227. distance = (M2 - M1) / Math.cos(heading);
  228. }
  229. return Math.abs(distance);
  230. }
  231. const scratchCart1 = new Cartesian3();
  232. const scratchCart2 = new Cartesian3();
  233. function computeProperties(ellipsoidRhumbLine, start, end, ellipsoid) {
  234. const firstCartesian = Cartesian3.normalize(
  235. ellipsoid.cartographicToCartesian(start, scratchCart2),
  236. scratchCart1
  237. );
  238. const lastCartesian = Cartesian3.normalize(
  239. ellipsoid.cartographicToCartesian(end, scratchCart2),
  240. scratchCart2
  241. );
  242. //>>includeStart('debug', pragmas.debug);
  243. Check.typeOf.number.greaterThanOrEquals(
  244. "value",
  245. Math.abs(
  246. Math.abs(Cartesian3.angleBetween(firstCartesian, lastCartesian)) - Math.PI
  247. ),
  248. 0.0125
  249. );
  250. //>>includeEnd('debug');
  251. const major = ellipsoid.maximumRadius;
  252. const minor = ellipsoid.minimumRadius;
  253. const majorSquared = major * major;
  254. const minorSquared = minor * minor;
  255. ellipsoidRhumbLine._ellipticitySquared =
  256. (majorSquared - minorSquared) / majorSquared;
  257. ellipsoidRhumbLine._ellipticity = Math.sqrt(
  258. ellipsoidRhumbLine._ellipticitySquared
  259. );
  260. ellipsoidRhumbLine._start = Cartographic.clone(
  261. start,
  262. ellipsoidRhumbLine._start
  263. );
  264. ellipsoidRhumbLine._start.height = 0;
  265. ellipsoidRhumbLine._end = Cartographic.clone(end, ellipsoidRhumbLine._end);
  266. ellipsoidRhumbLine._end.height = 0;
  267. ellipsoidRhumbLine._heading = calculateHeading(
  268. ellipsoidRhumbLine,
  269. start.longitude,
  270. start.latitude,
  271. end.longitude,
  272. end.latitude
  273. );
  274. ellipsoidRhumbLine._distance = calculateArcLength(
  275. ellipsoidRhumbLine,
  276. ellipsoid.maximumRadius,
  277. ellipsoid.minimumRadius,
  278. start.longitude,
  279. start.latitude,
  280. end.longitude,
  281. end.latitude
  282. );
  283. }
  284. function interpolateUsingSurfaceDistance(
  285. start,
  286. heading,
  287. distance,
  288. major,
  289. ellipticity,
  290. result
  291. ) {
  292. if (distance === 0.0) {
  293. return Cartographic.clone(start, result);
  294. }
  295. const ellipticitySquared = ellipticity * ellipticity;
  296. let longitude;
  297. let latitude;
  298. let deltaLongitude;
  299. //Check to see if the rhumb line has constant latitude
  300. //This won't converge if heading is close to 90 degrees
  301. if (
  302. Math.abs(CesiumMath.PI_OVER_TWO - Math.abs(heading)) > CesiumMath.EPSILON8
  303. ) {
  304. //Calculate latitude of the second point
  305. const M1 = calculateM(ellipticity, major, start.latitude);
  306. const deltaM = distance * Math.cos(heading);
  307. const M2 = M1 + deltaM;
  308. latitude = calculateInverseM(M2, ellipticity, major);
  309. //Now find the longitude of the second point
  310. const sigma1 = calculateSigma(ellipticity, start.latitude);
  311. const sigma2 = calculateSigma(ellipticity, latitude);
  312. deltaLongitude = Math.tan(heading) * (sigma2 - sigma1);
  313. longitude = CesiumMath.negativePiToPi(start.longitude + deltaLongitude);
  314. } else {
  315. //If heading is close to 90 degrees
  316. latitude = start.latitude;
  317. let localRad;
  318. if (ellipticity === 0.0) {
  319. // sphere
  320. localRad = major * Math.cos(start.latitude);
  321. } else {
  322. const sinPhi = Math.sin(start.latitude);
  323. localRad =
  324. (major * Math.cos(start.latitude)) /
  325. Math.sqrt(1 - ellipticitySquared * sinPhi * sinPhi);
  326. }
  327. deltaLongitude = distance / localRad;
  328. if (heading > 0.0) {
  329. longitude = CesiumMath.negativePiToPi(start.longitude + deltaLongitude);
  330. } else {
  331. longitude = CesiumMath.negativePiToPi(start.longitude - deltaLongitude);
  332. }
  333. }
  334. if (defined(result)) {
  335. result.longitude = longitude;
  336. result.latitude = latitude;
  337. result.height = 0;
  338. return result;
  339. }
  340. return new Cartographic(longitude, latitude, 0);
  341. }
  342. /**
  343. * Initializes a rhumb line on the ellipsoid connecting the two provided planetodetic points.
  344. *
  345. * @alias EllipsoidRhumbLine
  346. * @constructor
  347. *
  348. * @param {Cartographic} [start] The initial planetodetic point on the path.
  349. * @param {Cartographic} [end] The final planetodetic point on the path.
  350. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rhumb line lies.
  351. *
  352. * @exception {DeveloperError} angle between start and end must be at least 0.0125 radians.
  353. */
  354. function EllipsoidRhumbLine(start, end, ellipsoid) {
  355. const e = defaultValue(ellipsoid, Ellipsoid.WGS84);
  356. this._ellipsoid = e;
  357. this._start = new Cartographic();
  358. this._end = new Cartographic();
  359. this._heading = undefined;
  360. this._distance = undefined;
  361. this._ellipticity = undefined;
  362. this._ellipticitySquared = undefined;
  363. if (defined(start) && defined(end)) {
  364. computeProperties(this, start, end, e);
  365. }
  366. }
  367. Object.defineProperties(EllipsoidRhumbLine.prototype, {
  368. /**
  369. * Gets the ellipsoid.
  370. * @memberof EllipsoidRhumbLine.prototype
  371. * @type {Ellipsoid}
  372. * @readonly
  373. */
  374. ellipsoid: {
  375. get: function () {
  376. return this._ellipsoid;
  377. },
  378. },
  379. /**
  380. * Gets the surface distance between the start and end point
  381. * @memberof EllipsoidRhumbLine.prototype
  382. * @type {number}
  383. * @readonly
  384. */
  385. surfaceDistance: {
  386. get: function () {
  387. //>>includeStart('debug', pragmas.debug);
  388. Check.defined("distance", this._distance);
  389. //>>includeEnd('debug');
  390. return this._distance;
  391. },
  392. },
  393. /**
  394. * Gets the initial planetodetic point on the path.
  395. * @memberof EllipsoidRhumbLine.prototype
  396. * @type {Cartographic}
  397. * @readonly
  398. */
  399. start: {
  400. get: function () {
  401. return this._start;
  402. },
  403. },
  404. /**
  405. * Gets the final planetodetic point on the path.
  406. * @memberof EllipsoidRhumbLine.prototype
  407. * @type {Cartographic}
  408. * @readonly
  409. */
  410. end: {
  411. get: function () {
  412. return this._end;
  413. },
  414. },
  415. /**
  416. * Gets the heading from the start point to the end point.
  417. * @memberof EllipsoidRhumbLine.prototype
  418. * @type {number}
  419. * @readonly
  420. */
  421. heading: {
  422. get: function () {
  423. //>>includeStart('debug', pragmas.debug);
  424. Check.defined("distance", this._distance);
  425. //>>includeEnd('debug');
  426. return this._heading;
  427. },
  428. },
  429. });
  430. /**
  431. * Create a rhumb line using an initial position with a heading and distance.
  432. *
  433. * @param {Cartographic} start The initial planetodetic point on the path.
  434. * @param {number} heading The heading in radians.
  435. * @param {number} distance The rhumb line distance between the start and end point.
  436. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rhumb line lies.
  437. * @param {EllipsoidRhumbLine} [result] The object in which to store the result.
  438. * @returns {EllipsoidRhumbLine} The EllipsoidRhumbLine object.
  439. */
  440. EllipsoidRhumbLine.fromStartHeadingDistance = function (
  441. start,
  442. heading,
  443. distance,
  444. ellipsoid,
  445. result
  446. ) {
  447. //>>includeStart('debug', pragmas.debug);
  448. Check.defined("start", start);
  449. Check.defined("heading", heading);
  450. Check.defined("distance", distance);
  451. Check.typeOf.number.greaterThan("distance", distance, 0.0);
  452. //>>includeEnd('debug');
  453. const e = defaultValue(ellipsoid, Ellipsoid.WGS84);
  454. const major = e.maximumRadius;
  455. const minor = e.minimumRadius;
  456. const majorSquared = major * major;
  457. const minorSquared = minor * minor;
  458. const ellipticity = Math.sqrt((majorSquared - minorSquared) / majorSquared);
  459. heading = CesiumMath.negativePiToPi(heading);
  460. const end = interpolateUsingSurfaceDistance(
  461. start,
  462. heading,
  463. distance,
  464. e.maximumRadius,
  465. ellipticity
  466. );
  467. if (
  468. !defined(result) ||
  469. (defined(ellipsoid) && !ellipsoid.equals(result.ellipsoid))
  470. ) {
  471. return new EllipsoidRhumbLine(start, end, e);
  472. }
  473. result.setEndPoints(start, end);
  474. return result;
  475. };
  476. /**
  477. * Sets the start and end points of the rhumb line.
  478. *
  479. * @param {Cartographic} start The initial planetodetic point on the path.
  480. * @param {Cartographic} end The final planetodetic point on the path.
  481. */
  482. EllipsoidRhumbLine.prototype.setEndPoints = function (start, end) {
  483. //>>includeStart('debug', pragmas.debug);
  484. Check.defined("start", start);
  485. Check.defined("end", end);
  486. //>>includeEnd('debug');
  487. computeProperties(this, start, end, this._ellipsoid);
  488. };
  489. /**
  490. * Provides the location of a point at the indicated portion along the rhumb line.
  491. *
  492. * @param {number} fraction The portion of the distance between the initial and final points.
  493. * @param {Cartographic} [result] The object in which to store the result.
  494. * @returns {Cartographic} The location of the point along the rhumb line.
  495. */
  496. EllipsoidRhumbLine.prototype.interpolateUsingFraction = function (
  497. fraction,
  498. result
  499. ) {
  500. return this.interpolateUsingSurfaceDistance(
  501. fraction * this._distance,
  502. result
  503. );
  504. };
  505. /**
  506. * Provides the location of a point at the indicated distance along the rhumb line.
  507. *
  508. * @param {number} distance The distance from the inital point to the point of interest along the rhumbLine.
  509. * @param {Cartographic} [result] The object in which to store the result.
  510. * @returns {Cartographic} The location of the point along the rhumb line.
  511. *
  512. * @exception {DeveloperError} start and end must be set before calling function interpolateUsingSurfaceDistance
  513. */
  514. EllipsoidRhumbLine.prototype.interpolateUsingSurfaceDistance = function (
  515. distance,
  516. result
  517. ) {
  518. //>>includeStart('debug', pragmas.debug);
  519. Check.typeOf.number("distance", distance);
  520. if (!defined(this._distance) || this._distance === 0.0) {
  521. throw new DeveloperError(
  522. "EllipsoidRhumbLine must have distinct start and end set."
  523. );
  524. }
  525. //>>includeEnd('debug');
  526. return interpolateUsingSurfaceDistance(
  527. this._start,
  528. this._heading,
  529. distance,
  530. this._ellipsoid.maximumRadius,
  531. this._ellipticity,
  532. result
  533. );
  534. };
  535. /**
  536. * Provides the location of a point at the indicated longitude along the rhumb line.
  537. * If the longitude is outside the range of start and end points, the first intersection with the longitude from the start point in the direction of the heading is returned. This follows the spiral property of a rhumb line.
  538. *
  539. * @param {number} intersectionLongitude The longitude, in radians, at which to find the intersection point from the starting point using the heading.
  540. * @param {Cartographic} [result] The object in which to store the result.
  541. * @returns {Cartographic} The location of the intersection point along the rhumb line, undefined if there is no intersection or infinite intersections.
  542. *
  543. * @exception {DeveloperError} start and end must be set before calling function findIntersectionWithLongitude.
  544. */
  545. EllipsoidRhumbLine.prototype.findIntersectionWithLongitude = function (
  546. intersectionLongitude,
  547. result
  548. ) {
  549. //>>includeStart('debug', pragmas.debug);
  550. Check.typeOf.number("intersectionLongitude", intersectionLongitude);
  551. if (!defined(this._distance) || this._distance === 0.0) {
  552. throw new DeveloperError(
  553. "EllipsoidRhumbLine must have distinct start and end set."
  554. );
  555. }
  556. //>>includeEnd('debug');
  557. const ellipticity = this._ellipticity;
  558. const heading = this._heading;
  559. const absHeading = Math.abs(heading);
  560. const start = this._start;
  561. intersectionLongitude = CesiumMath.negativePiToPi(intersectionLongitude);
  562. if (
  563. CesiumMath.equalsEpsilon(
  564. Math.abs(intersectionLongitude),
  565. Math.PI,
  566. CesiumMath.EPSILON14
  567. )
  568. ) {
  569. intersectionLongitude = CesiumMath.sign(start.longitude) * Math.PI;
  570. }
  571. if (!defined(result)) {
  572. result = new Cartographic();
  573. }
  574. // If heading is -PI/2 or PI/2, this is an E-W rhumb line
  575. // If heading is 0 or PI, this is an N-S rhumb line
  576. if (Math.abs(CesiumMath.PI_OVER_TWO - absHeading) <= CesiumMath.EPSILON8) {
  577. result.longitude = intersectionLongitude;
  578. result.latitude = start.latitude;
  579. result.height = 0;
  580. return result;
  581. } else if (
  582. CesiumMath.equalsEpsilon(
  583. Math.abs(CesiumMath.PI_OVER_TWO - absHeading),
  584. CesiumMath.PI_OVER_TWO,
  585. CesiumMath.EPSILON8
  586. )
  587. ) {
  588. if (
  589. CesiumMath.equalsEpsilon(
  590. intersectionLongitude,
  591. start.longitude,
  592. CesiumMath.EPSILON12
  593. )
  594. ) {
  595. return undefined;
  596. }
  597. result.longitude = intersectionLongitude;
  598. result.latitude =
  599. CesiumMath.PI_OVER_TWO *
  600. CesiumMath.sign(CesiumMath.PI_OVER_TWO - heading);
  601. result.height = 0;
  602. return result;
  603. }
  604. // Use iterative solver from Equation 9 from http://edwilliams.org/ellipsoid/ellipsoid.pdf
  605. const phi1 = start.latitude;
  606. const eSinPhi1 = ellipticity * Math.sin(phi1);
  607. const leftComponent =
  608. Math.tan(0.5 * (CesiumMath.PI_OVER_TWO + phi1)) *
  609. Math.exp((intersectionLongitude - start.longitude) / Math.tan(heading));
  610. const denominator = (1 + eSinPhi1) / (1 - eSinPhi1);
  611. let newPhi = start.latitude;
  612. let phi;
  613. do {
  614. phi = newPhi;
  615. const eSinPhi = ellipticity * Math.sin(phi);
  616. const numerator = (1 + eSinPhi) / (1 - eSinPhi);
  617. newPhi =
  618. 2 *
  619. Math.atan(
  620. leftComponent * Math.pow(numerator / denominator, ellipticity / 2)
  621. ) -
  622. CesiumMath.PI_OVER_TWO;
  623. } while (!CesiumMath.equalsEpsilon(newPhi, phi, CesiumMath.EPSILON12));
  624. result.longitude = intersectionLongitude;
  625. result.latitude = newPhi;
  626. result.height = 0;
  627. return result;
  628. };
  629. /**
  630. * Provides the location of a point at the indicated latitude along the rhumb line.
  631. * If the latitude is outside the range of start and end points, the first intersection with the latitude from that start point in the direction of the heading is returned. This follows the spiral property of a rhumb line.
  632. *
  633. * @param {number} intersectionLatitude The latitude, in radians, at which to find the intersection point from the starting point using the heading.
  634. * @param {Cartographic} [result] The object in which to store the result.
  635. * @returns {Cartographic} The location of the intersection point along the rhumb line, undefined if there is no intersection or infinite intersections.
  636. *
  637. * @exception {DeveloperError} start and end must be set before calling function findIntersectionWithLongitude.
  638. */
  639. EllipsoidRhumbLine.prototype.findIntersectionWithLatitude = function (
  640. intersectionLatitude,
  641. result
  642. ) {
  643. //>>includeStart('debug', pragmas.debug);
  644. Check.typeOf.number("intersectionLatitude", intersectionLatitude);
  645. if (!defined(this._distance) || this._distance === 0.0) {
  646. throw new DeveloperError(
  647. "EllipsoidRhumbLine must have distinct start and end set."
  648. );
  649. }
  650. //>>includeEnd('debug');
  651. const ellipticity = this._ellipticity;
  652. const heading = this._heading;
  653. const start = this._start;
  654. // If start and end have same latitude, return undefined since it's either no intersection or infinite intersections
  655. if (
  656. CesiumMath.equalsEpsilon(
  657. Math.abs(heading),
  658. CesiumMath.PI_OVER_TWO,
  659. CesiumMath.EPSILON8
  660. )
  661. ) {
  662. return;
  663. }
  664. // Can be solved using the same equations from interpolateUsingSurfaceDistance
  665. const sigma1 = calculateSigma(ellipticity, start.latitude);
  666. const sigma2 = calculateSigma(ellipticity, intersectionLatitude);
  667. const deltaLongitude = Math.tan(heading) * (sigma2 - sigma1);
  668. const longitude = CesiumMath.negativePiToPi(start.longitude + deltaLongitude);
  669. if (defined(result)) {
  670. result.longitude = longitude;
  671. result.latitude = intersectionLatitude;
  672. result.height = 0;
  673. return result;
  674. }
  675. return new Cartographic(longitude, intersectionLatitude, 0);
  676. };
  677. export default EllipsoidRhumbLine;