1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3 
4 // Copyright (c) 2010-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 #include <iostream>
11 #include <string>
12 
13 // If defined, tests are run without rescaling-to-integer or robustness policy
14 // This multi_intersection currently contains no tests for double then failing
15 // #define BOOST_GEOMETRY_NO_ROBUSTNESS
16 
17 // #define BOOST_GEOMETRY_DEBUG_ASSEMBLE
18 
19 #include "test_intersection.hpp"
20 #include <algorithms/test_overlay.hpp>
21 #include <algorithms/overlay/multi_overlay_cases.hpp>
22 
23 #include <boost/geometry/algorithms/correct.hpp>
24 #include <boost/geometry/algorithms/intersection.hpp>
25 #include <boost/geometry/algorithms/within.hpp> // only for testing #77
26 
27 #include <boost/geometry/geometries/point_xy.hpp>
28 #include <boost/geometry/geometries/multi_point.hpp>
29 #include <boost/geometry/geometries/multi_linestring.hpp>
30 #include <boost/geometry/geometries/multi_polygon.hpp>
31 
32 #include <boost/geometry/io/wkt/read.hpp>
33 
34 template <typename Ring, typename Polygon, typename MultiPolygon>
test_areal()35 void test_areal()
36 {
37     test_one<Polygon, MultiPolygon, MultiPolygon>("simplex_multi",
38         case_multi_simplex[0], case_multi_simplex[1],
39         2, 12, 6.42);
40 
41     test_one<Polygon, MultiPolygon, MultiPolygon>("case_multi_no_ip",
42         case_multi_no_ip[0], case_multi_no_ip[1],
43         2, 8, 8.5);
44     test_one<Polygon, MultiPolygon, MultiPolygon>("case_multi_2",
45         case_multi_2[0], case_multi_2[1],
46         3, 12, 5.9);
47 
48     test_one<Polygon, MultiPolygon, Polygon>("simplex_multi_mp_p",
49         case_multi_simplex[0], case_single_simplex,
50         2, 12, 6.42);
51 
52     test_one<Polygon, Ring, MultiPolygon>("simplex_multi_r_mp",
53         case_single_simplex, case_multi_simplex[0],
54         2, 12, 6.42);
55     test_one<Ring, MultiPolygon, Polygon>("simplex_multi_mp_r",
56         case_multi_simplex[0], case_single_simplex,
57         2, 12, 6.42);
58 
59     // Constructed cases for multi/touch/equal/etc
60     test_one<Polygon, MultiPolygon, MultiPolygon>("case_61_multi",
61         case_61_multi[0], case_61_multi[1],
62         0, 0, 0.0);
63     test_one<Polygon, MultiPolygon, MultiPolygon>("case_62_multi",
64         case_62_multi[0], case_62_multi[1],
65         1, 5, 1.0);
66     test_one<Polygon, MultiPolygon, MultiPolygon>("case_63_multi",
67         case_63_multi[0], case_63_multi[1],
68         1, 5, 1.0);
69     test_one<Polygon, MultiPolygon, MultiPolygon>("case_64_multi",
70         case_64_multi[0], case_64_multi[1],
71         1, 5, 1.0);
72     test_one<Polygon, MultiPolygon, MultiPolygon>("case_65_multi",
73         case_65_multi[0], case_65_multi[1],
74         1, 5, 1.0);
75     test_one<Polygon, MultiPolygon, MultiPolygon>("case_72_multi",
76         case_72_multi[0], case_72_multi[1],
77         3, 14, 2.85);
78     test_one<Polygon, MultiPolygon, MultiPolygon>("case_77_multi",
79         case_77_multi[0], case_77_multi[1],
80         5, 33, 9);
81     test_one<Polygon, MultiPolygon, MultiPolygon>("case_78_multi",
82         case_78_multi[0], case_78_multi[1],
83         1, 0, 22); // In "get_turns" using partitioning, #points went from 17 to 16
84     test_one<Polygon, MultiPolygon, MultiPolygon>("case_101_multi",
85         case_101_multi[0], case_101_multi[1],
86         4, 22, 4.75);
87     test_one<Polygon, MultiPolygon, MultiPolygon>("case_102_multi",
88         case_102_multi[0], case_102_multi[1],
89         3, 26, 19.75);
90     test_one<Polygon, MultiPolygon, MultiPolygon>("case_107_multi",
91         case_107_multi[0], case_107_multi[1],
92         2, 10, 1.5);
93     test_one<Polygon, MultiPolygon, MultiPolygon>("case_recursive_boxes_1",
94         case_recursive_boxes_1[0], case_recursive_boxes_1[1],
95         10, 97, 47.0);
96     test_one<Polygon, MultiPolygon, MultiPolygon>("case_recursive_boxes_2",
97         case_recursive_boxes_2[0], case_recursive_boxes_2[1],
98         1, 47, 90.0); // Area from SQL Server
99 
100     test_one<Polygon, MultiPolygon, MultiPolygon>("case_recursive_boxes_3",
101         case_recursive_boxes_3[0], case_recursive_boxes_3[1],
102         19, 87, 12.5); // Area from SQL Server
103 
104     test_one<Polygon, MultiPolygon, MultiPolygon>("case_recursive_boxes_4",
105         case_recursive_boxes_4[0], case_recursive_boxes_4[1],
106         13, 157, 67.0); // Area from SQL Server
107 
108     test_one<Polygon, MultiPolygon, MultiPolygon>("ggl_list_20120915_h2_a",
109         ggl_list_20120915_h2[0], ggl_list_20120915_h2[1],
110         2, 10, 6.0); // Area from SQL Server
111     test_one<Polygon, MultiPolygon, MultiPolygon>("ggl_list_20120915_h2_b",
112         ggl_list_20120915_h2[0], ggl_list_20120915_h2[2],
113         2, 10, 6.0); // Area from SQL Server
114 
115     test_one<Polygon, MultiPolygon, MultiPolygon>("ticket_9081",
116         ticket_9081[0], ticket_9081[1],
117         2, 10, 0.0019812556);
118 }
119 
120 template <typename Polygon, typename MultiPolygon, typename Box>
test_areal_clip()121 void test_areal_clip()
122 {
123     static std::string const clip = "POLYGON((1 1,4 4))";
124     test_one<Polygon, Box, MultiPolygon>("simplex_multi_mp_b", clip, case_multi_simplex[0],
125         2, 11, 6.791666);
126     test_one<Polygon, MultiPolygon, Box>("simplex_multi_b_mp", case_multi_simplex[0], clip,
127         2, 11, 6.791666);
128 }
129 
130 template <typename LineString, typename MultiLineString, typename Box>
test_linear()131 void test_linear()
132 {
133     typedef typename bg::point_type<MultiLineString>::type point;
134     test_one<point, MultiLineString, MultiLineString>("case_multi_ml_ml_1",
135         "MULTILINESTRING((0 0,1 1))", "MULTILINESTRING((0 1,1 0))",
136         1, 1, 0);
137     test_one<point, MultiLineString, MultiLineString>("case_multi_ml_ml_2",
138         "MULTILINESTRING((0 0,1 1),(0.5 0,1.5 1))", "MULTILINESTRING((0 1,1 0),(0.5 1,1.5 0))",
139         4, 4, 0);
140 
141     test_one<point, LineString, MultiLineString>("case_multi_l_ml",
142         "LINESTRING(0 0,1 1)", "MULTILINESTRING((0 1,1 0),(0.5 1,1.5 0))",
143         2, 2, 0);
144     test_one<point, MultiLineString, LineString>("case_multi_ml_l",
145         "MULTILINESTRING((0 1,1 0),(0.5 1,1.5 0))", "LINESTRING(0 0,1 1)",
146         2, 2, 0);
147 
148     test_one<LineString, MultiLineString, Box>("case_multi_ml_b",
149         "MULTILINESTRING((0 0,3 3)(1 0,4 3))", "POLYGON((1 1,3 2))",
150         2, 4, 2 * std::sqrt(2.0));
151     test_one<LineString, Box, MultiLineString>("case_multi_b_ml",
152         "POLYGON((1 1,3 2))", "MULTILINESTRING((0 0,3 3)(1 0,4 3))",
153         2, 4, 2 * std::sqrt(2.0));
154 }
155 
156 template <typename P>
test_point_output()157 void test_point_output()
158 {
159     typedef bg::model::box<P> box;
160     typedef bg::model::linestring<P> linestring;
161     typedef bg::model::polygon<P> polygon;
162     typedef bg::model::multi_polygon<polygon> multi_polygon;
163 
164     test_point_output<multi_polygon, multi_polygon>(case_multi_simplex[0], case_multi_simplex[1], 10);
165     test_point_output<linestring, multi_polygon>("linestring(4 0,0 4)", case_multi_simplex[0], 4);
166     test_point_output<box, multi_polygon>("box(3 0,4 6)", case_multi_simplex[0], 8);
167 }
168 
169 template <typename MultiPolygon, typename MultiLineString>
test_areal_linear()170 void test_areal_linear()
171 {
172     typedef typename boost::range_value<MultiPolygon>::type Polygon;
173     typedef typename boost::range_value<MultiLineString>::type LineString;
174     typedef typename bg::point_type<Polygon>::type Point;
175     typedef bg::model::ring<Point> Ring;
176 
177     test_one_lp<LineString, MultiPolygon, LineString>("case_mp_ls_1", case_multi_simplex[0], "LINESTRING(2 0,2 5)", 2, 4, 3.70);
178     test_one_lp<LineString, Polygon, MultiLineString>("case_p_mls_1", case_single_simplex, "MULTILINESTRING((2 0,2 5),(3 0,3 5))", 2, 4, 7.5);
179     test_one_lp<LineString, MultiPolygon, MultiLineString>("case_mp_mls_1", case_multi_simplex[0], "MULTILINESTRING((2 0,2 5),(3 0,3 5))", 4, 8, 6.8333333);
180     test_one_lp<LineString, Ring, MultiLineString>("case_r_mls_1", case_single_simplex, "MULTILINESTRING((2 0,2 5),(3 0,3 5))", 2, 4, 7.5);
181 }
182 
183 template <typename P>
test_all()184 void test_all()
185 {
186     //typedef bg::model::box<P> box;
187     typedef bg::model::ring<P> ring;
188     typedef bg::model::polygon<P> polygon;
189     typedef bg::model::multi_polygon<polygon> multi_polygon;
190     test_areal<ring, polygon, multi_polygon>();
191 
192 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
193 
194     typedef bg::model::ring<P, false> ring_ccw;
195     typedef bg::model::polygon<P, false> polygon_ccw;
196     typedef bg::model::multi_polygon<polygon_ccw> multi_polygon_ccw;
197     test_areal<ring_ccw, polygon_ccw, multi_polygon_ccw>();
198 
199     typedef bg::model::ring<P, true, false> ring_open;
200     typedef bg::model::polygon<P, true, false> polygon_open;
201     typedef bg::model::multi_polygon<polygon_open> multi_polygon_open;
202     test_areal<ring_open, polygon_open, multi_polygon_open>();
203 
204     typedef bg::model::ring<P, false, false> ring_open_ccw;
205     typedef bg::model::polygon<P, false, false> polygon_open_ccw;
206     typedef bg::model::multi_polygon<polygon_open_ccw> multi_polygon_open_ccw;
207     test_areal<ring_open_ccw, polygon_open_ccw, multi_polygon_open_ccw>();
208 
209     test_areal_clip<polygon, multi_polygon, box>();
210     test_areal_clip<polygon_ccw, multi_polygon_ccw, box>();
211 
212     typedef bg::model::linestring<P> linestring;
213     typedef bg::model::multi_linestring<linestring> multi_linestring;
214 
215     test_linear<linestring, multi_linestring, box>();
216     test_areal_linear<multi_polygon, multi_linestring>();
217 #endif
218 
219     test_point_output<P>();
220     // linear
221 
222 }
223 
224 
test_main(int,char * [])225 int test_main(int, char* [])
226 {
227     test_all<bg::model::d2::point_xy<double> >();
228 
229 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
230     test_all<bg::model::d2::point_xy<float> >();
231 
232 #if defined(HAVE_TTMATH)
233     std::cout << "Testing TTMATH" << std::endl;
234     test_all<bg::model::d2::point_xy<ttmath_big> >();
235 #endif
236 
237 #endif
238 
239     return 0;
240 }
241