3 // Copyright (c) 2018 Adeel Ahmad, Islamabad, Pakistan.
5 // Contributed and/or modified by Adeel Ahmad,
6 // as part of Google Summer of Code 2018 program.
8 // This file was modified by Oracle on 2018.
9 // Modifications copyright (c) 2018 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_DIRECT_HPP
30 #define BOOST_GEOMETRY_FORMULAS_KARNEY_DIRECT_HPP
33 #include <boost/array.hpp>
35 #include <boost/math/constants/constants.hpp>
36 #include <boost/math/special_functions/hypot.hpp>
38 #include <boost/geometry/formulas/flattening.hpp>
39 #include <boost/geometry/formulas/result_direct.hpp>
41 #include <boost/geometry/util/condition.hpp>
42 #include <boost/geometry/util/math.hpp>
43 #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
44 #include <boost/geometry/util/series_expansion.hpp>
47 namespace boost { namespace geometry { namespace formula
50 namespace se = series_expansion;
53 \brief The solution of the direct problem of geodesics on latlong coordinates,
56 - Charles F.F Karney, Algorithms for geodesics, 2011
57 https://arxiv.org/pdf/1109.4448.pdf
61 bool EnableCoordinates = true,
62 bool EnableReverseAzimuth = false,
63 bool EnableReducedLength = false,
64 bool EnableGeodesicScale = false,
65 size_t SeriesOrder = 8
69 static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
70 static const bool CalcCoordinates = EnableCoordinates || CalcQuantities;
71 static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcCoordinates || CalcQuantities;
74 typedef result_direct<CT> result_type;
76 template <typename T, typename Dist, typename Azi, typename Spheroid>
77 static inline result_type apply(T const& lo1,
81 Spheroid const& spheroid)
88 Azi azi12 = azimuth12;
89 math::normalize_azimuth<degree, Azi>(azi12);
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;
100 CT const n = f / two_minus_f;
101 CT const e2 = f * two_minus_f;
102 CT const ep2 = e2 / math::sqr(one_minus_f);
104 CT sin_alpha1, cos_alpha1;
105 math::sin_cos_degrees<CT>(azi12, sin_alpha1, cos_alpha1);
107 // Find the reduced latitude.
108 CT sin_beta1, cos_beta1;
109 math::sin_cos_degrees<CT>(lat1, sin_beta1, cos_beta1);
110 sin_beta1 *= one_minus_f;
112 math::normalize_unit_vector<CT>(sin_beta1, cos_beta1);
114 cos_beta1 = (std::max)(c0, cos_beta1);
116 // Obtain alpha 0 by solving the spherical triangle.
117 CT const sin_alpha0 = sin_alpha1 * cos_beta1;
118 CT const cos_alpha0 = boost::math::hypot(cos_alpha1, sin_alpha1 * sin_beta1);
120 CT const k2 = math::sqr(cos_alpha0) * ep2;
122 CT const epsilon = k2 / (c2 * (c1 + math::sqrt(c1 + k2)) + k2);
124 // Find the coefficients for A1 by computing the
125 // series expansion using Horner scehme.
126 CT const expansion_A1 = se::evaluate_A1<SeriesOrder>(epsilon);
128 // Index zero element of coeffs_C1 is unused.
129 se::coeffs_C1<SeriesOrder, CT> const coeffs_C1(epsilon);
131 // Tau is an integration variable.
132 CT const tau12 = distance / (b * (c1 + expansion_A1));
134 CT const sin_tau12 = sin(tau12);
135 CT const cos_tau12 = cos(tau12);
137 CT sin_sigma1 = sin_beta1;
138 CT sin_omega1 = sin_alpha0 * sin_beta1;
140 CT cos_sigma1, cos_omega1;
141 cos_sigma1 = cos_omega1 = sin_beta1 != c0 || cos_alpha1 != c0 ? cos_beta1 * cos_alpha1 : c1;
142 math::normalize_unit_vector<CT>(sin_sigma1, cos_sigma1);
144 CT const B11 = se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C1);
145 CT const sin_B11 = sin(B11);
146 CT const cos_B11 = cos(B11);
148 CT const sin_tau1 = sin_sigma1 * cos_B11 + cos_sigma1 * sin_B11;
149 CT const cos_tau1 = cos_sigma1 * cos_B11 - sin_sigma1 * sin_B11;
151 // Index zero element of coeffs_C1p is unused.
152 se::coeffs_C1p<SeriesOrder, CT> const coeffs_C1p(epsilon);
154 CT const B12 = - se::sin_cos_series
155 (sin_tau1 * cos_tau12 + cos_tau1 * sin_tau12,
156 cos_tau1 * cos_tau12 - sin_tau1 * sin_tau12,
159 CT const sigma12 = tau12 - (B12 - B11);
160 CT const sin_sigma12 = sin(sigma12);
161 CT const cos_sigma12 = cos(sigma12);
163 CT const sin_sigma2 = sin_sigma1 * cos_sigma12 + cos_sigma1 * sin_sigma12;
164 CT const cos_sigma2 = cos_sigma1 * cos_sigma12 - sin_sigma1 * sin_sigma12;
166 if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
168 CT const sin_alpha2 = sin_alpha0;
169 CT const cos_alpha2 = cos_alpha0 * cos_sigma2;
171 result.reverse_azimuth = atan2(sin_alpha2, cos_alpha2);
173 // Convert the angle to radians.
174 result.reverse_azimuth /= math::d2r<CT>();
177 if (BOOST_GEOMETRY_CONDITION(CalcCoordinates))
179 // Find the latitude at the second point.
180 CT const sin_beta2 = cos_alpha0 * sin_sigma2;
181 CT const cos_beta2 = boost::math::hypot(sin_alpha0, cos_alpha0 * cos_sigma2);
183 result.lat2 = atan2(sin_beta2, one_minus_f * cos_beta2);
185 // Convert the coordinate to radians.
186 result.lat2 /= math::d2r<CT>();
188 // Find the longitude at the second point.
189 CT const sin_omega2 = sin_alpha0 * sin_sigma2;
190 CT const cos_omega2 = cos_sigma2;
192 CT const omega12 = atan2(sin_omega2 * cos_omega1 - cos_omega2 * sin_omega1,
193 cos_omega2 * cos_omega1 + sin_omega2 * sin_omega1);
195 se::coeffs_A3<SeriesOrder, CT> const coeffs_A3(n);
197 CT const A3 = math::horner_evaluate(epsilon, coeffs_A3.begin(), coeffs_A3.end());
198 CT const A3c = -f * sin_alpha0 * A3;
200 se::coeffs_C3<SeriesOrder, CT> const coeffs_C3(n, epsilon);
202 CT const B31 = se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C3);
204 CT const lam12 = omega12 + A3c *
205 (sigma12 + (se::sin_cos_series
210 // Convert to radians to get the
211 // longitudinal difference.
212 CT lon12 = lam12 / math::d2r<CT>();
214 // Add the longitude at first point to the longitudinal
215 // difference and normalize the result.
216 math::normalize_longitude<degree, CT>(lon1);
217 math::normalize_longitude<degree, CT>(lon12);
219 result.lon2 = lon1 + lon12;
222 if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
224 // Evaluate the coefficients for C2.
225 // Index zero element of coeffs_C2 is unused.
226 se::coeffs_C2<SeriesOrder, CT> const coeffs_C2(epsilon);
228 CT const B21 = se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2);
229 CT const B22 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2);
231 // Find the coefficients for A2 by computing the
232 // series expansion using Horner scehme.
233 CT const expansion_A2 = se::evaluate_A2<SeriesOrder>(epsilon);
235 CT const AB1 = (c1 + expansion_A1) * (B12 - B11);
236 CT const AB2 = (c1 + expansion_A2) * (B22 - B21);
237 CT const J12 = (expansion_A1 - expansion_A2) * sigma12 + (AB1 - AB2);
239 CT const dn1 = math::sqrt(c1 + ep2 * math::sqr(sin_beta1));
240 CT const dn2 = math::sqrt(c1 + k2 * math::sqr(sin_sigma2));
242 // Find the reduced length.
243 result.reduced_length = b * ((dn2 * (cos_sigma1 * sin_sigma2) -
244 dn1 * (sin_sigma1 * cos_sigma2)) -
245 cos_sigma1 * cos_sigma2 * J12);
247 // Find the geodesic scale.
248 CT const t = k2 * (sin_sigma2 - sin_sigma1) *
249 (sin_sigma2 + sin_sigma1) / (dn1 + dn2);
251 result.geodesic_scale = cos_sigma12 +
252 (t * sin_sigma2 - cos_sigma2 * J12) *
260 }}} // namespace boost::geometry::formula
263 #endif // BOOST_GEOMETRY_FORMULAS_KARNEY_DIRECT_HPP