Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / geometry / test / robustness / overlay / areal_areal / intersection_stars.cpp
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
16 #include <test_overlay_p_q.hpp>
17
18 #include <boost/program_options.hpp>
19 #include <boost/timer.hpp>
20
21 template <typename Polygon>
22 inline void make_star(Polygon& polygon,
23     int count, double factor1, double factor2, long double offset = 0)
24 {
25     typedef typename bg::point_type<Polygon>::type p;
26     typedef typename bg::select_most_precise
27         <
28             typename bg::coordinate_type<Polygon>::type,
29             long double
30         >::type coordinate_type;
31
32     // Create star
33     coordinate_type cx = 25.0;
34     coordinate_type cy = 25.0;
35
36     coordinate_type dx = 50.0;
37     coordinate_type dy = 50.0;
38
39     coordinate_type half = 0.5;
40     coordinate_type two = 2.0;
41
42     coordinate_type a1 = coordinate_type(factor1) * half * dx;
43     coordinate_type b1 = coordinate_type(factor1) * half * dy;
44     coordinate_type a2 = coordinate_type(factor2) * half * dx;
45     coordinate_type b2 = coordinate_type(factor2) * half * dy;
46
47     coordinate_type pi = boost::math::constants::pi<long double>();
48     coordinate_type delta = pi * two / coordinate_type(count - 1);
49     coordinate_type angle = coordinate_type(offset) * delta;
50     for (int i = 0; i < count - 1; i++, angle += delta)
51     {
52         bool even = i % 2 == 0;
53         coordinate_type s = sin(angle);
54         coordinate_type c = cos(angle);
55         coordinate_type x = cx + (even ? a1 : a2) * s;
56         coordinate_type y = cy + (even ? b1 : b2) * c;
57         bg::exterior_ring(polygon).push_back(bg::make<p>(x, y));
58
59     }
60     bg::exterior_ring(polygon).push_back(bg::exterior_ring(polygon).front());
61 }
62
63
64 template <typename T, typename CalculationType>
65 void test_star(int count, int min_points, int max_points, T rotation, p_q_settings const& settings)
66 {
67     boost::timer t;
68     typedef bg::model::d2::point_xy<T> point_type;
69     typedef bg::model::polygon<point_type> polygon;
70
71     int n = 0;
72     for (int c = 0; c < count; c++)
73     {
74         for (int i = min_points; i <= max_points; i++)
75         {
76             std::ostringstream out;
77             out << "_" << string_from_type<T>::name() << "_"
78                 << string_from_type<CalculationType>::name() << "_"
79                 << i << "_int";
80
81             polygon p;
82             make_star(p, i * 2 + 1, 0.5, 1.0);
83             polygon q;
84             make_star(q, i * 2 + 1, 0.5, 1.0, rotation);
85
86             if (! test_overlay_p_q
87                 <
88                     polygon,
89                     CalculationType
90                 >(out.str(), p, q, settings))
91             {
92                 return;
93             }
94             n++;
95         }
96     }
97     std::cout
98         << "polygons: " << n
99         << " type: " << string_from_type<T>::name()
100         << " time: " << t.elapsed()  << std::endl;
101 }
102
103 template <typename T, typename CalculationType>
104 void test_type(int count, int min_points, int max_points, T rotation, p_q_settings const& settings)
105 {
106     test_star<T, CalculationType>(count, min_points, max_points, rotation, settings);
107 }
108
109 template <typename T>
110 void test_all(std::string const& type, int count, int min_points, int max_points, T rotation, p_q_settings settings)
111 {
112     if (type == "float")
113     {
114         settings.tolerance = 1.0e-3;
115         test_type<float, float>(count, min_points, max_points, rotation, settings);
116     }
117     else if (type == "double")
118     {
119         test_type<double, double>(count, min_points, max_points, rotation, settings);
120     }
121 #if defined(HAVE_TTMATH)
122     else if (type == "ttmath")
123     {
124         test_type<ttmath_big, ttmath_big>(count, min_points, max_points, rotation, settings);
125     }
126 #endif
127 }
128
129 int main(int argc, char** argv)
130 {
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 }