1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3 
4 // Copyright (c) 2015, 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_is_empty
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/is_empty.hpp>
22 #include <boost/geometry/algorithms/num_points.hpp>
23 
24 #include <boost/geometry/core/closure.hpp>
25 #include <boost/geometry/core/tag.hpp>
26 #include <boost/geometry/core/tags.hpp>
27 #include <boost/geometry/geometries/geometries.hpp>
28 #include <boost/geometry/io/wkt/wkt.hpp>
29 #include <boost/geometry/io/dsv/write.hpp>
30 
31 namespace bg = boost::geometry;
32 
33 
34 typedef bg::model::point<double, 2, bg::cs::cartesian> point;
35 typedef bg::model::linestring<point> linestring;
36 typedef bg::model::segment<point> segment;
37 typedef bg::model::box<point> box;
38 typedef bg::model::ring<point, true, true> ring_cw_closed;
39 typedef bg::model::ring<point, true, false> ring_cw_open;
40 typedef bg::model::ring<point, false, true> ring_ccw_closed;
41 typedef bg::model::ring<point, false, false> ring_ccw_open;
42 typedef bg::model::polygon<point, true, true> polygon_cw_closed;
43 typedef bg::model::polygon<point, true, false> polygon_cw_open;
44 typedef bg::model::polygon<point, false, true> polygon_ccw_closed;
45 typedef bg::model::polygon<point, false, false> polygon_ccw_open;
46 typedef bg::model::multi_point<point> multi_point;
47 typedef bg::model::multi_linestring<linestring> multi_linestring;
48 typedef bg::model::multi_polygon<polygon_cw_closed> multi_polygon_cw_closed;
49 typedef bg::model::multi_polygon<polygon_cw_open> multi_polygon_cw_open;
50 typedef bg::model::multi_polygon<polygon_ccw_closed> multi_polygon_ccw_closed;
51 typedef bg::model::multi_polygon<polygon_ccw_open> multi_polygon_ccw_open;
52 
53 template <std::size_t D, typename T = double>
54 struct box_dD
55 {
56     typedef boost::geometry::model::box
57         <
58             boost::geometry::model::point<T, D, boost::geometry::cs::cartesian>
59         > type;
60 };
61 
62 template <typename Geometry, typename Tag = typename bg::tag<Geometry>::type>
63 struct test_is_empty
64 {
applytest_is_empty65     static inline void apply(Geometry const& geometry, bool expected)
66     {
67         bool detected = bg::is_empty(geometry);
68         BOOST_CHECK_MESSAGE( detected == expected,
69                              std::boolalpha
70                              << "Expected: " << expected
71                              << " detected: " << detected
72                              << " wkt: " << bg::wkt(geometry)
73                              << std::noboolalpha );
74         BOOST_CHECK_EQUAL(detected, bg::num_points(geometry) == 0);
75     }
76 
applytest_is_empty77     static inline void apply(std::string const& wkt, bool expected)
78     {
79         Geometry geometry;
80         bg::read_wkt(wkt, geometry);
81         apply(geometry, expected);
82     }
83 };
84 
85 template <typename Box>
86 struct test_is_empty<Box, bg::box_tag>
87 {
applytest_is_empty88     static inline void apply(Box const& box, bool expected)
89     {
90         bool detected = bg::is_empty(box);
91         BOOST_CHECK_MESSAGE( detected == expected,
92                              std::boolalpha
93                              << "Expected: " << expected
94                              << " detected: " << detected
95                              << " dsv: " << bg::dsv(box)
96                              << std::noboolalpha );
97         BOOST_CHECK_EQUAL(detected, bg::num_points(box) == 0);
98     }
99 
applytest_is_empty100     static inline void apply(std::string const& wkt, bool expected)
101     {
102         Box box;
103         bg::read_wkt(wkt, box);
104         apply(box, expected);
105     }
106 };
107 
BOOST_AUTO_TEST_CASE(test_point)108 BOOST_AUTO_TEST_CASE( test_point )
109 {
110     test_is_empty<point>::apply("POINT(0 0)", false);
111     test_is_empty<point>::apply("POINT(1 1)", false);
112 }
113 
BOOST_AUTO_TEST_CASE(test_segment)114 BOOST_AUTO_TEST_CASE( test_segment )
115 {
116     test_is_empty<segment>::apply("SEGMENT(0 0,0 0)", false);
117     test_is_empty<segment>::apply("SEGMENT(0 0,1 1)", false);
118 }
119 
BOOST_AUTO_TEST_CASE(test_box)120 BOOST_AUTO_TEST_CASE( test_box )
121 {
122     test_is_empty<box>::apply("BOX(0 0,1 1)", false);
123 
124     // test higher-dimensional boxes
125     test_is_empty<box_dD<3>::type>::apply("BOX(0 0 0,1 1 1)", false);
126     test_is_empty<box_dD<4>::type>::apply("BOX(0 0 0 0,1 1 1 1)", false);
127     test_is_empty<box_dD<5>::type>::apply("BOX(0 0 0 0 0,1 1 1 1 1)", false);
128 }
129 
BOOST_AUTO_TEST_CASE(test_linestring)130 BOOST_AUTO_TEST_CASE( test_linestring )
131 {
132     typedef test_is_empty<linestring> tester;
133 
134     tester::apply("LINESTRING()", true);
135     tester::apply("LINESTRING(0 0)", false);
136     tester::apply("LINESTRING(0 0,0 0)", false);
137     tester::apply("LINESTRING(0 0,0 0,1 1)", false);
138     tester::apply("LINESTRING(0 0,0 0,0 0,1 1)", false);
139 }
140 
BOOST_AUTO_TEST_CASE(test_multipoint)141 BOOST_AUTO_TEST_CASE( test_multipoint )
142 {
143     typedef test_is_empty<multi_point> tester;
144 
145     tester::apply("MULTIPOINT()", true);
146     tester::apply("MULTIPOINT(0 0)", false);
147     tester::apply("MULTIPOINT(0 0,0 0)", false);
148     tester::apply("MULTIPOINT(0 0,0 0,1 1)", false);
149 }
150 
BOOST_AUTO_TEST_CASE(test_multilinestring)151 BOOST_AUTO_TEST_CASE( test_multilinestring )
152 {
153     typedef test_is_empty<multi_linestring> tester;
154 
155     tester::apply("MULTILINESTRING()", true);
156     tester::apply("MULTILINESTRING(())", true);
157     tester::apply("MULTILINESTRING((),())", true);
158     tester::apply("MULTILINESTRING((),(0 0))", false);
159     tester::apply("MULTILINESTRING((),(0 0),())", false);
160     tester::apply("MULTILINESTRING((0 0))", false);
161     tester::apply("MULTILINESTRING((0 0,1 0))", false);
162     tester::apply("MULTILINESTRING((),(),(0 0,1 0))", false);
163     tester::apply("MULTILINESTRING((0 0,1 0,0 1),(0 0,1 0,0 1,0 0))", false);
164 }
165 
166 template <typename OpenRing>
test_open_ring()167 void test_open_ring()
168 {
169     typedef test_is_empty<OpenRing> tester;
170 
171     tester::apply("POLYGON(())", true);
172     tester::apply("POLYGON((0 0))", false);
173     tester::apply("POLYGON((0 0,1 0))", false);
174     tester::apply("POLYGON((0 0,1 0,0 1))", false);
175     tester::apply("POLYGON((0 0,0 0,1 0,0 1))", false);
176 }
177 
178 template <typename ClosedRing>
test_closed_ring()179 void test_closed_ring()
180 {
181     typedef test_is_empty<ClosedRing> tester;
182 
183     tester::apply("POLYGON(())", true);
184     tester::apply("POLYGON((0 0))", false);
185     tester::apply("POLYGON((0 0,0 0))", false);
186     tester::apply("POLYGON((0 0,1 0,0 0))", false);
187     tester::apply("POLYGON((0 0,1 0,0 1,0 0))", false);
188     tester::apply("POLYGON((0 0,1 0,1 0,0 1,0 0))", false);
189 }
190 
BOOST_AUTO_TEST_CASE(test_ring)191 BOOST_AUTO_TEST_CASE( test_ring )
192 {
193     test_open_ring<ring_ccw_open>();
194     test_open_ring<ring_cw_open>();
195     test_closed_ring<ring_ccw_closed>();
196     test_closed_ring<ring_cw_closed>();
197 }
198 
199 template <typename OpenPolygon>
test_open_polygon()200 void test_open_polygon()
201 {
202     typedef test_is_empty<OpenPolygon> tester;
203 
204     tester::apply("POLYGON(())", true);
205     tester::apply("POLYGON((),())", true);
206     tester::apply("POLYGON((),(),())", true);
207     tester::apply("POLYGON((),(0 0,0 1,1 0))", false);
208     tester::apply("POLYGON((),(0 0,0 1,1 0),())", false);
209     tester::apply("POLYGON((),(),(0 0,0 1,1 0))", false);
210     tester::apply("POLYGON((0 0))", false);
211     tester::apply("POLYGON((0 0,10 0),(0 0))", false);
212     tester::apply("POLYGON((0 0,10 0),(1 1,2 1))", false);
213     tester::apply("POLYGON((0 0,10 0,0 10))", false);
214     tester::apply("POLYGON((0 0,10 0,0 10),())", false);
215     tester::apply("POLYGON((0 0,10 0,0 10),(1 1))", false);
216     tester::apply("POLYGON((0 0,10 0,0 10),(1 1,2 1))", false);
217     tester::apply("POLYGON((0 0,10 0,0 10),(1 1,2 1,1 2))", false);
218     tester::apply("POLYGON((0 0,10 0,10 10,0 10),(1 1,2 1,1 2))", false);
219 }
220 
221 template <typename ClosedPolygon>
test_closed_polygon()222 void test_closed_polygon()
223 {
224     typedef test_is_empty<ClosedPolygon> tester;
225 
226     tester::apply("POLYGON(())", true);
227     tester::apply("POLYGON((),())", true);
228     tester::apply("POLYGON((),(),())", true);
229     tester::apply("POLYGON((),(0 0,0 1,1 0,0 0))", false);
230     tester::apply("POLYGON((),(0 0,0 1,1 0,0 0),())", false);
231     tester::apply("POLYGON((),(),(0 0,0 1,1 0,0 0))", false);
232     tester::apply("POLYGON((0 0))", false);
233     tester::apply("POLYGON((0 0,10 0,0 0),(0 0))", false);
234     tester::apply("POLYGON((0 0,10 0,0 0),(1 1,2 1,1 1))", false);
235     tester::apply("POLYGON((0 0,10 0,0 10,0 0))", false);
236     tester::apply("POLYGON((0 0,10 0,0 10,0 0),())", false);
237     tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1))", false);
238     tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 1))", false);
239     tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 2,1 1))", false);
240     tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1))", false);
241 }
242 
BOOST_AUTO_TEST_CASE(test_polygon)243 BOOST_AUTO_TEST_CASE( test_polygon )
244 {
245     test_open_polygon<polygon_ccw_open>();
246     test_open_polygon<polygon_cw_open>();
247     test_closed_polygon<polygon_ccw_closed>();
248     test_closed_polygon<polygon_cw_closed>();
249 }
250 
251 template <typename OpenMultiPolygon>
test_open_multipolygon()252 void test_open_multipolygon()
253 {
254     typedef test_is_empty<OpenMultiPolygon> tester;
255 
256     tester::apply("MULTIPOLYGON()", true);
257     tester::apply("MULTIPOLYGON((()))", true);
258     tester::apply("MULTIPOLYGON(((),()))", true);
259     tester::apply("MULTIPOLYGON(((),()),((),(),()))", true);
260     tester::apply("MULTIPOLYGON(((),()),((),(0 0,10 0,10 10,0 10),()))", false);
261     tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,1 2)))", false);
262     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)))", false);
263     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)))", false);
264 }
265 
266 template <typename ClosedMultiPolygon>
test_closed_multipolygon()267 void test_closed_multipolygon()
268 {
269     typedef test_is_empty<ClosedMultiPolygon> tester;
270 
271     tester::apply("MULTIPOLYGON()", true);
272     tester::apply("MULTIPOLYGON((()))", true);
273     tester::apply("MULTIPOLYGON(((),()))", true);
274     tester::apply("MULTIPOLYGON(((),()),((),(),()))", true);
275     tester::apply("MULTIPOLYGON(((),()),((),(0 0,10 0,10 10,0 10,0 0),()))", false);
276     tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)))", false);
277     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)))", false);
278     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)))", false);
279 }
280 
BOOST_AUTO_TEST_CASE(test_multipolygon)281 BOOST_AUTO_TEST_CASE( test_multipolygon )
282 {
283     test_open_multipolygon<multi_polygon_ccw_open>();
284     test_open_multipolygon<multi_polygon_cw_open>();
285     test_closed_multipolygon<multi_polygon_ccw_closed>();
286     test_closed_multipolygon<multi_polygon_cw_closed>();
287 }
288 
BOOST_AUTO_TEST_CASE(test_variant)289 BOOST_AUTO_TEST_CASE( test_variant )
290 {
291     typedef boost::variant
292         <
293             linestring, polygon_cw_open, polygon_cw_closed, multi_point
294         > variant_geometry_type;
295 
296     typedef test_is_empty<variant_geometry_type> tester;
297 
298     linestring ls_empty;
299     bg::read_wkt("LINESTRING()", ls_empty);
300 
301     linestring ls;
302     bg::read_wkt("LINESTRING(1 1,2 2,5 6)", ls);
303 
304     polygon_cw_open p_open;
305     bg::read_wkt("POLYGON((0 0,0 1,1 0))", p_open);
306 
307     polygon_cw_closed p_closed;
308     bg::read_wkt("POLYGON(())", p_closed);
309 
310     multi_point mp;
311     bg::read_wkt("MULTIPOINT((1 10))", mp);
312 
313     multi_point mp_empty;
314     bg::read_wkt("MULTIPOINT((1 10))", mp);
315 
316     variant_geometry_type variant_geometry;
317 
318     variant_geometry = ls_empty;
319     tester::apply(variant_geometry, true);
320 
321     variant_geometry = p_open;
322     tester::apply(variant_geometry, false);
323 
324     variant_geometry = p_closed;
325     tester::apply(variant_geometry, true);
326 
327     variant_geometry = mp;
328     tester::apply(variant_geometry, false);
329 
330     variant_geometry = mp_empty;
331     tester::apply(variant_geometry, true);
332 
333     variant_geometry = ls;
334     tester::apply(variant_geometry, false);
335 }
336