]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/robustness/convex_hull/random_multi_points.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / geometry / test / robustness / convex_hull / random_multi_points.cpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Robustness Test - convex_hull
3
4 // Copyright (c) 2012-2015 Barend Gehrels, Amsterdam, the Netherlands.
5
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10 #define BOOST_GEOMETRY_REPORT_OVERLAY_ERROR
11 #define BOOST_GEOMETRY_NO_BOOST_TEST
12
13 #include <sstream>
14 #include <fstream>
15
16 #include <boost/program_options.hpp>
17 #include <boost/random/linear_congruential.hpp>
18 #include <boost/random/uniform_int.hpp>
19 #include <boost/random/uniform_real.hpp>
20 #include <boost/random/variate_generator.hpp>
21 #include <boost/timer.hpp>
22
23 #include <boost/geometry.hpp>
24 #include <boost/geometry/geometries/geometries.hpp>
25 #include <boost/geometry/geometries/point_xy.hpp>
26 #include <boost/geometry/io/svg/svg_mapper.hpp>
27
28 struct settings_type
29 {
30 bool svg;
31 bool wkt;
32
33 settings_type()
34 : svg(false)
35 , wkt(false)
36 {}
37 };
38
39 namespace bg = boost::geometry;
40
41 template <typename Geometry1, typename Geometry2>
42 void create_svg(std::string const& filename, Geometry1 const& points, Geometry2 const& hull)
43 {
44 typedef typename boost::geometry::point_type<Geometry1>::type point_type;
45
46 boost::geometry::model::box<point_type> box;
47 bg::envelope(hull, box);
48 bg::buffer(box, box, 1.0);
49
50 std::ofstream svg(filename.c_str());
51 boost::geometry::svg_mapper<point_type> mapper(svg, 800, 800);
52 mapper.add(box);
53
54 mapper.map(hull, "opacity:0.8;fill:none;stroke:rgb(255,0,255);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
55 mapper.map(points, "fill-opacity:0.5;fill:rgb(0,0,255);", 5);
56 }
57
58
59 template <typename MultiPoint, typename Generator>
60 inline void make_multi_point(MultiPoint& mp, Generator& generator, int pcount)
61 {
62 typedef typename bg::point_type<MultiPoint>::type point_type;
63 typedef typename bg::coordinate_type<MultiPoint>::type coordinate_type;
64
65 for(int i = 0; i < pcount; i++)
66 {
67 coordinate_type x, y;
68 x = generator();
69 y = generator();
70
71 point_type p;
72 bg::set<0>(p, x);
73 bg::set<1>(p, y);
74
75 mp.push_back(p);
76 }
77 }
78
79 template <typename MultiPoint, typename Polygon>
80 bool check_hull(MultiPoint const& mp, Polygon const& poly)
81 {
82 for(typename boost::range_iterator<MultiPoint const>::type it = boost::begin(mp);
83 it != boost::end(mp);
84 ++it)
85 {
86 if (! bg::covered_by(*it, poly))
87 {
88 return false;
89 }
90 }
91 return true;
92 }
93
94
95 template <typename MultiPoint, typename Generator>
96 void test_random_multi_points(MultiPoint& result, int& index,
97 Generator& generator,
98 int pcount, settings_type const& settings)
99 {
100 typedef typename bg::point_type<MultiPoint>::type point_type;
101
102 MultiPoint mp;
103 bg::model::polygon<point_type> hull;
104
105 make_multi_point(mp, generator, pcount);
106 bg::convex_hull(mp, hull);
107 // Check if each point lies in the hull
108 bool correct = check_hull(mp, hull);
109 if (! correct)
110 {
111 std::cout << "ERROR! " << std::endl
112 << bg::wkt(mp) << std::endl
113 << bg::wkt(hull) << std::endl
114 << std::endl;
115 ;
116 }
117
118 if (settings.svg || ! correct)
119 {
120 std::ostringstream out;
121 out << "random_mp_" << index++ << "_" << pcount << ".svg";
122 create_svg(out.str(), mp, hull);
123 }
124 if (settings.wkt)
125 {
126 std::cout
127 << "input: " << bg::wkt(mp) << std::endl
128 << "output: " << bg::wkt(hull) << std::endl
129 << std::endl;
130 ;
131 }
132 }
133
134
135 template <typename T>
136 void test_all(int seed, int count, int field_size, int pcount, settings_type const& settings)
137 {
138 boost::timer t;
139
140 typedef boost::minstd_rand base_generator_type;
141
142 base_generator_type generator(seed);
143
144 boost::uniform_int<> random_coordinate(0, field_size - 1);
145 boost::variate_generator<base_generator_type&, boost::uniform_int<> >
146 coordinate_generator(generator, random_coordinate);
147
148 typedef bg::model::multi_point
149 <
150 bg::model::d2::point_xy<T>
151 > mp;
152
153 int index = 0;
154 for(int i = 0; i < count; i++)
155 {
156 mp p;
157 test_random_multi_points<mp>(p, index, coordinate_generator, pcount, settings);
158 }
159 std::cout
160 << "points: " << index
161 << " type: " << typeid(T).name()
162 << " time: " << t.elapsed() << std::endl;
163 }
164
165 int main(int argc, char** argv)
166 {
167 try
168 {
169 namespace po = boost::program_options;
170 po::options_description description("=== random_multi_points ===\nAllowed options");
171
172 std::string type = "double";
173 int count = 1;
174 int seed = static_cast<unsigned int>(std::time(0));
175 int pcount = 3;
176 int field_size = 10;
177 settings_type settings;
178
179 description.add_options()
180 ("help", "Help message")
181 ("seed", po::value<int>(&seed), "Initialization seed for random generator")
182 ("count", po::value<int>(&count)->default_value(1), "Number of tests")
183 ("number", po::value<int>(&pcount)->default_value(30), "Number of points")
184 ("size", po::value<int>(&field_size)->default_value(10), "Size of the field")
185 ("type", po::value<std::string>(&type)->default_value("double"), "Type (int,float,double)")
186 ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
187 ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
188 ;
189
190 po::variables_map varmap;
191 po::store(po::parse_command_line(argc, argv, description), varmap);
192 po::notify(varmap);
193
194 if (varmap.count("help"))
195 {
196 std::cout << description << std::endl;
197 return 1;
198 }
199
200 if (type == "float")
201 {
202 test_all<float>(seed, count, field_size, pcount, settings);
203 }
204 else if (type == "double")
205 {
206 test_all<double>(seed, count, field_size, pcount, settings);
207 }
208 else if (type == "int")
209 {
210 test_all<int>(seed, count, field_size, pcount, settings);
211 }
212
213 }
214 catch(std::exception const& e)
215 {
216 std::cout << "Exception " << e.what() << std::endl;
217 }
218 catch(...)
219 {
220 std::cout << "Other exception" << std::endl;
221 }
222
223 return 0;
224 }