]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/robustness/overlay/buffer/recursive_polygons_buffer.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / geometry / test / robustness / overlay / buffer / recursive_polygons_buffer.cpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Robustness Test
3
4 // Copyright (c) 2012-2015 Barend Gehrels, Amsterdam, the Netherlands.
5
6 // This file was modified by Oracle on 2015.
7 // Modifications copyright (c) 2015 Oracle and/or its affiliates.
8
9 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
10
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14
15 #if defined(_MSC_VER)
16 # pragma warning( disable : 4244 )
17 # pragma warning( disable : 4267 )
18 #endif
19
20 #include <fstream>
21 #include <sstream>
22
23 #include <boost/foreach.hpp>
24 #include <boost/program_options.hpp>
25 #include <boost/random/linear_congruential.hpp>
26 #include <boost/random/uniform_int.hpp>
27 #include <boost/random/uniform_real.hpp>
28 #include <boost/random/variate_generator.hpp>
29 #include <boost/timer.hpp>
30
31 #include <boost/geometry.hpp>
32 #include <boost/geometry/geometries/geometries.hpp>
33 #include <boost/geometry/geometries/point_xy.hpp>
34
35 #include <boost/geometry/algorithms/detail/buffer/buffer_inserter.hpp>
36
37 #include <boost/geometry/strategies/buffer.hpp>
38
39 #include <common/common_settings.hpp>
40 #include <common/make_square_polygon.hpp>
41
42
43 struct buffer_settings : public common_settings
44 {
45 int join_code;
46 double distance;
47 };
48
49 namespace bg = boost::geometry;
50
51 template <typename Geometry1, typename Geometry2>
52 void create_svg(std::string const& filename
53 , Geometry1 const& mp
54 , Geometry2 const& buffer
55 )
56 {
57 typedef typename boost::geometry::point_type<Geometry1>::type point_type;
58
59
60 std::ofstream svg(filename.c_str());
61 boost::geometry::svg_mapper<point_type> mapper(svg, 800, 800);
62
63 boost::geometry::model::box<point_type> box;
64 bg::envelope(mp, box);
65 bg::buffer(box, box, 1.0);
66 mapper.add(box);
67
68 if (! bg::is_empty(buffer))
69 {
70 bg::envelope(buffer, box);
71 bg::buffer(box, box, 1.0);
72 mapper.add(box);
73 }
74
75 mapper.map(mp, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3");
76 mapper.map(buffer, "stroke-opacity:0.9;stroke:rgb(0,0,0);fill:none;stroke-width:1");
77
78 //mapper.map(intersection,"opacity:0.6;stroke:rgb(0,128,0);stroke-width:5");
79 }
80
81
82
83
84 template <typename MultiPolygon, typename Settings>
85 bool verify(std::string const& caseid, MultiPolygon const& mp, MultiPolygon const& buffer, Settings const& settings)
86 {
87 bool result = true;
88
89 // Area of buffer must be larger than of original polygon
90 BOOST_AUTO(area_mp, bg::area(mp));
91 BOOST_AUTO(area_buf, bg::area(buffer));
92
93 if (area_buf < area_mp)
94 {
95 result = false;
96 }
97
98 if (result)
99 {
100 typedef typename boost::range_value<MultiPolygon const>::type polygon_type;
101 BOOST_FOREACH(polygon_type const& polygon, mp)
102 {
103 typename bg::point_type<polygon_type>::type point;
104 bg::point_on_border(point, polygon);
105 if (! bg::within(point, buffer))
106 {
107 result = false;
108 }
109 }
110 }
111
112 bool svg = settings.svg;
113 bool wkt = settings.wkt;
114 if (! result)
115 {
116 std::cout << "ERROR " << caseid << std::endl;
117 //std::cout << bg::wkt(mp) << std::endl;
118 //std::cout << bg::wkt(buffer) << std::endl;
119 svg = true;
120 wkt = true;
121 }
122
123 if (svg || wkt)
124 {
125 //std::cout << caseid << std::endl;
126 }
127
128 if (svg)
129 {
130 std::ostringstream filename;
131 filename << caseid << "_"
132 << typeid(typename bg::coordinate_type<MultiPolygon>::type).name()
133 << ".svg";
134 create_svg(filename.str(), mp, buffer);
135 }
136
137 if (wkt)
138 {
139 std::ostringstream filename;
140 filename << caseid << "_"
141 << typeid(typename bg::coordinate_type<MultiPolygon>::type).name()
142 << ".wkt";
143 std::ofstream stream(filename.str().c_str());
144 stream << bg::wkt(mp) << std::endl;
145 stream << bg::wkt(buffer) << std::endl;
146 }
147
148 return result;
149 }
150
151 template <typename MultiPolygon, typename Generator, typename Settings>
152 bool test_buffer(MultiPolygon& result, int& index,
153 Generator& generator,
154 int level, Settings const& settings)
155 {
156 MultiPolygon p, q;
157
158 // Generate two boxes
159 if (level == 0)
160 {
161 p.resize(1);
162 q.resize(1);
163 make_square_polygon(p.front(), generator, settings);
164 make_square_polygon(q.front(), generator, settings);
165 bg::correct(p);
166 bg::correct(q);
167 }
168 else
169 {
170 bg::correct(p);
171 bg::correct(q);
172 if (! test_buffer(p, index, generator, level - 1, settings)
173 || ! test_buffer(q, index, generator, level - 1, settings))
174 {
175 return false;
176 }
177 }
178
179 typedef typename boost::range_value<MultiPolygon>::type polygon;
180
181 MultiPolygon mp;
182 bg::detail::union_::union_insert
183 <
184 polygon
185 >(p, q, std::back_inserter(mp));
186
187 bg::unique(mp);
188 bg::unique(mp);
189 bg::correct(mp);
190 result = mp;
191
192
193 typedef typename bg::coordinate_type<MultiPolygon>::type coordinate_type;
194 typedef typename bg::point_type<MultiPolygon>::type point_type;
195 typedef bg::strategy::buffer::distance_asymmetric<coordinate_type> distance_strategy_type;
196 distance_strategy_type distance_strategy(settings.distance, settings.distance);
197
198 typedef typename boost::range_value<MultiPolygon>::type polygon_type;
199 MultiPolygon buffered;
200
201 std::ostringstream out;
202 out << "recursive_polygons_buffer_" << index++ << "_" << level;
203
204 bg::strategy::buffer::end_round end_strategy;
205 bg::strategy::buffer::point_circle point_strategy;
206 bg::strategy::buffer::side_straight side_strategy;
207 bg::strategy::buffer::join_round join_round_strategy(100); // Compatible with unit tests
208 bg::strategy::buffer::join_miter join_miter_strategy;
209
210 try
211 {
212 switch(settings.join_code)
213 {
214 case 1 :
215 bg::buffer(mp, buffered,
216 distance_strategy, side_strategy,
217 join_round_strategy,
218 end_strategy, point_strategy);
219 break;
220 case 2 :
221 bg::buffer(mp, buffered,
222 distance_strategy, side_strategy,
223 join_miter_strategy,
224 end_strategy, point_strategy);
225 break;
226 default :
227 return false;
228 }
229 }
230 catch(std::exception const& e)
231 {
232 MultiPolygon empty;
233 std::cout << out.str() << std::endl;
234 std::cout << "Exception " << e.what() << std::endl;
235 verify(out.str(), mp, empty, settings);
236 return false;
237 }
238
239
240 return verify(out.str(), mp, buffered, settings);
241 }
242
243
244 template <typename T, bool Clockwise, bool Closed, typename Settings>
245 void test_all(int seed, int count, int level, Settings const& settings)
246 {
247 boost::timer t;
248
249 typedef boost::minstd_rand base_generator_type;
250
251 base_generator_type generator(seed);
252
253 boost::uniform_int<> random_coordinate(0, settings.field_size - 1);
254 boost::variate_generator<base_generator_type&, boost::uniform_int<> >
255 coordinate_generator(generator, random_coordinate);
256
257 typedef bg::model::polygon
258 <
259 bg::model::d2::point_xy<T>, Clockwise, Closed
260 > polygon;
261 typedef bg::model::multi_polygon<polygon> mp;
262
263
264 int index = 0;
265 for(int i = 0; i < count; i++)
266 {
267 mp p;
268 test_buffer<mp>(p, index, coordinate_generator, level, settings);
269 }
270 std::cout
271 << "geometries: " << index
272 << " type: " << typeid(T).name()
273 << " time: " << t.elapsed() << std::endl;
274 }
275
276 int main(int argc, char** argv)
277 {
278 try
279 {
280 namespace po = boost::program_options;
281 po::options_description description("=== recursive_polygons_linear_areal ===\nAllowed options");
282
283 int count = 1;
284 int seed = static_cast<unsigned int>(std::time(0));
285 int level = 3;
286 bool ccw = false;
287 bool open = false;
288 buffer_settings settings;
289 std::string form = "box";
290 std::string join = "round";
291
292 description.add_options()
293 ("help", "Help message")
294 ("seed", po::value<int>(&seed), "Initialization seed for random generator")
295 ("count", po::value<int>(&count)->default_value(1), "Number of tests")
296 ("level", po::value<int>(&level)->default_value(3), "Level to reach (higher->slower)")
297 ("distance", po::value<double>(&settings.distance)->default_value(1.0), "Distance (1.0)")
298 ("form", po::value<std::string>(&form)->default_value("box"), "Form of the polygons (box, triangle)")
299 ("join", po::value<std::string>(&join)->default_value("round"), "Form of the joins (round, miter)")
300 ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
301 ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
302 ("size", po::value<int>(&settings.field_size)->default_value(10), "Size of the field")
303 ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
304 ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
305 ;
306
307 po::variables_map varmap;
308 po::store(po::parse_command_line(argc, argv, description), varmap);
309 po::notify(varmap);
310
311 if (varmap.count("help")
312 || (form != "box" && form != "triangle")
313 || (join != "round" && join != "miter")
314 )
315 {
316 std::cout << description << std::endl;
317 return 1;
318 }
319
320 settings.triangular = form != "box";
321 settings.join_code = join == "round" ? 1 : 2;
322
323 if (ccw && open)
324 {
325 test_all<double, false, false>(seed, count, level, settings);
326 }
327 else if (ccw)
328 {
329 test_all<double, false, true>(seed, count, level, settings);
330 }
331 else if (open)
332 {
333 test_all<double, true, false>(seed, count, level, settings);
334 }
335 else
336 {
337 test_all<double, true, true>(seed, count, level, settings);
338 }
339
340 #if defined(HAVE_TTMATH)
341 // test_all<ttmath_big, true, true>(seed, count, max, svg, level);
342 #endif
343 }
344 catch(std::exception const& e)
345 {
346 std::cout << "Exception " << e.what() << std::endl;
347 }
348 catch(...)
349 {
350 std::cout << "Other exception" << std::endl;
351 }
352
353 return 0;
354 }