1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2016-2017, Oracle and/or its affiliates.
6 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
11 #ifndef BOOST_GEOMETRY_TEST_DISTANCE_GEO_COMMON_HPP
12 #define BOOST_GEOMETRY_TEST_DISTANCE_GEO_COMMON_HPP
17 #include <boost/mpl/assert.hpp>
18 #include <boost/type_traits/is_integral.hpp>
19 #include <boost/type_traits/is_same.hpp>
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>
32 #include <boost/geometry/io/wkt/write.hpp>
33 #include <boost/geometry/io/dsv/write.hpp>
35 #include <boost/geometry/algorithms/num_interior_rings.hpp>
36 #include <boost/geometry/algorithms/distance.hpp>
38 #include <boost/geometry/strategies/strategies.hpp>
40 #include <from_wkt.hpp>
41 #include <string_from_type.hpp>
43 #include "distance_brute_force.hpp"
45 namespace bg = ::boost::geometry;
47 //========================================================================
53 template <typename Value, typename = void>
56 static inline void apply(Value const& x, Value const& y)
62 template <typename Dummy>
63 struct equal_to<double, Dummy>
65 static inline void apply(double x, double y)
67 BOOST_CHECK_CLOSE(x, y, 0.001);
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*/,
79 equal_to<T>::apply(expected, detected);
83 //========================================================================
87 typename Geometry1, typename Geometry2,
88 int id1 = bg::geometry_id<Geometry1>::value,
89 int id2 = bg::geometry_id<Geometry2>::value
91 struct test_distance_of_geometries
92 : public test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
96 template <typename Geometry1, typename Geometry2>
97 struct test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
99 template <typename DistanceType, typename Strategy>
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)
108 Geometry1 geometry1 = from_wkt<Geometry1>(wkt1);
109 Geometry2 geometry2 = from_wkt<Geometry2>(wkt2);
111 apply(case_id, geometry1, geometry2,
113 strategy, test_reversed);
119 typename DistanceType,
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)
130 #ifdef BOOST_GEOMETRY_TEST_DEBUG
131 std::cout << "case ID: " << case_id << "; "
132 << "G1: " << bg::wkt(geometry1)
134 << "G2: " << bg::wkt(geometry2)
137 namespace services = bg::strategy::distance::services;
139 using bg::unit_test::distance_brute_force;
141 typedef typename bg::default_distance_result
144 >::type default_distance_result;
146 typedef typename services::return_type
148 Strategy, Geometry1, Geometry2
149 >::type distance_result_from_strategy;
151 static const bool same_regular = boost::is_same
153 default_distance_result,
154 distance_result_from_strategy
157 BOOST_CHECK(same_regular);
159 // check distance with passed strategy
160 distance_result_from_strategy dist =
161 bg::distance(geometry1, geometry2, strategy);
165 distance_result_from_strategy
166 >::apply(case_id, "a", geometry1, geometry2,
167 dist, expected_distance);
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);
176 default_distance_result
177 >::apply(case_id, "b", geometry1, geometry2,
178 dist_brute_force, expected_distance);
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()
184 << string_from_type<default_distance_result>::name()
186 std::cout << "expected distance = "
187 << expected_distance << " ; "
189 std::cout << "distance = "
193 if ( !test_reversed )
195 std::cout << std::endl;
201 // check distance with given strategy
202 dist = bg::distance(geometry2, geometry1, strategy);
206 default_distance_result
207 >::apply(case_id, "ra", geometry2, geometry1,
208 dist, expected_distance);
210 #ifdef BOOST_GEOMETRY_TEST_DEBUG
211 std::cout << "distance[reversed args] = "
213 << "comp. distance[reversed args] = "
216 std::cout << std::endl;
224 //========================================================================
227 template <typename Geometry1, typename Geometry2, typename Strategy>
228 void test_empty_input(Geometry1 const& geometry1,
229 Geometry2 const& geometry2,
230 Strategy const& strategy)
234 bg::distance(geometry1, geometry2);
236 catch(bg::empty_input_exception const& )
240 BOOST_CHECK_MESSAGE(false,
241 "A empty_input_exception should have been thrown");
245 bg::distance(geometry2, geometry1);
247 catch(bg::empty_input_exception const& )
251 BOOST_CHECK_MESSAGE(false,
252 "A empty_input_exception should have been thrown");
256 bg::distance(geometry1, geometry2, strategy);
258 catch(bg::empty_input_exception const& )
262 BOOST_CHECK_MESSAGE(false,
263 "A empty_input_exception should have been thrown");
267 bg::distance(geometry2, geometry1, strategy);
269 catch(bg::empty_input_exception const& )
273 BOOST_CHECK_MESSAGE(false,
274 "A empty_input_exception should have been thrown");
277 #endif // BOOST_GEOMETRY_TEST_DISTANCE_GEO_COMMON_HPP