karney_inverse.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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.
  5. // Modifications copyright (c) 2019 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // This file is converted from GeographicLib, https://geographiclib.sourceforge.io
  11. // GeographicLib is originally written by Charles Karney.
  12. // Author: Charles Karney (2008-2017)
  13. // Last updated version of GeographicLib: 1.49
  14. // Original copyright notice:
  15. // Copyright (c) Charles Karney (2008-2017) <charles@karney.com> and licensed
  16. // under the MIT/X11 License. For more information, see
  17. // https://geographiclib.sourceforge.io
  18. #ifndef BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP
  19. #define BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP
  20. #include <boost/math/constants/constants.hpp>
  21. #include <boost/math/special_functions/hypot.hpp>
  22. #include <boost/geometry/util/condition.hpp>
  23. #include <boost/geometry/util/math.hpp>
  24. #include <boost/geometry/util/series_expansion.hpp>
  25. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  26. #include <boost/geometry/formulas/flattening.hpp>
  27. #include <boost/geometry/formulas/result_inverse.hpp>
  28. namespace boost { namespace geometry { namespace math {
  29. // TODO: Moved temporarily because of C++11 is used
  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. /*!
  50. \brief The solution of the inverse problem of geodesics on latlong coordinates,
  51. after Karney (2011).
  52. \author See
  53. - Charles F.F Karney, Algorithms for geodesics, 2011
  54. https://arxiv.org/pdf/1109.4448.pdf
  55. */
  56. template <
  57. typename CT,
  58. bool EnableDistance,
  59. bool EnableAzimuth,
  60. bool EnableReverseAzimuth = false,
  61. bool EnableReducedLength = false,
  62. bool EnableGeodesicScale = false,
  63. size_t SeriesOrder = 8
  64. >
  65. class karney_inverse
  66. {
  67. static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
  68. static const bool CalcAzimuths = EnableAzimuth || EnableReverseAzimuth || CalcQuantities;
  69. static const bool CalcFwdAzimuth = EnableAzimuth || CalcQuantities;
  70. static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
  71. public:
  72. typedef result_inverse<CT> result_type;
  73. template <typename T1, typename T2, typename Spheroid>
  74. static inline result_type apply(T1 const& lo1,
  75. T1 const& la1,
  76. T2 const& lo2,
  77. T2 const& la2,
  78. Spheroid const& spheroid)
  79. {
  80. static CT const c0 = 0;
  81. static CT const c0_001 = 0.001;
  82. static CT const c0_1 = 0.1;
  83. static CT const c1 = 1;
  84. static CT const c2 = 2;
  85. static CT const c3 = 3;
  86. static CT const c8 = 8;
  87. static CT const c16 = 16;
  88. static CT const c90 = 90;
  89. static CT const c180 = 180;
  90. static CT const c200 = 200;
  91. static CT const pi = math::pi<CT>();
  92. static CT const d2r = math::d2r<CT>();
  93. static CT const r2d = math::r2d<CT>();
  94. result_type result;
  95. CT lat1 = la1;
  96. CT lat2 = la2;
  97. CT lon1 = lo1;
  98. CT lon2 = lo2;
  99. CT const a = CT(get_radius<0>(spheroid));
  100. CT const b = CT(get_radius<2>(spheroid));
  101. CT const f = formula::flattening<CT>(spheroid);
  102. CT const one_minus_f = c1 - f;
  103. CT const two_minus_f = c2 - f;
  104. CT const tol0 = std::numeric_limits<CT>::epsilon();
  105. CT const tol1 = c200 * tol0;
  106. CT const tol2 = sqrt(tol0);
  107. // Check on bisection interval.
  108. CT const tol_bisection = tol0 * tol2;
  109. CT const etol2 = c0_1 * tol2 /
  110. sqrt((std::max)(c0_001, std::abs(f)) * (std::min)(c1, c1 - f / c2) / c2);
  111. CT tiny = std::sqrt((std::numeric_limits<CT>::min)());
  112. CT const n = f / two_minus_f;
  113. CT const e2 = f * two_minus_f;
  114. CT const ep2 = e2 / math::sqr(one_minus_f);
  115. // Compute the longitudinal difference.
  116. CT lon12_error;
  117. CT lon12 = math::difference_angle(lon1, lon2, lon12_error);
  118. int lon12_sign = lon12 >= 0 ? 1 : -1;
  119. // Make points close to the meridian to lie on it.
  120. lon12 = lon12_sign * lon12;
  121. lon12_error = (c180 - lon12) - lon12_sign * lon12_error;
  122. // Convert to radians.
  123. CT lam12 = lon12 * d2r;
  124. CT sin_lam12;
  125. CT cos_lam12;
  126. if (lon12 > c90)
  127. {
  128. math::sin_cos_degrees(lon12_error, sin_lam12, cos_lam12);
  129. cos_lam12 *= -c1;
  130. }
  131. else
  132. {
  133. math::sin_cos_degrees(lon12, sin_lam12, cos_lam12);
  134. }
  135. // Make points close to the equator to lie on it.
  136. lat1 = math::round_angle(std::abs(lat1) > c90 ? c90 : lat1);
  137. lat2 = math::round_angle(std::abs(lat2) > c90 ? c90 : lat2);
  138. // Arrange points in a canonical form, as explained in
  139. // paper, Algorithms for geodesics, Eq. (44):
  140. //
  141. // 0 <= lon12 <= 180
  142. // -90 <= lat1 <= 0
  143. // lat1 <= lat2 <= -lat1
  144. int swap_point = std::abs(lat1) < std::abs(lat2) ? -1 : 1;
  145. if (swap_point < 0)
  146. {
  147. lon12_sign *= -1;
  148. swap(lat1, lat2);
  149. }
  150. // Enforce lat1 to be <= 0.
  151. int lat_sign = lat1 < 0 ? 1 : -1;
  152. lat1 *= lat_sign;
  153. lat2 *= lat_sign;
  154. CT sin_beta1, cos_beta1;
  155. math::sin_cos_degrees(lat1, sin_beta1, cos_beta1);
  156. sin_beta1 *= one_minus_f;
  157. math::normalize_unit_vector<CT>(sin_beta1, cos_beta1);
  158. cos_beta1 = (std::max)(tiny, cos_beta1);
  159. CT sin_beta2, cos_beta2;
  160. math::sin_cos_degrees(lat2, sin_beta2, cos_beta2);
  161. sin_beta2 *= one_minus_f;
  162. math::normalize_unit_vector<CT>(sin_beta2, cos_beta2);
  163. cos_beta2 = (std::max)(tiny, cos_beta2);
  164. // If cos_beta1 < -sin_beta1, then cos_beta2 - cos_beta1 is a
  165. // sensitive measure of the |beta1| - |beta2|. Alternatively,
  166. // (cos_beta1 >= -sin_beta1), abs(sin_beta2) + sin_beta1 is
  167. // a better measure.
  168. // Sometimes these quantities vanish and in that case we
  169. // force beta2 = +/- bet1a exactly.
  170. if (cos_beta1 < -sin_beta1)
  171. {
  172. if (cos_beta1 == cos_beta2)
  173. {
  174. sin_beta2 = sin_beta2 < 0 ? sin_beta1 : -sin_beta1;
  175. }
  176. }
  177. else
  178. {
  179. if (std::abs(sin_beta2) == -sin_beta1)
  180. {
  181. cos_beta2 = cos_beta1;
  182. }
  183. }
  184. CT const dn1 = sqrt(c1 + ep2 * math::sqr(sin_beta1));
  185. CT const dn2 = sqrt(c1 + ep2 * math::sqr(sin_beta2));
  186. CT sigma12;
  187. CT m12x, s12x, M21;
  188. // Index zero element of coeffs_C1 is unused.
  189. se::coeffs_C1<SeriesOrder, CT> const coeffs_C1(n);
  190. bool meridian = lat1 == -90 || sin_lam12 == 0;
  191. CT cos_alpha1, sin_alpha1;
  192. CT cos_alpha2, sin_alpha2;
  193. if (meridian)
  194. {
  195. // Endpoints lie on a single full meridian.
  196. // Point to the target latitude.
  197. cos_alpha1 = cos_lam12;
  198. sin_alpha1 = sin_lam12;
  199. // Heading north at the target.
  200. cos_alpha2 = c1;
  201. sin_alpha2 = c0;
  202. CT sin_sigma1 = sin_beta1;
  203. CT cos_sigma1 = cos_alpha1 * cos_beta1;
  204. CT sin_sigma2 = sin_beta2;
  205. CT cos_sigma2 = cos_alpha2 * cos_beta2;
  206. CT sigma12 = std::atan2((std::max)(c0, cos_sigma1 * sin_sigma2 - sin_sigma1 * cos_sigma2),
  207. cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2);
  208. CT dummy;
  209. meridian_length(n, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
  210. sin_sigma2, cos_sigma2, dn2,
  211. cos_beta1, cos_beta2, s12x,
  212. m12x, dummy, result.geodesic_scale,
  213. M21, coeffs_C1);
  214. if (sigma12 < c1 || m12x >= c0)
  215. {
  216. if (sigma12 < c3 * tiny)
  217. {
  218. sigma12 = m12x = s12x = c0;
  219. }
  220. m12x *= b;
  221. s12x *= b;
  222. }
  223. else
  224. {
  225. // m12 < 0, i.e., prolate and too close to anti-podal.
  226. meridian = false;
  227. }
  228. }
  229. CT omega12;
  230. if (!meridian && sin_beta1 == c0 &&
  231. (f <= c0 || lon12_error >= f * c180))
  232. {
  233. // Points lie on the equator.
  234. cos_alpha1 = cos_alpha2 = c0;
  235. sin_alpha1 = sin_alpha2 = c1;
  236. s12x = a * lam12;
  237. sigma12 = omega12 = lam12 / one_minus_f;
  238. m12x = b * sin(sigma12);
  239. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  240. {
  241. result.geodesic_scale = cos(sigma12);
  242. }
  243. }
  244. else if (!meridian)
  245. {
  246. // If point1 and point2 belong within a hemisphere bounded by a
  247. // meridian and geodesic is neither meridional nor equatorial.
  248. // Find the starting point for Newton's method.
  249. CT dnm;
  250. sigma12 = newton_start(sin_beta1, cos_beta1, dn1,
  251. sin_beta2, cos_beta2, dn2,
  252. lam12, sin_lam12, cos_lam12,
  253. sin_alpha1, cos_alpha1,
  254. sin_alpha2, cos_alpha2,
  255. dnm, coeffs_C1, ep2,
  256. tol1, tol2, etol2,
  257. n, f);
  258. if (sigma12 >= c0)
  259. {
  260. // Short lines case (newton_start sets sin_alpha2, cos_alpha2, dnm).
  261. s12x = sigma12 * b * dnm;
  262. m12x = math::sqr(dnm) * b * sin(sigma12 / dnm);
  263. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  264. {
  265. result.geodesic_scale = cos(sigma12 / dnm);
  266. }
  267. // Convert to radians.
  268. omega12 = lam12 / (one_minus_f * dnm);
  269. }
  270. else
  271. {
  272. // Apply the Newton's method.
  273. CT sin_sigma1 = c0, cos_sigma1 = c0;
  274. CT sin_sigma2 = c0, cos_sigma2 = c0;
  275. CT eps = c0, diff_omega12 = c0;
  276. // Bracketing range.
  277. CT sin_alpha1a = tiny, cos_alpha1a = c1;
  278. CT sin_alpha1b = tiny, cos_alpha1b = -c1;
  279. size_t iteration = 0;
  280. size_t max_iterations = 20 + std::numeric_limits<size_t>::digits + 10;
  281. for (bool tripn = false, tripb = false;
  282. iteration < max_iterations;
  283. ++iteration)
  284. {
  285. CT dv;
  286. CT v = lambda12(sin_beta1, cos_beta1, dn1,
  287. sin_beta2, cos_beta2, dn2,
  288. sin_alpha1, cos_alpha1,
  289. sin_lam12, cos_lam12,
  290. sin_alpha2, cos_alpha2,
  291. sigma12,
  292. sin_sigma1, cos_sigma1,
  293. sin_sigma2, cos_sigma2,
  294. eps, diff_omega12,
  295. iteration < max_iterations,
  296. dv, f, n, ep2, tiny, coeffs_C1);
  297. // Reversed test to allow escape with NaNs.
  298. if (tripb || !(std::abs(v) >= (tripn ? c8 : c1) * tol0))
  299. break;
  300. // Update bracketing values.
  301. if (v > c0 && (iteration > max_iterations ||
  302. cos_alpha1 / sin_alpha1 > cos_alpha1b / sin_alpha1b))
  303. {
  304. sin_alpha1b = sin_alpha1;
  305. cos_alpha1b = cos_alpha1;
  306. }
  307. else if (v < c0 && (iteration > max_iterations ||
  308. cos_alpha1 / sin_alpha1 < cos_alpha1a / sin_alpha1a))
  309. {
  310. sin_alpha1a = sin_alpha1;
  311. cos_alpha1a = cos_alpha1;
  312. }
  313. if (iteration < max_iterations && dv > c0)
  314. {
  315. CT diff_alpha1 = -v / dv;
  316. CT sin_diff_alpha1 = sin(diff_alpha1);
  317. CT cos_diff_alpha1 = cos(diff_alpha1);
  318. CT nsin_alpha1 = sin_alpha1 * cos_diff_alpha1 +
  319. cos_alpha1 * sin_diff_alpha1;
  320. if (nsin_alpha1 > c0 && std::abs(diff_alpha1) < pi)
  321. {
  322. cos_alpha1 = cos_alpha1 * cos_diff_alpha1 - sin_alpha1 * sin_diff_alpha1;
  323. sin_alpha1 = nsin_alpha1;
  324. math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
  325. // In some regimes we don't get quadratic convergence because
  326. // slope -> 0. So use convergence conditions based on epsilon
  327. // instead of sqrt(epsilon).
  328. tripn = std::abs(v) <= c16 * tol0;
  329. continue;
  330. }
  331. }
  332. // Either dv was not positive or updated value was outside legal
  333. // range. Use the midpoint of the bracket as the next estimate.
  334. // This mechanism is not needed for the WGS84 ellipsoid, but it does
  335. // catch problems with more eeccentric ellipsoids. Its efficacy is
  336. // such for the WGS84 test set with the starting guess set to alp1 =
  337. // 90deg:
  338. // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
  339. // WGS84 and random input: mean = 4.74, sd = 0.99
  340. sin_alpha1 = (sin_alpha1a + sin_alpha1b) / c2;
  341. cos_alpha1 = (cos_alpha1a + cos_alpha1b) / c2;
  342. math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
  343. tripn = false;
  344. tripb = (std::abs(sin_alpha1a - sin_alpha1) + (cos_alpha1a - cos_alpha1) < tol_bisection ||
  345. std::abs(sin_alpha1 - sin_alpha1b) + (cos_alpha1 - cos_alpha1b) < tol_bisection);
  346. }
  347. CT dummy;
  348. se::coeffs_C1<SeriesOrder, CT> const coeffs_C1_eps(eps);
  349. // Ensure that the reduced length and geodesic scale are computed in
  350. // a "canonical" way, with the I2 integral.
  351. meridian_length(eps, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
  352. sin_sigma2, cos_sigma2, dn2,
  353. cos_beta1, cos_beta2, s12x,
  354. m12x, dummy, result.geodesic_scale,
  355. M21, coeffs_C1_eps);
  356. m12x *= b;
  357. s12x *= b;
  358. }
  359. }
  360. if (swap_point < 0)
  361. {
  362. swap(sin_alpha1, sin_alpha2);
  363. swap(cos_alpha1, cos_alpha2);
  364. swap(result.geodesic_scale, M21);
  365. }
  366. sin_alpha1 *= swap_point * lon12_sign;
  367. cos_alpha1 *= swap_point * lat_sign;
  368. sin_alpha2 *= swap_point * lon12_sign;
  369. cos_alpha2 *= swap_point * lat_sign;
  370. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
  371. {
  372. result.reduced_length = m12x;
  373. }
  374. if (BOOST_GEOMETRY_CONDITION(CalcAzimuths))
  375. {
  376. if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
  377. {
  378. result.azimuth = atan2(sin_alpha1, cos_alpha1) * r2d;
  379. }
  380. if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
  381. {
  382. result.reverse_azimuth = atan2(sin_alpha2, cos_alpha2) * r2d;
  383. }
  384. }
  385. if (BOOST_GEOMETRY_CONDITION(EnableDistance))
  386. {
  387. result.distance = s12x;
  388. }
  389. return result;
  390. }
  391. template <typename CoeffsC1>
  392. static inline void meridian_length(CT const& epsilon, CT const& ep2, CT const& sigma12,
  393. CT const& sin_sigma1, CT const& cos_sigma1, CT const& dn1,
  394. CT const& sin_sigma2, CT const& cos_sigma2, CT const& dn2,
  395. CT const& cos_beta1, CT const& cos_beta2,
  396. CT& s12x, CT& m12x, CT& m0,
  397. CT& M12, CT& M21,
  398. CoeffsC1 const& coeffs_C1)
  399. {
  400. static CT const c1 = 1;
  401. CT A12x = 0, J12 = 0;
  402. CT expansion_A1, expansion_A2;
  403. // Evaluate the coefficients for C2.
  404. se::coeffs_C2<SeriesOrder, CT> coeffs_C2(epsilon);
  405. if (BOOST_GEOMETRY_CONDITION(EnableDistance) ||
  406. BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  407. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  408. {
  409. // Find the coefficients for A1 by computing the
  410. // series expansion using Horner scehme.
  411. expansion_A1 = se::evaluate_A1<SeriesOrder>(epsilon);
  412. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  413. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  414. {
  415. // Find the coefficients for A2 by computing the
  416. // series expansion using Horner scehme.
  417. expansion_A2 = se::evaluate_A2<SeriesOrder>(epsilon);
  418. A12x = expansion_A1 - expansion_A2;
  419. expansion_A2 += c1;
  420. }
  421. expansion_A1 += c1;
  422. }
  423. if (BOOST_GEOMETRY_CONDITION(EnableDistance))
  424. {
  425. CT B1 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C1)
  426. - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C1);
  427. s12x = expansion_A1 * (sigma12 + B1);
  428. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  429. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  430. {
  431. CT B2 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2)
  432. - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2);
  433. J12 = A12x * sigma12 + (expansion_A1 * B1 - expansion_A2 * B2);
  434. }
  435. }
  436. else if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
  437. BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  438. {
  439. for (size_t i = 1; i <= SeriesOrder; ++i)
  440. {
  441. coeffs_C2[i] = expansion_A1 * coeffs_C1[i] -
  442. expansion_A2 * coeffs_C2[i];
  443. }
  444. J12 = A12x * sigma12 +
  445. (se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2)
  446. - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2));
  447. }
  448. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
  449. {
  450. m0 = A12x;
  451. m12x = dn2 * (cos_sigma1 * sin_sigma2) -
  452. dn1 * (sin_sigma1 * cos_sigma2) -
  453. cos_sigma1 * cos_sigma2 * J12;
  454. }
  455. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  456. {
  457. CT cos_sigma12 = cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2;
  458. CT t = ep2 * (cos_beta1 - cos_beta2) *
  459. (cos_beta1 + cos_beta2) / (dn1 + dn2);
  460. M12 = cos_sigma12 + (t * sin_sigma2 - cos_sigma2 * J12) * sin_sigma1 / dn1;
  461. M21 = cos_sigma12 - (t * sin_sigma1 - cos_sigma1 * J12) * sin_sigma2 / dn2;
  462. }
  463. }
  464. /*
  465. Return a starting point for Newton's method in sin_alpha1 and
  466. cos_alpha1 (function value is -1). If Newton's method
  467. doesn't need to be used, return also sin_alpha2 and
  468. cos_alpha2 and function value is sig12.
  469. */
  470. template <typename CoeffsC1>
  471. static inline CT newton_start(CT const& sin_beta1, CT const& cos_beta1, CT const& dn1,
  472. CT const& sin_beta2, CT const& cos_beta2, CT dn2,
  473. CT const& lam12, CT const& sin_lam12, CT const& cos_lam12,
  474. CT& sin_alpha1, CT& cos_alpha1,
  475. CT& sin_alpha2, CT& cos_alpha2,
  476. CT& dnm, CoeffsC1 const& coeffs_C1, CT const& ep2,
  477. CT const& tol1, CT const& tol2, CT const& etol2, CT const& n, CT const& f)
  478. {
  479. static CT const c0 = 0;
  480. static CT const c0_01 = 0.01;
  481. static CT const c0_1 = 0.1;
  482. static CT const c0_5 = 0.5;
  483. static CT const c1 = 1;
  484. static CT const c2 = 2;
  485. static CT const c6 = 6;
  486. static CT const c1000 = 1000;
  487. static CT const pi = math::pi<CT>();
  488. CT const one_minus_f = c1 - f;
  489. CT const x_thresh = c1000 * tol2;
  490. // Return a starting point for Newton's method in sin_alpha1
  491. // and cos_alpha1 (function value is -1). If Newton's method
  492. // doesn't need to be used, return also sin_alpha2 and
  493. // cos_alpha2 and function value is sig12.
  494. CT sig12 = -c1;
  495. // bet12 = bet2 - bet1 in [0, pi); beta12a = bet2 + bet1 in (-pi, 0]
  496. CT sin_beta12 = sin_beta2 * cos_beta1 - cos_beta2 * sin_beta1;
  497. CT cos_beta12 = cos_beta2 * cos_beta1 + sin_beta2 * sin_beta1;
  498. CT sin_beta12a = sin_beta2 * cos_beta1 + cos_beta2 * sin_beta1;
  499. bool shortline = cos_beta12 >= c0 && sin_beta12 < c0_5 &&
  500. cos_beta2 * lam12 < c0_5;
  501. CT sin_omega12, cos_omega12;
  502. if (shortline)
  503. {
  504. CT sin_beta_m2 = math::sqr(sin_beta1 + sin_beta2);
  505. sin_beta_m2 /= sin_beta_m2 + math::sqr(cos_beta1 + cos_beta2);
  506. dnm = math::sqrt(c1 + ep2 * sin_beta_m2);
  507. CT omega12 = lam12 / (one_minus_f * dnm);
  508. sin_omega12 = sin(omega12);
  509. cos_omega12 = cos(omega12);
  510. }
  511. else
  512. {
  513. sin_omega12 = sin_lam12;
  514. cos_omega12 = cos_lam12;
  515. }
  516. sin_alpha1 = cos_beta2 * sin_omega12;
  517. cos_alpha1 = cos_omega12 >= c0 ?
  518. sin_beta12 + cos_beta2 * sin_beta1 * math::sqr(sin_omega12) / (c1 + cos_omega12) :
  519. sin_beta12a - cos_beta2 * sin_beta1 * math::sqr(sin_omega12) / (c1 - cos_omega12);
  520. CT sin_sigma12 = boost::math::hypot(sin_alpha1, cos_alpha1);
  521. CT cos_sigma12 = sin_beta1 * sin_beta2 + cos_beta1 * cos_beta2 * cos_omega12;
  522. if (shortline && sin_sigma12 < etol2)
  523. {
  524. sin_alpha2 = cos_beta1 * sin_omega12;
  525. cos_alpha2 = sin_beta12 - cos_beta1 * sin_beta2 *
  526. (cos_omega12 >= c0 ? math::sqr(sin_omega12) /
  527. (c1 + cos_omega12) : c1 - cos_omega12);
  528. math::normalize_unit_vector<CT>(sin_alpha2, cos_alpha2);
  529. // Set return value.
  530. sig12 = atan2(sin_sigma12, cos_sigma12);
  531. }
  532. // Skip astroid calculation if too eccentric.
  533. else if (std::abs(n) > c0_1 ||
  534. cos_sigma12 >= c0 ||
  535. sin_sigma12 >= c6 * std::abs(n) * pi *
  536. math::sqr(cos_beta1))
  537. {
  538. // Nothing to do, zeroth order spherical approximation will do.
  539. }
  540. else
  541. {
  542. // Scale lam12 and bet2 to x, y coordinate system where antipodal
  543. // point is at origin and singular point is at y = 0, x = -1.
  544. CT lambda_scale, beta_scale;
  545. CT y;
  546. volatile CT x;
  547. CT lam12x = atan2(-sin_lam12, -cos_lam12);
  548. if (f >= c0)
  549. {
  550. CT k2 = math::sqr(sin_beta1) * ep2;
  551. CT eps = k2 / (c2 * (c1 + sqrt(c1 + k2)) + k2);
  552. se::coeffs_A3<SeriesOrder, CT> const coeffs_A3(n);
  553. CT const A3 = math::horner_evaluate(eps, coeffs_A3.begin(), coeffs_A3.end());
  554. lambda_scale = f * cos_beta1 * A3 * pi;
  555. beta_scale = lambda_scale * cos_beta1;
  556. x = lam12x / lambda_scale;
  557. y = sin_beta12a / beta_scale;
  558. }
  559. else
  560. {
  561. CT cos_beta12a = cos_beta2 * cos_beta1 - sin_beta2 * sin_beta1;
  562. CT beta12a = atan2(sin_beta12a, cos_beta12a);
  563. CT m12b, m0, 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 boost::geometry::formula
  770. #endif // BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP