123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #ifndef BOOST_GEOMETRY_FORMULAS_ANDOYER_INVERSE_HPP
- #define BOOST_GEOMETRY_FORMULAS_ANDOYER_INVERSE_HPP
- #include <boost/math/constants/constants.hpp>
- #include <boost/geometry/core/radius.hpp>
- #include <boost/geometry/util/constexpr.hpp>
- #include <boost/geometry/util/math.hpp>
- #include <boost/geometry/formulas/differential_quantities.hpp>
- #include <boost/geometry/formulas/flattening.hpp>
- #include <boost/geometry/formulas/result_inverse.hpp>
- namespace boost { namespace geometry { namespace formula
- {
- template <
- typename CT,
- bool EnableDistance,
- bool EnableAzimuth,
- bool EnableReverseAzimuth = false,
- bool EnableReducedLength = false,
- bool EnableGeodesicScale = false
- >
- class andoyer_inverse
- {
- static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
- static const bool CalcAzimuths = EnableAzimuth || EnableReverseAzimuth || CalcQuantities;
- static const bool CalcFwdAzimuth = EnableAzimuth || CalcQuantities;
- static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
- public:
- typedef result_inverse<CT> result_type;
- template <typename T1, typename T2, typename Spheroid>
- static inline result_type apply(T1 const& lon1,
- T1 const& lat1,
- T2 const& lon2,
- T2 const& lat2,
- Spheroid const& spheroid)
- {
- result_type result;
-
- if ( math::equals(lon1, lon2) && math::equals(lat1, lat2) )
- {
- return result;
- }
- CT const c0 = CT(0);
- CT const c1 = CT(1);
- CT const pi = math::pi<CT>();
- CT const f = formula::flattening<CT>(spheroid);
- CT const dlon = lon2 - lon1;
- CT const sin_dlon = sin(dlon);
- CT const cos_dlon = cos(dlon);
- CT const sin_lat1 = sin(lat1);
- CT const cos_lat1 = cos(lat1);
- CT const sin_lat2 = sin(lat2);
- CT const cos_lat2 = cos(lat2);
-
-
-
- CT cos_d = sin_lat1*sin_lat2 + cos_lat1*cos_lat2*cos_dlon;
-
- if (cos_d < -c1)
- cos_d = -c1;
- else if (cos_d > c1)
- cos_d = c1;
- CT const d = acos(cos_d);
- CT const sin_d = sin(d);
- if BOOST_GEOMETRY_CONSTEXPR (EnableDistance)
- {
- CT const K = math::sqr(sin_lat1-sin_lat2);
- CT const L = math::sqr(sin_lat1+sin_lat2);
- CT const three_sin_d = CT(3) * sin_d;
- CT const one_minus_cos_d = c1 - cos_d;
- CT const one_plus_cos_d = c1 + cos_d;
-
-
- CT const H = math::equals(one_minus_cos_d, c0) ?
- c0 :
- (d + three_sin_d) / one_minus_cos_d;
- CT const G = math::equals(one_plus_cos_d, c0) ?
- c0 :
- (d - three_sin_d) / one_plus_cos_d;
- CT const dd = -(f/CT(4))*(H*K+G*L);
- CT const a = CT(get_radius<0>(spheroid));
- result.distance = a * (d + dd);
- }
- if BOOST_GEOMETRY_CONSTEXPR (CalcAzimuths)
- {
-
- if (math::equals(sin_d, c0))
- {
-
-
-
-
-
-
-
-
-
-
-
-
- if (cos_d >= c0)
- {
- result.azimuth = c0;
- result.reverse_azimuth = c0;
- }
-
- else
- {
-
- if (! math::equals(sin_lat1, c1))
- {
- result.azimuth = c0;
- result.reverse_azimuth = pi;
- }
- else
- {
- result.azimuth = pi;
- result.reverse_azimuth = c0;
- }
- }
- }
- else
- {
- CT const c2 = CT(2);
- CT A = c0;
- CT U = c0;
- if (math::equals(cos_lat2, c0))
- {
- if (sin_lat2 < c0)
- {
- A = pi;
- }
- }
- else
- {
- CT const tan_lat2 = sin_lat2/cos_lat2;
- CT const M = cos_lat1*tan_lat2-sin_lat1*cos_dlon;
- A = atan2(sin_dlon, M);
- CT const sin_2A = sin(c2*A);
- U = (f/ c2)*math::sqr(cos_lat1)*sin_2A;
- }
- CT B = c0;
- CT V = c0;
- if (math::equals(cos_lat1, c0))
- {
- if (sin_lat1 < c0)
- {
- B = pi;
- }
- }
- else
- {
- CT const tan_lat1 = sin_lat1/cos_lat1;
- CT const N = cos_lat2*tan_lat1-sin_lat2*cos_dlon;
- B = atan2(sin_dlon, N);
- CT const sin_2B = sin(c2*B);
- V = (f/ c2)*math::sqr(cos_lat2)*sin_2B;
- }
- CT const T = d / sin_d;
-
-
-
-
- if BOOST_GEOMETRY_CONSTEXPR (CalcFwdAzimuth)
- {
- CT const dA = V*T - U;
- result.azimuth = A - dA;
- normalize_azimuth(result.azimuth, A, dA);
- }
- if BOOST_GEOMETRY_CONSTEXPR (CalcRevAzimuth)
- {
- CT const dB = -U*T + V;
- if (B >= 0)
- result.reverse_azimuth = pi - B - dB;
- else
- result.reverse_azimuth = -pi - B - dB;
- normalize_azimuth(result.reverse_azimuth, B, dB);
- }
- }
- }
- if BOOST_GEOMETRY_CONSTEXPR (CalcQuantities)
- {
- CT const b = CT(get_radius<2>(spheroid));
- typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 1> quantities;
- quantities::apply(dlon, sin_lat1, cos_lat1, sin_lat2, cos_lat2,
- result.azimuth, result.reverse_azimuth,
- b, f,
- result.reduced_length, result.geodesic_scale);
- }
- return result;
- }
- private:
- static inline void normalize_azimuth(CT & azimuth, CT const& A, CT const& dA)
- {
- CT const c0 = 0;
- if (A >= c0)
- {
- if (dA >= c0)
- {
- if (azimuth < c0)
- {
- azimuth = c0;
- }
- }
- else
- {
- CT const pi = math::pi<CT>();
- if (azimuth > pi)
- {
- azimuth = pi;
- }
- }
- }
- else
- {
- if (dA <= c0)
- {
- if (azimuth > c0)
- {
- azimuth = c0;
- }
- }
- else
- {
- CT const minus_pi = -math::pi<CT>();
- if (azimuth < minus_pi)
- {
- azimuth = minus_pi;
- }
- }
- }
- }
- };
- }}}
- #endif
|