3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
6 // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2013-2019.
9 // Modifications copyright (c) 2013-2019, Oracle and/or its affiliates.
11 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
12 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
13 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
15 // Use, modification and distribution is subject to the Boost Software License,
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISJOINT_SEGMENT_BOX_HPP
20 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISJOINT_SEGMENT_BOX_HPP
26 #include <boost/numeric/conversion/cast.hpp>
28 #include <boost/geometry/util/math.hpp>
29 #include <boost/geometry/util/calculation_type.hpp>
31 #include <boost/geometry/core/access.hpp>
32 #include <boost/geometry/core/tags.hpp>
33 #include <boost/geometry/core/coordinate_dimension.hpp>
34 #include <boost/geometry/core/point_type.hpp>
36 #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
38 #include <boost/geometry/strategies/cartesian/point_in_box.hpp>
39 #include <boost/geometry/strategies/disjoint.hpp>
42 namespace boost { namespace geometry { namespace strategy { namespace disjoint
48 template <std::size_t I>
49 struct compute_tmin_tmax_per_dim
51 template <typename SegmentPoint, typename Box, typename RelativeDistance>
52 static inline void apply(SegmentPoint const& p0,
53 SegmentPoint const& p1,
55 RelativeDistance& ti_min,
56 RelativeDistance& ti_max,
57 RelativeDistance& diff)
59 typedef typename coordinate_type<Box>::type box_coordinate_type;
60 typedef typename coordinate_type
63 >::type point_coordinate_type;
65 RelativeDistance c_p0 = boost::numeric_cast
68 >( geometry::get<I>(p0) );
70 RelativeDistance c_p1 = boost::numeric_cast
73 >( geometry::get<I>(p1) );
75 RelativeDistance c_b_min = boost::numeric_cast
78 >( geometry::get<geometry::min_corner, I>(box) );
80 RelativeDistance c_b_max = boost::numeric_cast
83 >( geometry::get<geometry::max_corner, I>(box) );
85 if ( geometry::get<I>(p1) >= geometry::get<I>(p0) )
88 ti_min = c_b_min - c_p0;
89 ti_max = c_b_max - c_p0;
94 ti_min = c_p0 - c_b_max;
95 ti_max = c_p0 - c_b_min;
103 typename RelativeDistance,
104 typename SegmentPoint,
107 std::size_t Dimension
109 struct disjoint_segment_box_impl
111 template <typename RelativeDistancePair>
112 static inline bool apply(SegmentPoint const& p0,
113 SegmentPoint const& p1,
115 RelativeDistancePair& t_min,
116 RelativeDistancePair& t_max)
118 RelativeDistance ti_min, ti_max, diff;
120 compute_tmin_tmax_per_dim<I>::apply(p0, p1, box, ti_min, ti_max, diff);
122 if ( geometry::math::equals(diff, 0) )
124 if ( (geometry::math::equals(t_min.second, 0)
125 && t_min.first > ti_max)
127 (geometry::math::equals(t_max.second, 0)
128 && t_max.first < ti_min)
130 (math::sign(ti_min) * math::sign(ti_max) > 0) )
136 RelativeDistance t_min_x_diff = t_min.first * diff;
137 RelativeDistance t_max_x_diff = t_max.first * diff;
139 if ( t_min_x_diff > ti_max * t_min.second
140 || t_max_x_diff < ti_min * t_max.second )
145 if ( ti_min * t_min.second > t_min_x_diff )
147 t_min.first = ti_min;
150 if ( ti_max * t_max.second < t_max_x_diff )
152 t_max.first = ti_max;
156 if ( t_min.first > t_min.second || t_max.first < 0 )
161 return disjoint_segment_box_impl
168 >::apply(p0, p1, box, t_min, t_max);
175 typename RelativeDistance,
176 typename SegmentPoint,
178 std::size_t Dimension
180 struct disjoint_segment_box_impl
182 RelativeDistance, SegmentPoint, Box, 0, Dimension
185 static inline bool apply(SegmentPoint const& p0,
186 SegmentPoint const& p1,
189 std::pair<RelativeDistance, RelativeDistance> t_min, t_max;
190 RelativeDistance diff;
192 compute_tmin_tmax_per_dim<0>::apply(p0, p1, box,
193 t_min.first, t_max.first, diff);
195 if ( geometry::math::equals(diff, 0) )
197 if ( geometry::math::equals(t_min.first, 0) ) { t_min.first = -1; }
198 if ( geometry::math::equals(t_max.first, 0) ) { t_max.first = 1; }
200 if (math::sign(t_min.first) * math::sign(t_max.first) > 0)
206 if ( t_min.first > diff || t_max.first < 0 )
211 t_min.second = t_max.second = diff;
213 return disjoint_segment_box_impl
215 RelativeDistance, SegmentPoint, Box, 1, Dimension
216 >::apply(p0, p1, box, t_min, t_max);
223 typename RelativeDistance,
224 typename SegmentPoint,
226 std::size_t Dimension
228 struct disjoint_segment_box_impl
230 RelativeDistance, SegmentPoint, Box, Dimension, Dimension
233 template <typename RelativeDistancePair>
234 static inline bool apply(SegmentPoint const&, SegmentPoint const&,
236 RelativeDistancePair&, RelativeDistancePair&)
242 } // namespace detail
244 // NOTE: This may be temporary place for this or corresponding strategy
245 // It seems to be more appropriate to implement the opposite of it
246 // e.g. intersection::segment_box because in disjoint() algorithm
247 // other strategies that are used are intersection and covered_by strategies.
250 typedef covered_by::cartesian_point_box disjoint_point_box_strategy_type;
252 static inline disjoint_point_box_strategy_type get_disjoint_point_box_strategy()
254 return disjoint_point_box_strategy_type();
257 template <typename Segment, typename Box>
258 static inline bool apply(Segment const& segment, Box const& box)
260 assert_dimension_equal<Segment, Box>();
262 typedef typename util::calculation_type::geometric::binary
265 >::type relative_distance_type;
267 typedef typename point_type<Segment>::type segment_point_type;
268 segment_point_type p0, p1;
269 geometry::detail::assign_point_from_index<0>(segment, p0);
270 geometry::detail::assign_point_from_index<1>(segment, p1);
272 return detail::disjoint_segment_box_impl
274 relative_distance_type, segment_point_type, Box,
275 0, dimension<Box>::value
276 >::apply(p0, p1, box);
281 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
287 template <typename Linear, typename Box, typename LinearTag>
288 struct default_strategy<Linear, Box, LinearTag, box_tag, 1, 2, cartesian_tag, cartesian_tag>
290 typedef disjoint::segment_box type;
293 template <typename Box, typename Linear, typename LinearTag>
294 struct default_strategy<Box, Linear, box_tag, LinearTag, 2, 1, cartesian_tag, cartesian_tag>
296 typedef disjoint::segment_box type;
300 } // namespace services
303 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
306 }}}} // namespace boost::geometry::strategy::disjoint
309 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISJOINT_SEGMENT_BOX_HPP