3 // Copyright (c) 2018 Adeel Ahmad, Islamabad, Pakistan.
5 // Contributed and/or modified by Adeel Ahmad, as part of Google Summer of Code 2018 program.
7 // This file was modified by Oracle on 2019.
8 // Modifications copyright (c) 2019 Oracle and/or its affiliates.
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Use, modification and distribution is subject to the Boost Software License,
13 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
14 // http://www.boost.org/LICENSE_1_0.txt)
16 // This file is converted from GeographicLib, https://geographiclib.sourceforge.io
17 // GeographicLib is originally written by Charles Karney.
19 // Author: Charles Karney (2008-2017)
21 // Last updated version of GeographicLib: 1.49
23 // Original copyright notice:
25 // Copyright (c) Charles Karney (2008-2017) <charles@karney.com> and licensed
26 // under the MIT/X11 License. For more information, see
27 // https://geographiclib.sourceforge.io
29 #ifndef BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP
30 #define BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP
33 #include <boost/math/constants/constants.hpp>
34 #include <boost/math/special_functions/hypot.hpp>
36 #include <boost/geometry/util/condition.hpp>
37 #include <boost/geometry/util/math.hpp>
38 #include <boost/geometry/util/series_expansion.hpp>
39 #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
41 #include <boost/geometry/formulas/flattening.hpp>
42 #include <boost/geometry/formulas/result_inverse.hpp>
45 namespace boost { namespace geometry { namespace math {
47 // TODO: Moved temporarily because of C++11 is used
50 \brief The exact difference of two angles reduced to (-180deg, 180deg].
53 inline T difference_angle(T const& x, T const& y, T& e)
55 T t, d = math::sum_error(std::remainder(-x, T(360)), std::remainder(y, T(360)), t);
57 normalize_azimuth<degree, T>(d);
59 // Here y - x = d + t (mod 360), exactly, where d is in (-180,180] and
60 // abs(t) <= eps (eps = 2^-45 for doubles). The only case where the
61 // addition of t takes the result outside the range (-180,180] is d = 180
62 // and t > 0. The case, d = -180 + eps, t = -eps, can't happen, since
63 // sum_error would have returned the exact result in such a case (i.e., given t = 0).
64 return math::sum_error(d == 180 && t > 0 ? -180 : d, t, e);
67 }}} // namespace boost::geometry::math
70 namespace boost { namespace geometry { namespace formula
73 namespace se = series_expansion;
76 \brief The solution of the inverse problem of geodesics on latlong coordinates,
79 - Charles F.F Karney, Algorithms for geodesics, 2011
80 https://arxiv.org/pdf/1109.4448.pdf
86 bool EnableReverseAzimuth = false,
87 bool EnableReducedLength = false,
88 bool EnableGeodesicScale = false,
89 size_t SeriesOrder = 8
93 static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
94 static const bool CalcAzimuths = EnableAzimuth || EnableReverseAzimuth || CalcQuantities;
95 static const bool CalcFwdAzimuth = EnableAzimuth || CalcQuantities;
96 static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
99 typedef result_inverse<CT> result_type;
101 template <typename T1, typename T2, typename Spheroid>
102 static inline result_type apply(T1 const& lo1,
106 Spheroid const& spheroid)
108 static CT const c0 = 0;
109 static CT const c0_001 = 0.001;
110 static CT const c0_1 = 0.1;
111 static CT const c1 = 1;
112 static CT const c2 = 2;
113 static CT const c3 = 3;
114 static CT const c8 = 8;
115 static CT const c16 = 16;
116 static CT const c90 = 90;
117 static CT const c180 = 180;
118 static CT const c200 = 200;
119 static CT const pi = math::pi<CT>();
120 static CT const d2r = math::d2r<CT>();
121 static CT const r2d = math::r2d<CT>();
131 CT const a = CT(get_radius<0>(spheroid));
132 CT const b = CT(get_radius<2>(spheroid));
133 CT const f = formula::flattening<CT>(spheroid);
134 CT const one_minus_f = c1 - f;
135 CT const two_minus_f = c2 - f;
137 CT const tol0 = std::numeric_limits<CT>::epsilon();
138 CT const tol1 = c200 * tol0;
139 CT const tol2 = sqrt(tol0);
141 // Check on bisection interval.
142 CT const tol_bisection = tol0 * tol2;
144 CT const etol2 = c0_1 * tol2 /
145 sqrt((std::max)(c0_001, std::abs(f)) * (std::min)(c1, c1 - f / c2) / c2);
147 CT tiny = std::sqrt((std::numeric_limits<CT>::min)());
149 CT const n = f / two_minus_f;
150 CT const e2 = f * two_minus_f;
151 CT const ep2 = e2 / math::sqr(one_minus_f);
153 // Compute the longitudinal difference.
155 CT lon12 = math::difference_angle(lon1, lon2, lon12_error);
157 int lon12_sign = lon12 >= 0 ? 1 : -1;
159 // Make points close to the meridian to lie on it.
160 lon12 = lon12_sign * lon12;
161 lon12_error = (c180 - lon12) - lon12_sign * lon12_error;
163 // Convert to radians.
164 CT lam12 = lon12 * d2r;
170 math::sin_cos_degrees(lon12_error, sin_lam12, cos_lam12);
175 math::sin_cos_degrees(lon12, sin_lam12, cos_lam12);
178 // Make points close to the equator to lie on it.
179 lat1 = math::round_angle(std::abs(lat1) > c90 ? c90 : lat1);
180 lat2 = math::round_angle(std::abs(lat2) > c90 ? c90 : lat2);
182 // Arrange points in a canonical form, as explained in
183 // paper, Algorithms for geodesics, Eq. (44):
187 // lat1 <= lat2 <= -lat1
188 int swap_point = std::abs(lat1) < std::abs(lat2) ? -1 : 1;
196 // Enforce lat1 to be <= 0.
197 int lat_sign = lat1 < 0 ? 1 : -1;
201 CT sin_beta1, cos_beta1;
202 math::sin_cos_degrees(lat1, sin_beta1, cos_beta1);
203 sin_beta1 *= one_minus_f;
205 math::normalize_unit_vector<CT>(sin_beta1, cos_beta1);
206 cos_beta1 = (std::max)(tiny, cos_beta1);
208 CT sin_beta2, cos_beta2;
209 math::sin_cos_degrees(lat2, sin_beta2, cos_beta2);
210 sin_beta2 *= one_minus_f;
212 math::normalize_unit_vector<CT>(sin_beta2, cos_beta2);
213 cos_beta2 = (std::max)(tiny, cos_beta2);
215 // If cos_beta1 < -sin_beta1, then cos_beta2 - cos_beta1 is a
216 // sensitive measure of the |beta1| - |beta2|. Alternatively,
217 // (cos_beta1 >= -sin_beta1), abs(sin_beta2) + sin_beta1 is
219 // Sometimes these quantities vanish and in that case we
220 // force beta2 = +/- bet1a exactly.
221 if (cos_beta1 < -sin_beta1)
223 if (cos_beta1 == cos_beta2)
225 sin_beta2 = sin_beta2 < 0 ? sin_beta1 : -sin_beta1;
230 if (std::abs(sin_beta2) == -sin_beta1)
232 cos_beta2 = cos_beta1;
236 CT const dn1 = sqrt(c1 + ep2 * math::sqr(sin_beta1));
237 CT const dn2 = sqrt(c1 + ep2 * math::sqr(sin_beta2));
242 // Index zero element of coeffs_C1 is unused.
243 se::coeffs_C1<SeriesOrder, CT> const coeffs_C1(n);
245 bool meridian = lat1 == -90 || sin_lam12 == 0;
247 CT cos_alpha1, sin_alpha1;
248 CT cos_alpha2, sin_alpha2;
252 // Endpoints lie on a single full meridian.
254 // Point to the target latitude.
255 cos_alpha1 = cos_lam12;
256 sin_alpha1 = sin_lam12;
258 // Heading north at the target.
262 CT sin_sigma1 = sin_beta1;
263 CT cos_sigma1 = cos_alpha1 * cos_beta1;
265 CT sin_sigma2 = sin_beta2;
266 CT cos_sigma2 = cos_alpha2 * cos_beta2;
268 CT sigma12 = std::atan2((std::max)(c0, cos_sigma1 * sin_sigma2 - sin_sigma1 * cos_sigma2),
269 cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2);
272 meridian_length(n, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
273 sin_sigma2, cos_sigma2, dn2,
274 cos_beta1, cos_beta2, s12x,
275 m12x, dummy, result.geodesic_scale,
278 if (sigma12 < c1 || m12x >= c0)
280 if (sigma12 < c3 * tiny)
282 sigma12 = m12x = s12x = c0;
290 // m12 < 0, i.e., prolate and too close to anti-podal.
297 if (!meridian && sin_beta1 == c0 &&
298 (f <= c0 || lon12_error >= f * c180))
300 // Points lie on the equator.
301 cos_alpha1 = cos_alpha2 = c0;
302 sin_alpha1 = sin_alpha2 = c1;
305 sigma12 = omega12 = lam12 / one_minus_f;
306 m12x = b * sin(sigma12);
308 if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
310 result.geodesic_scale = cos(sigma12);
315 // If point1 and point2 belong within a hemisphere bounded by a
316 // meridian and geodesic is neither meridional nor equatorial.
318 // Find the starting point for Newton's method.
320 sigma12 = newton_start(sin_beta1, cos_beta1, dn1,
321 sin_beta2, cos_beta2, dn2,
322 lam12, sin_lam12, cos_lam12,
323 sin_alpha1, cos_alpha1,
324 sin_alpha2, cos_alpha2,
331 // Short lines case (newton_start sets sin_alpha2, cos_alpha2, dnm).
332 s12x = sigma12 * b * dnm;
333 m12x = math::sqr(dnm) * b * sin(sigma12 / dnm);
334 if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
336 result.geodesic_scale = cos(sigma12 / dnm);
339 // Convert to radians.
340 omega12 = lam12 / (one_minus_f * dnm);
344 // Apply the Newton's method.
345 CT sin_sigma1 = c0, cos_sigma1 = c0;
346 CT sin_sigma2 = c0, cos_sigma2 = c0;
347 CT eps = c0, diff_omega12 = c0;
350 CT sin_alpha1a = tiny, cos_alpha1a = c1;
351 CT sin_alpha1b = tiny, cos_alpha1b = -c1;
353 size_t iteration = 0;
354 size_t max_iterations = 20 + std::numeric_limits<size_t>::digits + 10;
356 for (bool tripn = false, tripb = false;
357 iteration < max_iterations;
361 CT v = lambda12(sin_beta1, cos_beta1, dn1,
362 sin_beta2, cos_beta2, dn2,
363 sin_alpha1, cos_alpha1,
364 sin_lam12, cos_lam12,
365 sin_alpha2, cos_alpha2,
367 sin_sigma1, cos_sigma1,
368 sin_sigma2, cos_sigma2,
370 iteration < max_iterations,
371 dv, f, n, ep2, tiny, coeffs_C1);
373 // Reversed test to allow escape with NaNs.
374 if (tripb || !(std::abs(v) >= (tripn ? c8 : c1) * tol0))
377 // Update bracketing values.
378 if (v > c0 && (iteration > max_iterations ||
379 cos_alpha1 / sin_alpha1 > cos_alpha1b / sin_alpha1b))
381 sin_alpha1b = sin_alpha1;
382 cos_alpha1b = cos_alpha1;
384 else if (v < c0 && (iteration > max_iterations ||
385 cos_alpha1 / sin_alpha1 < cos_alpha1a / sin_alpha1a))
387 sin_alpha1a = sin_alpha1;
388 cos_alpha1a = cos_alpha1;
391 if (iteration < max_iterations && dv > c0)
393 CT diff_alpha1 = -v / dv;
395 CT sin_diff_alpha1 = sin(diff_alpha1);
396 CT cos_diff_alpha1 = cos(diff_alpha1);
398 CT nsin_alpha1 = sin_alpha1 * cos_diff_alpha1 +
399 cos_alpha1 * sin_diff_alpha1;
401 if (nsin_alpha1 > c0 && std::abs(diff_alpha1) < pi)
403 cos_alpha1 = cos_alpha1 * cos_diff_alpha1 - sin_alpha1 * sin_diff_alpha1;
404 sin_alpha1 = nsin_alpha1;
405 math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
407 // In some regimes we don't get quadratic convergence because
408 // slope -> 0. So use convergence conditions based on epsilon
409 // instead of sqrt(epsilon).
410 tripn = std::abs(v) <= c16 * tol0;
415 // Either dv was not positive or updated value was outside legal
416 // range. Use the midpoint of the bracket as the next estimate.
417 // This mechanism is not needed for the WGS84 ellipsoid, but it does
418 // catch problems with more eeccentric ellipsoids. Its efficacy is
419 // such for the WGS84 test set with the starting guess set to alp1 =
421 // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
422 // WGS84 and random input: mean = 4.74, sd = 0.99
423 sin_alpha1 = (sin_alpha1a + sin_alpha1b) / c2;
424 cos_alpha1 = (cos_alpha1a + cos_alpha1b) / c2;
425 math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
427 tripb = (std::abs(sin_alpha1a - sin_alpha1) + (cos_alpha1a - cos_alpha1) < tol_bisection ||
428 std::abs(sin_alpha1 - sin_alpha1b) + (cos_alpha1 - cos_alpha1b) < tol_bisection);
432 se::coeffs_C1<SeriesOrder, CT> const coeffs_C1_eps(eps);
433 // Ensure that the reduced length and geodesic scale are computed in
434 // a "canonical" way, with the I2 integral.
435 meridian_length(eps, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
436 sin_sigma2, cos_sigma2, dn2,
437 cos_beta1, cos_beta2, s12x,
438 m12x, dummy, result.geodesic_scale,
448 swap(sin_alpha1, sin_alpha2);
449 swap(cos_alpha1, cos_alpha2);
450 swap(result.geodesic_scale, M21);
453 sin_alpha1 *= swap_point * lon12_sign;
454 cos_alpha1 *= swap_point * lat_sign;
456 sin_alpha2 *= swap_point * lon12_sign;
457 cos_alpha2 *= swap_point * lat_sign;
459 if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
461 result.reduced_length = m12x;
464 if (BOOST_GEOMETRY_CONDITION(CalcAzimuths))
466 if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
468 result.azimuth = atan2(sin_alpha1, cos_alpha1) * r2d;
471 if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
473 result.reverse_azimuth = atan2(sin_alpha2, cos_alpha2) * r2d;
477 if (BOOST_GEOMETRY_CONDITION(EnableDistance))
479 result.distance = s12x;
485 template <typename CoeffsC1>
486 static inline void meridian_length(CT const& epsilon, CT const& ep2, CT const& sigma12,
487 CT const& sin_sigma1, CT const& cos_sigma1, CT const& dn1,
488 CT const& sin_sigma2, CT const& cos_sigma2, CT const& dn2,
489 CT const& cos_beta1, CT const& cos_beta2,
490 CT& s12x, CT& m12x, CT& m0,
492 CoeffsC1 const& coeffs_C1)
494 static CT const c1 = 1;
496 CT A12x = 0, J12 = 0;
497 CT expansion_A1, expansion_A2;
499 // Evaluate the coefficients for C2.
500 se::coeffs_C2<SeriesOrder, CT> coeffs_C2(epsilon);
502 if (BOOST_GEOMETRY_CONDITION(EnableDistance) ||
503 BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
504 BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
506 // Find the coefficients for A1 by computing the
507 // series expansion using Horner scehme.
508 expansion_A1 = se::evaluate_A1<SeriesOrder>(epsilon);
510 if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
511 BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
513 // Find the coefficients for A2 by computing the
514 // series expansion using Horner scehme.
515 expansion_A2 = se::evaluate_A2<SeriesOrder>(epsilon);
517 A12x = expansion_A1 - expansion_A2;
523 if (BOOST_GEOMETRY_CONDITION(EnableDistance))
525 CT B1 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C1)
526 - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C1);
528 s12x = expansion_A1 * (sigma12 + B1);
530 if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
531 BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
533 CT B2 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2)
534 - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2);
536 J12 = A12x * sigma12 + (expansion_A1 * B1 - expansion_A2 * B2);
539 else if (BOOST_GEOMETRY_CONDITION(EnableReducedLength) ||
540 BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
542 for (size_t i = 1; i <= SeriesOrder; ++i)
544 coeffs_C2[i] = expansion_A1 * coeffs_C1[i] -
545 expansion_A2 * coeffs_C2[i];
548 J12 = A12x * sigma12 +
549 (se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2)
550 - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2));
553 if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
557 m12x = dn2 * (cos_sigma1 * sin_sigma2) -
558 dn1 * (sin_sigma1 * cos_sigma2) -
559 cos_sigma1 * cos_sigma2 * J12;
562 if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
564 CT cos_sigma12 = cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2;
565 CT t = ep2 * (cos_beta1 - cos_beta2) *
566 (cos_beta1 + cos_beta2) / (dn1 + dn2);
568 M12 = cos_sigma12 + (t * sin_sigma2 - cos_sigma2 * J12) * sin_sigma1 / dn1;
569 M21 = cos_sigma12 - (t * sin_sigma1 - cos_sigma1 * J12) * sin_sigma2 / dn2;
574 Return a starting point for Newton's method in sin_alpha1 and
575 cos_alpha1 (function value is -1). If Newton's method
576 doesn't need to be used, return also sin_alpha2 and
577 cos_alpha2 and function value is sig12.
579 template <typename CoeffsC1>
580 static inline CT newton_start(CT const& sin_beta1, CT const& cos_beta1, CT const& dn1,
581 CT const& sin_beta2, CT const& cos_beta2, CT dn2,
582 CT const& lam12, CT const& sin_lam12, CT const& cos_lam12,
583 CT& sin_alpha1, CT& cos_alpha1,
584 CT& sin_alpha2, CT& cos_alpha2,
585 CT& dnm, CoeffsC1 const& coeffs_C1, CT const& ep2,
586 CT const& tol1, CT const& tol2, CT const& etol2, CT const& n, CT const& f)
588 static CT const c0 = 0;
589 static CT const c0_01 = 0.01;
590 static CT const c0_1 = 0.1;
591 static CT const c0_5 = 0.5;
592 static CT const c1 = 1;
593 static CT const c2 = 2;
594 static CT const c6 = 6;
595 static CT const c1000 = 1000;
596 static CT const pi = math::pi<CT>();
598 CT const one_minus_f = c1 - f;
599 CT const x_thresh = c1000 * tol2;
601 // Return a starting point for Newton's method in sin_alpha1
602 // and cos_alpha1 (function value is -1). If Newton's method
603 // doesn't need to be used, return also sin_alpha2 and
604 // cos_alpha2 and function value is sig12.
607 // bet12 = bet2 - bet1 in [0, pi); beta12a = bet2 + bet1 in (-pi, 0]
608 CT sin_beta12 = sin_beta2 * cos_beta1 - cos_beta2 * sin_beta1;
609 CT cos_beta12 = cos_beta2 * cos_beta1 + sin_beta2 * sin_beta1;
611 CT sin_beta12a = sin_beta2 * cos_beta1 + cos_beta2 * sin_beta1;
613 bool shortline = cos_beta12 >= c0 && sin_beta12 < c0_5 &&
614 cos_beta2 * lam12 < c0_5;
616 CT sin_omega12, cos_omega12;
620 CT sin_beta_m2 = math::sqr(sin_beta1 + sin_beta2);
622 sin_beta_m2 /= sin_beta_m2 + math::sqr(cos_beta1 + cos_beta2);
623 dnm = math::sqrt(c1 + ep2 * sin_beta_m2);
625 CT omega12 = lam12 / (one_minus_f * dnm);
627 sin_omega12 = sin(omega12);
628 cos_omega12 = cos(omega12);
632 sin_omega12 = sin_lam12;
633 cos_omega12 = cos_lam12;
636 sin_alpha1 = cos_beta2 * sin_omega12;
637 cos_alpha1 = cos_omega12 >= c0 ?
638 sin_beta12 + cos_beta2 * sin_beta1 * math::sqr(sin_omega12) / (c1 + cos_omega12) :
639 sin_beta12a - cos_beta2 * sin_beta1 * math::sqr(sin_omega12) / (c1 - cos_omega12);
641 CT sin_sigma12 = boost::math::hypot(sin_alpha1, cos_alpha1);
642 CT cos_sigma12 = sin_beta1 * sin_beta2 + cos_beta1 * cos_beta2 * cos_omega12;
644 if (shortline && sin_sigma12 < etol2)
646 sin_alpha2 = cos_beta1 * sin_omega12;
647 cos_alpha2 = sin_beta12 - cos_beta1 * sin_beta2 *
648 (cos_omega12 >= c0 ? math::sqr(sin_omega12) /
649 (c1 + cos_omega12) : c1 - cos_omega12);
651 math::normalize_unit_vector<CT>(sin_alpha2, cos_alpha2);
653 sig12 = atan2(sin_sigma12, cos_sigma12);
655 // Skip astroid calculation if too eccentric.
656 else if (std::abs(n) > c0_1 ||
658 sin_sigma12 >= c6 * std::abs(n) * pi *
659 math::sqr(cos_beta1))
661 // Nothing to do, zeroth order spherical approximation will do.
665 // Scale lam12 and bet2 to x, y coordinate system where antipodal
666 // point is at origin and singular point is at y = 0, x = -1.
667 CT lambda_scale, beta_scale;
672 CT lam12x = atan2(-sin_lam12, -cos_lam12);
675 CT k2 = math::sqr(sin_beta1) * ep2;
676 CT eps = k2 / (c2 * (c1 + sqrt(c1 + k2)) + k2);
678 se::coeffs_A3<SeriesOrder, CT> const coeffs_A3(n);
680 CT const A3 = math::horner_evaluate(eps, coeffs_A3.begin(), coeffs_A3.end());
682 lambda_scale = f * cos_beta1 * A3 * pi;
683 beta_scale = lambda_scale * cos_beta1;
685 x = lam12x / lambda_scale;
686 y = sin_beta12a / beta_scale;
690 CT cos_beta12a = cos_beta2 * cos_beta1 - sin_beta2 * sin_beta1;
691 CT beta12a = atan2(sin_beta12a, cos_beta12a);
694 meridian_length(n, ep2, pi + beta12a,
695 sin_beta1, -cos_beta1, dn1,
696 sin_beta2, cos_beta2, dn2,
697 cos_beta1, cos_beta2, dummy,
698 m12b, m0, dummy, dummy, coeffs_C1);
700 x = -c1 + m12b / (cos_beta1 * cos_beta2 * m0 * pi);
701 beta_scale = x < -c0_01
703 : -f * math::sqr(cos_beta1) * pi;
704 lambda_scale = beta_scale / cos_beta1;
706 y = lam12x / lambda_scale;
709 if (y > -tol1 && x > -c1 - x_thresh)
714 sin_alpha1 = (std::min)(c1, -CT(x));
715 cos_alpha1 = - math::sqrt(c1 - math::sqr(sin_alpha1));
719 cos_alpha1 = (std::max)(CT(x > -tol1 ? c0 : -c1), CT(x));
720 sin_alpha1 = math::sqrt(c1 - math::sqr(cos_alpha1));
725 // Solve the astroid problem.
726 CT k = astroid(CT(x), y);
728 CT omega12a = lambda_scale * (f >= c0 ? -x * k /
729 (c1 + k) : -y * (c1 + k) / k);
731 sin_omega12 = sin(omega12a);
732 cos_omega12 = -cos(omega12a);
734 // Update spherical estimate of alpha1 using omgega12 instead of lam12.
735 sin_alpha1 = cos_beta2 * sin_omega12;
736 cos_alpha1 = sin_beta12a - cos_beta2 * sin_beta1 *
737 math::sqr(sin_omega12) / (c1 - cos_omega12);
741 // Sanity check on starting guess. Backwards check allows NaN through.
742 if (!(sin_alpha1 <= c0))
744 math::normalize_unit_vector<CT>(sin_alpha1, cos_alpha1);
756 Solve the astroid problem using the equation:
757 κ4 + 2κ3 + (1 − x2 − y 2 )κ2 − 2y 2 κ − y 2 = 0.
759 For details, please refer to Eq. (65) in,
760 Geodesics on an ellipsoid of revolution, Charles F.F Karney,
761 https://arxiv.org/abs/1102.1215
763 static inline CT astroid(CT const& x, CT const& y)
765 static CT const c0 = 0;
766 static CT const c1 = 1;
767 static CT const c2 = 2;
768 static CT const c3 = 3;
769 static CT const c4 = 4;
770 static CT const c6 = 6;
776 CT r = (p + q - c1) / c6;
778 if (!(q == c0 && r <= c0))
780 // Avoid possible division by zero when r = 0 by multiplying
781 // equations for s and t by r^3 and r, respectively.
783 CT r2 = math::sqr(r);
786 // The discriminant of the quadratic equation for T3. This is
787 // zero on the evolute curve p^(1/3)+q^(1/3) = 1.
788 CT discriminant = S * (S + c2 * r3);
792 if (discriminant >= c0)
796 // Pick the sign on the sqrt to maximize abs(T3). This minimizes
797 // loss of precision due to cancellation. The result is unchanged
798 // because of the way the T is used in definition of u.
799 T3 += T3 < c0 ? -std::sqrt(discriminant) : std::sqrt(discriminant);
801 CT T = std::cbrt(T3);
803 // T can be zero; but then r2 / T -> 0.
804 u += T + (T != c0 ? r2 / T : c0);
808 CT ang = std::atan2(std::sqrt(-discriminant), -(S + r3));
810 // There are three possible cube roots. We choose the root which avoids
811 // cancellation. Note that discriminant < 0 implies that r < 0.
812 u += c2 * r * cos(ang / c3);
815 CT v = std::sqrt(math::sqr(u) + q);
817 // Avoid loss of accuracy when u < 0.
818 CT uv = u < c0 ? q / (v - u) : u + v;
819 CT w = (uv - q) / (c2 * v);
821 // Rearrange expression for k to avoid loss of accuracy due to
822 // subtraction. Division by 0 not possible because uv > 0, w >= 0.
823 k = uv / (std::sqrt(uv + math::sqr(w)) + w);
825 else // q == 0 && r <= 0
827 // y = 0 with |x| <= 1. Handle this case directly.
828 // For y small, positive root is k = abs(y)/sqrt(1-x^2).
834 template <typename CoeffsC1>
835 static inline CT lambda12(CT const& sin_beta1, CT const& cos_beta1, CT const& dn1,
836 CT const& sin_beta2, CT const& cos_beta2, CT const& dn2,
837 CT const& sin_alpha1, CT cos_alpha1,
838 CT const& sin_lam120, CT const& cos_lam120,
839 CT& sin_alpha2, CT& cos_alpha2,
841 CT& sin_sigma1, CT& cos_sigma1,
842 CT& sin_sigma2, CT& cos_sigma2,
843 CT& eps, CT& diff_omega12,
844 bool diffp, CT& diff_lam12,
845 CT const& f, CT const& n, CT const& ep2, CT const& tiny,
846 CoeffsC1 const& coeffs_C1)
848 static CT const c0 = 0;
849 static CT const c1 = 1;
850 static CT const c2 = 2;
852 CT const one_minus_f = c1 - f;
854 if (sin_beta1 == c0 && cos_alpha1 == c0)
856 // Break degeneracy of equatorial line.
861 CT sin_alpha0 = sin_alpha1 * cos_beta1;
862 CT cos_alpha0 = boost::math::hypot(cos_alpha1, sin_alpha1 * sin_beta1);
864 CT sin_omega1, cos_omega1;
865 CT sin_omega2, cos_omega2;
866 CT sin_omega12, cos_omega12;
870 sin_sigma1 = sin_beta1;
871 sin_omega1 = sin_alpha0 * sin_beta1;
873 cos_sigma1 = cos_omega1 = cos_alpha1 * cos_beta1;
875 math::normalize_unit_vector<CT>(sin_sigma1, cos_sigma1);
877 // Enforce symmetries in the case abs(beta2) = -beta1.
878 // Otherwise, this can yield singularities in the Newton iteration.
880 // sin(alpha2) * cos(beta2) = sin(alpha0).
881 sin_alpha2 = cos_beta2 != cos_beta1 ?
882 sin_alpha0 / cos_beta2 : sin_alpha1;
884 cos_alpha2 = cos_beta2 != cos_beta1 || std::abs(sin_beta2) != -sin_beta1 ?
885 sqrt(math::sqr(cos_alpha1 * cos_beta1) +
886 (cos_beta1 < -sin_beta1 ?
887 (cos_beta2 - cos_beta1) * (cos_beta1 + cos_beta2) :
888 (sin_beta1 - sin_beta2) * (sin_beta1 + sin_beta2))) / cos_beta2 :
889 std::abs(cos_alpha1);
891 sin_sigma2 = sin_beta2;
892 sin_omega2 = sin_alpha0 * sin_beta2;
894 cos_sigma2 = cos_omega2 =
895 (cos_alpha2 * cos_beta2);
897 // Break degeneracy of equatorial line.
898 math::normalize_unit_vector<CT>(sin_sigma2, cos_sigma2);
901 // sig12 = sig2 - sig1, limit to [0, pi].
902 sigma12 = atan2((std::max)(c0, cos_sigma1 * sin_sigma2 - sin_sigma1 * cos_sigma2),
903 cos_sigma1 * cos_sigma2 + sin_sigma1 * sin_sigma2);
905 // omg12 = omg2 - omg1, limit to [0, pi].
906 sin_omega12 = (std::max)(c0, cos_omega1 * sin_omega2 - sin_omega1 * cos_omega2);
907 cos_omega12 = cos_omega1 * cos_omega2 + sin_omega1 * sin_omega2;
909 // eta = omg12 - lam120.
910 CT eta = atan2(sin_omega12 * cos_lam120 - cos_omega12 * sin_lam120,
911 cos_omega12 * cos_lam120 + sin_omega12 * sin_lam120);
914 CT k2 = math::sqr(cos_alpha0) * ep2;
916 eps = k2 / (c2 * (c1 + std::sqrt(c1 + k2)) + k2);
918 se::coeffs_C3<SeriesOrder, CT> const coeffs_C3(n, eps);
920 B312 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C3)
921 - se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C3);
923 se::coeffs_A3<SeriesOrder, CT> const coeffs_A3(n);
925 CT const A3 = math::horner_evaluate(eps, coeffs_A3.begin(), coeffs_A3.end());
927 diff_omega12 = -f * A3 * sin_alpha0 * (sigma12 + B312);
928 lam12 = eta + diff_omega12;
932 if (cos_alpha2 == c0)
934 diff_lam12 = - c2 * one_minus_f * dn1 / sin_beta1;
939 meridian_length(eps, ep2, sigma12, sin_sigma1, cos_sigma1, dn1,
940 sin_sigma2, cos_sigma2, dn2,
941 cos_beta1, cos_beta2, dummy,
942 diff_lam12, dummy, dummy,
945 diff_lam12 *= one_minus_f / (cos_alpha2 * cos_beta2);
953 }}} // namespace boost::geometry::formula
956 #endif // BOOST_GEOMETRY_FORMULAS_KARNEY_INVERSE_HPP