]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/algorithms/distance/test_distance_geo_common.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / geometry / test / algorithms / distance / test_distance_geo_common.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2016-2017, Oracle and/or its affiliates.
5
6 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
7
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
10
11 #ifndef BOOST_GEOMETRY_TEST_DISTANCE_GEO_COMMON_HPP
12 #define BOOST_GEOMETRY_TEST_DISTANCE_GEO_COMMON_HPP
13
14 #include <iostream>
15 #include <string>
16
17 #include <boost/mpl/assert.hpp>
18 #include <boost/type_traits/is_integral.hpp>
19 #include <boost/type_traits/is_same.hpp>
20
21 #include <boost/geometry/geometries/point.hpp>
22 #include <boost/geometry/geometries/point_xy.hpp>
23 #include <boost/geometry/geometries/segment.hpp>
24 #include <boost/geometry/geometries/linestring.hpp>
25 #include <boost/geometry/geometries/polygon.hpp>
26 #include <boost/geometry/geometries/ring.hpp>
27 #include <boost/geometry/geometries/box.hpp>
28 #include <boost/geometry/geometries/multi_point.hpp>
29 #include <boost/geometry/geometries/multi_linestring.hpp>
30 #include <boost/geometry/geometries/multi_polygon.hpp>
31
32 #include <boost/geometry/io/wkt/write.hpp>
33 #include <boost/geometry/io/dsv/write.hpp>
34
35 #include <boost/geometry/algorithms/num_interior_rings.hpp>
36 #include <boost/geometry/algorithms/distance.hpp>
37
38 #include <boost/geometry/strategies/strategies.hpp>
39
40 #include <from_wkt.hpp>
41 #include <string_from_type.hpp>
42
43 #include "distance_brute_force.hpp"
44
45 namespace bg = ::boost::geometry;
46
47 //========================================================================
48
49
50 template <typename T>
51 struct check_equal
52 {
53 template <typename Value, typename = void>
54 struct equal_to
55 {
56 static inline void apply(Value const& x, Value const& y)
57 {
58 BOOST_CHECK(x == y);
59 }
60 };
61
62 template <typename Dummy>
63 struct equal_to<double, Dummy>
64 {
65 static inline void apply(double x, double y)
66 {
67 BOOST_CHECK_CLOSE(x, y, 0.001);
68 }
69 };
70
71 template <typename Geometry1, typename Geometry2>
72 static inline void apply(std::string const& /*case_id*/,
73 std::string const& /*subcase_id*/,
74 Geometry1 const& /*geometry1*/,
75 Geometry2 const& /*geometry2*/,
76 T const& detected,
77 T const& expected)
78 {
79 equal_to<T>::apply(expected, detected);
80 }
81 };
82
83 //========================================================================
84
85 template
86 <
87 typename Geometry1, typename Geometry2,
88 int id1 = bg::geometry_id<Geometry1>::value,
89 int id2 = bg::geometry_id<Geometry2>::value
90 >
91 struct test_distance_of_geometries
92 : public test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
93 {};
94
95
96 template <typename Geometry1, typename Geometry2>
97 struct test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
98 {
99 template <typename DistanceType, typename Strategy>
100 static inline
101 void apply(std::string const& case_id,
102 std::string const& wkt1,
103 std::string const& wkt2,
104 DistanceType const& expected_distance,
105 Strategy const& strategy,
106 bool test_reversed = true)
107 {
108 Geometry1 geometry1 = from_wkt<Geometry1>(wkt1);
109 Geometry2 geometry2 = from_wkt<Geometry2>(wkt2);
110
111 apply(case_id, geometry1, geometry2,
112 expected_distance,
113 strategy, test_reversed);
114 }
115
116
117 template
118 <
119 typename DistanceType,
120 typename Strategy
121 >
122 static inline
123 void apply(std::string const& case_id,
124 Geometry1 const& geometry1,
125 Geometry2 const& geometry2,
126 DistanceType const& expected_distance,
127 Strategy const& strategy,
128 bool test_reversed = true)
129 {
130 #ifdef BOOST_GEOMETRY_TEST_DEBUG
131 std::cout << "case ID: " << case_id << "; "
132 << "G1: " << bg::wkt(geometry1)
133 << " - "
134 << "G2: " << bg::wkt(geometry2)
135 << std::endl;
136 #endif
137 namespace services = bg::strategy::distance::services;
138
139 using bg::unit_test::distance_brute_force;
140
141 typedef typename bg::default_distance_result
142 <
143 Geometry1, Geometry2
144 >::type default_distance_result;
145
146 typedef typename services::return_type
147 <
148 Strategy, Geometry1, Geometry2
149 >::type distance_result_from_strategy;
150
151 static const bool same_regular = boost::is_same
152 <
153 default_distance_result,
154 distance_result_from_strategy
155 >::type::value;
156
157 BOOST_CHECK(same_regular);
158
159 // check distance with passed strategy
160 distance_result_from_strategy dist =
161 bg::distance(geometry1, geometry2, strategy);
162
163 check_equal
164 <
165 distance_result_from_strategy
166 >::apply(case_id, "a", geometry1, geometry2,
167 dist, expected_distance);
168
169 // check against the comparable distance computed in a
170 // brute-force manner
171 default_distance_result dist_brute_force
172 = distance_brute_force(geometry1, geometry2, strategy);
173
174 check_equal
175 <
176 default_distance_result
177 >::apply(case_id, "b", geometry1, geometry2,
178 dist_brute_force, expected_distance);
179
180 #ifdef BOOST_GEOMETRY_TEST_DEBUG
181 std::cout << string_from_type<typename bg::coordinate_type<Geometry1>::type>::name()
182 << string_from_type<typename bg::coordinate_type<Geometry2>::type>::name()
183 << " -> "
184 << string_from_type<default_distance_result>::name()
185 << std::endl;
186 std::cout << "expected distance = "
187 << expected_distance << " ; "
188 << std::endl;
189 std::cout << "distance = "
190 << dist << " ; "
191 << std::endl;
192
193 if ( !test_reversed )
194 {
195 std::cout << std::endl;
196 }
197 #endif
198
199 if ( test_reversed )
200 {
201 // check distance with given strategy
202 dist = bg::distance(geometry2, geometry1, strategy);
203
204 check_equal
205 <
206 default_distance_result
207 >::apply(case_id, "ra", geometry2, geometry1,
208 dist, expected_distance);
209
210 #ifdef BOOST_GEOMETRY_TEST_DEBUG
211 std::cout << "distance[reversed args] = "
212 << dist << " ; "
213 << "comp. distance[reversed args] = "
214 //<< cdist
215 << std::endl;
216 std::cout << std::endl;
217 #endif
218 }
219
220 }
221 };
222
223
224 //========================================================================
225
226
227 template <typename Geometry1, typename Geometry2, typename Strategy>
228 void test_empty_input(Geometry1 const& geometry1,
229 Geometry2 const& geometry2,
230 Strategy const& strategy)
231 {
232 try
233 {
234 bg::distance(geometry1, geometry2);
235 }
236 catch(bg::empty_input_exception const& )
237 {
238 return;
239 }
240 BOOST_CHECK_MESSAGE(false,
241 "A empty_input_exception should have been thrown");
242
243 try
244 {
245 bg::distance(geometry2, geometry1);
246 }
247 catch(bg::empty_input_exception const& )
248 {
249 return;
250 }
251 BOOST_CHECK_MESSAGE(false,
252 "A empty_input_exception should have been thrown");
253
254 try
255 {
256 bg::distance(geometry1, geometry2, strategy);
257 }
258 catch(bg::empty_input_exception const& )
259 {
260 return;
261 }
262 BOOST_CHECK_MESSAGE(false,
263 "A empty_input_exception should have been thrown");
264
265 try
266 {
267 bg::distance(geometry2, geometry1, strategy);
268 }
269 catch(bg::empty_input_exception const& )
270 {
271 return;
272 }
273 BOOST_CHECK_MESSAGE(false,
274 "A empty_input_exception should have been thrown");
275 }
276
277 #endif // BOOST_GEOMETRY_TEST_DISTANCE_GEO_COMMON_HPP