#### /Src/Dependencies/Boost/libs/geometry/test/algorithms/overlay/robustness/intersection_stars.cpp

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