• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3 
4 // Copyright (c) 2011-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 <sstream>
12 #include <fstream>
13 #include <iomanip>
14 #include <string>
15 
16 #define BOOST_GEOMETRY_NO_BOOST_TEST
17 
18 #include <test_overlay_p_q.hpp>
19 
20 #include <boost/program_options.hpp>
21 #include <boost/random/linear_congruential.hpp>
22 #include <boost/random/uniform_int.hpp>
23 #include <boost/random/uniform_real.hpp>
24 #include <boost/random/variate_generator.hpp>
25 #include <boost/timer.hpp>
26 
27 
28 template <typename MultiPolygon>
make_polygon(MultiPolygon & mp,int count_x,int count_y,int index,int width_x)29 inline void make_polygon(MultiPolygon& mp, int count_x, int count_y, int index, int width_x)
30 {
31     typedef typename bg::point_type<MultiPolygon>::type point_type;
32 
33     for(int j = 0; j < count_x; ++j)
34     {
35         for(int k = 0; k < count_y; ++k)
36         {
37             mp.push_back(typename MultiPolygon::value_type());
38             mp.back().outer().push_back(point_type(width_x + j * 10 + 1, k * 10 + 1));
39             mp.back().outer().push_back(point_type(width_x + j * 10 + width_x, k * 10 + 5 + index));
40             mp.back().outer().push_back(point_type(width_x + j * 10 + 5 + index, k * 10 + 7));
41             mp.back().outer().push_back(point_type(width_x + j * 10 + 1, k * 10 + 1));
42         }
43     }
44 }
45 
46 
47 
48 template <typename MultiPolygon>
test_intersects(int count_x,int count_y,int width_x,p_q_settings const & settings)49 void test_intersects(int count_x, int count_y, int width_x, p_q_settings const& settings)
50 {
51     MultiPolygon mp;
52 
53     make_polygon(mp, count_x, count_y, 0, width_x);
54 
55     bool const b = bg::intersects(mp);
56     if (b)
57     {
58         std::cout << " YES";
59     }
60 
61     if(settings.svg)
62     {
63         typedef typename bg::coordinate_type<MultiPolygon>::type coordinate_type;
64         typedef typename bg::point_type<MultiPolygon>::type point_type;
65         std::ostringstream filename;
66         filename << "intersects_"
67             << string_from_type<coordinate_type>::name()
68             << ".svg";
69 
70         std::ofstream svg(filename.str().c_str());
71         bg::svg_mapper<point_type> mapper(svg, 500, 500);
72         mapper.add(mp);
73         mapper.map(mp, "fill-opacity:0.5;fill:rgb(153,204,0);"
74                 "stroke:rgb(153,204,0);stroke-width:3");
75     }
76 }
77 
78 
79 template <typename T, bool Clockwise, bool Closed>
test_all(int count,int count_x,int count_y,int width_x,p_q_settings const & settings)80 void test_all(int count, int count_x, int count_y, int width_x, p_q_settings const& settings)
81 {
82     boost::timer t;
83 
84     typedef bg::model::polygon
85         <
86             bg::model::d2::point_xy<T>, Clockwise, Closed
87         > polygon;
88     typedef bg::model::multi_polygon
89         <
90             polygon
91         > multi_polygon;
92 
93 
94     for(int i = 0; i < count; i++)
95     {
96         test_intersects<multi_polygon>(count_x, count_y, width_x, settings);
97     }
98     std::cout
99         << " type: " << string_from_type<T>::name()
100         << " time: " << t.elapsed()  << std::endl;
101 }
102 
main(int argc,char ** argv)103 int main(int argc, char** argv)
104 {
105     BoostGeometryWriteTestConfiguration();
106     try
107     {
108         namespace po = boost::program_options;
109         po::options_description description("=== intersects ===\nAllowed options");
110 
111         int width_x = 7;
112         int count = 1;
113         int count_x = 10;
114         int count_y = 10;
115         bool ccw = false;
116         bool open = false;
117         p_q_settings settings;
118 
119         description.add_options()
120             ("help", "Help message")
121             ("count", po::value<int>(&count)->default_value(1), "Number of tests")
122             ("count_x", po::value<int>(&count_x)->default_value(10), "Triangle count in x-direction")
123             ("count_y", po::value<int>(&count_y)->default_value(10), "Triangle count in y-direction")
124             ("width_x", po::value<int>(&width_x)->default_value(7), "Width of triangle in x-direction")
125             ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
126             ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
127             ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
128             ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
129         ;
130 
131         po::variables_map varmap;
132         po::store(po::parse_command_line(argc, argv, description), varmap);
133         po::notify(varmap);
134 
135         if (varmap.count("help"))
136         {
137             std::cout << description << std::endl;
138             return 1;
139         }
140 
141         if (ccw && open)
142         {
143             test_all<double, false, false>(count, count_x, count_y, width_x, settings);
144         }
145         else if (ccw)
146         {
147             test_all<double, false, true>(count, count_x, count_y, width_x, settings);
148         }
149         else if (open)
150         {
151             test_all<double, true, false>(count, count_x, count_y, width_x, settings);
152         }
153         else
154         {
155             test_all<double, true, true>(count, count_x, count_y, width_x, settings);
156         }
157 
158 #if defined(HAVE_TTMATH)
159         // test_all<ttmath_big, true, true>(seed, count, max, svg, level);
160 #endif
161     }
162     catch(std::exception const& e)
163     {
164         std::cout << "Exception " << e.what() << std::endl;
165     }
166     catch(...)
167     {
168         std::cout << "Other exception" << std::endl;
169     }
170 
171     return 0;
172 }
173