File indexing completed on 2025-10-31 08:39:54
0001 
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 
0015 
0016 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_PATH_INTERSECTION_HPP
0017 #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_PATH_INTERSECTION_HPP
0018 
0019 
0020 #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
0021 #include <boost/geometry/algorithms/detail/distance/interface.hpp>
0022 
0023 #include <boost/geometry/core/static_assert.hpp>
0024 
0025 #include <boost/geometry/index/detail/algorithms/segment_intersection.hpp>
0026 
0027 #include <boost/geometry/strategies/default_distance_result.hpp>
0028 #include <boost/geometry/strategies/default_length_result.hpp>
0029 
0030 #include <boost/range/begin.hpp>
0031 #include <boost/range/end.hpp>
0032 #include <boost/range/size.hpp>
0033 #include <boost/range/value_type.hpp>
0034 
0035 namespace boost { namespace geometry { namespace index { namespace detail {
0036 
0037 namespace dispatch {
0038 
0039 template <typename Indexable, typename Geometry, typename IndexableTag, typename GeometryTag>
0040 struct path_intersection
0041 {
0042     BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
0043         "Not implemented for this Geometry or Indexable.",
0044         Indexable, Geometry, IndexableTag, GeometryTag);
0045 };
0046 
0047 
0048 
0049 
0050 
0051 
0052 template <typename Indexable, typename Segment>
0053 struct path_intersection<Indexable, Segment, box_tag, segment_tag>
0054 {
0055     using comparable_distance_type = typename default_distance_result<point_type_t<Segment>>::type;
0056 
0057     static inline bool apply(Indexable const& b, Segment const& segment, comparable_distance_type & comparable_distance)
0058     {
0059         point_type_t<Segment> p1, p2;
0060         geometry::detail::assign_point_from_index<0>(segment, p1);
0061         geometry::detail::assign_point_from_index<1>(segment, p2);
0062         return index::detail::segment_intersection(b, p1, p2, comparable_distance);
0063     }
0064 };
0065 
0066 template <typename Indexable, typename Linestring>
0067 struct path_intersection<Indexable, Linestring, box_tag, linestring_tag>
0068 {
0069     using comparable_distance_type = typename default_length_result<Linestring>::type;
0070 
0071     static inline bool apply(Indexable const& b, Linestring const& path, comparable_distance_type & comparable_distance)
0072     {
0073         typedef typename ::boost::range_value<Linestring>::type point_type;
0074         typedef typename ::boost::range_const_iterator<Linestring>::type const_iterator;
0075         typedef typename ::boost::range_size<Linestring>::type size_type;
0076 
0077         const size_type count = ::boost::size(path);
0078 
0079         if ( count == 2 )
0080         {
0081             return index::detail::segment_intersection(b, *::boost::begin(path), *(::boost::begin(path)+1), comparable_distance);
0082         }
0083         else if ( 2 < count )
0084         {
0085             const_iterator it0 = ::boost::begin(path);
0086             const_iterator it1 = ::boost::begin(path) + 1;
0087             const_iterator last = ::boost::end(path);
0088 
0089             comparable_distance = 0;
0090 
0091             for ( ; it1 != last ; ++it0, ++it1 )
0092             {
0093                 typename default_distance_result<point_type, point_type>::type
0094                     dist = geometry::distance(*it0, *it1);
0095 
0096                 comparable_distance_type rel_dist;
0097                 if ( index::detail::segment_intersection(b, *it0, *it1, rel_dist) )
0098                 {
0099                     comparable_distance += dist * rel_dist;
0100                     return true;
0101                 }
0102                 else
0103                     comparable_distance += dist;
0104             }
0105         }
0106 
0107         return false;
0108     }
0109 };
0110 
0111 } 
0112 
0113 template <typename Indexable, typename SegmentOrLinestring>
0114 struct default_path_intersection_distance_type
0115 {
0116     using type = typename dispatch::path_intersection
0117         <
0118             Indexable, SegmentOrLinestring,
0119             tag_t<Indexable>,
0120             tag_t<SegmentOrLinestring>
0121         >::comparable_distance_type;
0122 };
0123 
0124 template <typename Indexable, typename SegmentOrLinestring> inline
0125 bool path_intersection(Indexable const& b,
0126                        SegmentOrLinestring const& path,
0127                        typename default_path_intersection_distance_type<Indexable, SegmentOrLinestring>::type & comparable_distance)
0128 {
0129     
0130 
0131     return dispatch::path_intersection
0132         <
0133             Indexable, SegmentOrLinestring,
0134             tag_t<Indexable>,
0135             tag_t<SegmentOrLinestring>
0136         >::apply(b, path, comparable_distance);
0137 }
0138 
0139 }}}} 
0140 
0141 #endif