3 // Copyright (c) 2017-2018 Oracle and/or its affiliates.
5 // Contributed and/or modified by Vissarion Fysikopoulos, 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_MERIDIAN_INVERSE_HPP
12 #define BOOST_GEOMETRY_FORMULAS_MERIDIAN_INVERSE_HPP
14 #include <boost/math/constants/constants.hpp>
16 #include <boost/geometry/core/radius.hpp>
18 #include <boost/geometry/util/condition.hpp>
19 #include <boost/geometry/util/math.hpp>
20 #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
22 #include <boost/geometry/formulas/flattening.hpp>
23 #include <boost/geometry/formulas/meridian_segment.hpp>
25 namespace boost { namespace geometry { namespace formula
29 \brief Compute the arc length of an ellipse.
32 template <typename CT, unsigned int Order = 1>
33 class meridian_inverse
50 static bool meridian_not_crossing_pole(T lat1, T lat2, CT diff)
52 CT half_pi = math::pi<CT>()/CT(2);
53 return math::equals(diff, CT(0)) ||
54 (math::equals(lat2, half_pi) && math::equals(lat1, -half_pi));
57 static bool meridian_crossing_pole(CT diff)
59 return math::equals(math::abs(diff), math::pi<CT>());
63 template <typename T, typename Spheroid>
64 static CT meridian_not_crossing_pole_dist(T lat1, T lat2, Spheroid const& spheroid)
66 return math::abs(apply(lat2, spheroid) - apply(lat1, spheroid));
69 template <typename T, typename Spheroid>
70 static CT meridian_crossing_pole_dist(T lat1, T lat2, Spheroid const& spheroid)
73 CT half_pi = math::pi<CT>()/CT(2);
79 return math::abs(lat_sign * CT(2) * apply(half_pi, spheroid)
80 - apply(lat1, spheroid) - apply(lat2, spheroid));
83 template <typename T, typename Spheroid>
84 static result apply(T lon1, T lat1, T lon2, T lat2, Spheroid const& spheroid)
88 CT diff = geometry::math::longitude_distance_signed<geometry::radian>(lon1, lon2);
92 std::swap(lat1, lat2);
95 if ( meridian_not_crossing_pole(lat1, lat2, diff) )
97 res.distance = meridian_not_crossing_pole_dist(lat1, lat2, spheroid);
100 else if ( meridian_crossing_pole(diff) )
102 res.distance = meridian_crossing_pole_dist(lat1, lat2, spheroid);
108 // Distance computation on meridians using series approximations
109 // to elliptic integrals. Formula to compute distance from lattitude 0 to lat
110 // https://en.wikipedia.org/wiki/Meridian_arc
111 // latitudes are assumed to be in radians and in [-pi/2,pi/2]
112 template <typename T, typename Spheroid>
113 static CT apply(T lat, Spheroid const& spheroid)
115 CT const a = get_radius<0>(spheroid);
116 CT const f = formula::flattening<CT>(spheroid);
117 CT n = f / (CT(2) - f);
130 return M * (C0 * lat + C2 * sin(2*lat));
139 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat));
144 CT C6 = -0.729166667 * n3;
148 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
154 CT C8 = 0.615234375 * n4;
158 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
159 + C6 * sin(6*lat) + C8 * sin(8*lat));
163 C6 += 0.227864583 * n5;
164 CT C10 = -0.54140625 * n5;
167 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
168 + C6 * sin(6*lat) + C8 * sin(8*lat) + C10 * sin(10*lat));
173 }}} // namespace boost::geometry::formula
176 #endif // BOOST_GEOMETRY_FORMULAS_MERIDIAN_INVERSE_HPP