path_intersection.hpp 5.2 KB

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