]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // Performance Test | |
3 | ||
4 | // Copyright (c) 2012-2015 Barend Gehrels, Amsterdam, the Netherlands. | |
5 | ||
1e59de90 TL |
6 | // This file was modified by Oracle on 2021. |
7 | // Modifications copyright (c) 2021, Oracle and/or its affiliates. | |
8 | // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle | |
9 | ||
7c673cae FG |
10 | // Use, modification and distribution is subject to the Boost Software License, |
11 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
12 | // http://www.boost.org/LICENSE_1_0.txt) | |
13 | ||
1e59de90 TL |
14 | #include <iostream> |
15 | #include <fstream> | |
16 | #include <sstream> | |
7c673cae | 17 | |
7c673cae | 18 | #include <boost/timer.hpp> |
1e59de90 TL |
19 | |
20 | #include <boost/geometry/algorithms/buffer.hpp> | |
21 | #include <boost/geometry/geometries/geometries.hpp> | |
22 | #include <boost/geometry/io/wkt/write.hpp> | |
23 | #include <boost/geometry/strategies/strategies.hpp> | |
7c673cae FG |
24 | |
25 | ||
26 | namespace bg = boost::geometry; | |
27 | ||
28 | template | |
29 | < | |
30 | typename GeometryOut, | |
31 | typename Geometry | |
32 | > | |
33 | double test_growth(Geometry const& geometry, int n, int d, double distance) | |
34 | { | |
35 | ||
36 | typedef typename bg::coordinate_type<Geometry>::type coordinate_type; | |
37 | typedef typename bg::point_type<Geometry>::type point_type; | |
38 | ||
39 | // extern int point_buffer_count; | |
40 | std::ostringstream complete; | |
41 | complete | |
42 | << "point_growth" | |
43 | << "_" << "r" | |
44 | << "_" << n | |
45 | << "_" << d | |
46 | // << "_" << point_buffer_count | |
47 | ; | |
48 | ||
49 | //std::cout << complete.str() << std::endl; | |
50 | ||
51 | std::ostringstream filename; | |
52 | filename << "buffer_" << complete.str() << ".svg"; | |
53 | ||
54 | std::ofstream svg(filename.str().c_str()); | |
55 | ||
56 | #ifdef BOOST_GEOMETRY_DEBUG_WITH_MAPPER | |
57 | bg::svg_mapper<point_type> mapper(svg, 500, 500); | |
58 | ||
59 | { | |
60 | bg::model::box<point_type> box; | |
61 | bg::envelope(geometry, box); | |
62 | ||
63 | bg::buffer(box, box, distance * 1.01); | |
64 | mapper.add(box); | |
65 | } | |
66 | #endif | |
67 | ||
68 | bg::strategy::buffer::join_round join_strategy(100); | |
69 | bg::strategy::buffer::end_flat end_strategy; | |
70 | bg::strategy::buffer::point_circle point_strategy; | |
71 | bg::strategy::buffer::side_straight side_strategy; | |
72 | ||
73 | typedef bg::strategy::buffer::distance_symmetric<coordinate_type> distance_strategy_type; | |
74 | distance_strategy_type distance_strategy(distance); | |
75 | ||
76 | std::vector<GeometryOut> buffered; | |
77 | ||
78 | bg::buffer(geometry, buffered, | |
79 | distance_strategy, side_strategy, | |
80 | join_strategy, end_strategy, point_strategy); | |
81 | ||
82 | ||
83 | typename bg::default_area_result<GeometryOut>::type area = 0; | |
1e59de90 | 84 | for (GeometryOut const& polygon : buffered) |
7c673cae FG |
85 | { |
86 | area += bg::area(polygon); | |
87 | } | |
88 | ||
89 | #ifdef BOOST_GEOMETRY_DEBUG_WITH_MAPPER | |
90 | // Map input geometry in green | |
91 | mapper.map(geometry, "opacity:0.5;fill:rgb(0,128,0);stroke:rgb(0,128,0);stroke-width:10"); | |
92 | ||
1e59de90 | 93 | for (GeometryOut const& polygon : buffered) |
7c673cae FG |
94 | { |
95 | mapper.map(polygon, "opacity:0.4;fill:rgb(255,255,128);stroke:rgb(0,0,0);stroke-width:3"); | |
96 | } | |
97 | #endif | |
98 | ||
99 | return area; | |
100 | } | |
101 | ||
102 | template <typename P> | |
103 | void test_growth(int n, int distance_count) | |
104 | { | |
105 | srand(int(time(NULL))); | |
106 | //std::cout << typeid(bg::coordinate_type<P>::type).name() << std::endl; | |
107 | boost::timer t; | |
108 | ||
109 | namespace buf = bg::strategy::buffer; | |
110 | typedef bg::model::polygon<P> polygon; | |
111 | typedef bg::model::multi_point<P> multi_point_type; | |
112 | ||
113 | multi_point_type multi_point; | |
114 | for (int i = 0; i < n; i++) | |
115 | { | |
116 | P point(rand() % 100, rand() % 100); | |
117 | multi_point.push_back(point); | |
118 | } | |
119 | ||
120 | //std::cout << bg::wkt(multi_point) << std::endl; | |
121 | ||
122 | double previous_area = 0; | |
123 | double epsilon = 0.1; | |
124 | double distance = 15.0; | |
125 | for (int d = 0; d < distance_count; d++, distance += epsilon) | |
126 | { | |
127 | double area = test_growth<polygon>(multi_point, n, d, distance); | |
128 | if (area < previous_area) | |
129 | { | |
130 | std::cout << "Error: " << area << " < " << previous_area << std::endl | |
131 | << " n=" << n << " distance=" << distance | |
132 | << bg::wkt(multi_point) << std::endl; | |
133 | } | |
134 | previous_area = area; | |
135 | } | |
136 | std::cout << "n=" << n << " time=" << t.elapsed() << std::endl; | |
137 | } | |
138 | ||
139 | int main(int, char* []) | |
140 | { | |
141 | for (int i = 5; i <= 50; i++) | |
142 | { | |
143 | test_growth<bg::model::point<double, 2, bg::cs::cartesian> >(i, 20); | |
144 | } | |
145 | ||
146 | return 0; | |
147 | } |