1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2009-2012 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 #include <iostream>
11 #include <string>
12
13 #define BOOST_GEOMETRY_NO_BOOST_TEST
14
15 #include <test_overlay_p_q.hpp>
16
17 #include <boost/program_options.hpp>
18 #include <boost/timer.hpp>
19
20 template <typename Polygon>
make_star(Polygon & polygon,int count,double factor1,double factor2,long double offset=0)21 inline void make_star(Polygon& polygon,
22 int count, double factor1, double factor2, long double offset = 0)
23 {
24 typedef typename bg::point_type<Polygon>::type p;
25 typedef typename bg::select_most_precise
26 <
27 typename bg::coordinate_type<Polygon>::type,
28 long double
29 >::type coordinate_type;
30
31 // Create star
32 coordinate_type cx = 25.0;
33 coordinate_type cy = 25.0;
34
35 coordinate_type dx = 50.0;
36 coordinate_type dy = 50.0;
37
38 coordinate_type half = 0.5;
39 coordinate_type two = 2.0;
40
41 coordinate_type a1 = coordinate_type(factor1) * half * dx;
42 coordinate_type b1 = coordinate_type(factor1) * half * dy;
43 coordinate_type a2 = coordinate_type(factor2) * half * dx;
44 coordinate_type b2 = coordinate_type(factor2) * half * dy;
45
46 coordinate_type pi = boost::math::constants::pi<long double>();
47 coordinate_type delta = pi * two / coordinate_type(count - 1);
48 coordinate_type angle = coordinate_type(offset) * delta;
49 for (int i = 0; i < count - 1; i++, angle += delta)
50 {
51 bool even = i % 2 == 0;
52 coordinate_type s = sin(angle);
53 coordinate_type c = cos(angle);
54 coordinate_type x = cx + (even ? a1 : a2) * s;
55 coordinate_type y = cy + (even ? b1 : b2) * c;
56 bg::exterior_ring(polygon).push_back(bg::make<p>(x, y));
57
58 }
59 bg::exterior_ring(polygon).push_back(bg::exterior_ring(polygon).front());
60 }
61
62
63 template <typename T, typename CalculationType>
test_star(int count,int min_points,int max_points,T rotation,p_q_settings const & settings)64 void test_star(int count, int min_points, int max_points, T rotation, p_q_settings const& settings)
65 {
66 boost::timer t;
67 typedef bg::model::d2::point_xy<T> point_type;
68 typedef bg::model::polygon<point_type> polygon;
69
70 int n = 0;
71 for (int c = 0; c < count; c++)
72 {
73 for (int i = min_points; i <= max_points; i++)
74 {
75 std::ostringstream out;
76 out << "_" << string_from_type<T>::name() << "_"
77 << string_from_type<CalculationType>::name() << "_"
78 << i << "_int";
79
80 polygon p;
81 make_star(p, i * 2 + 1, 0.5, 1.0);
82 polygon q;
83 make_star(q, i * 2 + 1, 0.5, 1.0, rotation);
84
85 if (! test_overlay_p_q
86 <
87 polygon,
88 CalculationType
89 >(out.str(), p, q, settings))
90 {
91 return;
92 }
93 n++;
94 }
95 }
96 std::cout
97 << "polygons: " << n
98 << " type: " << string_from_type<T>::name()
99 << " time: " << t.elapsed() << std::endl;
100 }
101
102 template <typename T, typename CalculationType>
test_type(int count,int min_points,int max_points,T rotation,p_q_settings const & settings)103 void test_type(int count, int min_points, int max_points, T rotation, p_q_settings const& settings)
104 {
105 test_star<T, CalculationType>(count, min_points, max_points, rotation, settings);
106 }
107
108 template <typename T>
test_all(std::string const & type,int count,int min_points,int max_points,T rotation,p_q_settings settings)109 void test_all(std::string const& type, int count, int min_points, int max_points, T rotation, p_q_settings settings)
110 {
111 if (type == "float")
112 {
113 settings.tolerance = 1.0e-3;
114 test_type<float, float>(count, min_points, max_points, rotation, settings);
115 }
116 else if (type == "double")
117 {
118 test_type<double, double>(count, min_points, max_points, rotation, settings);
119 }
120 #if defined(HAVE_TTMATH)
121 else if (type == "ttmath")
122 {
123 test_type<ttmath_big, ttmath_big>(count, min_points, max_points, rotation, settings);
124 }
125 #endif
126 }
127
main(int argc,char ** argv)128 int main(int argc, char** argv)
129 {
130 BoostGeometryWriteTestConfiguration();
131 try
132 {
133 namespace po = boost::program_options;
134 po::options_description description("=== recursive_polygons ===\nAllowed options");
135
136 int count = 1;
137 //int seed = static_cast<unsigned int>(std::time(0));
138 std::string type = "float";
139 int min_points = 9;
140 int max_points = 9;
141 bool ccw = false;
142 bool open = false;
143 double rotation = 1.0e-13;
144 p_q_settings settings;
145
146 description.add_options()
147 ("help", "Help message")
148 //("seed", po::value<int>(&seed), "Initialization seed for random generator")
149 ("count", po::value<int>(&count)->default_value(1), "Number of tests")
150 ("diff", po::value<bool>(&settings.also_difference)->default_value(false), "Include testing on difference")
151 ("min_points", po::value<int>(&min_points)->default_value(9), "Minimum number of points")
152 ("max_points", po::value<int>(&max_points)->default_value(9), "Maximum number of points")
153 ("rotation", po::value<double>(&rotation)->default_value(1.0e-13), "Rotation angle")
154 ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
155 ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
156 ("type", po::value<std::string>(&type)->default_value("float"), "Type (float,double)")
157 ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
158 ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
159 ;
160
161 po::variables_map varmap;
162 po::store(po::parse_command_line(argc, argv, description), varmap);
163 po::notify(varmap);
164
165 if (varmap.count("help"))
166 {
167 std::cout << description << std::endl;
168 return 1;
169 }
170
171 test_all(type, count, min_points, max_points, rotation, settings);
172 }
173 catch(std::exception const& e)
174 {
175 std::cout << "Exception " << e.what() << std::endl;
176 }
177 catch(...)
178 {
179 std::cout << "Other exception" << std::endl;
180 }
181 return 0;
182 }
183