]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/strategies/andoyer.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / geometry / test / strategies / andoyer.cpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2007-2016 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2008-2016 Bruno Lalande, Paris, France.
6 // Copyright (c) 2009-2016 Mateusz Loskot, London, UK.
7
8 // This file was modified by Oracle on 2014-2017.
9 // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates.
10
11 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
12 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
13
14 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
15 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
16
17 // Use, modification and distribution is subject to the Boost Software License,
18 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
20
21
22 #include <geometry_test_common.hpp>
23
24 #include <boost/concept_check.hpp>
25
26 #include <boost/geometry/algorithms/assign.hpp>
27 #include <boost/geometry/algorithms/distance.hpp>
28 #include <boost/geometry/geometries/point.hpp>
29 #include <boost/geometry/srs/spheroid.hpp>
30 #include <boost/geometry/strategies/concepts/distance_concept.hpp>
31 #include <boost/geometry/strategies/geographic/distance_andoyer.hpp>
32 #include <boost/geometry/strategies/geographic/side_andoyer.hpp>
33
34 #include <test_common/test_point.hpp>
35
36 double make_deg(double deg, double min, double sec)
37 {
38 return deg + min / 60.0 + sec / 3600.0;
39 }
40
41 double to_rad(double deg)
42 {
43 return bg::math::pi<double>() * deg / 180.0;
44 }
45
46 double to_deg(double rad)
47 {
48 return 180.0 * rad / bg::math::pi<double>();
49 }
50
51 double normlized_deg(double deg)
52 {
53 if (deg > 180)
54 return deg - 360;
55 else if (deg < -180)
56 return deg + 360;
57 else
58 return deg;
59 }
60
61
62 template <typename P1, typename P2>
63 void test_distance(double lon1, double lat1, double lon2, double lat2, double expected_km)
64 {
65 // Set radius type, but for integer coordinates we want to have floating point radius type
66 typedef typename bg::promote_floating_point
67 <
68 typename bg::coordinate_type<P1>::type
69 >::type rtype;
70
71 typedef bg::srs::spheroid<rtype> stype;
72
73 typedef bg::strategy::distance::andoyer<stype> andoyer_type;
74 typedef bg::strategy::distance::geographic<bg::strategy::andoyer, stype> geographic_type;
75 typedef bg::formula::andoyer_inverse<rtype, true, false> andoyer_inverse_type;
76
77 BOOST_CONCEPT_ASSERT
78 (
79 (bg::concepts::PointDistanceStrategy<andoyer_type, P1, P2>)
80 );
81
82 andoyer_type andoyer;
83 geographic_type geographic;
84 typedef typename bg::strategy::distance
85 ::services::return_type<andoyer_type, P1, P2>::type return_type;
86
87 P1 p1;
88 P2 p2;
89
90 bg::assign_values(p1, lon1, lat1);
91 bg::assign_values(p2, lon2, lat2);
92
93 return_type d_strategy = andoyer.apply(p1, p2);
94 return_type d_strategy2 = geographic.apply(p1, p2);
95 return_type d_function = bg::distance(p1, p2, andoyer);
96
97 double diff = bg::math::longitude_distance_signed<bg::degree>(lon1, lon2);
98 return_type d_formula;
99
100 // if the points lay on a meridian, distance strategy calls the special formula
101 // for meridian distance that returns different result than andoyer formula
102 // for nearly antipodal points
103 if (bg::math::equals(diff, 0.0)
104 || bg::math::equals(bg::math::abs(diff), 180.0))
105 {
106 d_formula = d_strategy;
107 }
108 else
109 {
110 d_formula = andoyer_inverse_type::apply(to_rad(lon1), to_rad(lat1),
111 to_rad(lon2), to_rad(lat2),
112 stype()).distance;
113 }
114
115 BOOST_CHECK_CLOSE(d_strategy / 1000.0, expected_km, 0.001);
116 BOOST_CHECK_CLOSE(d_strategy2 / 1000.0, expected_km, 0.001);
117 BOOST_CHECK_CLOSE(d_function / 1000.0, expected_km, 0.001);
118 BOOST_CHECK_CLOSE(d_formula / 1000.0, expected_km, 0.001);
119 }
120
121 template <typename PS, typename P>
122 void test_azimuth(double lon1, double lat1,
123 double lon2, double lat2,
124 double expected_azimuth_deg)
125 {
126 // Set radius type, but for integer coordinates we want to have floating point radius type
127 typedef typename bg::promote_floating_point
128 <
129 typename bg::coordinate_type<PS>::type
130 >::type rtype;
131
132 typedef bg::srs::spheroid<rtype> stype;
133 typedef bg::formula::andoyer_inverse<rtype, false, true> andoyer_inverse_type;
134
135 rtype a_formula = andoyer_inverse_type::apply(to_rad(lon1), to_rad(lat1), to_rad(lon2), to_rad(lat2), stype()).azimuth;
136
137 rtype azimuth_deg = to_deg(a_formula);
138
139 if (bg::math::equals(azimuth_deg, -180.0))
140 azimuth_deg = 180.0;
141 if (bg::math::equals(expected_azimuth_deg, -180.0))
142 expected_azimuth_deg = 180.0;
143
144 if (bg::math::equals(expected_azimuth_deg, 0.0))
145 {
146 BOOST_CHECK(bg::math::equals(azimuth_deg, expected_azimuth_deg));
147 }
148 else
149 {
150 BOOST_CHECK_CLOSE(azimuth_deg, expected_azimuth_deg, 0.001);
151 }
152 }
153
154 template <typename P1, typename P2>
155 void test_distazi(double lon1, double lat1, double lon2, double lat2,
156 double expected_km, double expected_azimuth_deg)
157 {
158 test_distance<P1, P2>(lon1, lat1, lon2, lat2, expected_km);
159 test_azimuth<P1, P2>(lon1, lat1, lon2, lat2, expected_azimuth_deg);
160 }
161
162 // requires SW->NE
163 template <typename P1, typename P2>
164 void test_distazi_symm(double lon1, double lat1, double lon2, double lat2,
165 double expected_km, double expected_azimuth_deg,
166 bool is_antipodal = false)
167 {
168 double d180 = is_antipodal ? 0 : 180;
169 test_distazi<P1, P2>(lon1, lat1, lon2, lat2, expected_km, expected_azimuth_deg);
170 test_distazi<P1, P2>(-lon1, lat1, -lon2, lat2, expected_km, -expected_azimuth_deg);
171 test_distazi<P1, P2>(lon1, -lat1, lon2, -lat2, expected_km, d180 - expected_azimuth_deg);
172 test_distazi<P1, P2>(-lon1, -lat1, -lon2, -lat2, expected_km, -d180 + expected_azimuth_deg);
173 }
174
175 template <typename P1, typename P2>
176 void test_distazi_symmNS(double lon1, double lat1, double lon2, double lat2,
177 double expected_km, double expected_azimuth_deg)
178 {
179 test_distazi<P1, P2>(lon1, lat1, lon2, lat2, expected_km, expected_azimuth_deg);
180 test_distazi<P1, P2>(lon1, -lat1, lon2, -lat2, expected_km, 180 - expected_azimuth_deg);
181 }
182
183
184 template <typename PS, typename P>
185 void test_side(double lon1, double lat1,
186 double lon2, double lat2,
187 double lon, double lat,
188 int expected_side)
189 {
190 // Set radius type, but for integer coordinates we want to have floating point radius type
191 typedef typename bg::promote_floating_point
192 <
193 typename bg::coordinate_type<PS>::type
194 >::type rtype;
195
196 typedef bg::srs::spheroid<rtype> stype;
197
198 typedef bg::strategy::side::andoyer<stype> strategy_type;
199 typedef bg::strategy::side::geographic<bg::strategy::andoyer, stype> strategy2_type;
200
201 strategy_type strategy;
202 strategy2_type strategy2;
203
204 PS p1, p2;
205 P p;
206
207 bg::assign_values(p1, lon1, lat1);
208 bg::assign_values(p2, lon2, lat2);
209 bg::assign_values(p, lon, lat);
210
211 int side = strategy.apply(p1, p2, p);
212 int side2 = strategy2.apply(p1, p2, p);
213
214 BOOST_CHECK_EQUAL(side, expected_side);
215 BOOST_CHECK_EQUAL(side2, expected_side);
216 }
217
218 template <typename P1, typename P2>
219 void test_all()
220 {
221 // polar
222 test_distazi<P1, P2>(0, 90, 1, 80,
223 1116.814237, 179);
224
225 // no point difference
226 test_distazi<P1, P2>(4, 52, 4, 52,
227 0.0, 0.0);
228
229 // normal cases
230 test_distazi<P1, P2>(4, 52, 3, 40,
231 1336.039890, -176.3086);
232 test_distazi<P1, P2>(3, 52, 4, 40,
233 1336.039890, 176.3086);
234 test_distazi<P1, P2>(make_deg(17, 19, 43.28),
235 make_deg(40, 30, 31.151),
236 18, 40,
237 80.323245,
238 make_deg(134, 27, 50.05));
239
240 // antipodal
241 // ok? in those cases shorter path would pass through a pole
242 // but 90 or -90 would be consistent with distance?
243 test_distazi<P1, P2>(0, 0, 180, 0, 20003.9, 0.0);
244 test_distazi<P1, P2>(0, 0, -180, 0, 20003.9, 0.0);
245 test_distazi<P1, P2>(-90, 0, 90, 0, 20003.9, 0.0);
246 test_distazi<P1, P2>(90, 0, -90, 0, 20003.9, 0.0);
247
248 // 0, 45, 90 ...
249 for (int i = 0 ; i < 360 ; i += 45)
250 {
251 // 0 45 90 ...
252 double l = normlized_deg(i);
253 // -1 44 89 ...
254 double l1 = normlized_deg(i - 1);
255 // 1 46 91 ...
256 double l2 = normlized_deg(i + 1);
257
258 // near equator
259 test_distazi_symm<P1, P2>(l1, -1, l2, 1, 313.7956, 45.1964);
260
261 // near poles
262 test_distazi_symmNS<P1, P2>(l, -89.5, l, 89.5, 19892.2, 0.0);
263 test_distazi_symmNS<P1, P2>(l, -89.6, l, 89.6, 19914.6, 0.0);
264 test_distazi_symmNS<P1, P2>(l, -89.7, l, 89.7, 19936.9, 0.0);
265 test_distazi_symmNS<P1, P2>(l, -89.8, l, 89.8, 19959.2, 0.0);
266 test_distazi_symmNS<P1, P2>(l, -89.9, l, 89.9, 19981.6, 0.0);
267 test_distazi_symmNS<P1, P2>(l, -89.99, l, 89.99, 20001.7, 0.0);
268 test_distazi_symmNS<P1, P2>(l, -89.999, l, 89.999, 20003.7, 0.0);
269 // antipodal
270 test_distazi_symmNS<P1, P2>(l, -90, l, 90, 20003.9, 0.0);
271
272 test_distazi_symm<P1, P2>(normlized_deg(l-10.0), -10.0, normlized_deg(l+135), 45, 14892.1, 34.1802);
273 test_distazi_symm<P1, P2>(normlized_deg(l-30.0), -30.0, normlized_deg(l+135), 45, 17890.7, 33.7002);
274 test_distazi_symm<P1, P2>(normlized_deg(l-40.0), -40.0, normlized_deg(l+135), 45, 19319.7, 33.4801);
275 test_distazi_symm<P1, P2>(normlized_deg(l-41.0), -41.0, normlized_deg(l+135), 45, 19459.1, 33.2408);
276 test_distazi_symm<P1, P2>(normlized_deg(l-42.0), -42.0, normlized_deg(l+135), 45, 19597.8, 32.7844);
277 test_distazi_symm<P1, P2>(normlized_deg(l-43.0), -43.0, normlized_deg(l+135), 45, 19735.8, 31.7784);
278 test_distazi_symm<P1, P2>(normlized_deg(l-44.0), -44.0, normlized_deg(l+135), 45, 19873.0, 28.5588);
279 test_distazi_symm<P1, P2>(normlized_deg(l-44.1), -44.1, normlized_deg(l+135), 45, 19886.7, 27.8304);
280 test_distazi_symm<P1, P2>(normlized_deg(l-44.2), -44.2, normlized_deg(l+135), 45, 19900.4, 26.9173);
281 test_distazi_symm<P1, P2>(normlized_deg(l-44.3), -44.3, normlized_deg(l+135), 45, 19914.1, 25.7401);
282 test_distazi_symm<P1, P2>(normlized_deg(l-44.4), -44.4, normlized_deg(l+135), 45, 19927.7, 24.1668);
283 test_distazi_symm<P1, P2>(normlized_deg(l-44.5), -44.5, normlized_deg(l+135), 45, 19941.4, 21.9599);
284 test_distazi_symm<P1, P2>(normlized_deg(l-44.6), -44.6, normlized_deg(l+135), 45, 19955.0, 18.6438);
285 test_distazi_symm<P1, P2>(normlized_deg(l-44.7), -44.7, normlized_deg(l+135), 45, 19968.6, 13.1096);
286 test_distazi_symm<P1, P2>(normlized_deg(l-44.8), -44.8, normlized_deg(l+135), 45, 19982.3, 2.0300);
287 // nearly antipodal
288 test_distazi_symm<P1, P2>(normlized_deg(l-44.9), -44.9, normlized_deg(l+135), 45, 19995.9, 0.0);
289 test_distazi_symm<P1, P2>(normlized_deg(l-44.95), -44.95, normlized_deg(l+135), 45, 20002.7, 0.0);
290 test_distazi_symm<P1, P2>(normlized_deg(l-44.99), -44.99, normlized_deg(l+135), 45, 20008.1, 0.0);
291 test_distazi_symm<P1, P2>(normlized_deg(l-44.999), -44.999, normlized_deg(l+135), 45, 20009.4, 0.0);
292 // antipodal
293 test_distazi_symm<P1, P2>(normlized_deg(l-45), -45, normlized_deg(l+135), 45, 20003.92, 0.0, true);
294 }
295
296 /* SQL Server gives:
297 1116.82586908528, 0, 1336.02721932545
298
299 with:
300 SELECT 0.001 * geography::STGeomFromText('POINT(0 90)', 4326).STDistance(geography::STGeomFromText('POINT(1 80)', 4326))
301 union SELECT 0.001 * geography::STGeomFromText('POINT(4 52)', 4326).STDistance(geography::STGeomFromText('POINT(4 52)', 4326))
302 union SELECT 0.001 * geography::STGeomFromText('POINT(4 52)', 4326).STDistance(geography::STGeomFromText('POINT(3 40)', 4326))
303 */
304
305
306 test_side<P1, P2>(0, 0, 0, 1, 0, 2, 0);
307 test_side<P1, P2>(0, 0, 0, 1, 0, -2, 0);
308 test_side<P1, P2>(10, 0, 10, 1, 10, 2, 0);
309 test_side<P1, P2>(10, 0, 10, -1, 10, 2, 0);
310
311 test_side<P1, P2>(10, 0, 10, 1, 0, 2, 1); // left
312 test_side<P1, P2>(10, 0, 10, -1, 0, 2, -1); // right
313
314 test_side<P1, P2>(-10, -10, 10, 10, 10, 0, -1); // right
315 test_side<P1, P2>(-10, -10, 10, 10, -10, 0, 1); // left
316 test_side<P1, P2>(170, -10, -170, 10, -170, 0, -1); // right
317 test_side<P1, P2>(170, -10, -170, 10, 170, 0, 1); // left
318 }
319
320 template <typename P>
321 void test_all()
322 {
323 test_all<P, P>();
324 }
325
326 int test_main(int, char* [])
327 {
328 //test_all<float[2]>();
329 //test_all<double[2]>();
330 //test_all<bg::model::point<int, 2, bg::cs::geographic<bg::degree> > >();
331 //test_all<bg::model::point<float, 2, bg::cs::geographic<bg::degree> > >();
332 test_all<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
333
334 return 0;
335 }