1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2014-2015, Oracle and/or its affiliates.
4 
5 // Licensed under the Boost Software License version 1.0.
6 // http://www.boost.org/users/license.html
7 
8 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 
11 #ifndef BOOST_TEST_MODULE
12 #define BOOST_TEST_MODULE test_disjoint_coverage
13 #endif
14 
15 // unit test to test disjoint for all geometry combinations
16 
17 #include <iostream>
18 
19 #include <boost/test/included/unit_test.hpp>
20 
21 #include <boost/geometry/core/tag.hpp>
22 #include <boost/geometry/core/tags.hpp>
23 
24 #include <boost/geometry/strategies/strategies.hpp>
25 
26 #include <boost/geometry/io/wkt/wkt.hpp>
27 #include <boost/geometry/io/dsv/write.hpp>
28 
29 #include <boost/geometry/geometries/geometries.hpp>
30 
31 #include <boost/geometry/algorithms/disjoint.hpp>
32 
33 #include <from_wkt.hpp>
34 
35 
36 #ifdef HAVE_TTMATH
37 #include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
38 #endif
39 
40 namespace bg = ::boost::geometry;
41 
42 //============================================================================
43 
44 struct test_disjoint
45 {
46     template <typename Geometry1, typename Geometry2>
applytest_disjoint47     static inline void apply(std::string const& case_id,
48                              Geometry1 const& geometry1,
49                              Geometry2 const& geometry2,
50                              bool expected_result)
51     {
52         bool result = bg::disjoint(geometry1, geometry2);
53         BOOST_CHECK_MESSAGE(result == expected_result,
54             "case ID: " << case_id << ", G1: " << bg::wkt(geometry1)
55             << ", G2: " << bg::wkt(geometry2) << " -> Expected: "
56             << expected_result << ", detected: " << result);
57 
58         result = bg::disjoint(geometry2, geometry1);
59         BOOST_CHECK_MESSAGE(result == expected_result,
60             "case ID: " << case_id << ", G1: " << bg::wkt(geometry2)
61             << ", G2: " << bg::wkt(geometry1) << " -> Expected: "
62             << expected_result << ", detected: " << result);
63 
64 #ifdef BOOST_GEOMETRY_TEST_DEBUG
65         std::cout << "case ID: " << case_id << "; G1 - G2: ";
66         std::cout << bg::wkt(geometry1) << " - ";
67         std::cout << bg::wkt(geometry2) << std::endl;
68         std::cout << std::boolalpha;
69         std::cout << "expected/computed result: "
70                   << expected_result << " / " << result << std::endl;
71         std::cout << std::endl;
72         std::cout << std::noboolalpha;
73 #endif
74     }
75 };
76 
77 //============================================================================
78 
79 // pointlike-areal geometries
80 template <typename P>
test_point_box()81 inline void test_point_box()
82 {
83     typedef test_disjoint tester;
84     typedef bg::model::box<P> B;
85 
86     tester::apply("p-b-01",
87                   from_wkt<P>("POINT(0 0)"),
88                   from_wkt<B>("BOX(0 0,1 1)"),
89                   false);
90 
91     tester::apply("p-b-02",
92                   from_wkt<P>("POINT(2 2)"),
93                   from_wkt<B>("BOX(0 0,1 0)"),
94                   true);
95 }
96 
97 template <typename P>
test_point_ring()98 inline void test_point_ring()
99 {
100     typedef bg::model::ring<P, false, false> R; // ccw, open
101 
102     typedef test_disjoint tester;
103 
104     tester::apply("p-r-01",
105                   from_wkt<P>("POINT(0 0)"),
106                   from_wkt<R>("POLYGON((0 0,1 0,0 1))"),
107                   false);
108 
109     tester::apply("p-r-02",
110                   from_wkt<P>("POINT(1 1)"),
111                   from_wkt<R>("POLYGON((0 0,1 0,0 1))"),
112                   true);
113 }
114 
115 template <typename P>
test_point_polygon()116 inline void test_point_polygon()
117 {
118     typedef bg::model::polygon<P, false, false> PL; // ccw, open
119 
120     typedef test_disjoint tester;
121 
122     tester::apply("p-pg-01",
123                   from_wkt<P>("POINT(0 0)"),
124                   from_wkt<PL>("POLYGON((0 0,1 0,0 1))"),
125                   false);
126 
127     tester::apply("p-pg-02",
128                   from_wkt<P>("POINT(1 1)"),
129                   from_wkt<PL>("POLYGON((0 0,1 0,0 1))"),
130                   true);
131 }
132 
133 template <typename P>
test_point_multipolygon()134 inline void test_point_multipolygon()
135 {
136     typedef bg::model::polygon<P, false, false> PL; // ccw, open
137     typedef bg::model::multi_polygon<PL> MPL;
138 
139     typedef test_disjoint tester;
140 
141     tester::apply("p-mpg-01",
142                   from_wkt<P>("POINT(0 0)"),
143                   from_wkt<MPL>("MULTIPOLYGON(((0 0,1 0,0 1)),((2 0,3 0,2 1)))"),
144                   false);
145 
146     tester::apply("p-mpg-02",
147                   from_wkt<P>("POINT(1 1)"),
148                   from_wkt<MPL>("MULTIPOLYGON(((0 0,1 0,0 1)),((2 0,3 0,2 1)))"),
149                   true);
150 }
151 
152 template <typename P>
test_multipoint_box()153 inline void test_multipoint_box()
154 {
155     typedef test_disjoint tester;
156     typedef bg::model::multi_point<P> MP;
157     typedef bg::model::box<P> B;
158 
159     tester::apply("mp-b-01",
160                   from_wkt<MP>("MULTIPOINT(0 0,1 1)"),
161                   from_wkt<B>("BOX(0 0,2 2)"),
162                   false);
163 
164     tester::apply("mp-b-02",
165                   from_wkt<MP>("MULTIPOINT(1 1,3 3)"),
166                   from_wkt<B>("BOX(0 0,2 2)"),
167                   false);
168 
169     tester::apply("mp-b-03",
170                   from_wkt<MP>("MULTIPOINT(3 3,4 4)"),
171                   from_wkt<B>("BOX(0 0,2 2)"),
172                   true);
173 
174     tester::apply("mp-b-04",
175                   from_wkt<MP>("MULTIPOINT()"),
176                   from_wkt<B>("BOX(0 0,2 2)"),
177                   true);
178 }
179 
180 template <typename P>
test_multipoint_ring()181 inline void test_multipoint_ring()
182 {
183     typedef bg::model::multi_point<P> MP;
184     typedef bg::model::ring<P, false, false> R; // ccw, open
185 
186     typedef test_disjoint tester;
187 
188     tester::apply("mp-r-01",
189                   from_wkt<MP>("MULTIPOINT(0 0,1 0)"),
190                   from_wkt<R>("POLYGON((0 0,1 0,0 1))"),
191                   false);
192 
193     tester::apply("mp-r-02",
194                   from_wkt<MP>("MULTIPOINT(1 0,1 1)"),
195                   from_wkt<R>("POLYGON((0 0,1 0,0 1))"),
196                   false);
197 
198     tester::apply("mp-r-03",
199                   from_wkt<MP>("MULTIPOINT(1 1,2 2)"),
200                   from_wkt<R>("POLYGON((0 0,1 0,0 1))"),
201                   true);
202 }
203 
204 template <typename P>
test_multipoint_polygon()205 inline void test_multipoint_polygon()
206 {
207     typedef bg::model::multi_point<P> MP;
208     typedef bg::model::polygon<P, false, false> PL; // ccw, open
209 
210     typedef test_disjoint tester;
211 
212     tester::apply("mp-pg-01",
213                   from_wkt<MP>("MULTIPOINT(0 0,1 0)"),
214                   from_wkt<PL>("POLYGON(((0 0,1 0,0 1)))"),
215                   false);
216 
217     tester::apply("mp-pg-02",
218                   from_wkt<MP>("MULTIPOINT(0 0,2 0)"),
219                   from_wkt<PL>("POLYGON(((0 0,1 0,0 1)))"),
220                   false);
221 
222     tester::apply("mp-pg-03",
223                   from_wkt<MP>("MULTIPOINT(1 1,2 0)"),
224                   from_wkt<PL>("POLYGON(((0 0,1 0,0 1)))"),
225                   true);
226 
227     tester::apply("mp-pg-04",
228                   from_wkt<MP>("MULTIPOINT(1 1,2 3)"),
229                   from_wkt<PL>("POLYGON(((0 0,1 0,0 1)))"),
230                   true);
231 }
232 
233 template <typename P>
test_multipoint_multipolygon()234 inline void test_multipoint_multipolygon()
235 {
236     typedef bg::model::multi_point<P> MP;
237     typedef bg::model::polygon<P, false, false> PL; // ccw, open
238     typedef bg::model::multi_polygon<PL> MPL;
239 
240     typedef test_disjoint tester;
241 
242     tester::apply("mp-mp-01",
243                   from_wkt<MP>("MULTIPOINT(0 0,2 0)"),
244                   from_wkt<MPL>("MULTIPOLYGON((0 0,1 0,0 1)),(2 0,3 0,2 1))"),
245                   false);
246 
247     tester::apply("mp-mp-02",
248                   from_wkt<MP>("MULTIPOINT(0 0,1 0)"),
249                   from_wkt<MPL>("MULTIPOLYGON((0 0,1 0,0 1)),(2 0,3 0,2 1))"),
250                   false);
251 
252     tester::apply("mp-mp-03",
253                   from_wkt<MP>("MULTIPOINT(1 1,2 0)"),
254                   from_wkt<MPL>("MULTIPOLYGON((0 0,1 0,0 1)),(2 0,3 0,2 1))"),
255                   false);
256 
257     tester::apply("mp-mp-04",
258                   from_wkt<MP>("MULTIPOINT(1 1,2 3)"),
259                   from_wkt<MPL>("MULTIPOLYGON((0 0,1 0,0 1)),(2 0,3 0,2 1))"),
260                   true);
261 }
262 
263 //============================================================================
264 
265 template <typename CoordinateType>
test_pointlike_areal()266 inline void test_pointlike_areal()
267 {
268     typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
269 
270     test_point_polygon<point_type>();
271     test_point_multipolygon<point_type>();
272     test_point_ring<point_type>();
273     test_point_box<point_type>();
274 
275     // not implemented yet
276     //    test_multipoint_polygon<point_type>();
277     //    test_multipoint_multipolygon<point_type>();
278     //    test_multipoint_ring<point_type>();
279     test_multipoint_box<point_type>();
280 }
281 
282 //============================================================================
283 
BOOST_AUTO_TEST_CASE(test_pointlike_areal_all)284 BOOST_AUTO_TEST_CASE( test_pointlike_areal_all )
285 {
286     test_pointlike_areal<double>();
287     test_pointlike_areal<int>();
288 #ifdef HAVE_TTMATH
289     test_pointlike_areal<ttmath_big>();
290 #endif
291 }
292