123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- // Boost.Geometry (aka GGL, Generic Geometry Library)
- // Unit Test
- // Copyright (c) 2014-2017, Oracle and/or its affiliates.
- // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
- // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
- // Licensed under the Boost Software License version 1.0.
- // http://www.boost.org/users/license.html
- #ifndef BOOST_GEOMETRY_TEST_DISTANCE_COMMON_HPP
- #define BOOST_GEOMETRY_TEST_DISTANCE_COMMON_HPP
- #include <iostream>
- #include <string>
- #include <boost/math/special_functions/fpclassify.hpp>
- #include <boost/mpl/assert.hpp>
- #include <boost/type_traits/is_integral.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/geometry/geometries/point.hpp>
- #include <boost/geometry/geometries/point_xy.hpp>
- #include <boost/geometry/geometries/segment.hpp>
- #include <boost/geometry/geometries/linestring.hpp>
- #include <boost/geometry/geometries/polygon.hpp>
- #include <boost/geometry/geometries/ring.hpp>
- #include <boost/geometry/geometries/box.hpp>
- #include <boost/geometry/geometries/multi_point.hpp>
- #include <boost/geometry/geometries/multi_linestring.hpp>
- #include <boost/geometry/geometries/multi_polygon.hpp>
- #include <boost/geometry/io/wkt/write.hpp>
- #include <boost/geometry/io/dsv/write.hpp>
- #include <boost/geometry/algorithms/num_interior_rings.hpp>
- #include <boost/geometry/algorithms/distance.hpp>
- #include <boost/geometry/algorithms/comparable_distance.hpp>
- #include <boost/geometry/strategies/strategies.hpp>
- #include <from_wkt.hpp>
- #include <string_from_type.hpp>
- #ifndef BOOST_GEOMETRY_TEST_DISTANCE_HPP
- namespace bg = ::boost::geometry;
- // function copied from BG's test_distance.hpp
- template <typename Geometry1, typename Geometry2>
- void test_empty_input(Geometry1 const& geometry1, Geometry2 const& geometry2)
- {
- try
- {
- bg::distance(geometry1, geometry2);
- }
- catch(bg::empty_input_exception const& )
- {
- return;
- }
- BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );
- }
- #endif // BOOST_GEOMETRY_TEST_DISTANCE_HPP
- //========================================================================
- #ifdef BOOST_GEOMETRY_TEST_DEBUG
- // pretty print geometry -- START
- template <typename Geometry, typename GeometryTag>
- struct pretty_print_geometry_dispatch
- {
- template <typename Stream>
- static inline Stream& apply(Geometry const& geometry, Stream& os)
- {
- os << bg::wkt(geometry);
- return os;
- }
- };
- template <typename Geometry>
- struct pretty_print_geometry_dispatch<Geometry, bg::segment_tag>
- {
- template <typename Stream>
- static inline Stream& apply(Geometry const& geometry, Stream& os)
- {
- os << "SEGMENT" << bg::dsv(geometry);
- return os;
- }
- };
- template <typename Geometry>
- struct pretty_print_geometry_dispatch<Geometry, bg::box_tag>
- {
- template <typename Stream>
- static inline Stream& apply(Geometry const& geometry, Stream& os)
- {
- os << "BOX" << bg::dsv(geometry);
- return os;
- }
- };
- template <typename Geometry>
- struct pretty_print_geometry
- {
- template <typename Stream>
- static inline Stream& apply(Geometry const& geometry, Stream& os)
- {
- return pretty_print_geometry_dispatch
- <
- Geometry, typename bg::tag<Geometry>::type
- >::apply(geometry, os);
- }
- };
- // pretty print geometry -- END
- #endif // BOOST_GEOMETRY_TEST_DEBUG
- //========================================================================
- template <typename T>
- struct check_equal
- {
- static inline void apply(T const& detected, T const& expected,
- bool is_finite)
- {
- if (is_finite)
- {
- BOOST_CHECK(detected == expected);
- }
- else
- {
- BOOST_CHECK(! boost::math::isfinite(detected));
- }
- }
- };
- template <>
- struct check_equal<double>
- {
- static inline void apply(double detected, double expected,
- bool is_finite)
- {
- if (is_finite)
- {
- BOOST_CHECK_CLOSE(detected, expected, 0.0001);
- }
- else
- {
- BOOST_CHECK(! boost::math::isfinite(detected));
- }
- }
- };
- //========================================================================
- template
- <
- typename Geometry1, typename Geometry2,
- int id1 = bg::geometry_id<Geometry1>::value,
- int id2 = bg::geometry_id<Geometry2>::value
- >
- struct test_distance_of_geometries
- : public test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
- {};
- #ifdef BOOST_GEOMETRY_TEST_DEBUG
- #define ENABLE_IF_DEBUG(ID) ID
- #else
- #define ENABLE_IF_DEBUG(ID)
- #endif
- template <typename Geometry1, typename Geometry2>
- class test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
- {
- private:
- template
- <
- typename G1,
- typename G2,
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void base_test(std::string const& ENABLE_IF_DEBUG(header),
- G1 const& g1, G2 const& g2,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite)
- {
- typedef typename bg::default_distance_result
- <
- G1, G2
- >::type default_distance_result;
- typedef typename bg::strategy::distance::services::return_type
- <
- Strategy, G1, G2
- >::type distance_result_from_strategy;
- static const bool same_regular = boost::is_same
- <
- default_distance_result,
- distance_result_from_strategy
- >::type::value;
- BOOST_CHECK( same_regular );
-
- typedef typename bg::default_comparable_distance_result
- <
- G1, G2
- >::type default_comparable_distance_result;
- typedef typename bg::strategy::distance::services::return_type
- <
- typename bg::strategy::distance::services::comparable_type
- <
- Strategy
- >::type,
- G1,
- G2
- >::type comparable_distance_result_from_strategy;
- static const bool same_comparable = boost::is_same
- <
- default_comparable_distance_result,
- comparable_distance_result_from_strategy
- >::type::value;
-
- BOOST_CHECK( same_comparable );
- // check distance with default strategy
- default_distance_result dist_def = bg::distance(g1, g2);
- check_equal
- <
- default_distance_result
- >::apply(dist_def, expected_distance, is_finite);
- // check distance with passed strategy
- distance_result_from_strategy dist = bg::distance(g1, g2, strategy);
- check_equal
- <
- default_distance_result
- >::apply(dist, expected_distance, is_finite);
- // check comparable distance with default strategy
- default_comparable_distance_result cdist_def =
- bg::comparable_distance(g1, g2);
- check_equal
- <
- default_comparable_distance_result
- >::apply(cdist_def, expected_comparable_distance, is_finite);
- // check comparable distance with passed strategy
- comparable_distance_result_from_strategy cdist =
- bg::comparable_distance(g1, g2, strategy);
- check_equal
- <
- default_comparable_distance_result
- >::apply(cdist, expected_comparable_distance, is_finite);
- #ifdef BOOST_GEOMETRY_TEST_DEBUG
- std::cout << string_from_type<typename bg::coordinate_type<Geometry1>::type>::name()
- << string_from_type<typename bg::coordinate_type<Geometry2>::type>::name()
- << " -> "
- << string_from_type<default_distance_result>::name()
- << string_from_type<default_comparable_distance_result>::name()
- << std::endl;
- std::cout << "distance" << header
- << " (def. strategy) = " << dist_def << " ; "
- << "distance" << header
- <<" (passed strategy) = " << dist << " ; "
- << "comp. distance" << header <<" (def. strategy) = "
- << cdist_def << " ; "
- << "comp. distance" << header <<" (passed strategy) = "
- << cdist << std::endl;
- #endif
- }
- public:
- template
- <
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void apply(std::string const& wkt1,
- std::string const& wkt2,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite = true)
- {
- Geometry1 geometry1 = from_wkt<Geometry1>(wkt1);
- Geometry2 geometry2 = from_wkt<Geometry2>(wkt2);
- apply(geometry1, geometry2,
- expected_distance, expected_comparable_distance,
- strategy, is_finite);
- }
- template
- <
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void apply(Geometry1 const& geometry1,
- Geometry2 const& geometry2,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite = true)
- {
- #ifdef BOOST_GEOMETRY_TEST_DEBUG
- typedef pretty_print_geometry<Geometry1> PPG1;
- typedef pretty_print_geometry<Geometry2> PPG2;
- PPG1::apply(geometry1, std::cout);
- std::cout << " - ";
- PPG2::apply(geometry2, std::cout);
- std::cout << std::endl;
- #endif
- base_test("", geometry1, geometry2,
- expected_distance, expected_comparable_distance,
- strategy, is_finite);
- base_test("[reversed args]", geometry2, geometry1,
- expected_distance, expected_comparable_distance,
- strategy, is_finite);
- #ifdef BOOST_GEOMETRY_TEST_DEBUG
- std::cout << std::endl;
- #endif
- }
- };
- //========================================================================
- template <typename Segment, typename Polygon>
- struct test_distance_of_geometries
- <
- Segment, Polygon,
- 92 /* segment */, 3 /* polygon */
- >
- : public test_distance_of_geometries<Segment, Polygon, 0, 0>
- {
- typedef test_distance_of_geometries<Segment, Polygon, 0, 0> base;
- typedef typename bg::ring_type<Polygon>::type ring_type;
- template
- <
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void apply(std::string const& wkt_segment,
- std::string const& wkt_polygon,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite = true)
- {
- Segment segment = from_wkt<Segment>(wkt_segment);
- Polygon polygon = from_wkt<Polygon>(wkt_polygon);
- apply(segment,
- polygon,
- expected_distance,
- expected_comparable_distance,
- strategy,
- is_finite);
- }
- template
- <
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void apply(Segment const& segment,
- Polygon const& polygon,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite = true)
- {
- base::apply(segment, polygon, expected_distance,
- expected_comparable_distance, strategy, is_finite);
- if ( bg::num_interior_rings(polygon) == 0 ) {
- #ifdef BOOST_GEOMETRY_TEST_DEBUG
- std::cout << "... testing also exterior ring ..." << std::endl;
- #endif
- test_distance_of_geometries
- <
- Segment, ring_type
- >::apply(segment,
- bg::exterior_ring(polygon),
- expected_distance,
- expected_comparable_distance,
- strategy,
- is_finite);
- }
- }
- };
- //========================================================================
- template <typename Box, typename Segment>
- struct test_distance_of_geometries
- <
- Box, Segment,
- 94 /* box */, 92 /* segment */
- >
- {
- template
- <
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void apply(std::string const& wkt_box,
- std::string const& wkt_segment,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite = true)
- {
- test_distance_of_geometries
- <
- Segment, Box, 92, 94
- >::apply(wkt_segment,
- wkt_box,
- expected_distance,
- expected_comparable_distance,
- strategy,
- is_finite);
- }
- };
- template <typename Segment, typename Box>
- struct test_distance_of_geometries
- <
- Segment, Box,
- 92 /* segment */, 94 /* box */
- >
- : public test_distance_of_geometries<Segment, Box, 0, 0>
- {
- typedef test_distance_of_geometries<Segment, Box, 0, 0> base;
- template
- <
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void apply(std::string const& wkt_segment,
- std::string const& wkt_box,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite = true)
- {
- Segment segment = from_wkt<Segment>(wkt_segment);
- Box box = from_wkt<Box>(wkt_box);
- apply(segment,
- box,
- expected_distance,
- expected_comparable_distance,
- strategy,
- is_finite);
- }
- template
- <
- typename DistanceType,
- typename ComparableDistanceType,
- typename Strategy
- >
- static inline
- void apply(Segment const& segment,
- Box const& box,
- DistanceType const& expected_distance,
- ComparableDistanceType const& expected_comparable_distance,
- Strategy const& strategy,
- bool is_finite = true)
- {
- typedef typename bg::strategy::distance::services::return_type
- <
- Strategy, Segment, Box
- >::type distance_result_type;
- typedef typename bg::strategy::distance::services::comparable_type
- <
- Strategy
- >::type comparable_strategy;
- typedef typename bg::strategy::distance::services::return_type
- <
- comparable_strategy, Segment, Box
- >::type comparable_distance_result_type;
- base::apply(segment, box, expected_distance,
- expected_comparable_distance, strategy, is_finite);
- comparable_strategy cstrategy =
- bg::strategy::distance::services::get_comparable
- <
- Strategy
- >::apply(strategy);
- distance_result_type distance_generic =
- bg::detail::distance::segment_to_box_2D_generic
- <
- Segment, Box, Strategy
- >::apply(segment, box, strategy);
- comparable_distance_result_type comparable_distance_generic =
- bg::detail::distance::segment_to_box_2D_generic
- <
- Segment, Box, comparable_strategy
- >::apply(segment, box, cstrategy);
- check_equal
- <
- distance_result_type
- >::apply(distance_generic, expected_distance, is_finite);
- check_equal
- <
- comparable_distance_result_type
- >::apply(comparable_distance_generic,
- expected_comparable_distance,
- is_finite);
- #ifdef BOOST_GEOMETRY_TEST_DEBUG
- std::cout << "... testing with naive seg-box distance algorithm..."
- << std::endl;
- std::cout << "distance (generic algorithm) = "
- << distance_generic << " ; "
- << "comp. distance (generic algorithm) = "
- << comparable_distance_generic
- << std::endl;
- std::cout << std::endl << std::endl;
- #endif
- }
- };
- //========================================================================
- template <typename Geometry1, typename Geometry2, typename Strategy>
- void test_empty_input(Geometry1 const& geometry1,
- Geometry2 const& geometry2,
- Strategy const& strategy)
- {
- try
- {
- bg::distance(geometry1, geometry2, strategy);
- }
- catch(bg::empty_input_exception const& )
- {
- return;
- }
- BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );
- try
- {
- bg::distance(geometry2, geometry1, strategy);
- }
- catch(bg::empty_input_exception const& )
- {
- return;
- }
- BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );
- }
- #endif // BOOST_GEOMETRY_TEST_DISTANCE_COMMON_HPP
|