3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2014, 2016, 2017.
6 // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
15 #define BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
18 #include <boost/math/constants/constants.hpp>
20 #include <boost/geometry/core/radius.hpp>
21 #include <boost/geometry/core/srs.hpp>
23 #include <boost/geometry/util/condition.hpp>
24 #include <boost/geometry/util/math.hpp>
26 #include <boost/geometry/formulas/differential_quantities.hpp>
27 #include <boost/geometry/formulas/flattening.hpp>
28 #include <boost/geometry/formulas/result_inverse.hpp>
31 #ifndef BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS
32 #define BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS 1000
36 namespace boost { namespace geometry { namespace formula
40 \brief The solution of the inverse problem of geodesics on latlong coordinates, after Vincenty, 1975
42 - http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
43 - http://www.icsm.gov.au/gda/gda-v_2.4.pdf
44 \author Adapted from various implementations to get it close to the original document
45 - http://www.movable-type.co.uk/scripts/LatLongVincenty.html
46 - http://exogen.case.edu/projects/geopy/source/geopy.distance.html
47 - http://futureboy.homeip.net/fsp/colorize.fsp?fileName=navigation.frink
54 bool EnableReverseAzimuth = false,
55 bool EnableReducedLength = false,
56 bool EnableGeodesicScale = false
58 struct vincenty_inverse
60 static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
61 static const bool CalcAzimuths = EnableAzimuth || EnableReverseAzimuth || CalcQuantities;
62 static const bool CalcFwdAzimuth = EnableAzimuth || CalcQuantities;
63 static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
66 typedef result_inverse<CT> result_type;
68 template <typename T1, typename T2, typename Spheroid>
69 static inline result_type apply(T1 const& lon1,
73 Spheroid const& spheroid)
77 if (math::equals(lat1, lat2) && math::equals(lon1, lon2))
87 CT const c_e_12 = CT(1e-12);
89 CT const pi = geometry::math::pi<CT>();
90 CT const two_pi = c2 * pi;
92 // lambda: difference in longitude on an auxiliary sphere
96 if (L < -pi) L += two_pi;
97 if (L > pi) L -= two_pi;
99 CT const radius_a = CT(get_radius<0>(spheroid));
100 CT const radius_b = CT(get_radius<2>(spheroid));
101 CT const f = formula::flattening<CT>(spheroid);
103 // U: reduced latitude, defined by tan U = (1-f) tan phi
104 CT const one_min_f = c1 - f;
105 CT const tan_U1 = one_min_f * tan(lat1); // above (1)
106 CT const tan_U2 = one_min_f * tan(lat2); // above (1)
108 // calculate sin U and cos U using trigonometric identities
109 CT const temp_den_U1 = math::sqrt(c1 + math::sqr(tan_U1));
110 CT const temp_den_U2 = math::sqrt(c1 + math::sqr(tan_U2));
111 // cos = 1 / sqrt(1 + tan^2)
112 CT const cos_U1 = c1 / temp_den_U1;
113 CT const cos_U2 = c1 / temp_den_U2;
114 // sin = tan / sqrt(1 + tan^2)
116 CT const sin_U1 = tan_U1 * cos_U1;
117 CT const sin_U2 = tan_U2 * cos_U2;
119 // calculate sin U and cos U directly
120 //CT const U1 = atan(tan_U1);
121 //CT const U2 = atan(tan_U2);
124 //sin_U1 = tan_U1 * cos_U1; // sin(U1);
125 //sin_U2 = tan_U2 * cos_U2; // sin(U2);
137 int counter = 0; // robustness
141 previous_lambda = lambda; // (13)
142 sin_lambda = sin(lambda);
143 cos_lambda = cos(lambda);
144 sin_sigma = math::sqrt(math::sqr(cos_U2 * sin_lambda) + math::sqr(cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda)); // (14)
145 CT cos_sigma = sin_U1 * sin_U2 + cos_U1 * cos_U2 * cos_lambda; // (15)
146 sin_alpha = cos_U1 * cos_U2 * sin_lambda / sin_sigma; // (17)
147 cos2_alpha = c1 - math::sqr(sin_alpha);
148 cos_2sigma_m = math::equals(cos2_alpha, 0) ? 0 : cos_sigma - c2 * sin_U1 * sin_U2 / cos2_alpha; // (18)
149 cos2_2sigma_m = math::sqr(cos_2sigma_m);
151 CT C = f/c16 * cos2_alpha * (c4 + f * (c4 - c3 * cos2_alpha)); // (10)
152 sigma = atan2(sin_sigma, cos_sigma); // (16)
153 lambda = L + (c1 - C) * f * sin_alpha *
154 (sigma + C * sin_sigma * (cos_2sigma_m + C * cos_sigma * (-c1 + c2 * cos2_2sigma_m))); // (11)
156 ++counter; // robustness
158 } while ( geometry::math::abs(previous_lambda - lambda) > c_e_12
159 && geometry::math::abs(lambda) < pi
160 && counter < BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS ); // robustness
162 if ( BOOST_GEOMETRY_CONDITION(EnableDistance) )
164 // Oops getting hard here
165 // (again, problem is that ttmath cannot divide by doubles, which is OK)
178 CT const c1024 = 1024;
179 CT const c4096 = 4096;
180 CT const c16384 = 16384;
182 //CT sqr_u = cos2_alpha * (math::sqr(radius_a) - math::sqr(radius_b)) / math::sqr(radius_b); // above (1)
183 CT sqr_u = cos2_alpha * ( math::sqr(radius_a / radius_b) - c1 ); // above (1)
185 CT A = c1 + sqr_u/c16384 * (c4096 + sqr_u * (-c768 + sqr_u * (c320 - c175 * sqr_u))); // (3)
186 CT B = sqr_u/c1024 * (c256 + sqr_u * ( -c128 + sqr_u * (c74 - c47 * sqr_u))); // (4)
187 CT const cos_sigma = cos(sigma);
188 CT const sin2_sigma = math::sqr(sin_sigma);
189 CT delta_sigma = B * sin_sigma * (cos_2sigma_m + (B/c4) * (cos_sigma* (-c1 + c2 * cos2_2sigma_m)
190 - (B/c6) * cos_2sigma_m * (-c3 + c4 * sin2_sigma) * (-c3 + c4 * cos2_2sigma_m))); // (6)
192 result.distance = radius_b * A * (sigma - delta_sigma); // (19)
195 if ( BOOST_GEOMETRY_CONDITION(CalcAzimuths) )
197 if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
199 result.azimuth = atan2(cos_U2 * sin_lambda, cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda); // (20)
202 if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
204 result.reverse_azimuth = atan2(cos_U1 * sin_lambda, -sin_U1 * cos_U2 + cos_U1 * sin_U2 * cos_lambda); // (21)
208 if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
210 typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
211 quantities::apply(lon1, lat1, lon2, lat2,
212 result.azimuth, result.reverse_azimuth,
214 result.reduced_length, result.geodesic_scale);
221 }}} // namespace boost::geometry::formula
224 #endif // BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP