3 // Copyright (c) 2015-2017 Oracle and/or its affiliates.
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_GEOMETRY_FORMULAS_ANDOYER_INVERSE_HPP
12 #define BOOST_GEOMETRY_FORMULAS_ANDOYER_INVERSE_HPP
15 #include <boost/math/constants/constants.hpp>
17 #include <boost/geometry/core/radius.hpp>
18 #include <boost/geometry/core/srs.hpp>
20 #include <boost/geometry/util/condition.hpp>
21 #include <boost/geometry/util/math.hpp>
23 #include <boost/geometry/formulas/differential_quantities.hpp>
24 #include <boost/geometry/formulas/flattening.hpp>
25 #include <boost/geometry/formulas/result_inverse.hpp>
28 namespace boost { namespace geometry { namespace formula
32 \brief The solution of the inverse problem of geodesics on latlong coordinates,
33 Forsyth-Andoyer-Lambert type approximation with first order terms.
35 - Technical Report: PAUL D. THOMAS, MATHEMATICAL MODELS FOR NAVIGATION SYSTEMS, 1965
36 http://www.dtic.mil/docs/citations/AD0627893
37 - Technical Report: PAUL D. THOMAS, SPHEROIDAL GEODESICS, REFERENCE SYSTEMS, AND LOCAL GEOMETRY, 1970
38 http://www.dtic.mil/docs/citations/AD703541
44 bool EnableReverseAzimuth = false,
45 bool EnableReducedLength = false,
46 bool EnableGeodesicScale = false
50 static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
51 static const bool CalcAzimuths = EnableAzimuth || EnableReverseAzimuth || CalcQuantities;
52 static const bool CalcFwdAzimuth = EnableAzimuth || CalcQuantities;
53 static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
56 typedef result_inverse<CT> result_type;
58 template <typename T1, typename T2, typename Spheroid>
59 static inline result_type apply(T1 const& lon1,
63 Spheroid const& spheroid)
67 // coordinates in radians
69 if ( math::equals(lon1, lon2) && math::equals(lat1, lat2) )
76 CT const pi = math::pi<CT>();
77 CT const f = formula::flattening<CT>(spheroid);
79 CT const dlon = lon2 - lon1;
80 CT const sin_dlon = sin(dlon);
81 CT const cos_dlon = cos(dlon);
82 CT const sin_lat1 = sin(lat1);
83 CT const cos_lat1 = cos(lat1);
84 CT const sin_lat2 = sin(lat2);
85 CT const cos_lat2 = cos(lat2);
87 // H,G,T = infinity if cos_d = 1 or cos_d = -1
88 // lat1 == +-90 && lat2 == +-90
89 // lat1 == lat2 && lon1 == lon2
90 CT cos_d = sin_lat1*sin_lat2 + cos_lat1*cos_lat2*cos_dlon;
91 // on some platforms cos_d may be outside valid range
97 CT const d = acos(cos_d); // [0, pi]
98 CT const sin_d = sin(d); // [-1, 1]
100 if ( BOOST_GEOMETRY_CONDITION(EnableDistance) )
102 CT const K = math::sqr(sin_lat1-sin_lat2);
103 CT const L = math::sqr(sin_lat1+sin_lat2);
104 CT const three_sin_d = CT(3) * sin_d;
106 CT const one_minus_cos_d = c1 - cos_d;
107 CT const one_plus_cos_d = c1 + cos_d;
108 // cos_d = 1 or cos_d = -1 means that the points are antipodal
110 CT const H = math::equals(one_minus_cos_d, c0) ?
112 (d + three_sin_d) / one_minus_cos_d;
113 CT const G = math::equals(one_plus_cos_d, c0) ?
115 (d - three_sin_d) / one_plus_cos_d;
117 CT const dd = -(f/CT(4))*(H*K+G*L);
119 CT const a = get_radius<0>(spheroid);
121 result.distance = a * (d + dd);
124 if ( BOOST_GEOMETRY_CONDITION(CalcAzimuths) )
126 // sin_d = 0 <=> antipodal points (incl. poles)
127 if (math::equals(sin_d, c0))
133 // TODO: The following azimuths are inconsistent with distance
134 // i.e. according to azimuths below a segment with antipodal endpoints
135 // travels through the north pole, however the distance returned above
136 // is the length of a segment traveling along the equator.
137 // Furthermore, this special case handling is only done in andoyer
139 // The most correct way of fixing it is to handle antipodal regions
140 // correctly and consistently across all formulas.
142 // Set azimuth to 0 unless the first endpoint is the north pole
143 if (! math::equals(sin_lat1, c1))
146 result.reverse_azimuth = pi;
151 result.reverse_azimuth = 0;
160 if (math::equals(cos_lat2, c0))
169 CT const tan_lat2 = sin_lat2/cos_lat2;
170 CT const M = cos_lat1*tan_lat2-sin_lat1*cos_dlon;
171 A = atan2(sin_dlon, M);
172 CT const sin_2A = sin(c2*A);
173 U = (f/ c2)*math::sqr(cos_lat1)*sin_2A;
178 if (math::equals(cos_lat1, c0))
187 CT const tan_lat1 = sin_lat1/cos_lat1;
188 CT const N = cos_lat2*tan_lat1-sin_lat2*cos_dlon;
189 B = atan2(sin_dlon, N);
190 CT const sin_2B = sin(c2*B);
191 V = (f/ c2)*math::sqr(cos_lat2)*sin_2B;
194 CT const T = d / sin_d;
196 // even with sin_d == 0 checked above if the second point
197 // is somewhere in the antipodal area T may still be great
198 // therefore dA and dB may be great and the resulting azimuths
199 // may be some more or less arbitrary angles
201 if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
203 CT const dA = V*T - U;
204 result.azimuth = A - dA;
205 normalize_azimuth(result.azimuth, A, dA);
208 if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
210 CT const dB = -U*T + V;
212 result.reverse_azimuth = pi - B - dB;
214 result.reverse_azimuth = -pi - B - dB;
215 normalize_azimuth(result.reverse_azimuth, B, dB);
220 if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
222 typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 1> quantities;
223 quantities::apply(dlon, sin_lat1, cos_lat1, sin_lat2, cos_lat2,
224 result.azimuth, result.reverse_azimuth,
225 get_radius<2>(spheroid), f,
226 result.reduced_length, result.geodesic_scale);
233 static inline void normalize_azimuth(CT & azimuth, CT const& A, CT const& dA)
237 if (A >= c0) // A indicates Eastern hemisphere
239 if (dA >= c0) // A altered towards 0
246 else // dA < 0, A altered towards pi
248 CT const pi = math::pi<CT>();
255 else // A indicates Western hemisphere
257 if (dA <= c0) // A altered towards 0
264 else // dA > 0, A altered towards -pi
266 CT const minus_pi = -math::pi<CT>();
267 if (azimuth < minus_pi)
276 }}} // namespace boost::geometry::formula
279 #endif // BOOST_GEOMETRY_FORMULAS_ANDOYER_INVERSE_HPP