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