1 // Boost.Geometry Index
3 // n-dimensional box-segment intersection
5 // Copyright (c) 2011-2014 Adam Wulkiewicz, Lodz, Poland.
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)
11 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_SEGMENT_INTERSECTION_HPP
12 #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_SEGMENT_INTERSECTION_HPP
14 namespace boost { namespace geometry { namespace index { namespace detail {
16 //template <typename Indexable, typename Point>
17 //struct default_relative_distance_type
19 // typedef typename select_most_precise<
20 // typename select_most_precise<
21 // typename coordinate_type<Indexable>::type,
22 // typename coordinate_type<Point>::type
24 // float // TODO - use bigger type, calculated from the size of coordinate types
28 // BOOST_MPL_ASSERT_MSG((!::boost::is_unsigned<type>::value),
29 // THIS_TYPE_SHOULDNT_BE_UNSIGNED, (type));
34 template <typename Box, typename Point, size_t I>
35 struct box_segment_intersection_dim
37 BOOST_STATIC_ASSERT(0 <= dimension<Box>::value);
38 BOOST_STATIC_ASSERT(0 <= dimension<Point>::value);
39 BOOST_STATIC_ASSERT(I < size_t(dimension<Box>::value));
40 BOOST_STATIC_ASSERT(I < size_t(dimension<Point>::value));
41 BOOST_STATIC_ASSERT(dimension<Point>::value == dimension<Box>::value);
43 // WARNING! - RelativeDistance must be IEEE float for this to work
45 template <typename RelativeDistance>
46 static inline bool apply(Box const& b, Point const& p0, Point const& p1,
47 RelativeDistance & t_near, RelativeDistance & t_far)
49 RelativeDistance ray_d = geometry::get<I>(p1) - geometry::get<I>(p0);
50 RelativeDistance tn = ( geometry::get<min_corner, I>(b) - geometry::get<I>(p0) ) / ray_d;
51 RelativeDistance tf = ( geometry::get<max_corner, I>(b) - geometry::get<I>(p0) ) / ray_d;
60 return 0 <= t_far && t_near <= t_far;
64 template <typename Box, typename Point, size_t CurrentDimension>
65 struct box_segment_intersection
67 BOOST_STATIC_ASSERT(0 < CurrentDimension);
69 typedef box_segment_intersection_dim<Box, Point, CurrentDimension - 1> for_dim;
71 template <typename RelativeDistance>
72 static inline bool apply(Box const& b, Point const& p0, Point const& p1,
73 RelativeDistance & t_near, RelativeDistance & t_far)
75 return box_segment_intersection<Box, Point, CurrentDimension - 1>::apply(b, p0, p1, t_near, t_far)
76 && for_dim::apply(b, p0, p1, t_near, t_far);
80 template <typename Box, typename Point>
81 struct box_segment_intersection<Box, Point, 1>
83 typedef box_segment_intersection_dim<Box, Point, 0> for_dim;
85 template <typename RelativeDistance>
86 static inline bool apply(Box const& b, Point const& p0, Point const& p1,
87 RelativeDistance & t_near, RelativeDistance & t_far)
89 return for_dim::apply(b, p0, p1, t_near, t_far);
93 template <typename Indexable, typename Point, typename Tag>
94 struct segment_intersection
96 BOOST_MPL_ASSERT_MSG((false), NOT_IMPLEMENTED_FOR_THIS_GEOMETRY, (segment_intersection));
99 template <typename Indexable, typename Point>
100 struct segment_intersection<Indexable, Point, point_tag>
102 BOOST_MPL_ASSERT_MSG((false), SEGMENT_POINT_INTERSECTION_UNAVAILABLE, (segment_intersection));
105 template <typename Indexable, typename Point>
106 struct segment_intersection<Indexable, Point, box_tag>
108 typedef dispatch::box_segment_intersection<Indexable, Point, dimension<Indexable>::value> impl;
110 template <typename RelativeDistance>
111 static inline bool apply(Indexable const& b, Point const& p0, Point const& p1, RelativeDistance & relative_distance)
114 // TODO: this ASSERT CHECK is wrong for user-defined CoordinateTypes!
116 static const bool check = !::boost::is_integral<RelativeDistance>::value;
117 BOOST_MPL_ASSERT_MSG(check, RELATIVE_DISTANCE_MUST_BE_FLOATING_POINT_TYPE, (RelativeDistance));
119 RelativeDistance t_near = -(::std::numeric_limits<RelativeDistance>::max)();
120 RelativeDistance t_far = (::std::numeric_limits<RelativeDistance>::max)();
122 return impl::apply(b, p0, p1, t_near, t_far) &&
124 ( relative_distance = 0 < t_near ? t_near : 0, true );
128 } // namespace dispatch
130 template <typename Indexable, typename Point, typename RelativeDistance> inline
131 bool segment_intersection(Indexable const& b,
134 RelativeDistance & relative_distance)
136 // TODO check Indexable and Point concepts
138 return dispatch::segment_intersection<
140 typename tag<Indexable>::type
141 >::apply(b, p0, p1, relative_distance);
144 }}}} // namespace boost::geometry::index::detail
146 #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_SEGMENT_INTERSECTION_HPP