Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / geometry / test / algorithms / distance / distance_se_pl_l.cpp
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 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9
10 // Licensed under the Boost Software License version 1.0.
11 // http://www.boost.org/users/license.html
12
13 #include <iostream>
14
15 #ifndef BOOST_TEST_MODULE
16 #define BOOST_TEST_MODULE test_distance_spherical_equatorial_pointlike_linear
17 #endif
18
19 #include <boost/test/included/unit_test.hpp>
20
21 #include "test_distance_se_common.hpp"
22 #include "test_empty_geometry.hpp"
23
24 typedef bg::cs::spherical_equatorial<bg::degree> cs_type;
25 typedef bg::model::point<double, 2, cs_type> point_type;
26 typedef bg::model::segment<point_type> segment_type;
27 typedef bg::model::multi_point<point_type> multi_point_type;
28 typedef bg::model::segment<point_type> segment_type;
29 typedef bg::model::linestring<point_type> linestring_type;
30 typedef bg::model::multi_linestring<linestring_type> multi_linestring_type;
31
32 namespace services = bg::strategy::distance::services;
33 typedef bg::default_distance_result<point_type>::type return_type;
34
35 typedef bg::strategy::distance::haversine<double> point_point_strategy;
36 typedef bg::strategy::distance::cross_track<> point_segment_strategy;
37
38
39 //===========================================================================
40
41 template <typename Strategy>
42 inline bg::default_distance_result<point_type>::type
43 pp_distance(std::string const& wkt1,
44             std::string const& wkt2,
45             Strategy const& strategy)
46 {
47     point_type p1, p2;
48     bg::read_wkt(wkt1, p1);
49     bg::read_wkt(wkt2, p2);
50     return bg::distance(p1, p2) * strategy.radius();
51 }
52
53 template <typename Strategy>
54 inline bg::default_comparable_distance_result<point_type>::type
55 pp_comparable_distance(std::string const& wkt1,
56                        std::string const& wkt2,
57                        Strategy const&)
58 {
59     point_type p1, p2;
60     bg::read_wkt(wkt1, p1);
61     bg::read_wkt(wkt2, p2);
62     return bg::comparable_distance(p1, p2);
63 }
64
65 template <typename Strategy>
66 inline bg::default_distance_result<point_type>::type
67 ps_distance(std::string const& wkt1,
68             std::string const& wkt2,
69             Strategy const& strategy)
70 {
71     point_type p;
72     segment_type s;
73     bg::read_wkt(wkt1, p);
74     bg::read_wkt(wkt2, s);
75     return bg::distance(p, s, strategy);
76 }
77
78 template <typename Strategy>
79 inline bg::default_comparable_distance_result<point_type>::type
80 ps_comparable_distance(std::string const& wkt1,
81                        std::string const& wkt2,
82                        Strategy const& strategy)
83 {
84     point_type p;
85     segment_type s;
86     bg::read_wkt(wkt1, p);
87     bg::read_wkt(wkt2, s);
88     return bg::comparable_distance(p, s, strategy);
89 }
90
91 template <typename Strategy, typename T>
92 T to_comparable(Strategy const& strategy, T const& distance)
93 {
94     namespace services = bg::strategy::distance::services;
95
96     typedef typename services::comparable_type
97         <
98             Strategy
99         >::type comparable_strategy;
100
101     typedef typename services::result_from_distance
102         <
103             comparable_strategy,
104             point_type,
105             bg::point_type<segment_type>::type
106         > get_comparable_distance;
107
108     comparable_strategy cstrategy = services::get_comparable
109         <
110             Strategy
111         >::apply(strategy);
112
113     return get_comparable_distance::apply(cstrategy, distance);
114 }
115
116 //===========================================================================
117
118 template <typename Strategy>
119 void test_distance_point_segment(Strategy const& strategy)
120 {
121 #ifdef BOOST_GEOMETRY_TEST_DEBUG
122     std::cout << std::endl;
123     std::cout << "point/segment distance tests" << std::endl;
124 #endif
125     typedef test_distance_of_geometries<point_type, segment_type> tester;
126
127     double const d2r = bg::math::d2r<double>();
128
129     tester::apply("p-s-01",
130                   "POINT(0 0)",
131                   "SEGMENT(2 0,3 0)",
132                   2.0 * d2r * strategy.radius(),
133                   to_comparable(strategy, 2.0 * d2r * strategy.radius()),
134                   strategy);
135     tester::apply("p-s-02",
136                   "POINT(2.5 3)",
137                   "SEGMENT(2 0,3 0)",
138                   3.0 * d2r * strategy.radius(),
139                   to_comparable(strategy, 3.0 * d2r * strategy.radius()),
140                   strategy);
141     tester::apply("p-s-03",
142                   "POINT(2 0)",
143                   "SEGMENT(2 0,3 0)",
144                   0,
145                   strategy);
146     tester::apply("p-s-04",
147                   "POINT(3 0)",
148                   "SEGMENT(2 0,3 0)",
149                   0,
150                   strategy);
151     tester::apply("p-s-05",
152                   "POINT(2.5 0)",
153                   "SEGMENT(2 0,3 0)",
154                   0,
155                   strategy);
156     tester::apply("p-s-06",
157                   "POINT(3.5 3)",
158                   "SEGMENT(2 0,3 0)",
159                   pp_distance("POINT(3 0)", "POINT(3.5 3)", strategy),
160                   pp_comparable_distance("POINT(3 0)",
161                                          "POINT(3.5 3)",
162                                          strategy),
163                   strategy);
164     tester::apply("p-s-07",
165                   "POINT(0 0)",
166                   "SEGMENT(0 10,10 10)",
167                   ps_distance("POINT(0 0)", "SEGMENT(10 10,0 10)", strategy),
168                   pp_comparable_distance("POINT(0 0)",
169                                          "POINT(0 10)",
170                                          strategy),
171                   strategy);
172     // very small distances to segment
173     tester::apply("p-s-07",
174                   "POINT(90 1e-3)",
175                   "SEGMENT(0.5 0,175.5 0)",
176                   1e-3 * d2r * strategy.radius(),
177                   to_comparable(strategy, 1e-3 * d2r * strategy.radius()),
178                   strategy);
179     tester::apply("p-s-08",
180                   "POINT(90 1e-4)",
181                   "SEGMENT(0.5 0,175.5 0)",
182                   1e-4 * d2r * strategy.radius(),
183                   to_comparable(strategy, 1e-4 * d2r * strategy.radius()),
184                   strategy);
185     tester::apply("p-s-09",
186                   "POINT(90 1e-5)",
187                   "SEGMENT(0.5 0,175.5 0)",
188                   1e-5 * d2r * strategy.radius(),
189                   to_comparable(strategy, 1e-5 * d2r * strategy.radius()),
190                   strategy);
191     tester::apply("p-s-10",
192                   "POINT(90 1e-6)",
193                   "SEGMENT(0.5 0,175.5 0)",
194                   1e-6 * d2r * strategy.radius(),
195                   to_comparable(strategy, 1e-6 * d2r * strategy.radius()),
196                   strategy);
197     tester::apply("p-s-11",
198                   "POINT(90 1e-7)",
199                   "SEGMENT(0.5 0,175.5 0)",
200                   1e-7 * d2r * strategy.radius(),
201                   to_comparable(strategy, 1e-7 * d2r * strategy.radius()),
202                   strategy);
203     tester::apply("p-s-12",
204                   "POINT(90 1e-8)",
205                   "SEGMENT(0.5 0,175.5 0)",
206                   1e-8 * d2r * strategy.radius(),
207                   to_comparable(strategy, 1e-8 * d2r * strategy.radius()),
208                   strategy);
209 }
210
211 //===========================================================================
212
213 template <typename Strategy>
214 void test_distance_point_linestring(Strategy const& strategy)
215 {
216 #ifdef BOOST_GEOMETRY_TEST_DEBUG
217     std::cout << std::endl;
218     std::cout << "point/linestring distance tests" << std::endl;
219 #endif
220     typedef test_distance_of_geometries<point_type, linestring_type> tester;
221
222     double const r = strategy.radius();
223     double const d2r = bg::math::d2r<double>();
224
225     tester::apply("p-l-01",
226                   "POINT(0 0)",
227                   "LINESTRING(2 0,2 0)",
228                   2.0 * d2r * r,
229                   to_comparable(strategy, 2.0 * d2r * r),
230                   strategy);
231     tester::apply("p-l-02",
232                   "POINT(0 0)",
233                   "LINESTRING(2 0,3 0)",
234                   2.0 * d2r * r,
235                   to_comparable(strategy, 2.0 * d2r * r),
236                   strategy);
237     tester::apply("p-l-03",
238                   "POINT(2.5 3)",
239                   "LINESTRING(2 0,3 0)",
240                   3.0 * d2r * r,
241                   to_comparable(strategy, 3.0 * d2r * r),
242                   strategy);
243     tester::apply("p-l-04",
244                   "POINT(2 0)",
245                   "LINESTRING(2 0,3 0)",
246                   0,
247                   strategy);
248     tester::apply("p-l-05",
249                   "POINT(3 0)",
250                   "LINESTRING(2 0,3 0)",
251                   0,
252                   strategy);
253     tester::apply("p-l-06",
254                   "POINT(2.5 0)",
255                   "LINESTRING(2 0,3 0)",
256                   0,
257                   strategy);
258     tester::apply("p-l-07",
259                   "POINT(7.5 10)",
260                   "LINESTRING(1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 0)",
261                   10.0 * d2r * r,
262                   to_comparable(strategy, 10.0 * d2r * r),
263                   strategy);
264     tester::apply("p-l-08",
265                   "POINT(7.5 10)",
266                   "LINESTRING(1 1,2 1,3 1,4 1,5 1,6 1,7 1,20 2,21 2)",
267                   ps_distance("POINT(7.5 10)", "SEGMENT(7 1,20 2)", strategy),
268                   ps_comparable_distance("POINT(7.5 10)",
269                                          "SEGMENT(7 1,20 2)",
270                                          strategy),
271                   strategy);
272
273     // https://svn.boost.org/trac/boost/ticket/11982
274     tester::apply("p-l-09",
275                   "POINT(10.4 63.43)",
276                   "LINESTRING(10.733557 59.911923, 10.521812 59.887214)",
277                   0.06146397739758279 * r,
278                   0.000944156107132969,
279                   strategy);
280
281     //https://github.com/boostorg/geometry/issues/557
282     tester::apply("p-l-issue557",
283                   "POINT(51.99999790563572 43.71656981636763)",
284                   "LINESTRING(52.0000243071011 43.716569742012496,\
285                               52.0000121532845 43.71656942616241,\
286                               52.0 43.7165690998572,\
287                               51.999987847203 43.7165687638793)",
288                   1.35062e-08 * r,
289                   4.5604e-17,
290                   strategy);
291
292 }
293
294 //===========================================================================
295
296 template <typename Strategy>
297 void test_distance_point_multilinestring(Strategy const& strategy)
298 {
299 #ifdef BOOST_GEOMETRY_TEST_DEBUG
300     std::cout << std::endl;
301     std::cout << "point/multilinestring distance tests" << std::endl;
302 #endif
303     typedef test_distance_of_geometries
304         <
305             point_type, multi_linestring_type
306         > tester;
307
308     double const d2r = bg::math::d2r<double>();
309
310     tester::apply("p-ml-01",
311                   "POINT(0 0)",
312                   "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
313                   2.0 * d2r * strategy.radius(),
314                   to_comparable(strategy, 2.0 * d2r * strategy.radius()),
315                   strategy);
316     tester::apply("p-ml-02",
317                   "POINT(2.5 3)",
318                   "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
319                   3.0 * d2r * strategy.radius(),
320                   to_comparable(strategy, 3.0 * d2r * strategy.radius()),
321                   strategy);
322     tester::apply("p-ml-03",
323                   "POINT(2 0)",
324                   "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
325                   0,
326                   strategy);
327     tester::apply("p-ml-04",
328                   "POINT(3 0)",
329                   "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
330                   0,
331                   strategy);
332     tester::apply("p-ml-05",
333                   "POINT(2.5 0)",
334                   "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
335                   0,
336                   strategy);
337     tester::apply("p-ml-06",
338                   "POINT(7.5 10)",
339                   "MULTILINESTRING((-5 0,-3 0),(2 0,3 0,4 0,5 0,6 0,20 1,21 1))",
340                   ps_distance("POINT(7.5 10)", "SEGMENT(6 0,20 1)", strategy),
341                   ps_comparable_distance("POINT(7.5 10)",
342                                          "SEGMENT(6 0,20 1)",
343                                          strategy),
344                   strategy);
345     tester::apply("p-ml-07",
346                   "POINT(-8 10)",
347                   "MULTILINESTRING((-20 10,-19 11,-18 10,-6 0,-5 0,-3 0),(2 0,6 0,20 1,21 1))",
348                   ps_distance("POINT(-8 10)", "SEGMENT(-6 0,-18 10)", strategy),
349                   ps_comparable_distance("POINT(-8 10)",
350                                          "SEGMENT(-6 0,-18 10)",
351                                          strategy),
352                   strategy);
353 }
354
355 //===========================================================================
356
357 template <typename Strategy>
358 void test_distance_linestring_multipoint(Strategy const& strategy)
359 {
360 #ifdef BOOST_GEOMETRY_TEST_DEBUG
361     std::cout << std::endl;
362     std::cout << "linestring/multipoint distance tests" << std::endl;
363 #endif
364     typedef test_distance_of_geometries
365         <
366             linestring_type, multi_point_type
367         > tester;
368
369     tester::apply("l-mp-01",
370                   "LINESTRING(2 0,0 2,100 80)",
371                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
372                   ps_distance("POINT(1 1)", "SEGMENT(2 0,0 2)", strategy),
373                   ps_comparable_distance("POINT(1 1)",
374                                          "SEGMENT(2 0,0 2)",
375                                          strategy),
376                   strategy);
377     tester::apply("l-mp-02",
378                   "LINESTRING(4 0,0 4,100 80)",
379                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
380                   ps_distance("POINT(1 1)", "SEGMENT(0 4,4 0)", strategy),
381                   ps_comparable_distance("POINT(1 1)",
382                                          "SEGMENT(0 4,4 0)",
383                                          strategy),
384                   strategy);
385     tester::apply("l-mp-03",
386                   "LINESTRING(1 1,2 2,100 80)",
387                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
388                   0,
389                   strategy);
390     tester::apply("l-mp-04",
391                   "LINESTRING(3 3,4 4,100 80)",
392                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
393                   pp_distance("POINT(1 1)", "POINT(3 3)", strategy),
394                   pp_comparable_distance("POINT(1 1)", "POINT(3 3)", strategy),
395                   strategy);
396     tester::apply("l-mp-05",
397                   "LINESTRING(0 0,10 0,10 10,0 10,0 0)",
398                   "MULTIPOINT(1 -1,80 80,5 0,150 90)",
399                   0,
400                   strategy);
401 }
402
403 //===========================================================================
404
405 template <typename Strategy>
406 void test_distance_multipoint_multilinestring(Strategy const& strategy)
407 {
408 #ifdef BOOST_GEOMETRY_TEST_DEBUG
409     std::cout << std::endl;
410     std::cout << "multipoint/multilinestring distance tests" << std::endl;
411 #endif
412     typedef test_distance_of_geometries
413         <
414             multi_point_type, multi_linestring_type
415         > tester;
416
417     tester::apply("mp-ml-01",
418                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
419                   "MULTILINESTRING((2 0,0 2),(2 2,3 3))",
420                   ps_distance("POINT(1 1)", "SEGMENT(2 0,0 2)", strategy),
421                   ps_comparable_distance("POINT(1 1)",
422                                          "SEGMENT(2 0,0 2)",
423                                          strategy),
424                   strategy);
425     tester::apply("mp-ml-02",
426                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
427                   "MULTILINESTRING((3 0,0 3),(4 4,5 5))",
428                   ps_distance("POINT(1 1)", "SEGMENT(3 0,0 3)", strategy),
429                   ps_comparable_distance("POINT(1 1)",
430                                          "SEGMENT(3 0,0 3)",
431                                          strategy),
432                   strategy);
433     tester::apply("mp-ml-03",
434                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
435                   "MULTILINESTRING((4 4,5 5),(1 1,2 2))",
436                   0,
437                   strategy);
438     tester::apply("mp-ml-04",
439                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
440                   "MULTILINESTRING((4 4,3 3),(4 4,5 5))",
441                   pp_distance("POINT(1 1)", "POINT(3 3)", strategy),
442                   pp_comparable_distance("POINT(1 1)", "POINT(3 3)", strategy),
443                   strategy);
444 }
445
446 //===========================================================================
447
448 template <typename Strategy>
449 void test_distance_multipoint_segment(Strategy const& strategy)
450 {
451 #ifdef BOOST_GEOMETRY_TEST_DEBUG
452     std::cout << std::endl;
453     std::cout << "multipoint/segment distance tests" << std::endl;
454 #endif
455     typedef test_distance_of_geometries<multi_point_type, segment_type> tester;
456
457     double d2r = bg::math::d2r<double>();
458
459     tester::apply("mp-s-01",
460                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
461                   "SEGMENT(2 0,0 2)",
462                   ps_distance("POINT(1 1)", "SEGMENT(2 0,0 2)", strategy),
463                   ps_comparable_distance("POINT(1 1)",
464                                          "SEGMENT(2 0,0 2)",
465                                          strategy),
466                   strategy);
467     tester::apply("mp-s-02",
468                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
469                   "SEGMENT(0 -3,1 -10)",
470                   3.0 * d2r * strategy.radius(),
471                   to_comparable(strategy, 3.0 * d2r * strategy.radius()),
472                   strategy);
473     tester::apply("mp-s-03",
474                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
475                   "SEGMENT(1 1,2 2)",
476                   0,
477                   strategy);
478     tester::apply("mp-s-04",
479                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
480                   "SEGMENT(3 3,4 4)",
481                   pp_distance("POINT(1 1)", "POINT(3 3)", strategy),
482                   pp_comparable_distance("POINT(1 1)", "POINT(3 3)", strategy),
483                   strategy);
484     tester::apply("mp-s-05",
485                   "MULTIPOINT(0 0,1 0,0 1,1 1)",
486                   "SEGMENT(0.5 -3,1 -10)",
487                   pp_distance("POINT(1 0)", "POINT(0.5 -3)", strategy),
488                   pp_comparable_distance("POINT(1 0)",
489                                          "POINT(0.5 -3)",
490                                          strategy),
491                   strategy);
492 }
493
494 //===========================================================================
495 //===========================================================================
496 //===========================================================================
497
498 BOOST_AUTO_TEST_CASE( test_all_pointlike_linear )
499 {
500     test_distance_point_segment(point_segment_strategy());
501     test_distance_point_segment(point_segment_strategy(earth_radius_km));
502     test_distance_point_segment(point_segment_strategy(earth_radius_miles));
503 }
504
505 BOOST_AUTO_TEST_CASE( test_all_point_linestring )
506 {
507     test_distance_point_linestring(point_segment_strategy());
508     test_distance_point_linestring(point_segment_strategy(earth_radius_km));
509     test_distance_point_linestring(point_segment_strategy(earth_radius_miles));
510 }
511
512 BOOST_AUTO_TEST_CASE( test_all_point_multilinestring )
513 {
514     test_distance_point_multilinestring(point_segment_strategy());
515     test_distance_point_multilinestring(point_segment_strategy(earth_radius_km));
516     test_distance_point_multilinestring(point_segment_strategy(earth_radius_miles));
517 }
518
519 BOOST_AUTO_TEST_CASE( test_all_linestring_multipoint )
520 {
521     test_distance_linestring_multipoint(point_segment_strategy());
522     test_distance_linestring_multipoint(point_segment_strategy(earth_radius_km));
523     test_distance_linestring_multipoint(point_segment_strategy(earth_radius_miles));
524 }
525
526 BOOST_AUTO_TEST_CASE( test_all_multipoint_multilinestring )
527 {
528     test_distance_multipoint_multilinestring(point_segment_strategy());
529     test_distance_multipoint_multilinestring(point_segment_strategy(earth_radius_km));
530     test_distance_multipoint_multilinestring(point_segment_strategy(earth_radius_miles));
531 }
532
533 BOOST_AUTO_TEST_CASE( test_all_multipoint_segment )
534 {
535     test_distance_multipoint_segment(point_segment_strategy());
536     test_distance_multipoint_segment(point_segment_strategy(earth_radius_km));
537     test_distance_multipoint_segment(point_segment_strategy(earth_radius_miles));
538 }
539
540 BOOST_AUTO_TEST_CASE( test_all_empty_input_pointlike_linear )
541 {
542     test_more_empty_input_pointlike_linear
543         <
544             point_type
545         >(point_segment_strategy());
546
547     test_more_empty_input_pointlike_linear
548         <
549             point_type
550         >(point_segment_strategy(earth_radius_km));
551
552     test_more_empty_input_pointlike_linear
553         <
554             point_type
555         >(point_segment_strategy(earth_radius_miles));
556 }