test_frechet_distance.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Boost.Geometry
  2. // Copyright (c) 2018 Yaghyavardhan Singh Khangarot, Hyderabad, India.
  3. // Contributed and/or modified by Yaghyavardhan Singh Khangarot, as part of Google Summer of Code 2018 program.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_TEST_FRECHET_DISTANCE_HPP
  8. #define BOOST_GEOMETRY_TEST_FRECHET_DISTANCE_HPP
  9. #include <geometry_test_common.hpp>
  10. #include <boost/geometry/algorithms/discrete_frechet_distance.hpp>
  11. #include <boost/geometry/io/wkt/wkt.hpp>
  12. #include <boost/geometry/strategies/strategies.hpp>
  13. #include <boost/variant/variant.hpp>
  14. template <typename Geometry1,typename Geometry2>
  15. void test_frechet_distance(Geometry1 const& geometry1,Geometry2 const& geometry2,
  16. typename bg::distance_result
  17. <
  18. typename bg::point_type<Geometry1>::type,
  19. typename bg::point_type<Geometry2>::type
  20. >::type expected_frechet_distance )
  21. {
  22. using namespace bg;
  23. typedef typename distance_result
  24. <
  25. typename point_type<Geometry1>::type,
  26. typename point_type<Geometry2>::type
  27. >::type result_type;
  28. result_type h_distance = bg::discrete_frechet_distance(geometry1,geometry2);
  29. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  30. std::ostringstream out;
  31. out << typeid(typename bg::coordinate_type<Geometry1>::type).name()
  32. << std::endl
  33. << typeid(typename bg::coordinate_type<Geometry2>::type).name()
  34. << std::endl
  35. << typeid(h_distance).name()
  36. << std::endl
  37. << "frechet_distance : " << bg::discrete_frechet_distance(geometry1,geometry2)
  38. << std::endl;
  39. std::cout << out.str();
  40. #endif
  41. BOOST_CHECK_CLOSE(h_distance, expected_frechet_distance, 0.001);
  42. }
  43. template <typename Geometry1,typename Geometry2>
  44. void test_geometry(std::string const& wkt1,std::string const& wkt2,
  45. typename bg::distance_result
  46. <
  47. typename bg::point_type<Geometry1>::type,
  48. typename bg::point_type<Geometry2>::type
  49. >::type expected_frechet_distance)
  50. {
  51. Geometry1 geometry1;
  52. bg::read_wkt(wkt1, geometry1);
  53. Geometry2 geometry2;
  54. bg::read_wkt(wkt2, geometry2);
  55. test_frechet_distance(geometry1,geometry2,expected_frechet_distance);
  56. #if defined(BOOST_GEOMETRY_TEST_DEBUG)
  57. test_frechet_distance(boost::variant<Geometry1>(geometry1),boost::variant<Geometry2>(geometry2), expected_frechet_distance);
  58. #endif
  59. }
  60. template <typename Geometry1,typename Geometry2 ,typename Strategy>
  61. void test_frechet_distance(Geometry1 const& geometry1,Geometry2 const& geometry2,Strategy strategy,
  62. typename bg::distance_result
  63. <
  64. typename bg::point_type<Geometry1>::type,
  65. typename bg::point_type<Geometry2>::type,
  66. Strategy
  67. >::type expected_frechet_distance )
  68. {
  69. using namespace bg;
  70. typedef typename distance_result
  71. <
  72. typename point_type<Geometry1>::type,
  73. typename point_type<Geometry2>::type,
  74. Strategy
  75. >::type result_type;
  76. result_type h_distance = bg::discrete_frechet_distance(geometry1,geometry2,strategy);
  77. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  78. std::ostringstream out;
  79. out << typeid(typename bg::coordinate_type<Geometry1>::type).name()
  80. << std::endl
  81. << typeid(typename bg::coordinate_type<Geometry2>::type).name()
  82. << std::endl
  83. << typeid(h_distance).name()
  84. << std::endl
  85. << "frechet_distance : " << bg::discrete_frechet_distance(geometry1,geometry2,strategy)
  86. << std::endl;
  87. std::cout << out.str();
  88. #endif
  89. BOOST_CHECK_CLOSE(h_distance, expected_frechet_distance, 0.001);
  90. }
  91. template <typename Geometry1,typename Geometry2,typename Strategy>
  92. void test_geometry(std::string const& wkt1,std::string const& wkt2,Strategy strategy,
  93. typename bg::distance_result
  94. <
  95. typename bg::point_type<Geometry1>::type,
  96. typename bg::point_type<Geometry2>::type,
  97. Strategy
  98. >::type expected_frechet_distance)
  99. {
  100. Geometry1 geometry1;
  101. bg::read_wkt(wkt1, geometry1);
  102. Geometry2 geometry2;
  103. bg::read_wkt(wkt2, geometry2);
  104. test_frechet_distance(geometry1,geometry2,strategy,expected_frechet_distance);
  105. #if defined(BOOST_GEOMETRY_TEST_DEBUG)
  106. test_frechet_distance(boost::variant<Geometry1>(geometry1),boost::variant<Geometry2>(geometry2),strategy, expected_frechet_distance);
  107. #endif
  108. }
  109. template <typename Geometry1,typename Geometry2>
  110. void test_empty_input(Geometry1 const& geometry1,Geometry2 const& geometry2)
  111. {
  112. try
  113. {
  114. bg::discrete_frechet_distance(geometry1,geometry2);
  115. }
  116. catch(bg::empty_input_exception const& )
  117. {
  118. return;
  119. }
  120. BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );
  121. }
  122. #endif