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