]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/algorithms/overlay/test_get_turns.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / geometry / test / algorithms / overlay / test_get_turns.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
6
7 // This file was modified by Oracle on 2014, 2016.
8 // Modifications copyright (c) 2014-2016 Oracle and/or its affiliates.
9
10 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
11 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
12
13 // Use, modification and distribution is subject to the Boost Software License,
14 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
16
17 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
18
19 #ifndef BOOST_GEOMETRY_TEST_ALGORITHMS_OVERLAY_TEST_GET_TURNS_HPP
20 #define BOOST_GEOMETRY_TEST_ALGORITHMS_OVERLAY_TEST_GET_TURNS_HPP
21
22 #include <iostream>
23 #include <iomanip>
24
25 #include <geometry_test_common.hpp>
26
27 #include <boost/geometry/strategies/strategies.hpp>
28
29 #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
30
31 #include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
32
33 #include <boost/geometry/io/wkt/read.hpp>
34 #include <boost/geometry/io/wkt/write.hpp>
35
36 struct expected_pusher
37 {
38 void push_back(std::string const& ex)
39 {
40 vec.push_back(ex);
41 }
42
43 expected_pusher & operator()(std::string const& ex)
44 {
45 push_back(ex);
46 return *this;
47 }
48
49 typedef std::vector<std::string>::iterator iterator;
50 typedef std::vector<std::string>::const_iterator const_iterator;
51
52 iterator begin() { return vec.begin(); }
53 iterator end() { return vec.end(); }
54 const_iterator begin() const { return vec.begin(); }
55 const_iterator end() const { return vec.end(); }
56
57 std::vector<std::string> vec;
58 };
59
60 inline expected_pusher expected(std::string const& ex)
61 {
62 expected_pusher res;
63 return res(ex);
64 }
65
66 struct equal_turn
67 {
68 equal_turn(std::string const& s) : turn_ptr(&s) {}
69
70 template <typename T>
71 bool operator()(T const& t) const
72 {
73 std::string const& s = (*turn_ptr);
74 std::string::size_type const count = s.size();
75
76 return (count > 0
77 ? bg::method_char(t.method) == s[0]
78 : true)
79 && (count > 1
80 ? bg::operation_char(t.operations[0].operation) == s[1]
81 : true)
82 && (count > 2
83 ? bg::operation_char(t.operations[1].operation) == s[2]
84 : true)
85 && equal_operations_ex(t.operations[0], t.operations[1], s);
86 }
87
88 template <typename P, typename R>
89 static bool equal_operations_ex(bg::detail::overlay::turn_operation<P, R> const& op0,
90 bg::detail::overlay::turn_operation<P, R> const& op1,
91 std::string const& s)
92 {
93 return true;
94 }
95
96 template <typename P, typename R>
97 static bool equal_operations_ex(bg::detail::overlay::turn_operation_linear<P, R> const& op0,
98 bg::detail::overlay::turn_operation_linear<P, R> const& op1,
99 std::string const& s)
100 {
101 std::string::size_type const count = s.size();
102
103 return (count > 3
104 ? is_colinear_char(op0.is_collinear) == s[3]
105 : true)
106 && (count > 4
107 ? is_colinear_char(op1.is_collinear) == s[4]
108 : true);
109 }
110
111 static char is_colinear_char(bool is_collinear)
112 {
113 return is_collinear ? '=' : '+';
114 }
115
116 const std::string * turn_ptr;
117 };
118
119 template <typename Geometry1, typename Geometry2, typename Expected>
120 void check_geometry_range(Geometry1 const& g1,
121 Geometry2 const& g2,
122 std::string const& wkt1,
123 std::string const& wkt2,
124 Expected const& expected)
125 {
126 typedef bg::detail::no_rescale_policy robust_policy_type;
127 typedef typename bg::point_type<Geometry2>::type point_type;
128
129 typedef typename bg::segment_ratio_type<point_type, robust_policy_type>::type segment_ratio_type;
130
131 typedef bg::detail::overlay::turn_info
132 <
133 typename bg::point_type<Geometry2>::type,
134 segment_ratio_type,
135 typename bg::detail::get_turns::turn_operation_type
136 <
137 Geometry1,
138 Geometry2,
139 segment_ratio_type
140 >::type
141 > turn_info;
142 typedef bg::detail::overlay::assign_null_policy assign_policy_t;
143 typedef bg::detail::get_turns::no_interrupt_policy interrupt_policy_t;
144
145 std::vector<turn_info> turns;
146 interrupt_policy_t interrupt_policy;
147 robust_policy_type robust_policy;
148
149 // Don't switch the geometries
150 typedef bg::detail::get_turns::get_turn_info_type<Geometry1, Geometry2, assign_policy_t> turn_policy_t;
151 bg::dispatch::get_turns
152 <
153 typename bg::tag<Geometry1>::type, typename bg::tag<Geometry2>::type,
154 Geometry1, Geometry2, false, false,
155 turn_policy_t
156 >::apply(0, g1, 1, g2, robust_policy, turns, interrupt_policy);
157
158 bool ok = boost::size(expected) == turns.size();
159
160 #ifdef BOOST_GEOMETRY_TEST_DEBUG
161 std::vector<turn_info> turns_dbg = turns;
162 #endif
163
164 BOOST_CHECK_MESSAGE(ok,
165 "get_turns: " << wkt1 << " and " << wkt2
166 << " -> Expected turns #: " << boost::size(expected) << " detected turns #: " << turns.size());
167
168 for ( typename boost::range_iterator<Expected const>::type sit = boost::begin(expected) ;
169 sit != boost::end(expected) ; ++sit)
170 {
171 typename std::vector<turn_info>::iterator
172 it = std::find_if(turns.begin(), turns.end(), equal_turn(*sit));
173
174 if ( it != turns.end() )
175 turns.erase(it);
176 else
177 {
178 ok = false;
179 BOOST_CHECK_MESSAGE(false,
180 "get_turns: " << wkt1 << " and " << wkt2
181 << " -> Expected turn: " << *sit << " not found");
182 }
183 }
184
185 #ifdef BOOST_GEOMETRY_TEST_DEBUG
186 if ( !ok )
187 {
188 std::cout << "Coordinates: "
189 << typeid(typename bg::coordinate_type<Geometry1>::type).name()
190 << ", "
191 << typeid(typename bg::coordinate_type<Geometry2>::type).name()
192 << std::endl;
193 std::cout << "Detected: ";
194 if ( turns_dbg.empty() )
195 {
196 std::cout << "{empty}";
197 }
198 else
199 {
200 for ( typename std::vector<turn_info>::const_iterator it = turns_dbg.begin() ;
201 it != turns_dbg.end() ; ++it )
202 {
203 if ( it != turns_dbg.begin() )
204 std::cout << ", ";
205 std::cout << bg::method_char(it->method);
206 std::cout << bg::operation_char(it->operations[0].operation);
207 std::cout << bg::operation_char(it->operations[1].operation);
208 std::cout << equal_turn<1>::is_colinear_char(it->operations[0].is_collinear);
209 std::cout << equal_turn<1>::is_colinear_char(it->operations[1].is_collinear);
210 }
211 }
212 std::cout << std::endl;
213 }
214 #endif
215 }
216
217 template <typename Geometry1, typename Geometry2, typename Expected>
218 void test_geometry_range(std::string const& wkt1, std::string const& wkt2,
219 Expected const& expected)
220 {
221 Geometry1 geometry1;
222 Geometry2 geometry2;
223 bg::read_wkt(wkt1, geometry1);
224 bg::read_wkt(wkt2, geometry2);
225 check_geometry_range(geometry1, geometry2, wkt1, wkt2, expected);
226 }
227
228 template <typename G1, typename G2>
229 void test_geometry(std::string const& wkt1, std::string const& wkt2,
230 std::string const& ex0)
231 {
232 test_geometry_range<G1, G2>(wkt1, wkt2, expected(ex0));
233 }
234
235 template <typename G1, typename G2>
236 void test_geometry(std::string const& wkt1, std::string const& wkt2,
237 std::string const& ex0, std::string const& ex1)
238 {
239 test_geometry_range<G1, G2>(wkt1, wkt2, expected(ex0)(ex1));
240 }
241
242 template <typename G1, typename G2>
243 void test_geometry(std::string const& wkt1, std::string const& wkt2,
244 std::string const& ex0, std::string const& ex1, std::string const& ex2)
245 {
246 test_geometry_range<G1, G2>(wkt1, wkt2, expected(ex0)(ex1)(ex2));
247 }
248
249 template <typename G1, typename G2>
250 void test_geometry(std::string const& wkt1, std::string const& wkt2,
251 expected_pusher const& expected)
252 {
253 test_geometry_range<G1, G2>(wkt1, wkt2, expected);
254 }
255
256 #endif // BOOST_GEOMETRY_TEST_ALGORITHMS_OVERLAY_TEST_GET_TURNS_HPP