]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | |
b32b8144 FG |
90 | double area_mp = bg::area(mp); |
91 | double area_buf = bg::area(buffer); | |
7c673cae FG |
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 | ||
b32b8144 FG |
112 | if (result) |
113 | { | |
114 | std::string message; | |
115 | if (! bg::is_valid(buffer, message)) | |
116 | { | |
117 | std::cout << "Buffer is not valid: " << message << std::endl; | |
118 | result = false; | |
119 | } | |
120 | } | |
121 | ||
7c673cae FG |
122 | bool svg = settings.svg; |
123 | bool wkt = settings.wkt; | |
124 | if (! result) | |
125 | { | |
126 | std::cout << "ERROR " << caseid << std::endl; | |
127 | //std::cout << bg::wkt(mp) << std::endl; | |
128 | //std::cout << bg::wkt(buffer) << std::endl; | |
129 | svg = true; | |
130 | wkt = true; | |
131 | } | |
132 | ||
133 | if (svg || wkt) | |
134 | { | |
135 | //std::cout << caseid << std::endl; | |
136 | } | |
137 | ||
138 | if (svg) | |
139 | { | |
140 | std::ostringstream filename; | |
141 | filename << caseid << "_" | |
142 | << typeid(typename bg::coordinate_type<MultiPolygon>::type).name() | |
143 | << ".svg"; | |
144 | create_svg(filename.str(), mp, buffer); | |
145 | } | |
146 | ||
147 | if (wkt) | |
148 | { | |
149 | std::ostringstream filename; | |
150 | filename << caseid << "_" | |
151 | << typeid(typename bg::coordinate_type<MultiPolygon>::type).name() | |
152 | << ".wkt"; | |
153 | std::ofstream stream(filename.str().c_str()); | |
154 | stream << bg::wkt(mp) << std::endl; | |
155 | stream << bg::wkt(buffer) << std::endl; | |
156 | } | |
157 | ||
158 | return result; | |
159 | } | |
160 | ||
161 | template <typename MultiPolygon, typename Generator, typename Settings> | |
162 | bool test_buffer(MultiPolygon& result, int& index, | |
163 | Generator& generator, | |
164 | int level, Settings const& settings) | |
165 | { | |
166 | MultiPolygon p, q; | |
167 | ||
168 | // Generate two boxes | |
169 | if (level == 0) | |
170 | { | |
171 | p.resize(1); | |
172 | q.resize(1); | |
173 | make_square_polygon(p.front(), generator, settings); | |
174 | make_square_polygon(q.front(), generator, settings); | |
175 | bg::correct(p); | |
176 | bg::correct(q); | |
177 | } | |
178 | else | |
179 | { | |
180 | bg::correct(p); | |
181 | bg::correct(q); | |
182 | if (! test_buffer(p, index, generator, level - 1, settings) | |
183 | || ! test_buffer(q, index, generator, level - 1, settings)) | |
184 | { | |
185 | return false; | |
186 | } | |
187 | } | |
188 | ||
189 | typedef typename boost::range_value<MultiPolygon>::type polygon; | |
190 | ||
191 | MultiPolygon mp; | |
192 | bg::detail::union_::union_insert | |
193 | < | |
194 | polygon | |
195 | >(p, q, std::back_inserter(mp)); | |
196 | ||
197 | bg::unique(mp); | |
198 | bg::unique(mp); | |
199 | bg::correct(mp); | |
200 | result = mp; | |
201 | ||
202 | ||
203 | typedef typename bg::coordinate_type<MultiPolygon>::type coordinate_type; | |
204 | typedef typename bg::point_type<MultiPolygon>::type point_type; | |
205 | typedef bg::strategy::buffer::distance_asymmetric<coordinate_type> distance_strategy_type; | |
206 | distance_strategy_type distance_strategy(settings.distance, settings.distance); | |
207 | ||
208 | typedef typename boost::range_value<MultiPolygon>::type polygon_type; | |
209 | MultiPolygon buffered; | |
210 | ||
211 | std::ostringstream out; | |
212 | out << "recursive_polygons_buffer_" << index++ << "_" << level; | |
213 | ||
214 | bg::strategy::buffer::end_round end_strategy; | |
215 | bg::strategy::buffer::point_circle point_strategy; | |
216 | bg::strategy::buffer::side_straight side_strategy; | |
b32b8144 | 217 | bg::strategy::buffer::join_round join_round_strategy(32); // Compatible with MySQL |
7c673cae FG |
218 | bg::strategy::buffer::join_miter join_miter_strategy; |
219 | ||
220 | try | |
221 | { | |
222 | switch(settings.join_code) | |
223 | { | |
224 | case 1 : | |
225 | bg::buffer(mp, buffered, | |
226 | distance_strategy, side_strategy, | |
227 | join_round_strategy, | |
228 | end_strategy, point_strategy); | |
229 | break; | |
230 | case 2 : | |
231 | bg::buffer(mp, buffered, | |
232 | distance_strategy, side_strategy, | |
233 | join_miter_strategy, | |
234 | end_strategy, point_strategy); | |
235 | break; | |
236 | default : | |
237 | return false; | |
238 | } | |
239 | } | |
240 | catch(std::exception const& e) | |
241 | { | |
242 | MultiPolygon empty; | |
243 | std::cout << out.str() << std::endl; | |
244 | std::cout << "Exception " << e.what() << std::endl; | |
245 | verify(out.str(), mp, empty, settings); | |
246 | return false; | |
247 | } | |
248 | ||
249 | ||
250 | return verify(out.str(), mp, buffered, settings); | |
251 | } | |
252 | ||
253 | ||
254 | template <typename T, bool Clockwise, bool Closed, typename Settings> | |
255 | void test_all(int seed, int count, int level, Settings const& settings) | |
256 | { | |
257 | boost::timer t; | |
258 | ||
259 | typedef boost::minstd_rand base_generator_type; | |
260 | ||
261 | base_generator_type generator(seed); | |
262 | ||
263 | boost::uniform_int<> random_coordinate(0, settings.field_size - 1); | |
264 | boost::variate_generator<base_generator_type&, boost::uniform_int<> > | |
265 | coordinate_generator(generator, random_coordinate); | |
266 | ||
267 | typedef bg::model::polygon | |
268 | < | |
269 | bg::model::d2::point_xy<T>, Clockwise, Closed | |
270 | > polygon; | |
271 | typedef bg::model::multi_polygon<polygon> mp; | |
272 | ||
273 | ||
274 | int index = 0; | |
275 | for(int i = 0; i < count; i++) | |
276 | { | |
277 | mp p; | |
278 | test_buffer<mp>(p, index, coordinate_generator, level, settings); | |
279 | } | |
280 | std::cout | |
281 | << "geometries: " << index | |
282 | << " type: " << typeid(T).name() | |
283 | << " time: " << t.elapsed() << std::endl; | |
284 | } | |
285 | ||
286 | int main(int argc, char** argv) | |
287 | { | |
288 | try | |
289 | { | |
290 | namespace po = boost::program_options; | |
291 | po::options_description description("=== recursive_polygons_linear_areal ===\nAllowed options"); | |
292 | ||
293 | int count = 1; | |
294 | int seed = static_cast<unsigned int>(std::time(0)); | |
295 | int level = 3; | |
296 | bool ccw = false; | |
297 | bool open = false; | |
298 | buffer_settings settings; | |
299 | std::string form = "box"; | |
300 | std::string join = "round"; | |
301 | ||
302 | description.add_options() | |
303 | ("help", "Help message") | |
304 | ("seed", po::value<int>(&seed), "Initialization seed for random generator") | |
305 | ("count", po::value<int>(&count)->default_value(1), "Number of tests") | |
306 | ("level", po::value<int>(&level)->default_value(3), "Level to reach (higher->slower)") | |
307 | ("distance", po::value<double>(&settings.distance)->default_value(1.0), "Distance (1.0)") | |
308 | ("form", po::value<std::string>(&form)->default_value("box"), "Form of the polygons (box, triangle)") | |
309 | ("join", po::value<std::string>(&join)->default_value("round"), "Form of the joins (round, miter)") | |
310 | ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons") | |
311 | ("open", po::value<bool>(&open)->default_value(false), "Open polygons") | |
312 | ("size", po::value<int>(&settings.field_size)->default_value(10), "Size of the field") | |
313 | ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests") | |
314 | ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests") | |
315 | ; | |
316 | ||
317 | po::variables_map varmap; | |
318 | po::store(po::parse_command_line(argc, argv, description), varmap); | |
319 | po::notify(varmap); | |
320 | ||
321 | if (varmap.count("help") | |
322 | || (form != "box" && form != "triangle") | |
323 | || (join != "round" && join != "miter") | |
324 | ) | |
325 | { | |
326 | std::cout << description << std::endl; | |
327 | return 1; | |
328 | } | |
329 | ||
330 | settings.triangular = form != "box"; | |
331 | settings.join_code = join == "round" ? 1 : 2; | |
332 | ||
333 | if (ccw && open) | |
334 | { | |
335 | test_all<double, false, false>(seed, count, level, settings); | |
336 | } | |
337 | else if (ccw) | |
338 | { | |
339 | test_all<double, false, true>(seed, count, level, settings); | |
340 | } | |
341 | else if (open) | |
342 | { | |
343 | test_all<double, true, false>(seed, count, level, settings); | |
344 | } | |
345 | else | |
346 | { | |
347 | test_all<double, true, true>(seed, count, level, settings); | |
348 | } | |
349 | ||
350 | #if defined(HAVE_TTMATH) | |
351 | // test_all<ttmath_big, true, true>(seed, count, max, svg, level); | |
352 | #endif | |
353 | } | |
354 | catch(std::exception const& e) | |
355 | { | |
356 | std::cout << "Exception " << e.what() << std::endl; | |
357 | } | |
358 | catch(...) | |
359 | { | |
360 | std::cout << "Other exception" << std::endl; | |
361 | } | |
362 | ||
363 | return 0; | |
364 | } |