EllipsoidRhumbLine.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. var e2 = ellipticity * ellipticity;
  15. var e4 = e2 * e2;
  16. var e6 = e4 * e2;
  17. var e8 = e6 * e2;
  18. var e10 = e8 * e2;
  19. var e12 = e10 * e2;
  20. var phi = latitude;
  21. var sin2Phi = Math.sin(2 * phi);
  22. var sin4Phi = Math.sin(4 * phi);
  23. var sin6Phi = Math.sin(6 * phi);
  24. var sin8Phi = Math.sin(8 * phi);
  25. var sin10Phi = Math.sin(10 * phi);
  26. var 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. var d = M / major;
  63. if (ellipticity === 0.0) {
  64. // sphere
  65. return d;
  66. }
  67. var d2 = d * d;
  68. var d3 = d2 * d;
  69. var d4 = d3 * d;
  70. var e = ellipticity;
  71. var e2 = e * e;
  72. var e4 = e2 * e2;
  73. var e6 = e4 * e2;
  74. var e8 = e6 * e2;
  75. var e10 = e8 * e2;
  76. var e12 = e10 * e2;
  77. var sin2D = Math.sin(2 * d);
  78. var cos2D = Math.cos(2 * d);
  79. var sin4D = Math.sin(4 * d);
  80. var cos4D = Math.cos(4 * d);
  81. var sin6D = Math.sin(6 * d);
  82. var cos6D = Math.cos(6 * d);
  83. var sin8D = Math.sin(8 * d);
  84. var cos8D = Math.cos(8 * d);
  85. var sin10D = Math.sin(10 * d);
  86. var cos10D = Math.cos(10 * d);
  87. var 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. var 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. var sigma1 = calculateSigma(ellipsoidRhumbLine._ellipticity, firstLatitude);
  172. var sigma2 = calculateSigma(ellipsoidRhumbLine._ellipticity, secondLatitude);
  173. return Math.atan2(
  174. CesiumMath.negativePiToPi(secondLongitude - firstLongitude),
  175. sigma2 - sigma1
  176. );
  177. }
  178. function calculateArcLength(
  179. ellipsoidRhumbLine,
  180. major,
  181. minor,
  182. firstLongitude,
  183. firstLatitude,
  184. secondLongitude,
  185. secondLatitude
  186. ) {
  187. var heading = ellipsoidRhumbLine._heading;
  188. var deltaLongitude = secondLongitude - firstLongitude;
  189. var distance = 0.0;
  190. //Check to see if the rhumb line has constant latitude
  191. //This equation will diverge if heading gets close to 90 degrees
  192. if (
  193. CesiumMath.equalsEpsilon(
  194. Math.abs(heading),
  195. CesiumMath.PI_OVER_TWO,
  196. CesiumMath.EPSILON8
  197. )
  198. ) {
  199. //If heading is close to 90 degrees
  200. if (major === minor) {
  201. distance =
  202. major *
  203. Math.cos(firstLatitude) *
  204. CesiumMath.negativePiToPi(deltaLongitude);
  205. } else {
  206. var sinPhi = Math.sin(firstLatitude);
  207. distance =
  208. (major *
  209. Math.cos(firstLatitude) *
  210. CesiumMath.negativePiToPi(deltaLongitude)) /
  211. Math.sqrt(1 - ellipsoidRhumbLine._ellipticitySquared * sinPhi * sinPhi);
  212. }
  213. } else {
  214. var M1 = calculateM(ellipsoidRhumbLine._ellipticity, major, firstLatitude);
  215. var M2 = calculateM(ellipsoidRhumbLine._ellipticity, major, secondLatitude);
  216. distance = (M2 - M1) / Math.cos(heading);
  217. }
  218. return Math.abs(distance);
  219. }
  220. var scratchCart1 = new Cartesian3();
  221. var scratchCart2 = new Cartesian3();
  222. function computeProperties(ellipsoidRhumbLine, start, end, ellipsoid) {
  223. var firstCartesian = Cartesian3.normalize(
  224. ellipsoid.cartographicToCartesian(start, scratchCart2),
  225. scratchCart1
  226. );
  227. var lastCartesian = Cartesian3.normalize(
  228. ellipsoid.cartographicToCartesian(end, scratchCart2),
  229. scratchCart2
  230. );
  231. //>>includeStart('debug', pragmas.debug);
  232. Check.typeOf.number.greaterThanOrEquals(
  233. "value",
  234. Math.abs(
  235. Math.abs(Cartesian3.angleBetween(firstCartesian, lastCartesian)) - Math.PI
  236. ),
  237. 0.0125
  238. );
  239. //>>includeEnd('debug');
  240. var major = ellipsoid.maximumRadius;
  241. var minor = ellipsoid.minimumRadius;
  242. var majorSquared = major * major;
  243. var minorSquared = minor * minor;
  244. ellipsoidRhumbLine._ellipticitySquared =
  245. (majorSquared - minorSquared) / majorSquared;
  246. ellipsoidRhumbLine._ellipticity = Math.sqrt(
  247. ellipsoidRhumbLine._ellipticitySquared
  248. );
  249. ellipsoidRhumbLine._start = Cartographic.clone(
  250. start,
  251. ellipsoidRhumbLine._start
  252. );
  253. ellipsoidRhumbLine._start.height = 0;
  254. ellipsoidRhumbLine._end = Cartographic.clone(end, ellipsoidRhumbLine._end);
  255. ellipsoidRhumbLine._end.height = 0;
  256. ellipsoidRhumbLine._heading = calculateHeading(
  257. ellipsoidRhumbLine,
  258. start.longitude,
  259. start.latitude,
  260. end.longitude,
  261. end.latitude
  262. );
  263. ellipsoidRhumbLine._distance = calculateArcLength(
  264. ellipsoidRhumbLine,
  265. ellipsoid.maximumRadius,
  266. ellipsoid.minimumRadius,
  267. start.longitude,
  268. start.latitude,
  269. end.longitude,
  270. end.latitude
  271. );
  272. }
  273. function interpolateUsingSurfaceDistance(
  274. start,
  275. heading,
  276. distance,
  277. major,
  278. ellipticity,
  279. result
  280. ) {
  281. var ellipticitySquared = ellipticity * ellipticity;
  282. var longitude;
  283. var latitude;
  284. var deltaLongitude;
  285. //Check to see if the rhumb line has constant latitude
  286. //This won't converge if heading is close to 90 degrees
  287. if (
  288. Math.abs(CesiumMath.PI_OVER_TWO - Math.abs(heading)) > CesiumMath.EPSILON8
  289. ) {
  290. //Calculate latitude of the second point
  291. var M1 = calculateM(ellipticity, major, start.latitude);
  292. var deltaM = distance * Math.cos(heading);
  293. var M2 = M1 + deltaM;
  294. latitude = calculateInverseM(M2, ellipticity, major);
  295. //Now find the longitude of the second point
  296. var sigma1 = calculateSigma(ellipticity, start.latitude);
  297. var sigma2 = calculateSigma(ellipticity, latitude);
  298. deltaLongitude = Math.tan(heading) * (sigma2 - sigma1);
  299. longitude = CesiumMath.negativePiToPi(start.longitude + deltaLongitude);
  300. } else {
  301. //If heading is close to 90 degrees
  302. latitude = start.latitude;
  303. var localRad;
  304. if (ellipticity === 0.0) {
  305. // sphere
  306. localRad = major * Math.cos(start.latitude);
  307. } else {
  308. var sinPhi = Math.sin(start.latitude);
  309. localRad =
  310. (major * Math.cos(start.latitude)) /
  311. Math.sqrt(1 - ellipticitySquared * sinPhi * sinPhi);
  312. }
  313. deltaLongitude = distance / localRad;
  314. if (heading > 0.0) {
  315. longitude = CesiumMath.negativePiToPi(start.longitude + deltaLongitude);
  316. } else {
  317. longitude = CesiumMath.negativePiToPi(start.longitude - deltaLongitude);
  318. }
  319. }
  320. if (defined(result)) {
  321. result.longitude = longitude;
  322. result.latitude = latitude;
  323. result.height = 0;
  324. return result;
  325. }
  326. return new Cartographic(longitude, latitude, 0);
  327. }
  328. /**
  329. * Initializes a rhumb line on the ellipsoid connecting the two provided planetodetic points.
  330. *
  331. * @alias EllipsoidRhumbLine
  332. * @constructor
  333. *
  334. * @param {Cartographic} [start] The initial planetodetic point on the path.
  335. * @param {Cartographic} [end] The final planetodetic point on the path.
  336. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rhumb line lies.
  337. *
  338. * @exception {DeveloperError} angle between start and end must be at least 0.0125 radians.
  339. */
  340. function EllipsoidRhumbLine(start, end, ellipsoid) {
  341. var e = defaultValue(ellipsoid, Ellipsoid.WGS84);
  342. this._ellipsoid = e;
  343. this._start = new Cartographic();
  344. this._end = new Cartographic();
  345. this._heading = undefined;
  346. this._distance = undefined;
  347. this._ellipticity = undefined;
  348. this._ellipticitySquared = undefined;
  349. if (defined(start) && defined(end)) {
  350. computeProperties(this, start, end, e);
  351. }
  352. }
  353. Object.defineProperties(EllipsoidRhumbLine.prototype, {
  354. /**
  355. * Gets the ellipsoid.
  356. * @memberof EllipsoidRhumbLine.prototype
  357. * @type {Ellipsoid}
  358. * @readonly
  359. */
  360. ellipsoid: {
  361. get: function () {
  362. return this._ellipsoid;
  363. },
  364. },
  365. /**
  366. * Gets the surface distance between the start and end point
  367. * @memberof EllipsoidRhumbLine.prototype
  368. * @type {Number}
  369. * @readonly
  370. */
  371. surfaceDistance: {
  372. get: function () {
  373. //>>includeStart('debug', pragmas.debug);
  374. Check.defined("distance", this._distance);
  375. //>>includeEnd('debug');
  376. return this._distance;
  377. },
  378. },
  379. /**
  380. * Gets the initial planetodetic point on the path.
  381. * @memberof EllipsoidRhumbLine.prototype
  382. * @type {Cartographic}
  383. * @readonly
  384. */
  385. start: {
  386. get: function () {
  387. return this._start;
  388. },
  389. },
  390. /**
  391. * Gets the final planetodetic point on the path.
  392. * @memberof EllipsoidRhumbLine.prototype
  393. * @type {Cartographic}
  394. * @readonly
  395. */
  396. end: {
  397. get: function () {
  398. return this._end;
  399. },
  400. },
  401. /**
  402. * Gets the heading from the start point to the end point.
  403. * @memberof EllipsoidRhumbLine.prototype
  404. * @type {Number}
  405. * @readonly
  406. */
  407. heading: {
  408. get: function () {
  409. //>>includeStart('debug', pragmas.debug);
  410. Check.defined("distance", this._distance);
  411. //>>includeEnd('debug');
  412. return this._heading;
  413. },
  414. },
  415. });
  416. /**
  417. * Create a rhumb line using an initial position with a heading and distance.
  418. *
  419. * @param {Cartographic} start The initial planetodetic point on the path.
  420. * @param {Number} heading The heading in radians.
  421. * @param {Number} distance The rhumb line distance between the start and end point.
  422. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rhumb line lies.
  423. * @param {EllipsoidRhumbLine} [result] The object in which to store the result.
  424. * @returns {EllipsoidRhumbLine} The EllipsoidRhumbLine object.
  425. */
  426. EllipsoidRhumbLine.fromStartHeadingDistance = function (
  427. start,
  428. heading,
  429. distance,
  430. ellipsoid,
  431. result
  432. ) {
  433. //>>includeStart('debug', pragmas.debug);
  434. Check.defined("start", start);
  435. Check.defined("heading", heading);
  436. Check.defined("distance", distance);
  437. Check.typeOf.number.greaterThan("distance", distance, 0.0);
  438. //>>includeEnd('debug');
  439. var e = defaultValue(ellipsoid, Ellipsoid.WGS84);
  440. var major = e.maximumRadius;
  441. var minor = e.minimumRadius;
  442. var majorSquared = major * major;
  443. var minorSquared = minor * minor;
  444. var ellipticity = Math.sqrt((majorSquared - minorSquared) / majorSquared);
  445. heading = CesiumMath.negativePiToPi(heading);
  446. var end = interpolateUsingSurfaceDistance(
  447. start,
  448. heading,
  449. distance,
  450. e.maximumRadius,
  451. ellipticity
  452. );
  453. if (
  454. !defined(result) ||
  455. (defined(ellipsoid) && !ellipsoid.equals(result.ellipsoid))
  456. ) {
  457. return new EllipsoidRhumbLine(start, end, e);
  458. }
  459. result.setEndPoints(start, end);
  460. return result;
  461. };
  462. /**
  463. * Sets the start and end points of the rhumb line.
  464. *
  465. * @param {Cartographic} start The initial planetodetic point on the path.
  466. * @param {Cartographic} end The final planetodetic point on the path.
  467. */
  468. EllipsoidRhumbLine.prototype.setEndPoints = function (start, end) {
  469. //>>includeStart('debug', pragmas.debug);
  470. Check.defined("start", start);
  471. Check.defined("end", end);
  472. //>>includeEnd('debug');
  473. computeProperties(this, start, end, this._ellipsoid);
  474. };
  475. /**
  476. * Provides the location of a point at the indicated portion along the rhumb line.
  477. *
  478. * @param {Number} fraction The portion of the distance between the initial and final points.
  479. * @param {Cartographic} [result] The object in which to store the result.
  480. * @returns {Cartographic} The location of the point along the rhumb line.
  481. */
  482. EllipsoidRhumbLine.prototype.interpolateUsingFraction = function (
  483. fraction,
  484. result
  485. ) {
  486. return this.interpolateUsingSurfaceDistance(
  487. fraction * this._distance,
  488. result
  489. );
  490. };
  491. /**
  492. * Provides the location of a point at the indicated distance along the rhumb line.
  493. *
  494. * @param {Number} distance The distance from the inital point to the point of interest along the rhumbLine.
  495. * @param {Cartographic} [result] The object in which to store the result.
  496. * @returns {Cartographic} The location of the point along the rhumb line.
  497. *
  498. * @exception {DeveloperError} start and end must be set before calling function interpolateUsingSurfaceDistance
  499. */
  500. EllipsoidRhumbLine.prototype.interpolateUsingSurfaceDistance = function (
  501. distance,
  502. result
  503. ) {
  504. //>>includeStart('debug', pragmas.debug);
  505. Check.typeOf.number("distance", distance);
  506. if (!defined(this._distance) || this._distance === 0.0) {
  507. throw new DeveloperError(
  508. "EllipsoidRhumbLine must have distinct start and end set."
  509. );
  510. }
  511. //>>includeEnd('debug');
  512. return interpolateUsingSurfaceDistance(
  513. this._start,
  514. this._heading,
  515. distance,
  516. this._ellipsoid.maximumRadius,
  517. this._ellipticity,
  518. result
  519. );
  520. };
  521. /**
  522. * Provides the location of a point at the indicated longitude along the rhumb line.
  523. * 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.
  524. *
  525. * @param {Number} intersectionLongitude The longitude, in radians, at which to find the intersection point from the starting point using the heading.
  526. * @param {Cartographic} [result] The object in which to store the result.
  527. * @returns {Cartographic} The location of the intersection point along the rhumb line, undefined if there is no intersection or infinite intersections.
  528. *
  529. * @exception {DeveloperError} start and end must be set before calling function findIntersectionWithLongitude.
  530. */
  531. EllipsoidRhumbLine.prototype.findIntersectionWithLongitude = function (
  532. intersectionLongitude,
  533. result
  534. ) {
  535. //>>includeStart('debug', pragmas.debug);
  536. Check.typeOf.number("intersectionLongitude", intersectionLongitude);
  537. if (!defined(this._distance) || this._distance === 0.0) {
  538. throw new DeveloperError(
  539. "EllipsoidRhumbLine must have distinct start and end set."
  540. );
  541. }
  542. //>>includeEnd('debug');
  543. var ellipticity = this._ellipticity;
  544. var heading = this._heading;
  545. var absHeading = Math.abs(heading);
  546. var start = this._start;
  547. intersectionLongitude = CesiumMath.negativePiToPi(intersectionLongitude);
  548. if (
  549. CesiumMath.equalsEpsilon(
  550. Math.abs(intersectionLongitude),
  551. Math.PI,
  552. CesiumMath.EPSILON14
  553. )
  554. ) {
  555. intersectionLongitude = CesiumMath.sign(start.longitude) * Math.PI;
  556. }
  557. if (!defined(result)) {
  558. result = new Cartographic();
  559. }
  560. // If heading is -PI/2 or PI/2, this is an E-W rhumb line
  561. // If heading is 0 or PI, this is an N-S rhumb line
  562. if (Math.abs(CesiumMath.PI_OVER_TWO - absHeading) <= CesiumMath.EPSILON8) {
  563. result.longitude = intersectionLongitude;
  564. result.latitude = start.latitude;
  565. result.height = 0;
  566. return result;
  567. } else if (
  568. CesiumMath.equalsEpsilon(
  569. Math.abs(CesiumMath.PI_OVER_TWO - absHeading),
  570. CesiumMath.PI_OVER_TWO,
  571. CesiumMath.EPSILON8
  572. )
  573. ) {
  574. if (
  575. CesiumMath.equalsEpsilon(
  576. intersectionLongitude,
  577. start.longitude,
  578. CesiumMath.EPSILON12
  579. )
  580. ) {
  581. return undefined;
  582. }
  583. result.longitude = intersectionLongitude;
  584. result.latitude =
  585. CesiumMath.PI_OVER_TWO *
  586. CesiumMath.sign(CesiumMath.PI_OVER_TWO - heading);
  587. result.height = 0;
  588. return result;
  589. }
  590. // Use iterative solver from Equation 9 from http://edwilliams.org/ellipsoid/ellipsoid.pdf
  591. var phi1 = start.latitude;
  592. var eSinPhi1 = ellipticity * Math.sin(phi1);
  593. var leftComponent =
  594. Math.tan(0.5 * (CesiumMath.PI_OVER_TWO + phi1)) *
  595. Math.exp((intersectionLongitude - start.longitude) / Math.tan(heading));
  596. var denominator = (1 + eSinPhi1) / (1 - eSinPhi1);
  597. var newPhi = start.latitude;
  598. var phi;
  599. do {
  600. phi = newPhi;
  601. var eSinPhi = ellipticity * Math.sin(phi);
  602. var numerator = (1 + eSinPhi) / (1 - eSinPhi);
  603. newPhi =
  604. 2 *
  605. Math.atan(
  606. leftComponent * Math.pow(numerator / denominator, ellipticity / 2)
  607. ) -
  608. CesiumMath.PI_OVER_TWO;
  609. } while (!CesiumMath.equalsEpsilon(newPhi, phi, CesiumMath.EPSILON12));
  610. result.longitude = intersectionLongitude;
  611. result.latitude = newPhi;
  612. result.height = 0;
  613. return result;
  614. };
  615. /**
  616. * Provides the location of a point at the indicated latitude along the rhumb line.
  617. * 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.
  618. *
  619. * @param {Number} intersectionLatitude The latitude, in radians, at which to find the intersection point from the starting point using the heading.
  620. * @param {Cartographic} [result] The object in which to store the result.
  621. * @returns {Cartographic} The location of the intersection point along the rhumb line, undefined if there is no intersection or infinite intersections.
  622. *
  623. * @exception {DeveloperError} start and end must be set before calling function findIntersectionWithLongitude.
  624. */
  625. EllipsoidRhumbLine.prototype.findIntersectionWithLatitude = function (
  626. intersectionLatitude,
  627. result
  628. ) {
  629. //>>includeStart('debug', pragmas.debug);
  630. Check.typeOf.number("intersectionLatitude", intersectionLatitude);
  631. if (!defined(this._distance) || this._distance === 0.0) {
  632. throw new DeveloperError(
  633. "EllipsoidRhumbLine must have distinct start and end set."
  634. );
  635. }
  636. //>>includeEnd('debug');
  637. var ellipticity = this._ellipticity;
  638. var heading = this._heading;
  639. var start = this._start;
  640. // If start and end have same latitude, return undefined since it's either no intersection or infinite intersections
  641. if (
  642. CesiumMath.equalsEpsilon(
  643. Math.abs(heading),
  644. CesiumMath.PI_OVER_TWO,
  645. CesiumMath.EPSILON8
  646. )
  647. ) {
  648. return;
  649. }
  650. // Can be solved using the same equations from interpolateUsingSurfaceDistance
  651. var sigma1 = calculateSigma(ellipticity, start.latitude);
  652. var sigma2 = calculateSigma(ellipticity, intersectionLatitude);
  653. var deltaLongitude = Math.tan(heading) * (sigma2 - sigma1);
  654. var longitude = CesiumMath.negativePiToPi(start.longitude + deltaLongitude);
  655. if (defined(result)) {
  656. result.longitude = longitude;
  657. result.latitude = intersectionLatitude;
  658. result.height = 0;
  659. return result;
  660. }
  661. return new Cartographic(longitude, intersectionLatitude, 0);
  662. };
  663. export default EllipsoidRhumbLine;