1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2010-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_TEST_DISTANCE_HPP
10 #define BOOST_GEOMETRY_TEST_DISTANCE_HPP
12 #include <geometry_test_common.hpp>
14 #include <boost/geometry/algorithms/distance.hpp>
15 #include <boost/geometry/io/wkt/read.hpp>
16 #include <boost/geometry/strategies/strategies.hpp>
18 // Define a custom distance strategy
19 // For this one, the "taxicab" distance,
20 // see http://en.wikipedia.org/wiki/Taxicab_geometry
22 // For a point-point-distance operation, one typename Point is enough.
23 // For a point-segment-distance operation, there is some magic inside
24 // using another point type and casting if necessary. Therefore,
25 // two point-types are necessary.
26 struct taxicab_distance
28 template <typename P1, typename P2>
29 static inline typename bg::coordinate_type<P1>::type apply(
30 P1 const& p1, P2 const& p2)
34 return abs(get<0>(p1) - get<1>(p2))
35 + abs(get<1>(p1) - get<1>(p2));
41 namespace boost { namespace geometry { namespace strategy { namespace distance { namespace services
45 struct tag<taxicab_distance>
47 typedef strategy_tag_distance_point_point type;
51 template <typename P1, typename P2>
52 struct return_type<taxicab_distance, P1, P2>
54 typedef typename coordinate_type<P1>::type type;
59 struct comparable_type<taxicab_distance>
61 typedef taxicab_distance type;
65 struct get_comparable<taxicab_distance>
67 static inline taxicab_distance apply(taxicab_distance const& input)
73 template <typename P1, typename P2>
74 struct result_from_distance<taxicab_distance, P1, P2>
77 static inline typename coordinate_type<P1>::type apply(taxicab_distance const& , T const& value)
84 }}}}} // namespace bg::strategy::distance::services
90 template <typename Geometry1, typename Geometry2>
91 void test_distance(Geometry1 const& geometry1,
92 Geometry2 const& geometry2,
93 long double expected_distance)
95 typename bg::default_distance_result<Geometry1>::type distance = bg::distance(geometry1, geometry2);
97 #ifdef BOOST_GEOMETRY_TEST_DEBUG
98 std::ostringstream out;
99 out << typeid(typename bg::coordinate_type<Geometry1>::type).name()
101 << typeid(typename bg::default_distance_result<Geometry1>::type).name()
103 << "distance : " << bg::distance(geometry1, geometry2)
105 std::cout << out.str();
108 BOOST_CHECK_CLOSE(distance, expected_distance, 0.0001);
112 template <typename Geometry1, typename Geometry2>
113 void test_geometry(std::string const& wkt1, std::string const& wkt2, double expected_distance)
116 bg::read_wkt(wkt1, geometry1);
118 bg::read_wkt(wkt2, geometry2);
120 test_distance(geometry1, geometry2, expected_distance);
123 template <typename Geometry1, typename Geometry2>
124 void test_empty_input(Geometry1 const& geometry1, Geometry2 const& geometry2)
128 bg::distance(geometry1, geometry2);
130 catch(bg::empty_input_exception const& )
134 BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );