1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2014-2018, Oracle and/or its affiliates.
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
10 // Licensed under the Boost Software License version 1.0.
11 // http://www.boost.org/users/license.html
15 #ifndef BOOST_TEST_MODULE
16 #define BOOST_TEST_MODULE test_distance_spherical_equatorial_pointlike_linear
19 #include <boost/test/included/unit_test.hpp>
21 #include "test_distance_se_common.hpp"
22 #include "test_empty_geometry.hpp"
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
;
32 namespace services
= bg::strategy::distance::services
;
33 typedef bg::default_distance_result
<point_type
>::type return_type
;
35 typedef bg::strategy::distance::haversine
<double> point_point_strategy
;
36 typedef bg::strategy::distance::cross_track
<> point_segment_strategy
;
39 //===========================================================================
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
)
48 bg::read_wkt(wkt1
, p1
);
49 bg::read_wkt(wkt2
, p2
);
50 return bg::distance(p1
, p2
) * strategy
.radius();
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
,
60 bg::read_wkt(wkt1
, p1
);
61 bg::read_wkt(wkt2
, p2
);
62 return bg::comparable_distance(p1
, p2
);
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
)
73 bg::read_wkt(wkt1
, p
);
74 bg::read_wkt(wkt2
, s
);
75 return bg::distance(p
, s
, strategy
);
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
)
86 bg::read_wkt(wkt1
, p
);
87 bg::read_wkt(wkt2
, s
);
88 return bg::comparable_distance(p
, s
, strategy
);
91 template <typename Strategy
, typename T
>
92 T
to_comparable(Strategy
const& strategy
, T
const& distance
)
94 namespace services
= bg::strategy::distance::services
;
96 typedef typename
services::comparable_type
99 >::type comparable_strategy
;
101 typedef typename
services::result_from_distance
105 bg::point_type
<segment_type
>::type
106 > get_comparable_distance
;
108 comparable_strategy cstrategy
= services::get_comparable
113 return get_comparable_distance::apply(cstrategy
, distance
);
116 //===========================================================================
118 template <typename Strategy
>
119 void test_distance_point_segment(Strategy
const& strategy
)
121 #ifdef BOOST_GEOMETRY_TEST_DEBUG
122 std::cout
<< std::endl
;
123 std::cout
<< "point/segment distance tests" << std::endl
;
125 typedef test_distance_of_geometries
<point_type
, segment_type
> tester
;
127 double const d2r
= bg::math::d2r
<double>();
129 tester::apply("p-s-01",
132 2.0 * d2r
* strategy
.radius(),
133 to_comparable(strategy
, 2.0 * d2r
* strategy
.radius()),
135 tester::apply("p-s-02",
138 3.0 * d2r
* strategy
.radius(),
139 to_comparable(strategy
, 3.0 * d2r
* strategy
.radius()),
141 tester::apply("p-s-03",
146 tester::apply("p-s-04",
151 tester::apply("p-s-05",
156 tester::apply("p-s-06",
159 pp_distance("POINT(3 0)", "POINT(3.5 3)", strategy
),
160 pp_comparable_distance("POINT(3 0)",
164 tester::apply("p-s-07",
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)",
172 // very small distances to segment
173 tester::apply("p-s-07",
175 "SEGMENT(0.5 0,175.5 0)",
176 1e-3 * d2r
* strategy
.radius(),
177 to_comparable(strategy
, 1e-3 * d2r
* strategy
.radius()),
179 tester::apply("p-s-08",
181 "SEGMENT(0.5 0,175.5 0)",
182 1e-4 * d2r
* strategy
.radius(),
183 to_comparable(strategy
, 1e-4 * d2r
* strategy
.radius()),
185 tester::apply("p-s-09",
187 "SEGMENT(0.5 0,175.5 0)",
188 1e-5 * d2r
* strategy
.radius(),
189 to_comparable(strategy
, 1e-5 * d2r
* strategy
.radius()),
191 tester::apply("p-s-10",
193 "SEGMENT(0.5 0,175.5 0)",
194 1e-6 * d2r
* strategy
.radius(),
195 to_comparable(strategy
, 1e-6 * d2r
* strategy
.radius()),
197 tester::apply("p-s-11",
199 "SEGMENT(0.5 0,175.5 0)",
200 1e-7 * d2r
* strategy
.radius(),
201 to_comparable(strategy
, 1e-7 * d2r
* strategy
.radius()),
203 tester::apply("p-s-12",
205 "SEGMENT(0.5 0,175.5 0)",
206 1e-8 * d2r
* strategy
.radius(),
207 to_comparable(strategy
, 1e-8 * d2r
* strategy
.radius()),
211 //===========================================================================
213 template <typename Strategy
>
214 void test_distance_point_linestring(Strategy
const& strategy
)
216 #ifdef BOOST_GEOMETRY_TEST_DEBUG
217 std::cout
<< std::endl
;
218 std::cout
<< "point/linestring distance tests" << std::endl
;
220 typedef test_distance_of_geometries
<point_type
, linestring_type
> tester
;
222 double const r
= strategy
.radius();
223 double const d2r
= bg::math::d2r
<double>();
225 tester::apply("p-l-01",
227 "LINESTRING(2 0,2 0)",
229 to_comparable(strategy
, 2.0 * d2r
* r
),
231 tester::apply("p-l-02",
233 "LINESTRING(2 0,3 0)",
235 to_comparable(strategy
, 2.0 * d2r
* r
),
237 tester::apply("p-l-03",
239 "LINESTRING(2 0,3 0)",
241 to_comparable(strategy
, 3.0 * d2r
* r
),
243 tester::apply("p-l-04",
245 "LINESTRING(2 0,3 0)",
248 tester::apply("p-l-05",
250 "LINESTRING(2 0,3 0)",
253 tester::apply("p-l-06",
255 "LINESTRING(2 0,3 0)",
258 tester::apply("p-l-07",
260 "LINESTRING(1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 0)",
262 to_comparable(strategy
, 10.0 * d2r
* r
),
264 tester::apply("p-l-08",
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)",
273 // https://svn.boost.org/trac/boost/ticket/11982
274 tester::apply("p-l-09",
276 "LINESTRING(10.733557 59.911923, 10.521812 59.887214)",
277 0.06146397739758279 * r
,
278 0.000944156107132969,
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)",
294 //===========================================================================
296 template <typename Strategy
>
297 void test_distance_point_multilinestring(Strategy
const& strategy
)
299 #ifdef BOOST_GEOMETRY_TEST_DEBUG
300 std::cout
<< std::endl
;
301 std::cout
<< "point/multilinestring distance tests" << std::endl
;
303 typedef test_distance_of_geometries
305 point_type
, multi_linestring_type
308 double const d2r
= bg::math::d2r
<double>();
310 tester::apply("p-ml-01",
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()),
316 tester::apply("p-ml-02",
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()),
322 tester::apply("p-ml-03",
324 "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
327 tester::apply("p-ml-04",
329 "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
332 tester::apply("p-ml-05",
334 "MULTILINESTRING((-5 0,-3 0),(2 0,3 0))",
337 tester::apply("p-ml-06",
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)",
345 tester::apply("p-ml-07",
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)",
355 //===========================================================================
357 template <typename Strategy
>
358 void test_distance_linestring_multipoint(Strategy
const& strategy
)
360 #ifdef BOOST_GEOMETRY_TEST_DEBUG
361 std::cout
<< std::endl
;
362 std::cout
<< "linestring/multipoint distance tests" << std::endl
;
364 typedef test_distance_of_geometries
366 linestring_type
, multi_point_type
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)",
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)",
385 tester::apply("l-mp-03",
386 "LINESTRING(1 1,2 2,100 80)",
387 "MULTIPOINT(0 0,1 0,0 1,1 1)",
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
),
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)",
403 //===========================================================================
405 template <typename Strategy
>
406 void test_distance_multipoint_multilinestring(Strategy
const& strategy
)
408 #ifdef BOOST_GEOMETRY_TEST_DEBUG
409 std::cout
<< std::endl
;
410 std::cout
<< "multipoint/multilinestring distance tests" << std::endl
;
412 typedef test_distance_of_geometries
414 multi_point_type
, multi_linestring_type
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)",
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)",
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))",
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
),
446 //===========================================================================
448 template <typename Strategy
>
449 void test_distance_multipoint_segment(Strategy
const& strategy
)
451 #ifdef BOOST_GEOMETRY_TEST_DEBUG
452 std::cout
<< std::endl
;
453 std::cout
<< "multipoint/segment distance tests" << std::endl
;
455 typedef test_distance_of_geometries
<multi_point_type
, segment_type
> tester
;
457 double d2r
= bg::math::d2r
<double>();
459 tester::apply("mp-s-01",
460 "MULTIPOINT(0 0,1 0,0 1,1 1)",
462 ps_distance("POINT(1 1)", "SEGMENT(2 0,0 2)", strategy
),
463 ps_comparable_distance("POINT(1 1)",
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()),
473 tester::apply("mp-s-03",
474 "MULTIPOINT(0 0,1 0,0 1,1 1)",
478 tester::apply("mp-s-04",
479 "MULTIPOINT(0 0,1 0,0 1,1 1)",
481 pp_distance("POINT(1 1)", "POINT(3 3)", strategy
),
482 pp_comparable_distance("POINT(1 1)", "POINT(3 3)", 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)",
494 //===========================================================================
495 //===========================================================================
496 //===========================================================================
498 BOOST_AUTO_TEST_CASE( test_all_pointlike_linear
)
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
));
505 BOOST_AUTO_TEST_CASE( test_all_point_linestring
)
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
));
512 BOOST_AUTO_TEST_CASE( test_all_point_multilinestring
)
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
));
519 BOOST_AUTO_TEST_CASE( test_all_linestring_multipoint
)
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
));
526 BOOST_AUTO_TEST_CASE( test_all_multipoint_multilinestring
)
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
));
533 BOOST_AUTO_TEST_CASE( test_all_multipoint_segment
)
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
));
540 BOOST_AUTO_TEST_CASE( test_all_empty_input_pointlike_linear
)
542 test_more_empty_input_pointlike_linear
545 >(point_segment_strategy());
547 test_more_empty_input_pointlike_linear
550 >(point_segment_strategy(earth_radius_km
));
552 test_more_empty_input_pointlike_linear
555 >(point_segment_strategy(earth_radius_miles
));