path_intersection.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Boost.Geometry Index
  2. //
  3. // n-dimensional box-linestring intersection
  4. //
  5. // Copyright (c) 2011-2017 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_PATH_INTERSECTION_HPP
  11. #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_PATH_INTERSECTION_HPP
  12. #include <boost/geometry/index/detail/algorithms/segment_intersection.hpp>
  13. #include <boost/geometry/strategies/default_length_result.hpp>
  14. namespace boost { namespace geometry { namespace index { namespace detail {
  15. namespace dispatch {
  16. template <typename Indexable, typename Geometry, typename IndexableTag, typename GeometryTag>
  17. struct path_intersection
  18. {
  19. BOOST_MPL_ASSERT_MSG((false), NOT_IMPLEMENTED_FOR_THIS_GEOMETRY_OR_INDEXABLE, (path_intersection));
  20. };
  21. // TODO: FP type must be used as a relative distance type!
  22. // and default_distance_result can be some user-defined int type
  23. // BUT! This code is experimental and probably won't be released at all
  24. // since more flexible user-defined-nearest predicate should be added instead
  25. template <typename Indexable, typename Segment>
  26. struct path_intersection<Indexable, Segment, box_tag, segment_tag>
  27. {
  28. typedef typename default_distance_result<typename point_type<Segment>::type>::type comparable_distance_type;
  29. static inline bool apply(Indexable const& b, Segment const& segment, comparable_distance_type & comparable_distance)
  30. {
  31. typedef typename point_type<Segment>::type point_type;
  32. point_type p1, p2;
  33. geometry::detail::assign_point_from_index<0>(segment, p1);
  34. geometry::detail::assign_point_from_index<1>(segment, p2);
  35. return index::detail::segment_intersection(b, p1, p2, comparable_distance);
  36. }
  37. };
  38. template <typename Indexable, typename Linestring>
  39. struct path_intersection<Indexable, Linestring, box_tag, linestring_tag>
  40. {
  41. typedef typename default_length_result<Linestring>::type comparable_distance_type;
  42. static inline bool apply(Indexable const& b, Linestring const& path, comparable_distance_type & comparable_distance)
  43. {
  44. typedef typename ::boost::range_value<Linestring>::type point_type;
  45. typedef typename ::boost::range_const_iterator<Linestring>::type const_iterator;
  46. typedef typename ::boost::range_size<Linestring>::type size_type;
  47. const size_type count = ::boost::size(path);
  48. if ( count == 2 )
  49. {
  50. return index::detail::segment_intersection(b, *::boost::begin(path), *(::boost::begin(path)+1), comparable_distance);
  51. }
  52. else if ( 2 < count )
  53. {
  54. const_iterator it0 = ::boost::begin(path);
  55. const_iterator it1 = ::boost::begin(path) + 1;
  56. const_iterator last = ::boost::end(path);
  57. comparable_distance = 0;
  58. for ( ; it1 != last ; ++it0, ++it1 )
  59. {
  60. typename default_distance_result<point_type, point_type>::type
  61. dist = geometry::distance(*it0, *it1);
  62. comparable_distance_type rel_dist;
  63. if ( index::detail::segment_intersection(b, *it0, *it1, rel_dist) )
  64. {
  65. comparable_distance += dist * rel_dist;
  66. return true;
  67. }
  68. else
  69. comparable_distance += dist;
  70. }
  71. }
  72. return false;
  73. }
  74. };
  75. } // namespace dispatch
  76. template <typename Indexable, typename SegmentOrLinestring>
  77. struct default_path_intersection_distance_type
  78. {
  79. typedef typename dispatch::path_intersection<
  80. Indexable, SegmentOrLinestring,
  81. typename tag<Indexable>::type,
  82. typename tag<SegmentOrLinestring>::type
  83. >::comparable_distance_type type;
  84. };
  85. template <typename Indexable, typename SegmentOrLinestring> inline
  86. bool path_intersection(Indexable const& b,
  87. SegmentOrLinestring const& path,
  88. typename default_path_intersection_distance_type<Indexable, SegmentOrLinestring>::type & comparable_distance)
  89. {
  90. // TODO check Indexable and Linestring concepts
  91. return dispatch::path_intersection<
  92. Indexable, SegmentOrLinestring,
  93. typename tag<Indexable>::type,
  94. typename tag<SegmentOrLinestring>::type
  95. >::apply(b, path, comparable_distance);
  96. }
  97. }}}} // namespace boost::geometry::index::detail
  98. #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_PATH_INTERSECTION_HPP