1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
6 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
8 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
9 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
19 #include <algorithms/test_distance.hpp>
21 #include <boost/mpl/if.hpp>
22 #include <boost/array.hpp>
24 #include <boost/geometry/geometries/geometries.hpp>
25 #include <boost/geometry/geometries/point_xy.hpp>
26 #include <boost/geometry/geometries/adapted/c_array.hpp>
27 #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
29 #include <test_common/test_point.hpp>
30 #include <test_geometries/custom_segment.hpp>
31 #include <test_geometries/wrapped_boost_array.hpp>
33 // includes for multi-geometries
34 #include <boost/geometry/multi/geometries/multi_point.hpp>
35 #include <boost/geometry/multi/geometries/multi_linestring.hpp>
36 #include <boost/geometry/multi/geometries/multi_polygon.hpp>
37 #include <boost/geometry/multi/io/wkt/read.hpp>
39 #include <boost/variant/variant.hpp>
41 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
42 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
44 // Register boost array as a linestring
45 namespace boost { namespace geometry { namespace traits
47 template <typename Point, std::size_t PointCount>
48 struct tag< boost::array<Point, PointCount> >
50 typedef linestring_tag type;
56 void test_distance_point()
58 namespace services = bg::strategy::distance::services;
59 typedef typename bg::default_distance_result<P>::type return_type;
61 // Basic, trivial test
71 return_type d = bg::distance(p1, p2);
72 BOOST_CHECK_CLOSE(d, return_type(1.4142135), 0.001);
74 // Test specifying strategy manually
75 typename services::default_strategy
77 bg::point_tag, bg::point_tag, P
80 d = bg::distance(p1, p2, strategy);
81 BOOST_CHECK_CLOSE(d, return_type(1.4142135), 0.001);
84 // Test custom strategy
85 BOOST_CONCEPT_ASSERT( (bg::concept::PointDistanceStrategy<taxicab_distance, P, P>) );
87 typedef typename services::return_type<taxicab_distance, P, P>::type cab_return_type;
88 BOOST_MPL_ASSERT((boost::is_same<cab_return_type, typename bg::coordinate_type<P>::type>));
91 cab_return_type d = bg::distance(p1, p2, tcd);
93 BOOST_CHECK( bg::math::abs(d - cab_return_type(2)) <= cab_return_type(0.01) );
99 typedef typename services::default_strategy
101 bg::point_tag, bg::point_tag, P
102 >::type strategy_type;
103 typedef typename services::comparable_type<strategy_type>::type comparable_strategy_type;
105 strategy_type strategy;
106 comparable_strategy_type comparable_strategy = services::get_comparable<strategy_type>::apply(strategy);
107 return_type comparable = services::result_from_distance<comparable_strategy_type, P, P>::apply(comparable_strategy, 3);
109 BOOST_CHECK_CLOSE(comparable, return_type(9), 0.001);
113 template <typename P>
114 void test_distance_segment()
116 typedef typename bg::default_distance_result<P>::type return_type;
118 P s1; bg::set<0>(s1, 1); bg::set<1>(s1, 1);
119 P s2; bg::set<0>(s2, 4); bg::set<1>(s2, 4);
121 // Check points left, right, projected-left, projected-right, on segment
122 P p1; bg::set<0>(p1, 0); bg::set<1>(p1, 1);
123 P p2; bg::set<0>(p2, 1); bg::set<1>(p2, 0);
124 P p3; bg::set<0>(p3, 3); bg::set<1>(p3, 1);
125 P p4; bg::set<0>(p4, 1); bg::set<1>(p4, 3);
126 P p5; bg::set<0>(p5, 3); bg::set<1>(p5, 3);
128 bg::model::referring_segment<P const> const seg(s1, s2);
130 return_type d1 = bg::distance(p1, seg);
131 return_type d2 = bg::distance(p2, seg);
132 return_type d3 = bg::distance(p3, seg);
133 return_type d4 = bg::distance(p4, seg);
134 return_type d5 = bg::distance(p5, seg);
136 BOOST_CHECK_CLOSE(d1, return_type(1), 0.001);
137 BOOST_CHECK_CLOSE(d2, return_type(1), 0.001);
138 BOOST_CHECK_CLOSE(d3, return_type(1.4142135), 0.001);
139 BOOST_CHECK_CLOSE(d4, return_type(1.4142135), 0.001);
140 BOOST_CHECK_CLOSE(d5, return_type(0), 0.001);
142 // Reverse case: segment/point instead of point/segment
143 return_type dr1 = bg::distance(seg, p1);
144 return_type dr2 = bg::distance(seg, p2);
146 BOOST_CHECK_CLOSE(dr1, d1, 0.001);
147 BOOST_CHECK_CLOSE(dr2, d2, 0.001);
149 // Test specifying strategy manually:
150 // 1) point-point-distance
151 typename bg::strategy::distance::services::default_strategy
153 bg::point_tag, bg::point_tag, P
155 d1 = bg::distance(p1, seg, pp_strategy);
156 BOOST_CHECK_CLOSE(d1, return_type(1), 0.001);
158 // 2) point-segment-distance
159 typename bg::strategy::distance::services::default_strategy
161 bg::point_tag, bg::segment_tag, P
163 d1 = bg::distance(p1, seg, ps_strategy);
164 BOOST_CHECK_CLOSE(d1, return_type(1), 0.001);
166 // 3) custom point strategy
167 taxicab_distance tcd;
168 d1 = bg::distance(p1, seg, tcd);
169 BOOST_CHECK_CLOSE(d1, return_type(1), 0.001);
172 template <typename Point, typename Geometry, typename T>
173 void test_distance_linear(std::string const& wkt_point, std::string const& wkt_geometry, T const& expected)
176 bg::read_wkt(wkt_point, p);
179 bg::read_wkt(wkt_geometry, g);
181 typedef typename bg::default_distance_result<Point>::type return_type;
182 return_type d = bg::distance(p, g);
184 // For point-to-linestring (or point-to-polygon), both a point-strategy and a point-segment-strategy can be specified.
186 return_type ds1 = bg::distance(p, g, bg::strategy::distance::pythagoras<>());
187 return_type ds2 = bg::distance(p, g, bg::strategy::distance::projected_point<>());
189 BOOST_CHECK_CLOSE(d, return_type(expected), 0.001);
190 BOOST_CHECK_CLOSE(ds1, return_type(expected), 0.001);
191 BOOST_CHECK_CLOSE(ds2, return_type(expected), 0.001);
194 template <typename P>
195 void test_distance_array_as_linestring()
197 typedef typename bg::default_distance_result<P>::type return_type;
199 // Normal array does not have
200 boost::array<P, 2> points;
201 bg::set<0>(points[0], 1);
202 bg::set<1>(points[0], 1);
203 bg::set<0>(points[1], 3);
204 bg::set<1>(points[1], 3);
210 return_type d = bg::distance(p, points);
211 BOOST_CHECK_CLOSE(d, return_type(0.70710678), 0.001);
213 bg::set<0>(p, 5); bg::set<1>(p, 5);
214 d = bg::distance(p, points);
215 BOOST_CHECK_CLOSE(d, return_type(2.828427), 0.001);
219 // code moved from the distance unit test in multi/algorithms -- start
220 template <typename Geometry1, typename Geometry2>
221 void test_distance(std::string const& wkt1, std::string const& wkt2, double expected)
225 bg::read_wkt(wkt1, g1);
226 bg::read_wkt(wkt2, g2);
227 typename bg::default_distance_result<Geometry1, Geometry2>::type d = bg::distance(g1, g2);
229 BOOST_CHECK_CLOSE(d, expected, 0.0001);
232 template <typename Geometry1, typename Geometry2, typename Strategy>
233 void test_distance(Strategy const& strategy, std::string const& wkt1,
234 std::string const& wkt2, double expected)
238 bg::read_wkt(wkt1, g1);
239 bg::read_wkt(wkt2, g2);
240 typename bg::default_distance_result<Geometry1, Geometry2>::type d = bg::distance(g1, g2, strategy);
242 BOOST_CHECK_CLOSE(d, expected, 0.0001);
246 template <typename P>
249 typedef bg::model::multi_point<P> mp;
250 typedef bg::model::multi_linestring<bg::model::linestring<P> > ml;
251 test_distance<P, P>("POINT(0 0)", "POINT(1 1)", sqrt(2.0));
252 test_distance<P, mp>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
253 test_distance<mp, P>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
254 test_distance<mp, mp>("MULTIPOINT((1 1),(1 0),(0 2))", "MULTIPOINT((2 2),(2 3))", sqrt(2.0));
255 test_distance<P, ml>("POINT(0 0)", "MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", 1.0);
256 test_distance<ml, P>("MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", "POINT(0 0)", 1.0);
257 test_distance<ml, mp>("MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", "MULTIPOINT((0 0),(1 1))", 0.0);
259 // Test with a strategy
260 bg::strategy::distance::pythagoras<> pyth;
261 test_distance<P, P>(pyth, "POINT(0 0)", "POINT(1 1)", sqrt(2.0));
262 test_distance<P, mp>(pyth, "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
263 test_distance<mp, P>(pyth, "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
267 template <typename P>
270 typedef bg::model::multi_point<P> mp;
271 test_distance<P, P>("POINT(0 0 0)", "POINT(1 1 1)", sqrt(3.0));
272 test_distance<P, mp>("POINT(0 0 0)", "MULTIPOINT((1 1 1),(1 0 0),(0 1 2))", 1.0);
273 test_distance<mp, mp>("MULTIPOINT((1 1 1),(1 0 0),(0 0 2))", "MULTIPOINT((2 2 2),(2 3 4))", sqrt(3.0));
277 template <typename P1, typename P2>
280 typedef bg::model::multi_point<P1> mp1;
281 typedef bg::model::multi_point<P2> mp2;
283 test_distance<P1, P2>("POINT(0 0)", "POINT(1 1)", sqrt(2.0));
285 test_distance<P1, mp1>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
286 test_distance<P1, mp2>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
287 test_distance<P2, mp1>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
288 test_distance<P2, mp2>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
290 // Test automatic reversal
291 test_distance<mp1, P1>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
292 test_distance<mp1, P2>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
293 test_distance<mp2, P1>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
294 test_distance<mp2, P2>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
296 // Test multi-multi using different point types for each
297 test_distance<mp1, mp2>("MULTIPOINT((1 1),(1 0),(0 2))", "MULTIPOINT((2 2),(2 3))", sqrt(2.0));
299 // Test with a strategy
300 using namespace bg::strategy::distance;
302 test_distance<P1, P2>(pythagoras<>(), "POINT(0 0)", "POINT(1 1)", sqrt(2.0));
304 test_distance<P1, mp1>(pythagoras<>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
305 test_distance<P1, mp2>(pythagoras<>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
306 test_distance<P2, mp1>(pythagoras<>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
307 test_distance<P2, mp2>(pythagoras<>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
309 // Most interesting: reversal AND a strategy (note that the stategy must be reversed automatically
310 test_distance<mp1, P1>(pythagoras<>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
311 test_distance<mp1, P2>(pythagoras<>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
312 test_distance<mp2, P1>(pythagoras<>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
313 test_distance<mp2, P2>(pythagoras<>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
315 // code moved from the distance unit test in multi/algorithms -- end
320 template <typename P>
323 test_distance_point<P>();
324 test_distance_segment<P>();
325 test_distance_array_as_linestring<P>();
327 test_geometry<P, bg::model::segment<P> >("POINT(1 3)", "LINESTRING(1 1,4 4)", sqrt(2.0));
328 test_geometry<P, bg::model::segment<P> >("POINT(3 1)", "LINESTRING(1 1,4 4)", sqrt(2.0));
330 test_geometry<P, P>("POINT(1 1)", "POINT(2 2)", sqrt(2.0));
331 test_geometry<P, P>("POINT(0 0)", "POINT(0 3)", 3.0);
332 test_geometry<P, P>("POINT(0 0)", "POINT(4 0)", 4.0);
333 test_geometry<P, P>("POINT(0 3)", "POINT(4 0)", 5.0);
334 test_geometry<P, bg::model::linestring<P> >("POINT(1 3)", "LINESTRING(1 1,4 4)", sqrt(2.0));
335 test_geometry<P, bg::model::linestring<P> >("POINT(3 1)", "LINESTRING(1 1,4 4)", sqrt(2.0));
336 test_geometry<P, bg::model::linestring<P> >("POINT(50 50)", "LINESTRING(50 40, 40 50)", sqrt(50.0));
337 test_geometry<P, bg::model::linestring<P> >("POINT(50 50)", "LINESTRING(50 40, 40 50, 0 90)", sqrt(50.0));
338 test_geometry<bg::model::linestring<P>, P>("LINESTRING(1 1,4 4)", "POINT(1 3)", sqrt(2.0));
339 test_geometry<bg::model::linestring<P>, P>("LINESTRING(50 40, 40 50)", "POINT(50 50)", sqrt(50.0));
340 test_geometry<bg::model::linestring<P>, P>("LINESTRING(50 40, 40 50, 0 90)", "POINT(50 50)", sqrt(50.0));
343 test_geometry<P, bg::model::ring<P> >("POINT(1 3)", "POLYGON((1 1,4 4,5 0,1 1))", sqrt(2.0));
344 test_geometry<P, bg::model::ring<P> >("POINT(3 1)", "POLYGON((1 1,4 4,5 0,1 1))", 0.0);
346 test_geometry<bg::model::ring<P>, P>("POLYGON((1 1,4 4,5 0,1 1))", "POINT(3 1)", 0.0);
348 test_geometry<P, bg::model::ring<P, true, false> >("POINT(1 3)", "POLYGON((4 4,5 0,1 1))", sqrt(2.0));
351 test_geometry<P, bg::model::polygon<P> >("POINT(1 3)", "POLYGON((1 1,4 4,5 0,1 1))", sqrt(2.0));
352 test_geometry<P, bg::model::polygon<P> >("POINT(3 1)", "POLYGON((1 1,4 4,5 0,1 1))", 0.0);
354 test_geometry<bg::model::polygon<P>, P>("POLYGON((1 1,4 4,5 0,1 1))", "POINT(3 1)", 0.0);
356 test_geometry<P, bg::model::polygon<P, true, false> >("POINT(1 3)", "POLYGON((4 4,5 0,1 1))", sqrt(2.0));
358 // Polygons with holes
359 std::string donut = "POLYGON ((0 0,1 9,8 1,0 0),(1 1,4 1,1 4,1 1))";
360 test_geometry<P, bg::model::polygon<P> >("POINT(2 2)", donut, 0.5 * sqrt(2.0));
361 test_geometry<P, bg::model::polygon<P> >("POINT(3 3)", donut, 0.0);
363 test_geometry<bg::model::polygon<P>, P>(donut, "POINT(2 2)", 0.5 * sqrt(2.0));
365 test_geometry<P, bg::model::polygon<P, true, false> >("POINT(2 2)", "POLYGON ((0 0,1 9,8 1),(1 1,4 1,1 4))", 0.5 * sqrt(2.0));
367 // Should (currently) give compiler assertion
368 // test_geometry<bg::model::polygon<P>, bg::model::polygon<P> >(donut, donut, 0.5 * sqrt(2.0));
370 // DOES NOT COMPILE - cannot do read_wkt (because boost::array is not variably sized)
371 // test_geometry<P, boost::array<P, 2> >("POINT(3 1)", "LINESTRING(1 1,4 4)", sqrt(2.0));
373 test_geometry<P, test::wrapped_boost_array<P, 2> >("POINT(3 1)", "LINESTRING(1 1,4 4)", sqrt(2.0));
375 test_distance_linear<P, bg::model::linestring<P> >("POINT(3 1)", "LINESTRING(1 1,4 4)", sqrt(2.0));
378 template <typename P>
379 void test_empty_input()
382 bg::model::linestring<P> line_empty;
383 bg::model::polygon<P> poly_empty;
384 bg::model::ring<P> ring_empty;
385 bg::model::multi_point<P> mp_empty;
386 bg::model::multi_linestring<bg::model::linestring<P> > ml_empty;
388 test_empty_input(p, line_empty);
389 test_empty_input(p, poly_empty);
390 test_empty_input(p, ring_empty);
392 test_empty_input(p, mp_empty);
393 test_empty_input(p, ml_empty);
394 test_empty_input(mp_empty, mp_empty);
396 // Test behaviour if one of the inputs is empty
397 bg::model::multi_point<P> mp;
399 test_empty_input(mp_empty, mp);
400 test_empty_input(mp, mp_empty);
403 void test_large_integers()
405 typedef bg::model::point<int, 2, bg::cs::cartesian> int_point_type;
406 typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type;
410 std::string const a = "POINT(2544000 528000)";
411 std::string const b = "POINT(2768040 528000)";
412 int_point_type ia, ib;
413 double_point_type da, db;
419 BOOST_AUTO(idist, bg::distance(ia, ib));
420 BOOST_AUTO(ddist, bg::distance(da, db));
422 BOOST_CHECK_MESSAGE(std::abs(idist - ddist) < 0.1,
423 "within<a double> different from within<an int>");
427 std::string const a = "POINT(2600000 529000)";
428 std::string const b = "LINESTRING(2544000 528000, 2768040 528000)";
430 double_point_type da;
431 bg::model::segment<int_point_type> ib;
432 bg::model::segment<double_point_type> db;
438 BOOST_AUTO(idist, bg::distance(ia, ib));
439 BOOST_AUTO(ddist, bg::distance(da, db));
441 BOOST_CHECK_MESSAGE(std::abs(idist - ddist) < 0.1,
442 "within<a double> different from within<an int>");
446 template <typename T>
449 typedef bg::model::point<T, 2, bg::cs::cartesian> point_type;
450 typedef bg::model::segment<point_type> segment_type;
451 typedef bg::model::box<point_type> box_type;
452 typedef boost::variant<point_type, segment_type, box_type> variant_type;
455 std::string const point_li = "POINT(1 3)";
456 bg::read_wkt(point_li, point);
459 std::string const seg_li = "LINESTRING(1 1,4 4)";
460 bg::read_wkt(seg_li, seg);
467 typename bg::distance_result
469 variant_type, variant_type, bg::default_strategy
478 BOOST_CHECK_CLOSE(bg::distance(v1, v2), bg::distance(point, point), 0.0001);
479 BOOST_CHECK_CLOSE(bg::distance(v1, point), bg::distance(point, point), 0.0001);
480 BOOST_CHECK_CLOSE(bg::distance(point, v2), bg::distance(point, point), 0.0001);
483 BOOST_CHECK_CLOSE(bg::distance(v1, v2), bg::distance(point, seg), 0.0001);
484 BOOST_CHECK_CLOSE(bg::distance(v1, seg), bg::distance(point, seg), 0.0001);
485 BOOST_CHECK_CLOSE(bg::distance(point, v2), bg::distance(point, seg), 0.0001);
487 // User defined strategy
490 bg::strategy::distance::haversine<double> s;
491 //BOOST_CHECK_CLOSE(bg::distance(v1, v2, s), bg::distance(point, point, s), 0.0001);
492 //BOOST_CHECK_CLOSE(bg::distance(v1, point, s), bg::distance(point, point, s), 0.0001);
493 //BOOST_CHECK_CLOSE(bg::distance(point, v2, s), bg::distance(point, point, s), 0.0001);
496 int test_main(int, char* [])
499 //test_all<int[2]>();
500 //test_all<float[2]>();
501 //test_all<double[2]>();
502 //test_all<test::test_point>(); // located here because of 3D
505 test_large_integers();
507 test_all<bg::model::d2::point_xy<int> >();
508 test_all<boost::tuple<float, float> >();
509 test_all<bg::model::d2::point_xy<float> >();
510 test_all<bg::model::d2::point_xy<double> >();
513 test_all<bg::model::d2::point_xy<ttmath_big> >();
516 test_empty_input<bg::model::d2::point_xy<int> >();
518 // below are the test cases moved here from the distance unit test
519 // in test/multi/algorithms
520 test_2d<boost::tuple<float, float> >();
521 test_2d<bg::model::d2::point_xy<float> >();
522 test_2d<bg::model::d2::point_xy<double> >();
524 test_3d<boost::tuple<float, float, float> >();
525 test_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
527 test_mixed<bg::model::d2::point_xy<float>, bg::model::d2::point_xy<double> >();
530 test_2d<bg::model::d2::point_xy<ttmath_big> >();
531 test_mixed<bg::model::d2::point_xy<ttmath_big>, bg::model::d2::point_xy<double> >();
534 test_empty_input<bg::model::d2::point_xy<int> >();
536 test_variant<double>();