• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3 
4 // Copyright (c) 2014-2018, Oracle and/or its affiliates.
5 
6 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
7 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
8 
9 // Licensed under the Boost Software License version 1.0.
10 // http://www.boost.org/users/license.html
11 
12 #include <iostream>
13 
14 #ifndef BOOST_TEST_MODULE
15 #define BOOST_TEST_MODULE test_distance_cartesian_areal_areal
16 #endif
17 
18 #include <boost/test/included/unit_test.hpp>
19 
20 #include "test_distance_common.hpp"
21 
22 
23 typedef bg::model::point<int,2,bg::cs::cartesian>     int_point_type;
24 typedef bg::model::point<double,2,bg::cs::cartesian>  point_type;
25 typedef bg::model::polygon<point_type, false>         polygon_type;
26 typedef bg::model::multi_polygon<polygon_type>        multi_polygon_type;
27 typedef bg::model::ring<point_type, false>            ring_type;
28 typedef bg::model::box<int_point_type>                int_box_type;
29 typedef bg::model::box<point_type>                    box_type;
30 
31 namespace services = bg::strategy::distance::services;
32 typedef bg::default_distance_result<point_type>::type return_type;
33 
34 typedef bg::strategy::distance::projected_point<> point_segment_strategy;
35 typedef bg::strategy::distance::pythagoras_box_box<> box_box_strategy;
36 
37 //===========================================================================
38 
39 template <typename Strategy>
test_distance_polygon_polygon(Strategy const & strategy)40 void test_distance_polygon_polygon(Strategy const& strategy)
41 {
42 #ifdef BOOST_GEOMETRY_TEST_DEBUG
43     std::cout << std::endl;
44     std::cout << "polygon/polygon distance tests" << std::endl;
45 #endif
46     typedef test_distance_of_geometries<polygon_type, polygon_type> tester;
47 
48     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
49                   "polygon((-5 20,5 20,5 25,-5 25,-5 20))",
50                   10, 100, strategy);
51 
52     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
53                   "polygon((-5 20,-5 5,5 5,5 20,-5 20))",
54                   0, 0, strategy);
55 
56     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
57                   "polygon((-5 20,-5 -20,5 -20,5 20,-5 20))",
58                   0, 0, strategy);
59 
60     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10),\
61                    (-5 -5,-5 5,5 5,5 -5,-5 -5))",
62                   "polygon((-1 -1,0 0,-1 0,-1 -1))",
63                   4, 16, strategy);
64 }
65 
66 //===========================================================================
67 
68 template <typename Strategy>
test_distance_polygon_multipolygon(Strategy const & strategy)69 void test_distance_polygon_multipolygon(Strategy const& strategy)
70 {
71 #ifdef BOOST_GEOMETRY_TEST_DEBUG
72     std::cout << std::endl;
73     std::cout << "polygon/multipolygon distance tests" << std::endl;
74 #endif
75     typedef test_distance_of_geometries
76         <
77             polygon_type, multi_polygon_type
78         > tester;
79 
80     tester::apply("polygon((12 0,14 0,19 0,19.9 -1,12 0))",
81                   "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
82                    ((20 -1,21 2,30 -10,20 -1)))",
83                   0.1, 0.01, strategy);
84 
85     tester::apply("polygon((19 0,19.9 -1,12 0,20.5 0.5,19 0))",
86                   "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
87                    ((20 -1,21 2,30 -10,20 -1)))",
88                   0, 0, strategy);
89 }
90 
91 //===========================================================================
92 
93 template <typename Strategy>
test_distance_polygon_ring(Strategy const & strategy)94 void test_distance_polygon_ring(Strategy const& strategy)
95 {
96 #ifdef BOOST_GEOMETRY_TEST_DEBUG
97     std::cout << std::endl;
98     std::cout << "polygon/ring distance tests" << std::endl;
99 #endif
100     typedef test_distance_of_geometries<polygon_type, ring_type> tester;
101 
102     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
103                   "polygon((-5 20,5 20,5 25,-5 25,-5 20))",
104                   10, 100, strategy);
105 
106     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
107                   "polygon((-5 20,-5 5,5 5,5 20,-5 20))",
108                   0, 0, strategy);
109 
110     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
111                   "polygon((-5 20,-5 -20,5 -20,5 20,-5 20))",
112                   0, 0, strategy);
113 }
114 
115 //===========================================================================
116 
117 template <typename Strategy>
test_distance_multipolygon_multipolygon(Strategy const & strategy)118 void test_distance_multipolygon_multipolygon(Strategy const& strategy)
119 {
120 #ifdef BOOST_GEOMETRY_TEST_DEBUG
121     std::cout << std::endl;
122     std::cout << "multipolygon/multipolygon distance tests" << std::endl;
123 #endif
124     typedef test_distance_of_geometries
125         <
126             multi_polygon_type, multi_polygon_type
127         > tester;
128 
129     tester::apply("multipolygon(((12 0,14 0,14 1,12 0)),\
130                    ((18 0,19 0,19.9 -1,18 0)))",
131                   "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
132                    ((20 -1,21 2,30 -10,20 -1)))",
133                   0.1, 0.01, strategy);
134 
135     tester::apply("multipolygon(((18 0,19 0,19.9 -1,18 0)),\
136                    ((12 0,14 0,20.5 0.5,12 0)))",
137                   "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
138                    ((20 -1,21 2,30 -10,20 -1)))",
139                   0, 0, strategy);
140 }
141 
142 //===========================================================================
143 
144 template <typename Strategy>
test_distance_multipolygon_ring(Strategy const & strategy)145 void test_distance_multipolygon_ring(Strategy const& strategy)
146 {
147 #ifdef BOOST_GEOMETRY_TEST_DEBUG
148     std::cout << std::endl;
149     std::cout << "multipolygon/ring distance tests" << std::endl;
150 #endif
151     typedef test_distance_of_geometries
152         <
153             multi_polygon_type, ring_type
154         > tester;
155 
156     tester::apply("multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
157                    ((20 -1,21 2,30 -10,20 -1)))",
158                   "polygon((12 0,14 0,19 0,19.9 -1,12 0))",
159                   0.1, 0.01, strategy);
160 
161     tester::apply("multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
162                    ((20 -1,21 2,30 -10,20 -1)))",
163                   "polygon((19 0,19.9 -1,12 0,20.5 0.5,19 0))",
164                   0, 0, strategy);
165 }
166 
167 //===========================================================================
168 
169 template <typename Strategy>
test_distance_ring_ring(Strategy const & strategy)170 void test_distance_ring_ring(Strategy const& strategy)
171 {
172 #ifdef BOOST_GEOMETRY_TEST_DEBUG
173     std::cout << std::endl;
174     std::cout << "ring/ring distance tests" << std::endl;
175 #endif
176     typedef test_distance_of_geometries<ring_type, ring_type> tester;
177 
178     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
179                   "polygon((-5 20,5 20,5 25,-5 25,-5 20))",
180                   10, 100, strategy);
181 
182     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
183                   "polygon((-5 20,-5 5,5 5,5 20,-5 20))",
184                   0, 0, strategy);
185 
186     tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
187                   "polygon((-5 20,-5 -20,5 -20,5 20,-5 20))",
188                   0, 0, strategy);
189 }
190 
191 //===========================================================================
192 
193 template <typename Strategy>
test_distance_box_box(Strategy const & strategy)194 void test_distance_box_box(Strategy const& strategy)
195 {
196 #ifdef BOOST_GEOMETRY_TEST_DEBUG
197     std::cout << std::endl;
198     std::cout << "box/box distance tests" << std::endl;
199 #endif
200     typedef test_distance_of_geometries<int_box_type, int_box_type> int_tester;
201     typedef test_distance_of_geometries<box_type, box_type> tester;
202 
203     int_tester::apply("box(5 5,10 10)",
204                       "box(0 0,1 1)",
205                       sqrt(32.0), 32, strategy);
206 
207     tester::apply("box(5 5,10 10)",
208                   "box(0 0,1 1)",
209                   sqrt(32.0), 32, strategy);
210 
211     tester::apply("box(3 8,13 18)",
212                   "box(0 0,5 5)",
213                   3, 9, strategy);
214 
215     tester::apply("box(5 5,10 10)",
216                   "box(0 0,5 5)",
217                   0, 0, strategy);
218 
219     tester::apply("box(5 5,10 10)",
220                   "box(0 0,6 6)",
221                   0, 0, strategy);
222 
223     tester::apply("box(3 5,13 15)",
224                   "box(0 0,5 5)",
225                   0, 0, strategy);
226 }
227 
228 //===========================================================================
229 
230 template <typename Strategy>
test_distance_polygon_box(Strategy const & strategy)231 void test_distance_polygon_box(Strategy const& strategy)
232 {
233 #ifdef BOOST_GEOMETRY_TEST_DEBUG
234     std::cout << std::endl;
235     std::cout << "polygon/box distance tests" << std::endl;
236 #endif
237     typedef test_distance_of_geometries<polygon_type, box_type> tester;
238 
239     tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
240                   "box(0 0,1 1)",
241                   sqrt(32.0), 32, strategy);
242 
243     tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
244                   "box(0 0,5 5)",
245                   0, 0, strategy);
246 
247     tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
248                   "box(0 0,6 6)",
249                   0, 0, strategy);
250 
251     tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
252                   "box(5 0,7.5 2.5)",
253                   0, 0, strategy);
254 
255     tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
256                   "box(5 0,6 1)",
257                   sqrt(4.5), 4.5, strategy);
258 }
259 
260 //===========================================================================
261 
262 template <typename Strategy>
test_distance_multipolygon_box(Strategy const & strategy)263 void test_distance_multipolygon_box(Strategy const& strategy)
264 {
265 #ifdef BOOST_GEOMETRY_TEST_DEBUG
266     std::cout << std::endl;
267     std::cout << "multipolygon/box distance tests" << std::endl;
268 #endif
269     typedef test_distance_of_geometries<multi_polygon_type, box_type> tester;
270 
271     tester::apply("multipolygon(((-10 -10,-10 -9,-9 -9,-9 -10,-10 -10)),\
272                    ((2 2,2 3,3 3,3 2,2 2)))",
273                   "box(0 0,1 1)",
274                   sqrt(2.0), 2, strategy);
275 
276     tester::apply("multipolygon(((-10 -10,-10 -9,-9 -9,-9 -10,-10 -10)),\
277                    ((2 2,2 3,3 3,3 2,2 2)))",
278                   "box(0 0,2 2)",
279                   0, 0, strategy);
280 
281     tester::apply("multipolygon(((-10 -10,-10 -9,-9 -9,-9 -10,-10 -10)),\
282                    ((2 2,2 3,3 3,3 2,2 2)))",
283                   "box(0 0,2.5 2)",
284                   0, 0, strategy);
285 }
286 
287 //===========================================================================
288 
289 template <typename Strategy>
test_distance_ring_box(Strategy const & strategy)290 void test_distance_ring_box(Strategy const& strategy)
291 {
292 #ifdef BOOST_GEOMETRY_TEST_DEBUG
293     std::cout << std::endl;
294     std::cout << "ring/box distance tests" << std::endl;
295 #endif
296     typedef test_distance_of_geometries<ring_type, box_type> tester;
297 
298     tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
299                   "box(0 0,1 1)",
300                   sqrt(32.0), 32, strategy);
301 
302     tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
303                   "box(0 0,5 5)",
304                   0, 0, strategy);
305 
306     tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
307                   "box(0 0,6 6)",
308                   0, 0, strategy);
309 
310     tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
311                   "box(5 0,7.5 2.5)",
312                   0, 0, strategy);
313 
314     tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
315                   "box(5 0,6 1)",
316                   sqrt(4.5), 4.5, strategy);
317 }
318 
319 //===========================================================================
320 
321 template <typename Point, typename Strategy>
test_more_empty_input_areal_areal(Strategy const & strategy)322 void test_more_empty_input_areal_areal(Strategy const& strategy)
323 {
324 #ifdef BOOST_GEOMETRY_TEST_DEBUG
325     std::cout << std::endl;
326     std::cout << "testing on empty inputs... " << std::flush;
327 #endif
328     bg::model::polygon<Point> polygon_empty;
329     bg::model::multi_polygon<bg::model::polygon<Point> > multipolygon_empty;
330     bg::model::ring<Point> ring_empty;
331 
332     bg::model::polygon<Point> polygon =
333         from_wkt<bg::model::polygon<Point> >("polygon((0 0,1 0,0 1))");
334 
335     bg::model::multi_polygon<bg::model::polygon<Point> > multipolygon =
336         from_wkt
337             <
338                 bg::model::multi_polygon<bg::model::polygon<Point> >
339             >("multipolygon(((0 0,1 0,0 1)))");
340 
341     bg::model::ring<Point> ring =
342         from_wkt<bg::model::ring<Point> >("polygon((0 0,1 0,0 1))");
343 
344     // 1st geometry is empty
345     test_empty_input(polygon_empty, polygon, strategy);
346     test_empty_input(polygon_empty, multipolygon, strategy);
347     test_empty_input(polygon_empty, ring, strategy);
348     test_empty_input(multipolygon_empty, polygon, strategy);
349     test_empty_input(multipolygon_empty, multipolygon, strategy);
350     test_empty_input(multipolygon_empty, ring, strategy);
351     test_empty_input(ring_empty, polygon, strategy);
352     test_empty_input(ring_empty, multipolygon, strategy);
353     test_empty_input(ring_empty, ring, strategy);
354 
355     // 2nd geometry is empty
356     test_empty_input(polygon, polygon_empty, strategy);
357     test_empty_input(polygon, multipolygon_empty, strategy);
358     test_empty_input(polygon, ring_empty, strategy);
359     test_empty_input(multipolygon, polygon_empty, strategy);
360     test_empty_input(multipolygon, multipolygon_empty, strategy);
361     test_empty_input(multipolygon, ring_empty, strategy);
362     test_empty_input(ring, polygon_empty, strategy);
363     test_empty_input(ring, multipolygon_empty, strategy);
364     test_empty_input(ring, ring_empty, strategy);
365 
366     // both geometries are empty
367     test_empty_input(polygon_empty, polygon_empty, strategy);
368     test_empty_input(polygon_empty, multipolygon_empty, strategy);
369     test_empty_input(polygon_empty, ring_empty, strategy);
370     test_empty_input(multipolygon_empty, polygon_empty, strategy);
371     test_empty_input(multipolygon_empty, multipolygon_empty, strategy);
372     test_empty_input(multipolygon_empty, ring_empty, strategy);
373     test_empty_input(ring_empty, polygon_empty, strategy);
374     test_empty_input(ring_empty, multipolygon_empty, strategy);
375     test_empty_input(ring_empty, ring_empty, strategy);
376 
377 #ifdef BOOST_GEOMETRY_TEST_DEBUG
378     std::cout << "done!" << std::endl;
379 #endif
380 }
381 
382 //===========================================================================
383 
BOOST_AUTO_TEST_CASE(test_all_polygon_polygon)384 BOOST_AUTO_TEST_CASE( test_all_polygon_polygon )
385 {
386     test_distance_polygon_polygon(point_segment_strategy());
387 }
388 
BOOST_AUTO_TEST_CASE(test_all_polygon_multipolygon)389 BOOST_AUTO_TEST_CASE( test_all_polygon_multipolygon )
390 {
391     test_distance_polygon_multipolygon(point_segment_strategy());
392 }
393 
BOOST_AUTO_TEST_CASE(test_all_polygon_ring)394 BOOST_AUTO_TEST_CASE( test_all_polygon_ring )
395 {
396     test_distance_polygon_ring(point_segment_strategy());
397 }
398 
BOOST_AUTO_TEST_CASE(test_all_multipolygon_multipolygon)399 BOOST_AUTO_TEST_CASE( test_all_multipolygon_multipolygon )
400 {
401     test_distance_multipolygon_multipolygon(point_segment_strategy());
402 }
403 
BOOST_AUTO_TEST_CASE(test_all_multipolygon_ring)404 BOOST_AUTO_TEST_CASE( test_all_multipolygon_ring )
405 {
406     test_distance_multipolygon_ring(point_segment_strategy());
407 }
408 
BOOST_AUTO_TEST_CASE(test_all_ring_ring)409 BOOST_AUTO_TEST_CASE( test_all_ring_ring )
410 {
411     test_distance_ring_ring(point_segment_strategy());
412 }
413 
BOOST_AUTO_TEST_CASE(test_all_box_box)414 BOOST_AUTO_TEST_CASE( test_all_box_box )
415 {
416     test_distance_box_box(box_box_strategy());
417 }
418 
BOOST_AUTO_TEST_CASE(test_all_polygon_box)419 BOOST_AUTO_TEST_CASE( test_all_polygon_box )
420 {
421     test_distance_polygon_box(point_segment_strategy());
422 }
423 
BOOST_AUTO_TEST_CASE(test_all_multipolygon_box)424 BOOST_AUTO_TEST_CASE( test_all_multipolygon_box )
425 {
426     test_distance_multipolygon_box(point_segment_strategy());
427 }
428 
BOOST_AUTO_TEST_CASE(test_all_ring_box)429 BOOST_AUTO_TEST_CASE( test_all_ring_box )
430 {
431     test_distance_ring_box(point_segment_strategy());
432 }
433 
BOOST_AUTO_TEST_CASE(test_all_empty_input_areal_areal)434 BOOST_AUTO_TEST_CASE( test_all_empty_input_areal_areal )
435 {
436     test_more_empty_input_areal_areal<point_type>(point_segment_strategy());
437 }
438