1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7 // Licensed under the Boost Software License version 1.0.
8 // http://www.boost.org/users/license.html
10 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP
11 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP
15 #include <boost/range.hpp>
17 #include <boost/geometry/core/point_type.hpp>
18 #include <boost/geometry/core/tag.hpp>
19 #include <boost/geometry/core/tags.hpp>
21 #include <boost/geometry/strategies/distance.hpp>
22 #include <boost/geometry/strategies/tags.hpp>
24 #include <boost/geometry/algorithms/assign.hpp>
25 #include <boost/geometry/algorithms/intersects.hpp>
26 #include <boost/geometry/algorithms/num_points.hpp>
28 #include <boost/geometry/iterators/point_iterator.hpp>
29 #include <boost/geometry/iterators/segment_iterator.hpp>
31 #include <boost/geometry/algorithms/dispatch/distance.hpp>
33 #include <boost/geometry/algorithms/detail/closest_feature/geometry_to_range.hpp>
34 #include <boost/geometry/algorithms/detail/closest_feature/point_to_range.hpp>
36 #include <boost/geometry/algorithms/detail/distance/is_comparable.hpp>
38 #include <boost/geometry/util/condition.hpp>
41 namespace boost { namespace geometry
44 #ifndef DOXYGEN_NO_DETAIL
45 namespace detail { namespace distance
49 // closure of segment or box point range
52 typename SegmentOrBox,
53 typename Tag = typename tag<SegmentOrBox>::type
55 struct segment_or_box_point_range_closure
56 : not_implemented<SegmentOrBox>
59 template <typename Segment>
60 struct segment_or_box_point_range_closure<Segment, segment_tag>
62 static const closure_selector value = closed;
65 template <typename Box>
66 struct segment_or_box_point_range_closure<Box, box_tag>
68 static const closure_selector value = open;
76 typename SegmentOrBox,
78 typename Tag = typename tag<Geometry>::type
80 class geometry_to_segment_or_box
83 typedef typename point_type<SegmentOrBox>::type segment_or_box_point;
85 typedef typename strategy::distance::services::comparable_type
88 >::type comparable_strategy;
90 typedef detail::closest_feature::point_to_point_range
92 typename point_type<Geometry>::type,
93 std::vector<segment_or_box_point>,
94 segment_or_box_point_range_closure<SegmentOrBox>::value,
96 > point_to_point_range;
98 typedef detail::closest_feature::geometry_to_range geometry_to_range;
100 typedef typename strategy::distance::services::return_type
103 typename point_type<Geometry>::type,
105 >::type comparable_return_type;
108 // assign the new minimum value for an iterator of the point range
109 // of a segment or a box
113 typename SegOrBoxTag = typename tag<SegOrBox>::type
115 struct assign_new_min_iterator
116 : not_implemented<SegOrBox>
119 template <typename Segment>
120 struct assign_new_min_iterator<Segment, segment_tag>
122 template <typename Iterator>
123 static inline void apply(Iterator&, Iterator)
128 template <typename Box>
129 struct assign_new_min_iterator<Box, box_tag>
131 template <typename Iterator>
132 static inline void apply(Iterator& it_min, Iterator it)
139 // assign the points of a segment or a box to a range
144 typename SegOrBoxTag = typename tag<SegOrBox>::type
146 struct assign_segment_or_box_points
149 template <typename Segment, typename PointRange>
150 struct assign_segment_or_box_points<Segment, PointRange, segment_tag>
152 static inline void apply(Segment const& segment, PointRange& range)
154 detail::assign_point_from_index<0>(segment, range[0]);
155 detail::assign_point_from_index<1>(segment, range[1]);
159 template <typename Box, typename PointRange>
160 struct assign_segment_or_box_points<Box, PointRange, box_tag>
162 static inline void apply(Box const& box, PointRange& range)
164 detail::assign_box_corners_oriented<true>(box, range);
170 typedef typename strategy::distance::services::return_type
173 typename point_type<Geometry>::type,
177 static inline return_type apply(Geometry const& geometry,
178 SegmentOrBox const& segment_or_box,
179 Strategy const& strategy,
180 bool check_intersection = true)
182 typedef geometry::point_iterator<Geometry const> point_iterator_type;
183 typedef geometry::segment_iterator
186 > segment_iterator_type;
188 typedef typename boost::range_const_iterator
190 std::vector<segment_or_box_point>
191 >::type seg_or_box_const_iterator;
193 typedef assign_new_min_iterator<SegmentOrBox> assign_new_value;
196 if (check_intersection
197 && geometry::intersects(geometry, segment_or_box))
202 comparable_strategy cstrategy =
203 strategy::distance::services::get_comparable
208 // get all points of the segment or the box
209 std::vector<segment_or_box_point>
210 seg_or_box_points(geometry::num_points(segment_or_box));
212 assign_segment_or_box_points
215 std::vector<segment_or_box_point>
216 >::apply(segment_or_box, seg_or_box_points);
218 // consider all distances of the points in the geometry to the
220 comparable_return_type cd_min1(0);
221 point_iterator_type pit_min;
222 seg_or_box_const_iterator it_min1 = boost::const_begin(seg_or_box_points);
223 seg_or_box_const_iterator it_min2 = it_min1;
227 for (point_iterator_type pit = points_begin(geometry);
228 pit != points_end(geometry); ++pit, first = false)
230 comparable_return_type cd;
233 seg_or_box_const_iterator, seg_or_box_const_iterator
235 = point_to_point_range::apply(*pit,
236 boost::const_begin(seg_or_box_points),
237 boost::const_end(seg_or_box_points),
241 if (first || cd < cd_min1)
245 assign_new_value::apply(it_min1, it_pair.first);
246 assign_new_value::apply(it_min2, it_pair.second);
250 // consider all distances of the points in the segment or box to the
251 // segments of the geometry
252 comparable_return_type cd_min2(0);
253 segment_iterator_type sit_min;
254 seg_or_box_const_iterator it_min;
257 for (seg_or_box_const_iterator it = boost::const_begin(seg_or_box_points);
258 it != boost::const_end(seg_or_box_points); ++it, first = false)
260 comparable_return_type cd;
261 segment_iterator_type sit
262 = geometry_to_range::apply(*it,
263 segments_begin(geometry),
264 segments_end(geometry),
268 if (first || cd < cd_min2)
276 if (BOOST_GEOMETRY_CONDITION(is_comparable<Strategy>::value))
278 return (std::min)(cd_min1, cd_min2);
281 if (cd_min1 < cd_min2)
283 return strategy.apply(*pit_min, *it_min1, *it_min2);
287 return dispatch::distance
289 segment_or_box_point,
290 typename std::iterator_traits
292 segment_iterator_type
295 >::apply(*it_min, *sit_min, strategy);
300 static inline return_type
301 apply(SegmentOrBox const& segment_or_box, Geometry const& geometry,
302 Strategy const& strategy, bool check_intersection = true)
304 return apply(geometry, segment_or_box, strategy, check_intersection);
310 template <typename MultiPoint, typename SegmentOrBox, typename Strategy>
311 class geometry_to_segment_or_box
313 MultiPoint, SegmentOrBox, Strategy, multi_point_tag
317 typedef detail::closest_feature::geometry_to_range base_type;
319 typedef typename boost::range_iterator
322 >::type iterator_type;
324 typedef detail::closest_feature::geometry_to_range geometry_to_range;
327 typedef typename strategy::distance::services::return_type
330 typename point_type<SegmentOrBox>::type,
331 typename point_type<MultiPoint>::type
334 static inline return_type apply(MultiPoint const& multipoint,
335 SegmentOrBox const& segment_or_box,
336 Strategy const& strategy)
338 namespace sds = strategy::distance::services;
340 typename sds::return_type
342 typename sds::comparable_type<Strategy>::type,
343 typename point_type<SegmentOrBox>::type,
344 typename point_type<MultiPoint>::type
348 = geometry_to_range::apply(segment_or_box,
349 boost::begin(multipoint),
350 boost::end(multipoint),
358 is_comparable<Strategy>::value
364 typename point_type<MultiPoint>::type,
367 >::apply(*it_min, segment_or_box, strategy);
373 }} // namespace detail::distance
374 #endif // DOXYGEN_NO_DETAIL
378 #ifndef DOXYGEN_NO_DISPATCH
383 template <typename Linear, typename Segment, typename Strategy>
386 Linear, Segment, Strategy, linear_tag, segment_tag,
387 strategy_tag_distance_point_segment, false
388 > : detail::distance::geometry_to_segment_or_box<Linear, Segment, Strategy>
392 template <typename Areal, typename Segment, typename Strategy>
395 Areal, Segment, Strategy, areal_tag, segment_tag,
396 strategy_tag_distance_point_segment, false
397 > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
401 template <typename Segment, typename Areal, typename Strategy>
404 Segment, Areal, Strategy, segment_tag, areal_tag,
405 strategy_tag_distance_point_segment, false
406 > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
410 template <typename Linear, typename Box, typename Strategy>
413 Linear, Box, Strategy, linear_tag, box_tag,
414 strategy_tag_distance_point_segment, false
415 > : detail::distance::geometry_to_segment_or_box
417 Linear, Box, Strategy
422 template <typename Areal, typename Box, typename Strategy>
425 Areal, Box, Strategy, areal_tag, box_tag,
426 strategy_tag_distance_point_segment, false
427 > : detail::distance::geometry_to_segment_or_box<Areal, Box, Strategy>
431 template <typename MultiPoint, typename Segment, typename Strategy>
434 MultiPoint, Segment, Strategy,
435 multi_point_tag, segment_tag,
436 strategy_tag_distance_point_segment, false
437 > : detail::distance::geometry_to_segment_or_box
439 MultiPoint, Segment, Strategy
444 template <typename MultiPoint, typename Box, typename Strategy>
447 MultiPoint, Box, Strategy,
448 multi_point_tag, box_tag,
449 strategy_tag_distance_point_box, false
450 > : detail::distance::geometry_to_segment_or_box
452 MultiPoint, Box, Strategy
457 } // namespace dispatch
458 #endif // DOXYGEN_NO_DISPATCH
461 }} // namespace boost::geometry
464 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP