karney_inverse.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. // Boost.Geometry
  2. // Copyright (c) 2018 Adeel Ahmad, Islamabad, Pakistan.
  3. // Contributed and/or modified by Adeel Ahmad, as part of Google Summer of Code 2018 program.
  4. // This file was modified by Oracle on 2019-2021.
  5. // Modifications copyright (c) 2019-2021 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. // This file is converted from GeographicLib, https://geographiclib.sourceforge.io
  12. // GeographicLib is originally written by Charles Karney.
  13. // Author: Charles Karney (2008-2017)
  14. // Last updated version of GeographicLib: 1.49
  15. // Original copyright notice:
  16. // Copyright (c) Charles Karney (2008-2017) <charles@karney.com> and licensed
  17. // under the MIT/X11 License. For more information, see
  18. // https://geographiclib.sourceforge.io
  19. #ifndef BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP
  20. #define BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP
  21. #include <boost/math/constants/constants.hpp>
  22. #include <boost/math/special_functions/hypot.hpp>
  23. #include <boost/geometry/util/condition.hpp>
  24. #include <boost/geometry/util/math.hpp>
  25. #include <boost/geometry/util/series_expansion.hpp>
  26. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  27. #include <boost/geometry/formulas/flattening.hpp>
  28. #include <boost/geometry/formulas/result_inverse.hpp>
  29. namespace boost { namespace geometry { namespace math {
  30. /*!
  31. \brief The exact difference of two angles reduced to (-180deg, 180deg].
  32. */
  33. template<typename T>
  34. inline T difference_angle(T const& x, T const& y, T& e)
  35. {
  36. T t, d = math::sum_error(std::remainder(-x, T(360)), std::remainder(y, T(360)), t);
  37. normalize_azimuth<degree, T>(d);
  38. // Here y - x = d + t (mod 360), exactly, where d is in (-180,180] and
  39. // abs(t) <= eps (eps = 2^-45 for doubles). The only case where the
  40. // addition of t takes the result outside the range (-180,180] is d = 180
  41. // and t > 0. The case, d = -180 + eps, t = -eps, can't happen, since
  42. // sum_error would have returned the exact result in such a case (i.e., given t = 0).
  43. return math::sum_error(d == 180 && t > 0 ? -180 : d, t, e);
  44. }
  45. }}} // namespace boost::geometry::math
  46. namespace boost { namespace geometry { namespace formula
  47. {
  48. namespace se = series_expansion;
  49. namespace detail
  50. {
  51. template <
  52. typename CT,
  53. bool EnableDistance,
  54. bool EnableAzimuth,
  55. bool EnableReverseAzimuth = false,
  56. bool EnableReducedLength = false,
  57. bool EnableGeodesicScale = false,
  58. size_t SeriesOrder = 8
  59. >
  60. class karney_inverse
  61. {
  62. static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
  63. static const bool CalcAzimuths = EnableAzimuth || EnableReverseAzimuth || CalcQuantities;
  64. static const bool CalcFwdAzimuth = EnableAzimuth || CalcQuantities;
  65. static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
  66. public:
  67. typedef result_inverse<CT> result_type;
  68. template <typename T1, typename T2, typename Spheroid>
  69. static inline result_type apply(T1 const& lo1,
  70. T1 const& la1,
  71. T2 const& lo2,
  72. T2 const& la2,
  73. Spheroid const& spheroid)
  74. {
  75. static CT const c0 = 0;
  76. static CT const c0_001 = 0.001;
  77. static CT const c0_1 = 0.1;
  78. static CT const c1 = 1;
  79. static CT const c2 = 2;
  80. static CT const c3 = 3;
  81. static CT const c8 = 8;
  82. static CT const c16 = 16;
  83. static CT const c90 = 90;
  84. static CT const c180 = 180;
  85. static CT const c200 = 200;
  86. static CT const pi = math::pi<CT>();
  87. static CT const d2r = math::d2r<CT>();
  88. static CT const r2d = math::r2d<CT>();
  89. result_type result;
  90. CT lat1 = la1 * r2d;
  91. CT lat2 = la2 * r2d;
  92. CT lon1 = lo1 * r2d;
  93. CT lon2 = lo2 * r2d;
  94. CT const a = CT(get_radius<0>(spheroid));
  95. CT const b = CT(get_radius<2>(spheroid));
  96. CT const f = formula::flattening<CT>(spheroid);
  97. CT const one_minus_f = c1 - f;
  98. CT const two_minus_f = c2 - f;
  99. CT const tol0 = std::numeric_limits<CT>::epsilon();
  100. CT const tol1 = c200 * tol0;
  101. CT const tol2 = sqrt(tol0);
  102. // Check on bisection interval.
  103. CT const tol_bisection = tol0 * tol2;
  104. CT const etol2 = c0_1 * tol2 /
  105. sqrt((std::max)(c0_001, std::abs(f)) * (std::min)(c1, c1 - f / c2) / c2);
  106. CT tiny = std::sqrt((std::numeric_limits<CT>::min)());
  107. CT const n = f / two_minus_f;
  108. CT const e2 = f * two_minus_f;
  109. CT const ep2 = e2 / math::sqr(one_minus_f);
  110. // Compute the longitudinal difference.
  111. CT lon12_error;
  112. CT lon12 = math::difference_angle(lon1, lon2, lon12_error);
  113. int lon12_sign = lon12 >= 0 ? 1 : -1;
  114. // Make points close to the meridian to lie on it.
  115. lon12 = lon12_sign * lon12;
  116. lon12_error = (c180 - lon12) - lon12_sign * lon12_error;
  117. // Convert to radians.
  118. CT lam12 = lon12 * d2r;
  119. CT sin_lam12;
  120. CT cos_lam12;
  121. if (lon12 > c90)
  122. {
  123. math::sin_cos_degrees(lon12_error, sin_lam12, cos_lam12);
  124. cos_lam12 *= -c1;
  125. }
  126. else
  127. {
  128. math::sin_cos_degrees(lon12, sin_lam12, cos_lam12);
  129. }
  130. // Make points close to the equator to lie on it.
  131. lat1 = math::round_angle(std::abs(lat1) > c90 ? c90 : lat1);
  132. lat2 = math::round_angle(std::abs(lat2) > c90 ? c90 : lat2);
  133. // Arrange points in a canonical form, as explained in
  134. // paper, Algorithms for geodesics, Eq. (44):
  135. //
  136. // 0 <= lon12 <= 180
  137. // -90 <= lat1 <= 0
  138. // lat1 <= lat2 <= -lat1
  139. int swap_point = std::abs(lat1) < std::abs(lat2) ? -1 : 1;
  140. if (swap_point < 0)
  141. {
  142. lon12_sign *= -1;
  143. swap(lat1, lat2);
  144. }
  145. // Enforce lat1 to be <= 0.
  146. int lat_sign = lat1 < 0 ? 1 : -1;
  147. lat1 *= lat_sign;
  148. lat2 *= lat_sign;
  149. CT sin_beta1, cos_beta1;
  150. math::sin_cos_degrees(lat1, sin_beta1, cos_beta1);
  151. sin_beta1 *= one_minus_f;
  152. math::normalize_unit_vector<CT>(sin_beta1, cos_beta1);
  153. cos_beta1 = (std::max)(tiny, cos_beta1);
  154. CT sin_beta2, cos_beta2;
  155. math::sin_cos_degrees(lat2, sin_beta2, cos_beta2);
  156. sin_beta2 *= one_minus_f;
  157. math::normalize_unit_vector<CT>(sin_beta2, cos_beta2);
  158. cos_beta2 = (std::max)(tiny, cos_beta2);
  159. // If cos_beta1 < -sin_beta1, then cos_beta2 - cos_beta1 is a
  160. // sensitive measure of the |beta1| - |beta2|. Alternatively,
  161. // (cos_beta1 >= -sin_beta1), abs(sin_beta2) + sin_beta1 is
  162. // a better measure.
  163. // Sometimes these quantities vanish and in that case we
  164. // force beta2 = +/- bet1a exactly.
  165. if (cos_beta1 < -sin_beta1)
  166. {
  167. if (cos_beta1 == cos_beta2)
  168. {
  169. sin_beta2 = sin_beta2 < 0 ? sin_beta1 : -sin_beta1;
  170. }
  171. }
  172. else
  173. {
  174. if (std::abs(sin_beta2) == -sin_beta1)
  175. {
  176. cos_beta2 = cos_beta1;
  177. }
  178. }
  179. CT const dn1 = sqrt(c1 + ep2 * math::sqr(sin_beta1));
  180. CT const dn2 = sqrt(c1 + ep2 * math::sqr(sin_beta2));
  181. CT sigma12;
  182. CT m12x = c0;
  183. CT s12x;
  184. CT M21;
  185. // Index zero element of coeffs_C1 is unused.
  186. se::coeffs_C1<SeriesOrder, CT> const coeffs_C1(n);
  187. bool meridian = lat1 == -90 || sin_lam12 == 0;
  188. CT cos_alpha1, sin_alpha1;
  189. CT cos_alpha2, sin_alpha2;
  190. if (meridian)
  191. {
  192. // Endpoints lie on a single full meridian.
  193. // Point to the target latitude.
  194. cos_alpha1 = cos_lam12;
  195. sin_alpha1 = sin_lam12;
  196. // Heading north at the target.
  197. cos_alpha2 = c1;
  198. sin_alpha2 = c0;
  199. CT sin_sigma1 = sin_beta1;
  200. CT cos_sigma1 = cos_alpha1 * cos_beta1;
  201. CT sin_sigma2 = sin_beta2;
  202. CT cos_sigma2 = cos_alpha2 * cos_beta2;
  203. CT sigma12 = std::atan2((std::max)(c0, cos_sigma1 * sin_sigma2 - sin_sigma1 * cos_sigma2),
  204. cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2);
  205. CT dummy;
  206. meridian_length(n, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
  207. sin_sigma2, cos_sigma2, dn2,
  208. cos_beta1, cos_beta2, s12x,
  209. m12x, dummy, result.geodesic_scale,
  210. M21, coeffs_C1);
  211. if (sigma12 < c1 || m12x >= c0)
  212. {
  213. if (sigma12 < c3 * tiny)
  214. {
  215. sigma12 = m12x = s12x = c0;
  216. }
  217. m12x *= b;
  218. s12x *= b;
  219. }
  220. else
  221. {
  222. // m12 < 0, i.e., prolate and too close to anti-podal.
  223. meridian = false;
  224. }
  225. }
  226. CT omega12;
  227. if (!meridian && sin_beta1 == c0 &&
  228. (f <= c0 || lon12_error >= f * c180))
  229. {
  230. // Points lie on the equator.
  231. cos_alpha1 = cos_alpha2 = c0;
  232. sin_alpha1 = sin_alpha2 = c1;
  233. s12x = a * lam12;
  234. sigma12 = omega12 = lam12 / one_minus_f;
  235. m12x = b * sin(sigma12);
  236. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  237. {
  238. result.geodesic_scale = cos(sigma12);
  239. }
  240. }
  241. else if (!meridian)
  242. {
  243. // If point1 and point2 belong within a hemisphere bounded by a
  244. // meridian and geodesic is neither meridional nor equatorial.
  245. // Find the starting point for Newton's method.
  246. CT dnm = c1;
  247. sigma12 = newton_start(sin_beta1, cos_beta1, dn1,
  248. sin_beta2, cos_beta2, dn2,
  249. lam12, sin_lam12, cos_lam12,
  250. sin_alpha1, cos_alpha1,
  251. sin_alpha2, cos_alpha2,
  252. dnm, coeffs_C1, ep2,
  253. tol1, tol2, etol2,
  254. n, f);
  255. if (sigma12 >= c0)
  256. {
  257. // Short lines case (newton_start sets sin_alpha2, cos_alpha2, dnm).
  258. s12x = sigma12 * b * dnm;
  259. m12x = math::sqr(dnm) * b * sin(sigma12 / dnm);
  260. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  261. {
  262. result.geodesic_scale = cos(sigma12 / dnm);
  263. }
  264. // Convert to radians.
  265. omega12 = lam12 / (one_minus_f * dnm);
  266. }
  267. else
  268. {
  269. // Apply the Newton's method.
  270. CT sin_sigma1 = c0, cos_sigma1 = c0;
  271. CT sin_sigma2 = c0, cos_sigma2 = c0;
  272. CT eps = c0, diff_omega12 = c0;
  273. // Bracketing range.
  274. CT sin_alpha1a = tiny, cos_alpha1a = c1;
  275. CT sin_alpha1b = tiny, cos_alpha1b = -c1;
  276. size_t iteration = 0;
  277. size_t max_iterations = 20 + std::numeric_limits<size_t>::digits + 10;
  278. for (bool tripn = false, tripb = false;
  279. iteration < max_iterations;
  280. ++iteration)
  281. {
  282. CT dv = c0;
  283. CT v = lambda12(sin_beta1, cos_beta1, dn1,
  284. sin_beta2, cos_beta2, dn2,
  285. sin_alpha1, cos_alpha1,
  286. sin_lam12, cos_lam12,
  287. sin_alpha2, cos_alpha2,
  288. sigma12,
  289. sin_sigma1, cos_sigma1,
  290. sin_sigma2, cos_sigma2,
  291. eps, diff_omega12,
  292. iteration < max_iterations,
  293. dv, f, n, ep2, tiny, coeffs_C1);
  294. // Reversed test to allow escape with NaNs.
  295. if (tripb || !(std::abs(v) >= (tripn ? c8 : c1) * tol0))
  296. break;
  297. // Update bracketing values.
  298. if (v > c0 && (iteration > max_iterations ||
  299. cos_alpha1 / sin_alpha1 > cos_alpha1b / sin_alpha1b))
  300. {
  301. sin_alpha1b = sin_alpha1;
  302. cos_alpha1b = cos_alpha1;
  303. }
  304. else if (v < c0 && (iteration > max_iterations ||
  305. cos_alpha1 / sin_alpha1 < cos_alpha1a / sin_alpha1a))
  306. {
  307. sin_alpha1a = sin_alpha1;
  308. cos_alpha1a = cos_alpha1;
  309. }
  310. if (iteration < max_iterations && dv > c0)
  311. {
  312. CT diff_alpha1 = -v / dv;
  313. CT sin_diff_alpha1 = sin(diff_alpha1);
  314. CT cos_diff_alpha1 = cos(diff_alpha1);
  315. CT nsin_alpha1 = sin_alpha1 * cos_diff_alpha1 +
  316. cos_alpha1 * sin_diff_alpha1;
  317. if (nsin_alpha1 > c0 && std::abs(diff_alpha1) < pi)
  318. {
  319. cos_alpha1 = cos_alpha1 * cos_diff_alpha1 - sin_alpha1 * sin_diff_alpha1;
  320. sin_alpha1 = nsin_alpha1;
  321. math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
  322. // In some regimes we don't get quadratic convergence because
  323. // slope -> 0. So use convergence conditions based on epsilon
  324. // instead of sqrt(epsilon).
  325. tripn = std::abs(v) <= c16 * tol0;
  326. continue;
  327. }
  328. }
  329. // Either dv was not positive or updated value was outside legal
  330. // range. Use the midpoint of the bracket as the next estimate.
  331. // This mechanism is not needed for the WGS84 ellipsoid, but it does
  332. // catch problems with more eeccentric ellipsoids. Its efficacy is
  333. // such for the WGS84 test set with the starting guess set to alp1 =
  334. // 90deg:
  335. // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
  336. // WGS84 and random input: mean = 4.74, sd = 0.99
  337. sin_alpha1 = (sin_alpha1a + sin_alpha1b) / c2;
  338. cos_alpha1 = (cos_alpha1a + cos_alpha1b) / c2;
  339. math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
  340. tripn = false;
  341. tripb = (std::abs(sin_alpha1a - sin_alpha1) + (cos_alpha1a - cos_alpha1) < tol_bisection ||
  342. std::abs(sin_alpha1 - sin_alpha1b) + (cos_alpha1 - cos_alpha1b) < tol_bisection);
  343. }
  344. CT dummy;
  345. se::coeffs_C1<SeriesOrder, CT> const coeffs_C1_eps(eps);
  346. // Ensure that the reduced length and geodesic scale are computed in
  347. // a "canonical" way, with the I2 integral.
  348. meridian_length(eps, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
  349. sin_sigma2, cos_sigma2, dn2,
  350. cos_beta1, cos_beta2, s12x,
  351. m12x, dummy, result.geodesic_scale,
  352. M21, coeffs_C1_eps);
  353. m12x *= b;
  354. s12x *= b;
  355. }
  356. }
  357. if (swap_point < 0)
  358. {
  359. swap(sin_alpha1, sin_alpha2);
  360. swap(cos_alpha1, cos_alpha2);
  361. swap(result.geodesic_scale, M21);
  362. }
  363. sin_alpha1 *= swap_point * lon12_sign;
  364. cos_alpha1 *= swap_point * lat_sign;
  365. sin_alpha2 *= swap_point * lon12_sign;
  366. cos_alpha2 *= swap_point * lat_sign;
  367. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
  368. {
  369. result.reduced_length = m12x;
  370. }
  371. if (BOOST_GEOMETRY_CONDITION(CalcAzimuths))
  372. {
  373. if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
  374. {
  375. result.azimuth = atan2(sin_alpha1, cos_alpha1);
  376. }
  377. if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
  378. {
  379. result.reverse_azimuth = atan2(sin_alpha2, cos_alpha2);
  380. }
  381. }
  382. if (BOOST_GEOMETRY_CONDITION(EnableDistance))
  383. {
  384. result.distance = s12x;
  385. }
  386. return result;
  387. }
  388. template <typename CoeffsC1>
  389. static inline void meridian_length(CT const& epsilon, CT const& ep2, CT const& sigma12,
  390. CT const& sin_sigma1, CT const& cos_sigma1, CT const& dn1,
  391. CT const& sin_sigma2, CT const& cos_sigma2, CT const& dn2,
  392. CT const& cos_beta1, CT const& cos_beta2,
  393. CT& s12x, CT& m12x, CT& m0,
  394. CT& M12, CT& M21,
  395. CoeffsC1 const& coeffs_C1)
  396. {
  397. static CT const c1 = 1;
  398. CT A12x = 0, J12 = 0;
  399. CT expansion_A1, expansion_A2;
  400. // Evaluate the coefficients for C2.
  401. se::coeffs_C2<SeriesOrder, CT> coeffs_C2(epsilon);
  402. if (BOOST_GEOMETRY_CONDITION(EnableDistance) ||
  403. BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  404. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  405. {
  406. // Find the coefficients for A1 by computing the
  407. // series expansion using Horner scehme.
  408. expansion_A1 = se::evaluate_A1<SeriesOrder>(epsilon);
  409. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  410. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  411. {
  412. // Find the coefficients for A2 by computing the
  413. // series expansion using Horner scehme.
  414. expansion_A2 = se::evaluate_A2<SeriesOrder>(epsilon);
  415. A12x = expansion_A1 - expansion_A2;
  416. expansion_A2 += c1;
  417. }
  418. expansion_A1 += c1;
  419. }
  420. if (BOOST_GEOMETRY_CONDITION(EnableDistance))
  421. {
  422. CT B1 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C1)
  423. - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C1);
  424. s12x = expansion_A1 * (sigma12 + B1);
  425. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  426. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  427. {
  428. CT B2 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2)
  429. - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2);
  430. J12 = A12x * sigma12 + (expansion_A1 * B1 - expansion_A2 * B2);
  431. }
  432. }
  433. else if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  434. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  435. {
  436. for (size_t i = 1; i <= SeriesOrder; ++i)
  437. {
  438. coeffs_C2[i] = expansion_A1 * coeffs_C1[i] -
  439. expansion_A2 * coeffs_C2[i];
  440. }
  441. J12 = A12x * sigma12 +
  442. (se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2)
  443. - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2));
  444. }
  445. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
  446. {
  447. m0 = A12x;
  448. m12x = dn2 * (cos_sigma1 * sin_sigma2) -
  449. dn1 * (sin_sigma1 * cos_sigma2) -
  450. cos_sigma1 * cos_sigma2 * J12;
  451. }
  452. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  453. {
  454. CT cos_sigma12 = cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2;
  455. CT t = ep2 * (cos_beta1 - cos_beta2) *
  456. (cos_beta1 + cos_beta2) / (dn1 + dn2);
  457. M12 = cos_sigma12 + (t * sin_sigma2 - cos_sigma2 * J12) * sin_sigma1 / dn1;
  458. M21 = cos_sigma12 - (t * sin_sigma1 - cos_sigma1 * J12) * sin_sigma2 / dn2;
  459. }
  460. }
  461. /*
  462. Return a starting point for Newton's method in sin_alpha1 and
  463. cos_alpha1 (function value is -1). If Newton's method
  464. doesn't need to be used, return also sin_alpha2 and
  465. cos_alpha2 and function value is sig12.
  466. */
  467. template <typename CoeffsC1>
  468. static inline CT newton_start(CT const& sin_beta1, CT const& cos_beta1, CT const& dn1,
  469. CT const& sin_beta2, CT const& cos_beta2, CT dn2,
  470. CT const& lam12, CT const& sin_lam12, CT const& cos_lam12,
  471. CT& sin_alpha1, CT& cos_alpha1,
  472. CT& sin_alpha2, CT& cos_alpha2,
  473. CT& dnm, CoeffsC1 const& coeffs_C1, CT const& ep2,
  474. CT const& tol1, CT const& tol2, CT const& etol2, CT const& n,
  475. CT const& f)
  476. {
  477. static CT const c0 = 0;
  478. static CT const c0_01 = 0.01;
  479. static CT const c0_1 = 0.1;
  480. static CT const c0_5 = 0.5;
  481. static CT const c1 = 1;
  482. static CT const c2 = 2;
  483. static CT const c6 = 6;
  484. static CT const c1000 = 1000;
  485. static CT const pi = math::pi<CT>();
  486. CT const one_minus_f = c1 - f;
  487. CT const x_thresh = c1000 * tol2;
  488. // Return a starting point for Newton's method in sin_alpha1
  489. // and cos_alpha1 (function value is -1). If Newton's method
  490. // doesn't need to be used, return also sin_alpha2 and
  491. // cos_alpha2 and function value is sig12.
  492. CT sig12 = -c1;
  493. // bet12 = bet2 - bet1 in [0, pi); beta12a = bet2 + bet1 in (-pi, 0]
  494. CT sin_beta12 = sin_beta2 * cos_beta1 - cos_beta2 * sin_beta1;
  495. CT cos_beta12 = cos_beta2 * cos_beta1 + sin_beta2 * sin_beta1;
  496. CT sin_beta12a = sin_beta2 * cos_beta1 + cos_beta2 * sin_beta1;
  497. bool shortline = cos_beta12 >= c0 && sin_beta12 < c0_5 &&
  498. cos_beta2 * lam12 < c0_5;
  499. CT sin_omega12, cos_omega12;
  500. if (shortline)
  501. {
  502. CT sin_beta_m2 = math::sqr(sin_beta1 + sin_beta2);
  503. sin_beta_m2 /= sin_beta_m2 + math::sqr(cos_beta1 + cos_beta2);
  504. dnm = math::sqrt(c1 + ep2 * sin_beta_m2);
  505. CT omega12 = lam12 / (one_minus_f * dnm);
  506. sin_omega12 = sin(omega12);
  507. cos_omega12 = cos(omega12);
  508. }
  509. else
  510. {
  511. sin_omega12 = sin_lam12;
  512. cos_omega12 = cos_lam12;
  513. }
  514. sin_alpha1 = cos_beta2 * sin_omega12;
  515. cos_alpha1 = cos_omega12 >= c0 ?
  516. sin_beta12 + cos_beta2 * sin_beta1 * math::sqr(sin_omega12) / (c1 + cos_omega12) :
  517. sin_beta12a - cos_beta2 * sin_beta1 * math::sqr(sin_omega12) / (c1 - cos_omega12);
  518. CT sin_sigma12 = boost::math::hypot(sin_alpha1, cos_alpha1);
  519. CT cos_sigma12 = sin_beta1 * sin_beta2 + cos_beta1 * cos_beta2 * cos_omega12;
  520. if (shortline && sin_sigma12 < etol2)
  521. {
  522. sin_alpha2 = cos_beta1 * sin_omega12;
  523. cos_alpha2 = sin_beta12 - cos_beta1 * sin_beta2 *
  524. (cos_omega12 >= c0 ? math::sqr(sin_omega12) /
  525. (c1 + cos_omega12) : c1 - cos_omega12);
  526. math::normalize_unit_vector<CT>(sin_alpha2, cos_alpha2);
  527. // Set return value.
  528. sig12 = atan2(sin_sigma12, cos_sigma12);
  529. }
  530. // Skip astroid calculation if too eccentric.
  531. else if (std::abs(n) > c0_1 ||
  532. cos_sigma12 >= c0 ||
  533. sin_sigma12 >= c6 * std::abs(n) * pi *
  534. math::sqr(cos_beta1))
  535. {
  536. // Nothing to do, zeroth order spherical approximation will do.
  537. }
  538. else
  539. {
  540. // Scale lam12 and bet2 to x, y coordinate system where antipodal
  541. // point is at origin and singular point is at y = 0, x = -1.
  542. CT lambda_scale, beta_scale;
  543. CT y;
  544. volatile CT x;
  545. CT lam12x = atan2(-sin_lam12, -cos_lam12);
  546. if (f >= c0)
  547. {
  548. CT k2 = math::sqr(sin_beta1) * ep2;
  549. CT eps = k2 / (c2 * (c1 + sqrt(c1 + k2)) + k2);
  550. se::coeffs_A3<SeriesOrder, CT> const coeffs_A3(n);
  551. CT const A3 = math::horner_evaluate(eps, coeffs_A3.begin(), coeffs_A3.end());
  552. lambda_scale = f * cos_beta1 * A3 * pi;
  553. beta_scale = lambda_scale * cos_beta1;
  554. x = lam12x / lambda_scale;
  555. y = sin_beta12a / beta_scale;
  556. }
  557. else
  558. {
  559. CT cos_beta12a = cos_beta2 * cos_beta1 - sin_beta2 * sin_beta1;
  560. CT beta12a = atan2(sin_beta12a, cos_beta12a);
  561. CT m12b = c0;
  562. CT m0 = c1;
  563. CT dummy;
  564. meridian_length(n, ep2, pi + beta12a,
  565. sin_beta1, -cos_beta1, dn1,
  566. sin_beta2, cos_beta2, dn2,
  567. cos_beta1, cos_beta2, dummy,
  568. m12b, m0, dummy, dummy, coeffs_C1);
  569. x = -c1 + m12b / (cos_beta1 * cos_beta2 * m0 * pi);
  570. beta_scale = x < -c0_01
  571. ? sin_beta12a / x
  572. : -f * math::sqr(cos_beta1) * pi;
  573. lambda_scale = beta_scale / cos_beta1;
  574. y = lam12x / lambda_scale;
  575. }
  576. if (y > -tol1 && x > -c1 - x_thresh)
  577. {
  578. // Strip near cut.
  579. if (f >= c0)
  580. {
  581. sin_alpha1 = (std::min)(c1, -CT(x));
  582. cos_alpha1 = - math::sqrt(c1 - math::sqr(sin_alpha1));
  583. }
  584. else
  585. {
  586. cos_alpha1 = (std::max)(CT(x > -tol1 ? c0 : -c1), CT(x));
  587. sin_alpha1 = math::sqrt(c1 - math::sqr(cos_alpha1));
  588. }
  589. }
  590. else
  591. {
  592. // Solve the astroid problem.
  593. CT k = astroid(CT(x), y);
  594. CT omega12a = lambda_scale * (f >= c0 ? -x * k /
  595. (c1 + k) : -y * (c1 + k) / k);
  596. sin_omega12 = sin(omega12a);
  597. cos_omega12 = -cos(omega12a);
  598. // Update spherical estimate of alpha1 using omgega12 instead of lam12.
  599. sin_alpha1 = cos_beta2 * sin_omega12;
  600. cos_alpha1 = sin_beta12a - cos_beta2 * sin_beta1 *
  601. math::sqr(sin_omega12) / (c1 - cos_omega12);
  602. }
  603. }
  604. // Sanity check on starting guess. Backwards check allows NaN through.
  605. if (!(sin_alpha1 <= c0))
  606. {
  607. math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
  608. }
  609. else
  610. {
  611. sin_alpha1 = c1;
  612. cos_alpha1 = c0;
  613. }
  614. return sig12;
  615. }
  616. /*
  617. Solve the astroid problem using the equation:
  618. κ4 + 2κ3 + (1 − x2 − y 2 )κ2 − 2y 2 κ − y 2 = 0.
  619. For details, please refer to Eq. (65) in,
  620. Geodesics on an ellipsoid of revolution, Charles F.F Karney,
  621. https://arxiv.org/abs/1102.1215
  622. */
  623. static inline CT astroid(CT const& x, CT const& y)
  624. {
  625. static CT const c0 = 0;
  626. static CT const c1 = 1;
  627. static CT const c2 = 2;
  628. static CT const c3 = 3;
  629. static CT const c4 = 4;
  630. static CT const c6 = 6;
  631. CT k;
  632. CT p = math::sqr(x);
  633. CT q = math::sqr(y);
  634. CT r = (p + q - c1) / c6;
  635. if (!(q == c0 && r <= c0))
  636. {
  637. // Avoid possible division by zero when r = 0 by multiplying
  638. // equations for s and t by r^3 and r, respectively.
  639. CT S = p * q / c4;
  640. CT r2 = math::sqr(r);
  641. CT r3 = r * r2;
  642. // The discriminant of the quadratic equation for T3. This is
  643. // zero on the evolute curve p^(1/3)+q^(1/3) = 1.
  644. CT discriminant = S * (S + c2 * r3);
  645. CT u = r;
  646. if (discriminant >= c0)
  647. {
  648. CT T3 = S + r3;
  649. // Pick the sign on the sqrt to maximize abs(T3). This minimizes
  650. // loss of precision due to cancellation. The result is unchanged
  651. // because of the way the T is used in definition of u.
  652. T3 += T3 < c0 ? -std::sqrt(discriminant) : std::sqrt(discriminant);
  653. CT T = std::cbrt(T3);
  654. // T can be zero; but then r2 / T -> 0.
  655. u += T + (T != c0 ? r2 / T : c0);
  656. }
  657. else
  658. {
  659. CT ang = std::atan2(std::sqrt(-discriminant), -(S + r3));
  660. // There are three possible cube roots. We choose the root which avoids
  661. // cancellation. Note that discriminant < 0 implies that r < 0.
  662. u += c2 * r * cos(ang / c3);
  663. }
  664. CT v = std::sqrt(math::sqr(u) + q);
  665. // Avoid loss of accuracy when u < 0.
  666. CT uv = u < c0 ? q / (v - u) : u + v;
  667. CT w = (uv - q) / (c2 * v);
  668. // Rearrange expression for k to avoid loss of accuracy due to
  669. // subtraction. Division by 0 not possible because uv > 0, w >= 0.
  670. k = uv / (std::sqrt(uv + math::sqr(w)) + w);
  671. }
  672. else // q == 0 && r <= 0
  673. {
  674. // y = 0 with |x| <= 1. Handle this case directly.
  675. // For y small, positive root is k = abs(y)/sqrt(1-x^2).
  676. k = c0;
  677. }
  678. return k;
  679. }
  680. template <typename CoeffsC1>
  681. static inline CT lambda12(CT const& sin_beta1, CT const& cos_beta1, CT const& dn1,
  682. CT const& sin_beta2, CT const& cos_beta2, CT const& dn2,
  683. CT const& sin_alpha1, CT cos_alpha1,
  684. CT const& sin_lam120, CT const& cos_lam120,
  685. CT& sin_alpha2, CT& cos_alpha2,
  686. CT& sigma12,
  687. CT& sin_sigma1, CT& cos_sigma1,
  688. CT& sin_sigma2, CT& cos_sigma2,
  689. CT& eps, CT& diff_omega12,
  690. bool diffp, CT& diff_lam12,
  691. CT const& f, CT const& n, CT const& ep2, CT const& tiny,
  692. CoeffsC1 const& coeffs_C1)
  693. {
  694. static CT const c0 = 0;
  695. static CT const c1 = 1;
  696. static CT const c2 = 2;
  697. CT const one_minus_f = c1 - f;
  698. if (sin_beta1 == c0 && cos_alpha1 == c0)
  699. {
  700. // Break degeneracy of equatorial line.
  701. cos_alpha1 = -tiny;
  702. }
  703. CT sin_alpha0 = sin_alpha1 * cos_beta1;
  704. CT cos_alpha0 = boost::math::hypot(cos_alpha1, sin_alpha1 * sin_beta1);
  705. CT sin_omega1, cos_omega1;
  706. CT sin_omega2, cos_omega2;
  707. CT sin_omega12, cos_omega12;
  708. CT lam12;
  709. sin_sigma1 = sin_beta1;
  710. sin_omega1 = sin_alpha0 * sin_beta1;
  711. cos_sigma1 = cos_omega1 = cos_alpha1 * cos_beta1;
  712. math::normalize_unit_vector<CT>(sin_sigma1, cos_sigma1);
  713. // Enforce symmetries in the case abs(beta2) = -beta1.
  714. // Otherwise, this can yield singularities in the Newton iteration.
  715. // sin(alpha2) * cos(beta2) = sin(alpha0).
  716. sin_alpha2 = cos_beta2 != cos_beta1 ?
  717. sin_alpha0 / cos_beta2 : sin_alpha1;
  718. cos_alpha2 = cos_beta2 != cos_beta1 || std::abs(sin_beta2) != -sin_beta1 ?
  719. sqrt(math::sqr(cos_alpha1 * cos_beta1) +
  720. (cos_beta1 < -sin_beta1 ?
  721. (cos_beta2 - cos_beta1) * (cos_beta1 + cos_beta2) :
  722. (sin_beta1 - sin_beta2) * (sin_beta1 + sin_beta2))) / cos_beta2 :
  723. std::abs(cos_alpha1);
  724. sin_sigma2 = sin_beta2;
  725. sin_omega2 = sin_alpha0 * sin_beta2;
  726. cos_sigma2 = cos_omega2 =
  727. (cos_alpha2 * cos_beta2);
  728. // Break degeneracy of equatorial line.
  729. math::normalize_unit_vector<CT>(sin_sigma2, cos_sigma2);
  730. // sig12 = sig2 - sig1, limit to [0, pi].
  731. sigma12 = atan2((std::max)(c0, cos_sigma1 * sin_sigma2 - sin_sigma1 * cos_sigma2),
  732. cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2);
  733. // omg12 = omg2 - omg1, limit to [0, pi].
  734. sin_omega12 = (std::max)(c0, cos_omega1 * sin_omega2 - sin_omega1 * cos_omega2);
  735. cos_omega12 = cos_omega1 * cos_omega2 + sin_omega1 * sin_omega2;
  736. // eta = omg12 - lam120.
  737. CT eta = atan2(sin_omega12 * cos_lam120 - cos_omega12 * sin_lam120,
  738. cos_omega12 * cos_lam120 + sin_omega12 * sin_lam120);
  739. CT B312;
  740. CT k2 = math::sqr(cos_alpha0) * ep2;
  741. eps = k2 / (c2 * (c1 + std::sqrt(c1 + k2)) + k2);
  742. se::coeffs_C3<SeriesOrder, CT> const coeffs_C3(n, eps);
  743. B312 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C3)
  744. - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C3);
  745. se::coeffs_A3<SeriesOrder, CT> const coeffs_A3(n);
  746. CT const A3 = math::horner_evaluate(eps, coeffs_A3.begin(), coeffs_A3.end());
  747. diff_omega12 = -f * A3 * sin_alpha0 * (sigma12 + B312);
  748. lam12 = eta + diff_omega12;
  749. if (diffp)
  750. {
  751. if (cos_alpha2 == c0)
  752. {
  753. diff_lam12 = - c2 * one_minus_f * dn1 / sin_beta1;
  754. }
  755. else
  756. {
  757. CT dummy;
  758. meridian_length(eps, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
  759. sin_sigma2, cos_sigma2, dn2,
  760. cos_beta1, cos_beta2, dummy,
  761. diff_lam12, dummy, dummy,
  762. dummy, coeffs_C1);
  763. diff_lam12 *= one_minus_f / (cos_alpha2 * cos_beta2);
  764. }
  765. }
  766. return lam12;
  767. }
  768. };
  769. } // namespace detail
  770. /*!
  771. \brief The solution of the inverse problem of geodesics on latlong coordinates,
  772. after Karney (2011).
  773. \author See
  774. - Charles F.F Karney, Algorithms for geodesics, 2011
  775. https://arxiv.org/pdf/1109.4448.pdf
  776. */
  777. template <
  778. typename CT,
  779. bool EnableDistance,
  780. bool EnableAzimuth,
  781. bool EnableReverseAzimuth = false,
  782. bool EnableReducedLength = false,
  783. bool EnableGeodesicScale = false
  784. >
  785. struct karney_inverse
  786. : detail::karney_inverse
  787. <
  788. CT,
  789. EnableDistance,
  790. EnableAzimuth,
  791. EnableReverseAzimuth,
  792. EnableReducedLength,
  793. EnableGeodesicScale
  794. >
  795. {};
  796. }}} // namespace boost::geometry::formula
  797. #endif // BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP