]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/formulas/vincenty_inverse.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / geometry / formulas / vincenty_inverse.hpp
1 // Boost.Geometry
2
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4
5 // This file was modified by Oracle on 2014, 2016, 2017.
6 // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates.
7
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9
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)
13
14 #ifndef BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
15 #define BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
16
17
18 #include <boost/math/constants/constants.hpp>
19
20 #include <boost/geometry/core/radius.hpp>
21 #include <boost/geometry/core/srs.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>
69 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 c1 = 1;
83 CT const c2 = 2;
84 CT const c3 = 3;
85 CT const c4 = 4;
86 CT const c16 = 16;
87 CT const c_e_12 = CT(1e-12);
88
89 CT const pi = geometry::math::pi<CT>();
90 CT const two_pi = c2 * pi;
91
92 // lambda: difference in longitude on an auxiliary sphere
93 CT L = lon2 - lon1;
94 CT lambda = L;
95
96 if (L < -pi) L += two_pi;
97 if (L > pi) L -= two_pi;
98
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);
102
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)
107
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)
115 // sin = tan * cos
116 CT const sin_U1 = tan_U1 * cos_U1;
117 CT const sin_U2 = tan_U2 * cos_U2;
118
119 // calculate sin U and cos U directly
120 //CT const U1 = atan(tan_U1);
121 //CT const U2 = atan(tan_U2);
122 //cos_U1 = cos(U1);
123 //cos_U2 = cos(U2);
124 //sin_U1 = tan_U1 * cos_U1; // sin(U1);
125 //sin_U2 = tan_U2 * cos_U2; // sin(U2);
126
127 CT previous_lambda;
128 CT sin_lambda;
129 CT cos_lambda;
130 CT sin_sigma;
131 CT sin_alpha;
132 CT cos2_alpha;
133 CT cos_2sigma_m;
134 CT cos2_2sigma_m;
135 CT sigma;
136
137 int counter = 0; // robustness
138
139 do
140 {
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);
150
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)
155
156 ++counter; // robustness
157
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
161
162 if ( BOOST_GEOMETRY_CONDITION(EnableDistance) )
163 {
164 // Oops getting hard here
165 // (again, problem is that ttmath cannot divide by doubles, which is OK)
166 CT const c1 = 1;
167 CT const c2 = 2;
168 CT const c3 = 3;
169 CT const c4 = 4;
170 CT const c6 = 6;
171 CT const c47 = 47;
172 CT const c74 = 74;
173 CT const c128 = 128;
174 CT const c256 = 256;
175 CT const c175 = 175;
176 CT const c320 = 320;
177 CT const c768 = 768;
178 CT const c1024 = 1024;
179 CT const c4096 = 4096;
180 CT const c16384 = 16384;
181
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)
184
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)
191
192 result.distance = radius_b * A * (sigma - delta_sigma); // (19)
193 }
194
195 if ( BOOST_GEOMETRY_CONDITION(CalcAzimuths) )
196 {
197 if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
198 {
199 result.azimuth = atan2(cos_U2 * sin_lambda, cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda); // (20)
200 }
201
202 if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
203 {
204 result.reverse_azimuth = atan2(cos_U1 * sin_lambda, -sin_U1 * cos_U2 + cos_U1 * sin_U2 * cos_lambda); // (21)
205 }
206 }
207
208 if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
209 {
210 typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
211 quantities::apply(lon1, lat1, lon2, lat2,
212 result.azimuth, result.reverse_azimuth,
213 radius_b, f,
214 result.reduced_length, result.geodesic_scale);
215 }
216
217 return result;
218 }
219 };
220
221 }}} // namespace boost::geometry::formula
222
223
224 #endif // BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP