1 // Boost.Geometry
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2018 Adam Wulkiewicz, Lodz, Poland.
5 
6 // This file was modified by Oracle on 2014, 2016, 2017.
7 // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates.
8 
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14 
15 #ifndef BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
16 #define BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
17 
18 
19 #include <boost/math/constants/constants.hpp>
20 
21 #include <boost/geometry/core/radius.hpp>
22 
23 #include <boost/geometry/util/condition.hpp>
24 #include <boost/geometry/util/math.hpp>
25 
26 #include <boost/geometry/formulas/differential_quantities.hpp>
27 #include <boost/geometry/formulas/flattening.hpp>
28 #include <boost/geometry/formulas/result_inverse.hpp>
29 
30 
31 #ifndef BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS
32 #define BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS 1000
33 #endif
34 
35 
36 namespace boost { namespace geometry { namespace formula
37 {
38 
39 /*!
40 \brief The solution of the inverse problem of geodesics on latlong coordinates, after Vincenty, 1975
41 \author See
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
48 
49 */
50 template <
51     typename CT,
52     bool EnableDistance,
53     bool EnableAzimuth,
54     bool EnableReverseAzimuth = false,
55     bool EnableReducedLength = false,
56     bool EnableGeodesicScale = false
57 >
58 struct vincenty_inverse
59 {
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;
64 
65 public:
66     typedef result_inverse<CT> result_type;
67 
68     template <typename T1, typename T2, typename Spheroid>
applyboost::geometry::formula::vincenty_inverse69     static inline result_type apply(T1 const& lon1,
70                                     T1 const& lat1,
71                                     T2 const& lon2,
72                                     T2 const& lat2,
73                                     Spheroid const& spheroid)
74     {
75         result_type result;
76 
77         if (math::equals(lat1, lat2) && math::equals(lon1, lon2))
78         {
79             return result;
80         }
81 
82         CT const c0 = 0;
83         CT const c1 = 1;
84         CT const c2 = 2;
85         CT const c3 = 3;
86         CT const c4 = 4;
87         CT const c16 = 16;
88         CT const c_e_12 = CT(1e-12);
89 
90         CT const pi = geometry::math::pi<CT>();
91         CT const two_pi = c2 * pi;
92 
93         // lambda: difference in longitude on an auxiliary sphere
94         CT L = lon2 - lon1;
95         CT lambda = L;
96 
97         if (L < -pi) L += two_pi;
98         if (L > pi) L -= two_pi;
99 
100         CT const radius_a = CT(get_radius<0>(spheroid));
101         CT const radius_b = CT(get_radius<2>(spheroid));
102         CT const f = formula::flattening<CT>(spheroid);
103 
104         // U: reduced latitude, defined by tan U = (1-f) tan phi
105         CT const one_min_f = c1 - f;
106         CT const tan_U1 = one_min_f * tan(lat1); // above (1)
107         CT const tan_U2 = one_min_f * tan(lat2); // above (1)
108 
109         // calculate sin U and cos U using trigonometric identities
110         CT const temp_den_U1 = math::sqrt(c1 + math::sqr(tan_U1));
111         CT const temp_den_U2 = math::sqrt(c1 + math::sqr(tan_U2));
112         // cos = 1 / sqrt(1 + tan^2)
113         CT const cos_U1 = c1 / temp_den_U1;
114         CT const cos_U2 = c1 / temp_den_U2;
115         // sin = tan / sqrt(1 + tan^2)
116         // sin = tan * cos
117         CT const sin_U1 = tan_U1 * cos_U1;
118         CT const sin_U2 = tan_U2 * cos_U2;
119 
120         // calculate sin U and cos U directly
121         //CT const U1 = atan(tan_U1);
122         //CT const U2 = atan(tan_U2);
123         //cos_U1 = cos(U1);
124         //cos_U2 = cos(U2);
125         //sin_U1 = tan_U1 * cos_U1; // sin(U1);
126         //sin_U2 = tan_U2 * cos_U2; // sin(U2);
127 
128         CT previous_lambda;
129         CT sin_lambda;
130         CT cos_lambda;
131         CT sin_sigma;
132         CT sin_alpha;
133         CT cos2_alpha;
134         CT cos_2sigma_m;
135         CT cos2_2sigma_m;
136         CT sigma;
137 
138         int counter = 0; // robustness
139 
140         do
141         {
142             previous_lambda = lambda; // (13)
143             sin_lambda = sin(lambda);
144             cos_lambda = cos(lambda);
145             sin_sigma = math::sqrt(math::sqr(cos_U2 * sin_lambda) + math::sqr(cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda)); // (14)
146             CT cos_sigma = sin_U1 * sin_U2 + cos_U1 * cos_U2 * cos_lambda; // (15)
147             sin_alpha = cos_U1 * cos_U2 * sin_lambda / sin_sigma; // (17)
148             cos2_alpha = c1 - math::sqr(sin_alpha);
149             cos_2sigma_m = math::equals(cos2_alpha, c0) ? c0 : cos_sigma - c2 * sin_U1 * sin_U2 / cos2_alpha; // (18)
150             cos2_2sigma_m = math::sqr(cos_2sigma_m);
151 
152             CT C = f/c16 * cos2_alpha * (c4 + f * (c4 - c3 * cos2_alpha)); // (10)
153             sigma = atan2(sin_sigma, cos_sigma); // (16)
154             lambda = L + (c1 - C) * f * sin_alpha *
155                 (sigma + C * sin_sigma * (cos_2sigma_m + C * cos_sigma * (-c1 + c2 * cos2_2sigma_m))); // (11)
156 
157             ++counter; // robustness
158 
159         } while ( geometry::math::abs(previous_lambda - lambda) > c_e_12
160                && geometry::math::abs(lambda) < pi
161                && counter < BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS ); // robustness
162 
163         if ( BOOST_GEOMETRY_CONDITION(EnableDistance) )
164         {
165             // Oops getting hard here
166             // (again, problem is that ttmath cannot divide by doubles, which is OK)
167             CT const c6 = 6;
168             CT const c47 = 47;
169             CT const c74 = 74;
170             CT const c128 = 128;
171             CT const c256 = 256;
172             CT const c175 = 175;
173             CT const c320 = 320;
174             CT const c768 = 768;
175             CT const c1024 = 1024;
176             CT const c4096 = 4096;
177             CT const c16384 = 16384;
178 
179             //CT sqr_u = cos2_alpha * (math::sqr(radius_a) - math::sqr(radius_b)) / math::sqr(radius_b); // above (1)
180             CT sqr_u = cos2_alpha * ( math::sqr(radius_a / radius_b) - c1 ); // above (1)
181 
182             CT A = c1 + sqr_u/c16384 * (c4096 + sqr_u * (-c768 + sqr_u * (c320 - c175 * sqr_u))); // (3)
183             CT B = sqr_u/c1024 * (c256 + sqr_u * ( -c128 + sqr_u * (c74 - c47 * sqr_u))); // (4)
184             CT const cos_sigma = cos(sigma);
185             CT const sin2_sigma = math::sqr(sin_sigma);
186             CT delta_sigma = B * sin_sigma * (cos_2sigma_m + (B/c4) * (cos_sigma* (-c1 + c2 * cos2_2sigma_m)
187                 - (B/c6) * cos_2sigma_m * (-c3 + c4 * sin2_sigma) * (-c3 + c4 * cos2_2sigma_m))); // (6)
188 
189             result.distance = radius_b * A * (sigma - delta_sigma); // (19)
190         }
191 
192         if ( BOOST_GEOMETRY_CONDITION(CalcAzimuths) )
193         {
194             if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
195             {
196                 result.azimuth = atan2(cos_U2 * sin_lambda, cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda); // (20)
197             }
198 
199             if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
200             {
201                 result.reverse_azimuth = atan2(cos_U1 * sin_lambda, -sin_U1 * cos_U2 + cos_U1 * sin_U2 * cos_lambda); // (21)
202             }
203         }
204 
205         if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
206         {
207             typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
208             quantities::apply(lon1, lat1, lon2, lat2,
209                               result.azimuth, result.reverse_azimuth,
210                               radius_b, f,
211                               result.reduced_length, result.geodesic_scale);
212         }
213 
214         return result;
215     }
216 };
217 
218 }}} // namespace boost::geometry::formula
219 
220 
221 #endif // BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
222