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 double area_mp
= bg::area(mp
);
91 double 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
))
115 if (! bg::is_valid(buffer
, message
))
117 std::cout
<< "Buffer is not valid: " << message
<< std::endl
;
122 bool svg
= settings
.svg
;
123 bool wkt
= settings
.wkt
;
126 std::cout
<< "ERROR " << caseid
<< std::endl
;
127 //std::cout << bg::wkt(mp) << std::endl;
128 //std::cout << bg::wkt(buffer) << std::endl;
135 //std::cout << caseid << std::endl;
140 std::ostringstream filename
;
141 filename
<< caseid
<< "_"
142 << typeid(typename
bg::coordinate_type
<MultiPolygon
>::type
).name()
144 create_svg(filename
.str(), mp
, buffer
);
149 std::ostringstream filename
;
150 filename
<< caseid
<< "_"
151 << typeid(typename
bg::coordinate_type
<MultiPolygon
>::type
).name()
153 std::ofstream
stream(filename
.str().c_str());
154 stream
<< bg::wkt(mp
) << std::endl
;
155 stream
<< bg::wkt(buffer
) << std::endl
;
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
)
168 // Generate two boxes
173 make_square_polygon(p
.front(), generator
, settings
);
174 make_square_polygon(q
.front(), generator
, settings
);
182 if (! test_buffer(p
, index
, generator
, level
- 1, settings
)
183 || ! test_buffer(q
, index
, generator
, level
- 1, settings
))
189 typedef typename
boost::range_value
<MultiPolygon
>::type polygon
;
192 bg::detail::union_::union_insert
195 >(p
, q
, std::back_inserter(mp
));
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
);
208 typedef typename
boost::range_value
<MultiPolygon
>::type polygon_type
;
209 MultiPolygon buffered
;
211 std::ostringstream out
;
212 out
<< "recursive_polygons_buffer_" << index
++ << "_" << level
;
214 bg::strategy::buffer::end_round end_strategy
;
215 bg::strategy::buffer::point_circle point_strategy
;
216 bg::strategy::buffer::side_straight side_strategy
;
217 bg::strategy::buffer::join_round
join_round_strategy(32); // Compatible with MySQL
218 bg::strategy::buffer::join_miter join_miter_strategy
;
222 switch(settings
.join_code
)
225 bg::buffer(mp
, buffered
,
226 distance_strategy
, side_strategy
,
228 end_strategy
, point_strategy
);
231 bg::buffer(mp
, buffered
,
232 distance_strategy
, side_strategy
,
234 end_strategy
, point_strategy
);
240 catch(std::exception
const& e
)
243 std::cout
<< out
.str() << std::endl
;
244 std::cout
<< "Exception " << e
.what() << std::endl
;
245 verify(out
.str(), mp
, empty
, settings
);
250 return verify(out
.str(), mp
, buffered
, settings
);
254 template <typename T
, bool Clockwise
, bool Closed
, typename Settings
>
255 void test_all(int seed
, int count
, int level
, Settings
const& settings
)
259 typedef boost::minstd_rand base_generator_type
;
261 base_generator_type
generator(seed
);
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
);
267 typedef bg::model::polygon
269 bg::model::d2::point_xy
<T
>, Clockwise
, Closed
271 typedef bg::model::multi_polygon
<polygon
> mp
;
275 for(int i
= 0; i
< count
; i
++)
278 test_buffer
<mp
>(p
, index
, coordinate_generator
, level
, settings
);
281 << "geometries: " << index
282 << " type: " << typeid(T
).name()
283 << " time: " << t
.elapsed() << std::endl
;
286 int main(int argc
, char** argv
)
290 namespace po
= boost::program_options
;
291 po::options_description
description("=== recursive_polygons_linear_areal ===\nAllowed options");
294 int seed
= static_cast<unsigned int>(std::time(0));
298 buffer_settings settings
;
299 std::string form
= "box";
300 std::string join
= "round";
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")
317 po::variables_map varmap
;
318 po::store(po::parse_command_line(argc
, argv
, description
), varmap
);
321 if (varmap
.count("help")
322 || (form
!= "box" && form
!= "triangle")
323 || (join
!= "round" && join
!= "miter")
326 std::cout
<< description
<< std::endl
;
330 settings
.triangular
= form
!= "box";
331 settings
.join_code
= join
== "round" ? 1 : 2;
335 test_all
<double, false, false>(seed
, count
, level
, settings
);
339 test_all
<double, false, true>(seed
, count
, level
, settings
);
343 test_all
<double, true, false>(seed
, count
, level
, settings
);
347 test_all
<double, true, true>(seed
, count
, level
, settings
);
350 #if defined(HAVE_TTMATH)
351 // test_all<ttmath_big, true, true>(seed, count, max, svg, level);
354 catch(std::exception
const& e
)
356 std::cout
<< "Exception " << e
.what() << std::endl
;
360 std::cout
<< "Other exception" << std::endl
;