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