]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/geometry/formulas/elliptic_arc_length.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / geometry / formulas / elliptic_arc_length.hpp
CommitLineData
b32b8144
FG
1// Boost.Geometry
2
3// Copyright (c) 2017 Oracle and/or its affiliates.
4
5// Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
6
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)
10
11#ifndef BOOST_GEOMETRY_FORMULAS_ELLIPTIC_ARC_LENGTH_HPP
12#define BOOST_GEOMETRY_FORMULAS_ELLIPTIC_ARC_LENGTH_HPP
13
14#include <boost/math/constants/constants.hpp>
15
16#include <boost/geometry/core/radius.hpp>
17#include <boost/geometry/core/srs.hpp>
18
19#include <boost/geometry/util/condition.hpp>
20#include <boost/geometry/util/math.hpp>
21#include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
22
23#include <boost/geometry/formulas/flattening.hpp>
24
25namespace boost { namespace geometry { namespace formula
26{
27
28/*!
29\brief Compute the arc length of an ellipse.
30*/
31
32template <typename CT, unsigned int Order = 1>
33class elliptic_arc_length
34{
35
36public :
37
38 struct result
39 {
40 result()
41 : distance(0)
42 , meridian(false)
43 {}
44
45 CT distance;
46 bool meridian;
47 };
48
49 template <typename T, typename Spheroid>
50 static result apply(T lon1, T lat1, T lon2, T lat2, Spheroid const& spheroid)
51 {
52 result res;
53
54 CT c0 = 0;
55 CT pi = math::pi<CT>();
56 CT half_pi = pi/CT(2);
57 CT diff = geometry::math::longitude_distance_signed<geometry::radian>(lon1, lon2);
58
59 if (lat1 > lat2)
60 {
61 std::swap(lat1, lat2);
62 }
63
64 if ( math::equals(diff, c0) ||
65 (math::equals(lat2, half_pi) && math::equals(lat1, -half_pi)) )
66 {
67 // single meridian not crossing pole
68 res.distance = apply(lat2, spheroid) - apply(lat1, spheroid);
69 res.meridian = true;
70 }
71
72 if (math::equals(math::abs(diff), pi))
73 {
74 // meridian crosses pole
75 CT lat_sign = 1;
76 if (lat1+lat2 < c0)
77 {
78 lat_sign = CT(-1);
79 }
80 res.distance = math::abs(lat_sign * CT(2) * apply(half_pi, spheroid)
81 - apply(lat1, spheroid) - apply(lat2, spheroid));
82 res.meridian = true;
83 }
84 return res;
85 }
86
87 // Distance computation on meridians using series approximations
88 // to elliptic integrals. Formula to compute distance from lattitude 0 to lat
89 // https://en.wikipedia.org/wiki/Meridian_arc
90 // latitudes are assumed to be in radians and in [-pi/2,pi/2]
91 template <typename T, typename Spheroid>
92 static CT apply(T lat, Spheroid const& spheroid)
93 {
94 CT const a = get_radius<0>(spheroid);
95 CT const f = formula::flattening<CT>(spheroid);
96 CT n = f / (CT(2) - f);
97 CT M = a/(1+n);
98 CT C0 = 1;
99
100 if (Order == 0)
101 {
102 return M * C0 * lat;
103 }
104
105 CT C2 = -1.5 * n;
106
107 if (Order == 1)
108 {
109 return M * (C0 * lat + C2 * sin(2*lat));
110 }
111
112 CT n2 = n * n;
113 C0 += .25 * n2;
114 CT C4 = 0.9375 * n2;
115
116 if (Order == 2)
117 {
118 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat));
119 }
120
121 CT n3 = n2 * n;
122 C2 += 0.1875 * n3;
123 CT C6 = -0.729166667 * n3;
124
125 if (Order == 3)
126 {
127 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
128 + C6 * sin(6*lat));
129 }
130
131 CT n4 = n2 * n2;
132 C4 -= 0.234375 * n4;
133 CT C8 = 0.615234375 * n4;
134
135 if (Order == 4)
136 {
137 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
138 + C6 * sin(6*lat) + C8 * sin(8*lat));
139 }
140
141 CT n5 = n4 * n;
142 C6 += 0.227864583 * n5;
143 CT C10 = -0.54140625 * n5;
144
145 // Order 5 or higher
146 return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
147 + C6 * sin(6*lat) + C8 * sin(8*lat) + C10 * sin(10*lat));
148
149 }
150
151 // Iterative method to elliptic arc length based on
152 // http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5115/
153 // Geographic-Distance-and-Azimuth-Calculations.htm
154 // latitudes are assumed to be in radians and in [-pi/2,pi/2]
155 template <typename T1, typename T2, typename Spheroid>
156 CT interative_method(T1 lat1,
157 T2 lat2,
158 Spheroid const& spheroid)
159 {
160 CT result = 0;
161 CT const zero = 0;
162 CT const one = 1;
163 CT const c1 = 2;
164 CT const c2 = 0.5;
165 CT const c3 = 4000;
166
167 CT const a = get_radius<0>(spheroid);
168 CT const f = formula::flattening<CT>(spheroid);
169
170 // how many steps to use
171
172 CT lat1_deg = lat1 * geometry::math::r2d<CT>();
173 CT lat2_deg = lat2 * geometry::math::r2d<CT>();
174
175 int steps = c1 + (c2 + (lat2_deg > lat1_deg) ? CT(lat2_deg - lat1_deg)
176 : CT(lat1_deg - lat2_deg));
177 steps = (steps > c3) ? c3 : steps;
178
179 //std::cout << "Steps=" << steps << std::endl;
180
181 CT snLat1 = sin(lat1);
182 CT snLat2 = sin(lat2);
183 CT twoF = 2 * f - f * f;
184
185 // limits of integration
186 CT x1 = a * cos(lat1) /
187 sqrt(1 - twoF * snLat1 * snLat1);
188 CT x2 = a * cos(lat2) /
189 sqrt(1 - twoF * snLat2 * snLat2);
190
191 CT dx = (x2 - x1) / (steps - one);
192 CT x, y1, y2, dy, dydx;
193 CT adx = (dx < zero) ? -dx : dx; // absolute value of dx
194
195 CT a2 = a * a;
196 CT oneF = 1 - f;
197
198 // now loop through each step adding up all the little
199 // hypotenuses
200 for (int i = 0; i < (steps - 1); i++){
201 x = x1 + dx * i;
202 dydx = ((a * oneF * sqrt((one - ((x+dx)*(x+dx))/a2))) -
203 (a * oneF * sqrt((one - (x*x)/a2)))) / dx;
204 result += adx * sqrt(one + dydx*dydx);
205 }
206
207 return result;
208 }
209};
210
211}}} // namespace boost::geometry::formula
212
213
214#endif // BOOST_GEOMETRY_FORMULAS_ELLIPTIC_ARC_LENGTH_HPP