1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2014, Oracle and/or its affiliates.
5
6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
10
11 #ifndef BOOST_TEST_MODULE
12 #define BOOST_TEST_MODULE test_num_segments
13 #endif
14
15 #include <iostream>
16
17 #include <boost/test/included/unit_test.hpp>
18
19 #include <boost/variant/variant.hpp>
20
21 #include <boost/geometry/algorithms/num_segments.hpp>
22
23 #include <boost/geometry/core/closure.hpp>
24 #include <boost/geometry/core/tag.hpp>
25 #include <boost/geometry/core/tags.hpp>
26 #include <boost/geometry/geometries/geometries.hpp>
27 #include <boost/geometry/io/wkt/wkt.hpp>
28 #include <boost/geometry/io/dsv/write.hpp>
29
30 namespace bg = boost::geometry;
31
32
33 typedef bg::model::point<double, 2, bg::cs::cartesian> point;
34 typedef bg::model::linestring<point> linestring;
35 typedef bg::model::segment<point> segment;
36 typedef bg::model::box<point> box;
37 typedef bg::model::ring<point, true, true> ring_cw_closed;
38 typedef bg::model::ring<point, true, false> ring_cw_open;
39 typedef bg::model::ring<point, false, true> ring_ccw_closed;
40 typedef bg::model::ring<point, false, false> ring_ccw_open;
41 typedef bg::model::polygon<point, true, true> polygon_cw_closed;
42 typedef bg::model::polygon<point, true, false> polygon_cw_open;
43 typedef bg::model::polygon<point, false, true> polygon_ccw_closed;
44 typedef bg::model::polygon<point, false, false> polygon_ccw_open;
45 typedef bg::model::multi_point<point> multi_point;
46 typedef bg::model::multi_linestring<linestring> multi_linestring;
47 typedef bg::model::multi_polygon<polygon_cw_closed> multi_polygon_cw_closed;
48 typedef bg::model::multi_polygon<polygon_cw_open> multi_polygon_cw_open;
49 typedef bg::model::multi_polygon<polygon_ccw_closed> multi_polygon_ccw_closed;
50 typedef bg::model::multi_polygon<polygon_ccw_open> multi_polygon_ccw_open;
51
52 template <std::size_t D, typename T = double>
53 struct box_dD
54 {
55 typedef boost::geometry::model::box
56 <
57 boost::geometry::model::point<T, D, boost::geometry::cs::cartesian>
58 > type;
59 };
60
61 template <typename Geometry, typename Tag = typename bg::tag<Geometry>::type>
62 struct test_num_segments
63 {
applytest_num_segments64 static inline void apply(Geometry const& geometry, std::size_t expected)
65 {
66 std::size_t detected = bg::num_segments(geometry);
67 BOOST_CHECK_MESSAGE( detected == expected,
68 "Expected: " << expected
69 << " detected: " << detected
70 << " wkt: " << bg::wkt(geometry) );
71 }
72
applytest_num_segments73 static inline void apply(std::string const& wkt, std::size_t expected)
74 {
75 Geometry geometry;
76 bg::read_wkt(wkt, geometry);
77 apply(geometry, expected);
78 }
79 };
80
81 template <typename Box>
82 struct test_num_segments<Box, bg::box_tag>
83 {
applytest_num_segments84 static inline void apply(Box const& box, std::size_t expected)
85 {
86 std::size_t detected = bg::num_segments(box);
87 BOOST_CHECK_MESSAGE( detected == expected,
88 "Expected: " << expected
89 << " detected: " << detected
90 << " dsv: " << bg::dsv(box) );
91 }
92
applytest_num_segments93 static inline void apply(std::string const& wkt, std::size_t expected)
94 {
95 Box box;
96 bg::read_wkt(wkt, box);
97 apply(box, expected);
98 }
99 };
100
BOOST_AUTO_TEST_CASE(test_point)101 BOOST_AUTO_TEST_CASE( test_point )
102 {
103 test_num_segments<point>::apply("POINT(0 0)", 0);
104 }
105
BOOST_AUTO_TEST_CASE(test_segment)106 BOOST_AUTO_TEST_CASE( test_segment )
107 {
108 test_num_segments<segment>::apply("SEGMENT(0 0,1 1)", 1);
109 }
110
BOOST_AUTO_TEST_CASE(test_box)111 BOOST_AUTO_TEST_CASE( test_box )
112 {
113 test_num_segments<box>::apply("BOX(0 0,1 1)", 4);
114
115 // test higher-dimensional boxes
116 test_num_segments<box_dD<3>::type>::apply("BOX(0 0 0,1 1 1)", 12);
117 test_num_segments<box_dD<4>::type>::apply("BOX(0 0 0 0,1 1 1 1)", 32);
118 test_num_segments<box_dD<5>::type>::apply("BOX(0 0 0 0 0,1 1 1 1 1)", 80);
119 }
120
BOOST_AUTO_TEST_CASE(test_linestring)121 BOOST_AUTO_TEST_CASE( test_linestring )
122 {
123 typedef test_num_segments<linestring> tester;
124
125 tester::apply("LINESTRING()", 0);
126 tester::apply("LINESTRING(0 0)", 0);
127 tester::apply("LINESTRING(0 0,0 0)", 1);
128 tester::apply("LINESTRING(0 0,0 0,1 1)", 2);
129 tester::apply("LINESTRING(0 0,0 0,0 0,1 1)", 3);
130 }
131
BOOST_AUTO_TEST_CASE(test_multipoint)132 BOOST_AUTO_TEST_CASE( test_multipoint )
133 {
134 typedef test_num_segments<multi_point> tester;
135
136 tester::apply("MULTIPOINT()", 0);
137 tester::apply("MULTIPOINT(0 0)", 0);
138 tester::apply("MULTIPOINT(0 0,0 0)", 0);
139 tester::apply("MULTIPOINT(0 0,0 0,1 1)", 0);
140 }
141
BOOST_AUTO_TEST_CASE(test_multilinestring)142 BOOST_AUTO_TEST_CASE( test_multilinestring )
143 {
144 typedef test_num_segments<multi_linestring> tester;
145
146 tester::apply("MULTILINESTRING()", 0);
147 tester::apply("MULTILINESTRING((),(0 0))", 0);
148 tester::apply("MULTILINESTRING((0 0))", 0);
149 tester::apply("MULTILINESTRING((0 0,1 0))", 1);
150 tester::apply("MULTILINESTRING((),(),(0 0,1 0))", 1);
151 tester::apply("MULTILINESTRING((0 0,1 0,0 1),(0 0,1 0,0 1,0 0))", 5);
152 }
153
154 template <typename OpenRing>
test_open_ring()155 void test_open_ring()
156 {
157 typedef test_num_segments<OpenRing> tester;
158
159 tester::apply("POLYGON(())", 0);
160 tester::apply("POLYGON((0 0))", 0);
161 tester::apply("POLYGON((0 0,1 0))", 2);
162 tester::apply("POLYGON((0 0,1 0,0 1))", 3);
163 tester::apply("POLYGON((0 0,0 0,1 0,0 1))", 4);
164 }
165
166 template <typename ClosedRing>
test_closed_ring()167 void test_closed_ring()
168 {
169 typedef test_num_segments<ClosedRing> tester;
170
171 tester::apply("POLYGON(())", 0);
172 tester::apply("POLYGON((0 0))", 0);
173 tester::apply("POLYGON((0 0,0 0))", 1);
174 tester::apply("POLYGON((0 0,1 0,0 0))", 2);
175 tester::apply("POLYGON((0 0,1 0,0 1,0 0))", 3);
176 tester::apply("POLYGON((0 0,1 0,1 0,0 1,0 0))", 4);
177 }
178
BOOST_AUTO_TEST_CASE(test_ring)179 BOOST_AUTO_TEST_CASE( test_ring )
180 {
181 test_open_ring<ring_ccw_open>();
182 test_open_ring<ring_cw_open>();
183 test_closed_ring<ring_ccw_closed>();
184 test_closed_ring<ring_cw_closed>();
185 }
186
187 template <typename OpenPolygon>
test_open_polygon()188 void test_open_polygon()
189 {
190 typedef test_num_segments<OpenPolygon> tester;
191
192 tester::apply("POLYGON(())", 0);
193 tester::apply("POLYGON((0 0))", 0);
194 tester::apply("POLYGON((0 0,10 0),(0 0))", 2);
195 tester::apply("POLYGON((0 0,10 0),(1 1,2 1))", 4);
196 tester::apply("POLYGON((0 0,10 0,0 10))", 3);
197 tester::apply("POLYGON((0 0,10 0,0 10),())", 3);
198 tester::apply("POLYGON((0 0,10 0,0 10),(1 1))", 3);
199 tester::apply("POLYGON((0 0,10 0,0 10),(1 1,2 1))", 5);
200 tester::apply("POLYGON((0 0,10 0,0 10),(1 1,2 1,1 2))", 6);
201 tester::apply("POLYGON((0 0,10 0,10 10,0 10),(1 1,2 1,1 2))", 7);
202 tester::apply("POLYGON((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2))", 8);
203 tester::apply("POLYGON((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2),(5 5,6 5,6 6,5 6))", 12);
204 }
205
206 template <typename ClosedPolygon>
test_closed_polygon()207 void test_closed_polygon()
208 {
209 typedef test_num_segments<ClosedPolygon> tester;
210
211 tester::apply("POLYGON(())", 0);
212 tester::apply("POLYGON((0 0))", 0);
213 tester::apply("POLYGON((0 0,10 0,0 0),(0 0))", 2);
214 tester::apply("POLYGON((0 0,10 0,0 0),(1 1,2 1,1 1))", 4);
215 tester::apply("POLYGON((0 0,10 0,0 10,0 0))", 3);
216 tester::apply("POLYGON((0 0,10 0,0 10,0 0),())", 3);
217 tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1))", 3);
218 tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 1))", 5);
219 tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 2,1 1))", 6);
220 tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1))", 7);
221 tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1))", 8);
222 tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5))", 12);
223 }
224
BOOST_AUTO_TEST_CASE(test_polygon)225 BOOST_AUTO_TEST_CASE( test_polygon )
226 {
227 test_open_polygon<polygon_ccw_open>();
228 test_open_polygon<polygon_cw_open>();
229 test_closed_polygon<polygon_ccw_closed>();
230 test_closed_polygon<polygon_cw_closed>();
231 }
232
233 template <typename OpenMultiPolygon>
test_open_multipolygon()234 void test_open_multipolygon()
235 {
236 typedef test_num_segments<OpenMultiPolygon> tester;
237
238 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,1 2)))", 7);
239 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2),(5 5,6 5,6 6,5 6)))", 12);
240 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,1 2)),((100 100,110 100,110 110),(101 101,102 101,102 102)))", 13);
241 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2),(5 5,6 5,6 6,5 6)),((100 100,110 100,110 110),(101 101,102 101,102 102),(105 105,106 105,106 106,105 106)))", 22);
242 }
243
244 template <typename ClosedMultiPolygon>
test_closed_multipolygon()245 void test_closed_multipolygon()
246 {
247 typedef test_num_segments<ClosedMultiPolygon> tester;
248
249 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)))", 7);
250 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)))", 12);
251 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101)))", 13);
252 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101),(105 105,106 105,106 106,105 106,105 105)))", 22);
253 }
254
BOOST_AUTO_TEST_CASE(test_multipolygon)255 BOOST_AUTO_TEST_CASE( test_multipolygon )
256 {
257 test_open_multipolygon<multi_polygon_ccw_open>();
258 test_open_multipolygon<multi_polygon_cw_open>();
259 test_closed_multipolygon<multi_polygon_ccw_closed>();
260 test_closed_multipolygon<multi_polygon_cw_closed>();
261 }
262
BOOST_AUTO_TEST_CASE(test_variant)263 BOOST_AUTO_TEST_CASE( test_variant )
264 {
265 typedef boost::variant
266 <
267 linestring, polygon_cw_open, polygon_cw_closed
268 > variant_geometry_type;
269
270 typedef test_num_segments<variant_geometry_type> tester;
271
272 linestring ls;
273 bg::read_wkt("LINESTRING(0 0,1 1,2 2)", ls);
274
275 polygon_cw_open p_open;
276 bg::read_wkt("POLYGON((0 0,0 1,1 0))", p_open);
277
278 polygon_cw_closed p_closed;
279 bg::read_wkt("POLYGON((0 0,0 1,1 1,1 0,0 0))", p_closed);
280
281 variant_geometry_type variant_geometry;
282
283 variant_geometry = ls;
284 tester::apply(variant_geometry, 2);
285
286 variant_geometry = p_open;
287 tester::apply(variant_geometry, 3);
288
289 variant_geometry = p_closed;
290 tester::apply(variant_geometry, 4);
291 }
292