1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2012-2015 Barend Gehrels, Amsterdam, the Netherlands.
6 // This file was modified by Oracle on 2015.
7 // Modifications copyright (c) 2015 Oracle and/or its affiliates.
9 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
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)
16 # pragma warning( disable : 4244 )
17 # pragma warning( disable : 4267 )
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>
31 #include <boost/geometry.hpp>
32 #include <boost/geometry/geometries/geometries.hpp>
33 #include <boost/geometry/geometries/point_xy.hpp>
35 #include <boost/geometry/algorithms/detail/buffer/buffer_inserter.hpp>
37 #include <boost/geometry/strategies/buffer.hpp>
39 #include <common/common_settings.hpp>
40 #include <common/make_square_polygon.hpp>
43 struct buffer_settings
: public common_settings
49 namespace bg
= boost::geometry
;
51 template <typename Geometry1
, typename Geometry2
>
52 void create_svg(std::string
const& filename
54 , Geometry2
const& buffer
57 typedef typename
boost::geometry::point_type
<Geometry1
>::type point_type
;
60 std::ofstream
svg(filename
.c_str());
61 boost::geometry::svg_mapper
<point_type
> mapper(svg
, 800, 800);
63 boost::geometry::model::box
<point_type
> box
;
64 bg::envelope(mp
, box
);
65 bg::buffer(box
, box
, 1.0);
68 if (! bg::is_empty(buffer
))
70 bg::envelope(buffer
, box
);
71 bg::buffer(box
, box
, 1.0);
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");
78 //mapper.map(intersection,"opacity:0.6;stroke:rgb(0,128,0);stroke-width:5");
84 template <typename MultiPolygon
, typename Settings
>
85 bool verify(std::string
const& caseid
, MultiPolygon
const& mp
, MultiPolygon
const& buffer
, Settings
const& settings
)
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
));
93 if (area_buf
< area_mp
)
100 typedef typename
boost::range_value
<MultiPolygon
const>::type polygon_type
;
101 BOOST_FOREACH(polygon_type
const& polygon
, mp
)
103 typename
bg::point_type
<polygon_type
>::type point
;
104 bg::point_on_border(point
, polygon
);
105 if (! bg::within(point
, buffer
))
112 bool svg
= settings
.svg
;
113 bool wkt
= settings
.wkt
;
116 std::cout
<< "ERROR " << caseid
<< std::endl
;
117 //std::cout << bg::wkt(mp) << std::endl;
118 //std::cout << bg::wkt(buffer) << std::endl;
125 //std::cout << caseid << std::endl;
130 std::ostringstream filename
;
131 filename
<< caseid
<< "_"
132 << typeid(typename
bg::coordinate_type
<MultiPolygon
>::type
).name()
134 create_svg(filename
.str(), mp
, buffer
);
139 std::ostringstream filename
;
140 filename
<< caseid
<< "_"
141 << typeid(typename
bg::coordinate_type
<MultiPolygon
>::type
).name()
143 std::ofstream
stream(filename
.str().c_str());
144 stream
<< bg::wkt(mp
) << std::endl
;
145 stream
<< bg::wkt(buffer
) << std::endl
;
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
)
158 // Generate two boxes
163 make_square_polygon(p
.front(), generator
, settings
);
164 make_square_polygon(q
.front(), generator
, settings
);
172 if (! test_buffer(p
, index
, generator
, level
- 1, settings
)
173 || ! test_buffer(q
, index
, generator
, level
- 1, settings
))
179 typedef typename
boost::range_value
<MultiPolygon
>::type polygon
;
182 bg::detail::union_::union_insert
185 >(p
, q
, std::back_inserter(mp
));
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
);
198 typedef typename
boost::range_value
<MultiPolygon
>::type polygon_type
;
199 MultiPolygon buffered
;
201 std::ostringstream out
;
202 out
<< "recursive_polygons_buffer_" << index
++ << "_" << level
;
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
;
212 switch(settings
.join_code
)
215 bg::buffer(mp
, buffered
,
216 distance_strategy
, side_strategy
,
218 end_strategy
, point_strategy
);
221 bg::buffer(mp
, buffered
,
222 distance_strategy
, side_strategy
,
224 end_strategy
, point_strategy
);
230 catch(std::exception
const& e
)
233 std::cout
<< out
.str() << std::endl
;
234 std::cout
<< "Exception " << e
.what() << std::endl
;
235 verify(out
.str(), mp
, empty
, settings
);
240 return verify(out
.str(), mp
, buffered
, settings
);
244 template <typename T
, bool Clockwise
, bool Closed
, typename Settings
>
245 void test_all(int seed
, int count
, int level
, Settings
const& settings
)
249 typedef boost::minstd_rand base_generator_type
;
251 base_generator_type
generator(seed
);
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
);
257 typedef bg::model::polygon
259 bg::model::d2::point_xy
<T
>, Clockwise
, Closed
261 typedef bg::model::multi_polygon
<polygon
> mp
;
265 for(int i
= 0; i
< count
; i
++)
268 test_buffer
<mp
>(p
, index
, coordinate_generator
, level
, settings
);
271 << "geometries: " << index
272 << " type: " << typeid(T
).name()
273 << " time: " << t
.elapsed() << std::endl
;
276 int main(int argc
, char** argv
)
280 namespace po
= boost::program_options
;
281 po::options_description
description("=== recursive_polygons_linear_areal ===\nAllowed options");
284 int seed
= static_cast<unsigned int>(std::time(0));
288 buffer_settings settings
;
289 std::string form
= "box";
290 std::string join
= "round";
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")
307 po::variables_map varmap
;
308 po::store(po::parse_command_line(argc
, argv
, description
), varmap
);
311 if (varmap
.count("help")
312 || (form
!= "box" && form
!= "triangle")
313 || (join
!= "round" && join
!= "miter")
316 std::cout
<< description
<< std::endl
;
320 settings
.triangular
= form
!= "box";
321 settings
.join_code
= join
== "round" ? 1 : 2;
325 test_all
<double, false, false>(seed
, count
, level
, settings
);
329 test_all
<double, false, true>(seed
, count
, level
, settings
);
333 test_all
<double, true, false>(seed
, count
, level
, settings
);
337 test_all
<double, true, true>(seed
, count
, level
, settings
);
340 #if defined(HAVE_TTMATH)
341 // test_all<ttmath_big, true, true>(seed, count, max, svg, level);
344 catch(std::exception
const& e
)
346 std::cout
<< "Exception " << e
.what() << std::endl
;
350 std::cout
<< "Other exception" << std::endl
;