1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2009-2012 Barend Gehrels, Amsterdam, the Netherlands.
5
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10
11 #define BOOST_GEOMETRY_REPORT_OVERLAY_ERROR
12 #define BOOST_GEOMETRY_NO_BOOST_TEST
13
14 #include <test_overlay_p_q.hpp>
15
16 #include <boost/program_options.hpp>
17 #include <boost/timer.hpp>
18
19 template <typename Polygon>
make_pie(Polygon & polygon,int count,int offset,int offset_y,double factor1,int total_segment_count=36)20 inline void make_pie(Polygon& polygon,
21 int count, int offset, int offset_y, double factor1, int total_segment_count = 36)
22 {
23 typedef typename bg::point_type<Polygon>::type p;
24 typedef typename bg::select_most_precise
25 <
26 typename bg::coordinate_type<Polygon>::type,
27 long double
28 >::type coordinate_type;
29
30 // Create pie
31 coordinate_type cx = 2500.0;
32 coordinate_type cy = 2500.0 + offset_y;
33
34 bg::exterior_ring(polygon).push_back(bg::make<p>(int(cx), int(cy)));
35
36 coordinate_type const dx = 5000.0;
37 coordinate_type const dy = 5000.0;
38
39 coordinate_type const half = 0.5;
40 coordinate_type const two = 2.0;
41
42 coordinate_type const a = coordinate_type(factor1) * half * dx;
43 coordinate_type const b = coordinate_type(factor1) * half * dy;
44
45 coordinate_type const pi = boost::math::constants::pi<long double>();
46 coordinate_type const delta = pi * two / total_segment_count;
47 coordinate_type angle = coordinate_type(offset) * delta;
48 for (int i = 0; i < count; i++, angle += delta)
49 {
50 coordinate_type const s = sin(angle);
51 coordinate_type const c = cos(angle);
52 coordinate_type const x = cx + a * s;
53 coordinate_type const y = cy + b * c;
54 bg::exterior_ring(polygon).push_back(bg::make<p>(int(x), int(y)));
55 }
56 bg::exterior_ring(polygon).push_back(bg::make<p>(int(cx), int(cy)));
57 bg::correct(polygon);
58 }
59
60
61 /*
62 sql server
63
64 TWO INNERS
65 1a) select geometry::STGeomFromText('POLYGON((0 0,0 10,10 10,10 0,0 0),(4 4,5 5,4 6,4 4),(6 6,5 5,6 4,6 6)) ', 0).STIsValid(); -> valid
66 same:
67 1b) POLYGON((0 0,0 10,10 10,10 0,0 0),(4 4,5 5,6 4,6 6,5 5,4 6,4 4)) -> valid
68
69 I-E tangency IET
70 2a) select geometry::STGeomFromText('POLYGON((0 0,0 10,10 10,10 0,0 0),(5 0,6 1,4 1,5 0))', 0).STIsValid(); -> valid
71 same by Self tangency ST
72 2b) select geometry::STGeomFromText('POLYGON((0 0,0 10,10 10,10 0,5 0,6 1,4 1,5 0, 0 0))', 0).STIsValid(); -> valid
73
74 Two inners all tangent
75 3a) POLYGON((0 0,0 10,10 10,10 0,0 0),(5 0,6 1,4 1,5 0),(5 1,6 2,4 2,5 1)) -> valid
76
77 postgis:
78 1a) select ST_IsValid(ST_GeomFromText('POLYGON((0 0,0 10,10 10,10 0,0 0),(4 4,5 5,4 6,4 4),(6 6,5 5,6 4,6 6))', 0)); -> valid
79 1b) POLYGON((0 0,0 10,10 10,10 0,0 0),(4 4,5 5,6 4,6 6,5 5,4 6,4 4)) -> NOT valid
80
81 2a) POLYGON((0 0,0 10,10 10,10 0,0 0),(5 0,6 1,4 1,5 0)) -> valid
82 2b) POLYGON((0 0,0 10,10 10,10 0,5 0,6 1,4 1,5 0, 0 0)) -> NOT valid (?)
83
84 3a) POLYGON((0 0,0 10,10 10,10 0,0 0),(5 0,6 1,4 1,5 0),(5 1,6 2,4 2,5 1)) -> valid
85
86
87 */
88
89 template <typename Polygon>
holify(Polygon & polygon)90 inline void holify(Polygon& polygon)
91 {
92 typedef typename bg::point_type<Polygon>::type point_type;
93
94 Polygon p;
95 bg::exterior_ring(p).push_back(bg::make<point_type>(0, 0));
96 bg::exterior_ring(p).push_back(bg::make<point_type>(0, 5000));
97 bg::exterior_ring(p).push_back(bg::make<point_type>(5000, 5000));
98 bg::exterior_ring(p).push_back(bg::make<point_type>(5000, 0));
99 bg::exterior_ring(p).push_back(bg::make<point_type>(0, 0));
100 bg::interior_rings(p).push_back(bg::exterior_ring(polygon));
101 bg::correct(p);
102
103 polygon = p;
104 }
105
106 template <typename MultiPolygon>
holify_multi(MultiPolygon & multi_polygon)107 inline void holify_multi(MultiPolygon& multi_polygon)
108 {
109 typedef typename bg::point_type<MultiPolygon>::type point_type;
110
111 typename boost::range_value<MultiPolygon>::type p;
112
113 bg::exterior_ring(p).push_back(bg::make<point_type>(0, 0));
114 bg::exterior_ring(p).push_back(bg::make<point_type>(0, 5000));
115 bg::exterior_ring(p).push_back(bg::make<point_type>(5000, 5000));
116 bg::exterior_ring(p).push_back(bg::make<point_type>(5000, 0));
117 bg::exterior_ring(p).push_back(bg::make<point_type>(0, 0));
118
119 for (int i = 0; i < multi_polygon.size(); i++)
120 {
121 bg::interior_rings(p).push_back(bg::exterior_ring(multi_polygon[i]));
122 }
123
124 bg::correct(p);
125
126 multi_polygon.clear();
127 multi_polygon.push_back(p);
128 }
129
130
131
132 template <typename T, bool Clockwise, bool Closed>
test_pie(int total_segment_count,T factor_p,T factor_q,bool multi,bool single_selftangent,p_q_settings const & settings)133 void test_pie(int total_segment_count, T factor_p, T factor_q,
134 bool multi, bool single_selftangent, p_q_settings const& settings)
135 {
136 boost::timer t;
137 typedef bg::model::d2::point_xy<T> point_type;
138 typedef bg::model::polygon<point_type, Clockwise, Closed> polygon;
139 typedef bg::model::multi_polygon<polygon> multi_polygon;
140
141 int good_count = 0;
142 int bad_count = 0;
143
144 for (int a = 2; a < total_segment_count; a++)
145 {
146 polygon p;
147 make_pie(p, a, 0, 0, factor_p, total_segment_count);
148
149 //holify(p);
150
151 for (int b = 2; b < total_segment_count; b++)
152 {
153 for (int offset = 1; offset < total_segment_count; offset++)
154 {
155 //for (int y = 0; y <= 2500; y += 500)
156 int y = 0;
157 {
158 polygon q;
159 make_pie(q, b, offset, y, factor_q, total_segment_count);
160
161 if (! multi)
162 {
163 //holify(q);
164
165 std::ostringstream out;
166 out << "pie_" << a << "_" << b << "_" << offset << "_" << y;
167 if (test_overlay_p_q<polygon, T>(out.str(), p, q, settings))
168 {
169 good_count++;
170 }
171 else
172 {
173 bad_count++;
174 }
175 }
176 else
177 {
178 int left = total_segment_count - b - 2;
179 //std::cout << a << " " << b << " " << left << std::endl;
180 for (int c = 2; c < left; c++)
181 {
182 polygon q2;
183 make_pie(q2, c, offset + b + 1, y, factor_q, total_segment_count);
184
185 std::ostringstream out;
186 out << "pie_" << a << "_" << b << "_" << offset << "_" << y
187 << "_" << c
188 ;
189
190 bool good = false;
191
192 // Represent as either multi-polygon, or as single-self-touching-polygon (INVALID)
193 if (single_selftangent)
194 {
195 polygon q1 = q;
196 for (unsigned int i = 1; i < q2.outer().size(); i++)
197 {
198 q1.outer().push_back(q2.outer()[i]);
199 }
200 //holify(q1);
201 good = test_overlay_p_q<polygon, T>(out.str(), p, q1, settings);
202 }
203 else
204 {
205 multi_polygon mq;
206 mq.push_back(q);
207 mq.push_back(q2);
208 //holify_multi(mq);
209 good = test_overlay_p_q<polygon, T>(out.str(), p, mq, settings);
210 }
211
212 if (good)
213 {
214 good_count++;
215 }
216 else
217 {
218 bad_count++;
219 }
220 }
221 }
222 }
223 }
224 }
225 }
226 std::cout
227 << "Time: " << t.elapsed() << std::endl
228 << "Good: " << good_count << std::endl
229 << "Bad: " << bad_count << std::endl;
230 }
231
232
233 template <typename T, bool Clockwise, bool Closed>
test_all(bool multi,bool single_selftangent,p_q_settings const & settings)234 void test_all(bool multi, bool single_selftangent, p_q_settings const& settings)
235 {
236 test_pie<T, Clockwise, Closed>(24, 0.55, 0.45, multi, single_selftangent, settings);
237 }
238
main(int argc,char ** argv)239 int main(int argc, char** argv)
240 {
241 try
242 {
243 namespace po = boost::program_options;
244 po::options_description description("=== intersection_pies ===\nAllowed options");
245
246 p_q_settings settings;
247 bool multi = false;
248 bool ccw = false;
249 bool open = false;
250 bool single_selftangent = false; // keep false, true does not work!
251
252 description.add_options()
253 ("help", "Help message")
254 ("multi", po::value<bool>(&multi)->default_value(false), "Multiple tangencies at one point")
255 ("diff", po::value<bool>(&settings.also_difference)->default_value(false), "Include testing on difference")
256 ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
257 ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
258 ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
259 ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
260 ;
261
262 po::variables_map varmap;
263 po::store(po::parse_command_line(argc, argv, description), varmap);
264 po::notify(varmap);
265
266 if (varmap.count("help"))
267 {
268 std::cout << description << std::endl;
269 return 1;
270 }
271
272 // template par's are: CoordinateType, Clockwise, Closed
273 if (ccw && open)
274 {
275 test_all<double, false, false>(multi, single_selftangent, settings);
276 }
277 else if (ccw)
278 {
279 test_all<double, false, true>(multi, single_selftangent, settings);
280 }
281 else if (open)
282 {
283 test_all<double, true, false>(multi, single_selftangent, settings);
284 }
285 else
286 {
287 test_all<double, true, true>(multi, single_selftangent, settings);
288 }
289 //test_all<long double>();
290 }
291 catch(std::exception const& e)
292 {
293 std::cout << "Exception " << e.what() << std::endl;
294 }
295 catch(...)
296 {
297 std::cout << "Other exception" << std::endl;
298 }
299 return 0;
300 }
301