• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define BOOST_GEOMETRY_NO_BOOST_TEST
16 
17 #if defined(_MSC_VER)
18 #  pragma warning( disable : 4244 )
19 #  pragma warning( disable : 4267 )
20 #endif
21 
22 #include <fstream>
23 #include <sstream>
24 
25 #include <boost/foreach.hpp>
26 #include <boost/program_options.hpp>
27 #include <boost/random/linear_congruential.hpp>
28 #include <boost/random/uniform_int.hpp>
29 #include <boost/random/uniform_real.hpp>
30 #include <boost/random/variate_generator.hpp>
31 #include <boost/timer.hpp>
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 
41 #include <geometry_test_common.hpp>
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>
create_svg(std::string const & filename,Geometry1 const & mp,Geometry2 const & buffer)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>
verify(std::string const & caseid,MultiPolygon const & mp,MultiPolygon const & buffer,Settings const & 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
93     double area_mp = bg::area(mp);
94     double area_buf = bg::area(buffer);
95 
96     if (area_buf < area_mp)
97     {
98         result = false;
99     }
100 
101     // Verify if all points are IN the buffer
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 
116     if (result)
117     {
118         bg::validity_failure_type failure;
119         if (! bg::is_valid(buffer, failure)
120             && failure != bg::failure_intersecting_interiors)
121         {
122             std::cout << "Buffer is not valid: " << bg::validity_failure_type_message(failure) << std::endl;
123             result = false;
124         }
125     }
126 
127     bool svg = settings.svg;
128     bool wkt = settings.wkt;
129     if (! result)
130     {
131         std::cout << "ERROR " << caseid << std::endl;
132         //std::cout << bg::wkt(mp) << std::endl;
133         //std::cout << bg::wkt(buffer) << std::endl;
134         svg = true;
135         wkt = true;
136     }
137 
138     if (svg || wkt)
139     {
140         //std::cout << caseid << std::endl;
141     }
142 
143     if (svg)
144     {
145         std::ostringstream filename;
146         filename << caseid << "_"
147             << typeid(typename bg::coordinate_type<MultiPolygon>::type).name()
148             << ".svg";
149         create_svg(filename.str(), mp, buffer);
150     }
151 
152     if (wkt)
153     {
154         std::ostringstream filename;
155         filename << caseid << "_"
156             << typeid(typename bg::coordinate_type<MultiPolygon>::type).name()
157             << ".wkt";
158         std::ofstream stream(filename.str().c_str());
159         stream << bg::wkt(mp) << std::endl;
160         stream << bg::wkt(buffer) << std::endl;
161     }
162 
163     return result;
164 }
165 
166 template <typename MultiPolygon, typename Generator, typename Settings>
test_buffer(MultiPolygon & result,int & index,Generator & generator,int level,Settings const & 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;
209     typedef bg::strategy::buffer::distance_asymmetric<coordinate_type> distance_strategy_type;
210     distance_strategy_type distance_strategy(settings.distance, settings.distance);
211 
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;
220     bg::strategy::buffer::join_round join_round_strategy(32); // Compatible with MySQL
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>
test_all(int seed,int count,int level,Settings const & settings)258 void test_all(int seed, int count, int level, Settings const& settings)
259 {
260     boost::timer t;
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;
278     for(int i = 0; i < count; i++)
279     {
280         mp p;
281         test_buffer<mp>(p, index, coordinate_generator, level, settings);
282     }
283     std::cout
284         << "geometries: " << index
285         << " type: " << typeid(T).name()
286         << " time: " << t.elapsed()  << std::endl;
287 }
288 
main(int argc,char ** argv)289 int main(int argc, char** argv)
290 {
291     BoostGeometryWriteTestConfiguration();
292     try
293     {
294         namespace po = boost::program_options;
295         po::options_description description("=== recursive_polygons_linear_areal ===\nAllowed options");
296 
297         int count = 1;
298         int seed = static_cast<unsigned int>(std::time(0));
299         int level = 3;
300         bool ccw = false;
301         bool open = false;
302         buffer_settings settings;
303         std::string form = "box";
304         std::string join = "round";
305 
306         description.add_options()
307             ("help", "Help message")
308             ("seed", po::value<int>(&seed), "Initialization seed for random generator")
309             ("count", po::value<int>(&count)->default_value(1), "Number of tests")
310             ("level", po::value<int>(&level)->default_value(3), "Level to reach (higher->slower)")
311             ("distance", po::value<double>(&settings.distance)->default_value(1.0), "Distance (1.0)")
312             ("form", po::value<std::string>(&form)->default_value("box"), "Form of the polygons (box, triangle)")
313             ("join", po::value<std::string>(&join)->default_value("round"), "Form of the joins (round, miter)")
314             ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
315             ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
316             ("size", po::value<int>(&settings.field_size)->default_value(10), "Size of the field")
317             ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
318             ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
319         ;
320 
321         po::variables_map varmap;
322         po::store(po::parse_command_line(argc, argv, description), varmap);
323         po::notify(varmap);
324 
325         if (varmap.count("help")
326             || (form != "box" && form != "triangle")
327             || (join != "round" && join != "miter")
328             )
329         {
330             std::cout << description << std::endl;
331             return 1;
332         }
333 
334         settings.triangular = form != "box";
335         settings.join_code = join == "round" ? 1 : 2;
336 
337         if (ccw && open)
338         {
339             test_all<double, false, false>(seed, count, level, settings);
340         }
341         else if (ccw)
342         {
343             test_all<double, false, true>(seed, count, level, settings);
344         }
345         else if (open)
346         {
347             test_all<double, true, false>(seed, count, level, settings);
348         }
349         else
350         {
351             test_all<double, true, true>(seed, count, level, settings);
352         }
353 
354 #if defined(HAVE_TTMATH)
355         // test_all<ttmath_big, true, true>(seed, count, max, svg, level);
356 #endif
357     }
358     catch(std::exception const& e)
359     {
360         std::cout << "Exception " << e.what() << std::endl;
361     }
362     catch(...)
363     {
364         std::cout << "Other exception" << std::endl;
365     }
366 
367     return 0;
368 }
369