EllipsoidRhumbLine-daebc75b.js 23 KB

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