discrete_frechet_distance.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Boost.Geometry
  2. // Copyright (c) 2018 Yaghyavardhan Singh Khangarot, Hyderabad, India.
  3. // Contributed and/or modified by Yaghyavardhan Singh Khangarot,
  4. // as part of Google Summer of Code 2018 program.
  5. // This file was modified by Oracle on 2018-2023.
  6. // Modifications copyright (c) 2018-2023 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_ALGORITHMS_DISCRETE_FRECHET_DISTANCE_HPP
  13. #define BOOST_GEOMETRY_ALGORITHMS_DISCRETE_FRECHET_DISTANCE_HPP
  14. #ifdef BOOST_GEOMETRY_DEBUG_FRECHET_DISTANCE
  15. #include <iostream>
  16. #endif
  17. #include <vector>
  18. #include <boost/range/size.hpp>
  19. #include <boost/geometry/algorithms/detail/dummy_geometries.hpp>
  20. #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
  21. #include <boost/geometry/algorithms/not_implemented.hpp>
  22. #include <boost/geometry/core/assert.hpp>
  23. #include <boost/geometry/core/tag.hpp>
  24. #include <boost/geometry/core/tags.hpp>
  25. #include <boost/geometry/core/point_type.hpp>
  26. #include <boost/geometry/strategies/detail.hpp>
  27. #include <boost/geometry/strategies/discrete_distance/cartesian.hpp>
  28. #include <boost/geometry/strategies/discrete_distance/geographic.hpp>
  29. #include <boost/geometry/strategies/discrete_distance/spherical.hpp>
  30. #include <boost/geometry/strategies/distance_result.hpp>
  31. #include <boost/geometry/util/range.hpp>
  32. namespace boost { namespace geometry
  33. {
  34. #ifndef DOXYGEN_NO_DETAIL
  35. namespace detail { namespace discrete_frechet_distance
  36. {
  37. // TODO: The implementation should calculate comparable distances
  38. template <typename SizeType1, typename SizeType2, typename ResultType>
  39. class coup_mat
  40. {
  41. public:
  42. coup_mat(SizeType1 w, SizeType2 h)
  43. : m_data(w * h,-1), m_width(w), m_height(h)
  44. {}
  45. ResultType & operator()(SizeType1 i, SizeType2 j)
  46. {
  47. BOOST_GEOMETRY_ASSERT(i < m_width && j < m_height);
  48. return m_data[j * m_width + i];
  49. }
  50. private:
  51. std::vector<ResultType> m_data;
  52. SizeType1 m_width;
  53. SizeType2 m_height;
  54. };
  55. struct linestring_linestring
  56. {
  57. template <typename Linestring1, typename Linestring2, typename Strategies>
  58. static inline auto apply(Linestring1 const& ls1, Linestring2 const& ls2,
  59. Strategies const& strategies)
  60. {
  61. typedef typename distance_result
  62. <
  63. typename point_type<Linestring1>::type,
  64. typename point_type<Linestring2>::type,
  65. Strategies
  66. >::type result_type;
  67. typedef typename boost::range_size<Linestring1>::type size_type1;
  68. typedef typename boost::range_size<Linestring2>::type size_type2;
  69. boost::geometry::detail::throw_on_empty_input(ls1);
  70. boost::geometry::detail::throw_on_empty_input(ls2);
  71. // We can assume the inputs are not empty
  72. auto const strategy = strategies.distance(dummy_point(), dummy_point());
  73. size_type1 const a = boost::size(ls1);
  74. size_type2 const b = boost::size(ls2);
  75. //Coupling Matrix CoupMat(a,b,-1);
  76. coup_mat<size_type1, size_type2, result_type> coup_matrix(a, b);
  77. result_type const not_feasible = -100;
  78. //findin the Coupling Measure
  79. for (size_type1 i = 0 ; i < a ; i++ )
  80. {
  81. for (size_type2 j = 0 ; j < b ; j++ )
  82. {
  83. result_type dis = strategy.apply(range::at(ls1,i), range::at(ls2,j));
  84. if(i==0 && j==0)
  85. coup_matrix(i,j) = dis;
  86. else if(i==0 && j>0)
  87. coup_matrix(i,j) =
  88. (std::max)(coup_matrix(i,j-1), dis);
  89. else if(i>0 && j==0)
  90. coup_matrix(i,j) =
  91. (std::max)(coup_matrix(i-1,j), dis);
  92. else if(i>0 && j>0)
  93. coup_matrix(i,j) =
  94. (std::max)((std::min)(coup_matrix(i,j-1),
  95. (std::min)(coup_matrix(i-1,j),
  96. coup_matrix(i-1,j-1))),
  97. dis);
  98. else
  99. coup_matrix(i,j) = not_feasible;
  100. }
  101. }
  102. #ifdef BOOST_GEOMETRY_DEBUG_FRECHET_DISTANCE
  103. //Print CoupLing Matrix
  104. for(size_type i = 0; i <a; i++)
  105. {
  106. for(size_type j = 0; j <b; j++)
  107. std::cout << coup_matrix(i,j) << " ";
  108. std::cout << std::endl;
  109. }
  110. #endif
  111. return coup_matrix(a-1,b-1);
  112. }
  113. };
  114. }} // namespace detail::frechet_distance
  115. #endif // DOXYGEN_NO_DETAIL
  116. #ifndef DOXYGEN_NO_DISPATCH
  117. namespace dispatch
  118. {
  119. template
  120. <
  121. typename Geometry1,
  122. typename Geometry2,
  123. typename Tag1 = typename tag<Geometry1>::type,
  124. typename Tag2 = typename tag<Geometry2>::type
  125. >
  126. struct discrete_frechet_distance : not_implemented<Tag1, Tag2>
  127. {};
  128. template <typename Linestring1, typename Linestring2>
  129. struct discrete_frechet_distance
  130. <
  131. Linestring1,
  132. Linestring2,
  133. linestring_tag,
  134. linestring_tag
  135. >
  136. : detail::discrete_frechet_distance::linestring_linestring
  137. {};
  138. } // namespace dispatch
  139. #endif // DOXYGEN_NO_DISPATCH
  140. namespace resolve_strategy {
  141. template
  142. <
  143. typename Strategies,
  144. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  145. >
  146. struct discrete_frechet_distance
  147. {
  148. template <typename Geometry1, typename Geometry2>
  149. static inline auto apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  150. Strategies const& strategies)
  151. {
  152. return dispatch::discrete_frechet_distance
  153. <
  154. Geometry1, Geometry2
  155. >::apply(geometry1, geometry2, strategies);
  156. }
  157. };
  158. template <typename Strategy>
  159. struct discrete_frechet_distance<Strategy, false>
  160. {
  161. template <typename Geometry1, typename Geometry2>
  162. static inline auto apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  163. Strategy const& strategy)
  164. {
  165. using strategies::discrete_distance::services::strategy_converter;
  166. return dispatch::discrete_frechet_distance
  167. <
  168. Geometry1, Geometry2
  169. >::apply(geometry1, geometry2,
  170. strategy_converter<Strategy>::get(strategy));
  171. }
  172. };
  173. template <>
  174. struct discrete_frechet_distance<default_strategy, false>
  175. {
  176. template <typename Geometry1, typename Geometry2>
  177. static inline auto apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  178. default_strategy const&)
  179. {
  180. typedef typename strategies::discrete_distance::services::default_strategy
  181. <
  182. Geometry1, Geometry2
  183. >::type strategies_type;
  184. return dispatch::discrete_frechet_distance
  185. <
  186. Geometry1, Geometry2
  187. >::apply(geometry1, geometry2, strategies_type());
  188. }
  189. };
  190. } // namespace resolve_strategy
  191. /*!
  192. \brief Calculate discrete Frechet distance between two geometries (currently
  193. works for LineString-LineString) using specified strategy.
  194. \ingroup discrete_frechet_distance
  195. \tparam Geometry1 \tparam_geometry
  196. \tparam Geometry2 \tparam_geometry
  197. \tparam Strategy A type fulfilling a DistanceStrategy concept
  198. \param geometry1 Input geometry
  199. \param geometry2 Input geometry
  200. \param strategy Distance strategy to be used to calculate Pt-Pt distance
  201. \qbk{distinguish,with strategy}
  202. \qbk{[include reference/algorithms/discrete_frechet_distance.qbk]}
  203. \qbk{
  204. [heading Available Strategies]
  205. \* [link geometry.reference.strategies.strategy_distance_pythagoras Pythagoras (cartesian)]
  206. \* [link geometry.reference.strategies.strategy_distance_haversine Haversine (spherical)]
  207. [/ \* more (currently extensions): Vincenty\, Andoyer (geographic) ]
  208. [heading Example]
  209. [discrete_frechet_distance_strategy]
  210. [discrete_frechet_distance_strategy_output]
  211. }
  212. */
  213. template <typename Geometry1, typename Geometry2, typename Strategy>
  214. inline auto discrete_frechet_distance(Geometry1 const& geometry1,
  215. Geometry2 const& geometry2,
  216. Strategy const& strategy)
  217. {
  218. return resolve_strategy::discrete_frechet_distance
  219. <
  220. Strategy
  221. >::apply(geometry1, geometry2, strategy);
  222. }
  223. // Algorithm overload using default Pt-Pt distance strategy
  224. /*!
  225. \brief Calculate discrete Frechet distance between two geometries (currently
  226. work for LineString-LineString).
  227. \ingroup discrete_frechet_distance
  228. \tparam Geometry1 \tparam_geometry
  229. \tparam Geometry2 \tparam_geometry
  230. \param geometry1 Input geometry
  231. \param geometry2 Input geometry
  232. \qbk{[include reference/algorithms/discrete_frechet_distance.qbk]}
  233. \qbk{
  234. [heading Example]
  235. [discrete_frechet_distance]
  236. [discrete_frechet_distance_output]
  237. }
  238. */
  239. template <typename Geometry1, typename Geometry2>
  240. inline auto discrete_frechet_distance(Geometry1 const& geometry1,
  241. Geometry2 const& geometry2)
  242. {
  243. return resolve_strategy::discrete_frechet_distance
  244. <
  245. default_strategy
  246. >::apply(geometry1, geometry2, default_strategy());
  247. }
  248. }} // namespace boost::geometry
  249. #endif // BOOST_GEOMETRY_ALGORITHMS_DISCRETE_FRECHET_DISTANCE_HPP