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>
39 namespace boost { namespace geometry
42 #ifndef DOXYGEN_NO_DETAIL
43 namespace detail { namespace distance
47 // closure of segment or box point range
50 typename SegmentOrBox,
51 typename Tag = typename tag<SegmentOrBox>::type
53 struct segment_or_box_point_range_closure
54 : not_implemented<SegmentOrBox>
57 template <typename Segment>
58 struct segment_or_box_point_range_closure<Segment, segment_tag>
60 static const closure_selector value = closed;
63 template <typename Box>
64 struct segment_or_box_point_range_closure<Box, box_tag>
66 static const closure_selector value = open;
74 typename SegmentOrBox,
76 typename Tag = typename tag<Geometry>::type
78 class geometry_to_segment_or_box
81 typedef typename point_type<SegmentOrBox>::type segment_or_box_point;
83 typedef typename strategy::distance::services::comparable_type
86 >::type comparable_strategy;
88 typedef detail::closest_feature::point_to_point_range
90 typename point_type<Geometry>::type,
91 std::vector<segment_or_box_point>,
92 segment_or_box_point_range_closure<SegmentOrBox>::value,
94 > point_to_point_range;
96 typedef detail::closest_feature::geometry_to_range geometry_to_range;
98 typedef typename strategy::distance::services::return_type
101 typename point_type<Geometry>::type,
103 >::type comparable_return_type;
106 // assign the new minimum value for an iterator of the point range
107 // of a segment or a box
111 typename SegOrBoxTag = typename tag<SegOrBox>::type
113 struct assign_new_min_iterator
114 : not_implemented<SegOrBox>
117 template <typename Segment>
118 struct assign_new_min_iterator<Segment, segment_tag>
120 template <typename Iterator>
121 static inline void apply(Iterator&, Iterator)
126 template <typename Box>
127 struct assign_new_min_iterator<Box, box_tag>
129 template <typename Iterator>
130 static inline void apply(Iterator& it_min, Iterator it)
137 // assign the points of a segment or a box to a range
142 typename SegOrBoxTag = typename tag<SegOrBox>::type
144 struct assign_segment_or_box_points
147 template <typename Segment, typename PointRange>
148 struct assign_segment_or_box_points<Segment, PointRange, segment_tag>
150 static inline void apply(Segment const& segment, PointRange& range)
152 detail::assign_point_from_index<0>(segment, range[0]);
153 detail::assign_point_from_index<1>(segment, range[1]);
157 template <typename Box, typename PointRange>
158 struct assign_segment_or_box_points<Box, PointRange, box_tag>
160 static inline void apply(Box const& box, PointRange& range)
162 detail::assign_box_corners_oriented<true>(box, range);
168 typedef typename strategy::distance::services::return_type
171 typename point_type<Geometry>::type,
175 static inline return_type apply(Geometry const& geometry,
176 SegmentOrBox const& segment_or_box,
177 Strategy const& strategy,
178 bool check_intersection = true)
180 typedef geometry::point_iterator<Geometry const> point_iterator_type;
181 typedef geometry::segment_iterator
184 > segment_iterator_type;
186 typedef typename std::vector
189 >::const_iterator seg_or_box_iterator_type;
191 typedef assign_new_min_iterator<SegmentOrBox> assign_new_value;
194 if (check_intersection
195 && geometry::intersects(geometry, segment_or_box))
200 comparable_strategy cstrategy =
201 strategy::distance::services::get_comparable
206 // get all points of the segment or the box
207 std::vector<segment_or_box_point>
208 seg_or_box_points(geometry::num_points(segment_or_box));
210 assign_segment_or_box_points
213 std::vector<segment_or_box_point>
214 >::apply(segment_or_box, seg_or_box_points);
216 // consider all distances of the points in the geometry to the
218 comparable_return_type cd_min1;
219 point_iterator_type pit_min;
220 seg_or_box_iterator_type it_min1 = seg_or_box_points.begin();
221 seg_or_box_iterator_type it_min2 = ++seg_or_box_points.begin();
224 for (point_iterator_type pit = points_begin(geometry);
225 pit != points_end(geometry); ++pit, first = false)
227 comparable_return_type cd;
230 seg_or_box_iterator_type, seg_or_box_iterator_type
232 = point_to_point_range::apply(*pit,
233 seg_or_box_points.begin(),
234 seg_or_box_points.end(),
238 if (first || cd < cd_min1)
242 assign_new_value::apply(it_min1, it_pair.first);
243 assign_new_value::apply(it_min2, it_pair.second);
247 // consider all distances of the points in the segment or box to the
248 // segments of the geometry
249 comparable_return_type cd_min2;
250 segment_iterator_type sit_min;
251 typename std::vector<segment_or_box_point>::const_iterator it_min;
254 for (typename std::vector<segment_or_box_point>::const_iterator it
255 = seg_or_box_points.begin();
256 it != seg_or_box_points.end(); ++it, first = false)
258 comparable_return_type cd;
259 segment_iterator_type sit
260 = geometry_to_range::apply(*it,
261 segments_begin(geometry),
262 segments_end(geometry),
266 if (first || cd < cd_min2)
274 if (is_comparable<Strategy>::value)
276 return (std::min)(cd_min1, cd_min2);
279 if (cd_min1 < cd_min2)
281 return strategy.apply(*pit_min, *it_min1, *it_min2);
285 return dispatch::distance
287 segment_or_box_point,
288 typename std::iterator_traits
290 segment_iterator_type
293 >::apply(*it_min, *sit_min, strategy);
298 static inline return_type
299 apply(SegmentOrBox const& segment_or_box, Geometry const& geometry,
300 Strategy const& strategy, bool check_intersection = true)
302 return apply(geometry, segment_or_box, strategy, check_intersection);
308 template <typename MultiPoint, typename SegmentOrBox, typename Strategy>
309 class geometry_to_segment_or_box
311 MultiPoint, SegmentOrBox, Strategy, multi_point_tag
315 typedef detail::closest_feature::geometry_to_range base_type;
317 typedef typename boost::range_iterator
320 >::type iterator_type;
322 typedef detail::closest_feature::geometry_to_range geometry_to_range;
325 typedef typename strategy::distance::services::return_type
328 typename point_type<SegmentOrBox>::type,
329 typename point_type<MultiPoint>::type
332 static inline return_type apply(MultiPoint const& multipoint,
333 SegmentOrBox const& segment_or_box,
334 Strategy const& strategy)
336 namespace sds = strategy::distance::services;
338 typename sds::return_type
340 typename sds::comparable_type<Strategy>::type,
341 typename point_type<SegmentOrBox>::type,
342 typename point_type<MultiPoint>::type
346 = geometry_to_range::apply(segment_or_box,
347 boost::begin(multipoint),
348 boost::end(multipoint),
356 is_comparable<Strategy>::value
362 typename point_type<MultiPoint>::type,
365 >::apply(*it_min, segment_or_box, strategy);
371 }} // namespace detail::distance
372 #endif // DOXYGEN_NO_DETAIL
376 #ifndef DOXYGEN_NO_DISPATCH
381 template <typename Linear, typename Segment, typename Strategy>
384 Linear, Segment, Strategy, linear_tag, segment_tag,
385 strategy_tag_distance_point_segment, false
386 > : detail::distance::geometry_to_segment_or_box<Linear, Segment, Strategy>
390 template <typename Areal, typename Segment, typename Strategy>
393 Areal, Segment, Strategy, areal_tag, segment_tag,
394 strategy_tag_distance_point_segment, false
395 > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
399 template <typename Segment, typename Areal, typename Strategy>
402 Segment, Areal, Strategy, segment_tag, areal_tag,
403 strategy_tag_distance_point_segment, false
404 > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
408 template <typename Linear, typename Box, typename Strategy>
411 Linear, Box, Strategy, linear_tag, box_tag,
412 strategy_tag_distance_point_segment, false
413 > : detail::distance::geometry_to_segment_or_box
415 Linear, Box, Strategy
420 template <typename Areal, typename Box, typename Strategy>
423 Areal, Box, Strategy, areal_tag, box_tag,
424 strategy_tag_distance_point_segment, false
425 > : detail::distance::geometry_to_segment_or_box<Areal, Box, Strategy>
429 template <typename MultiPoint, typename Segment, typename Strategy>
432 MultiPoint, Segment, Strategy,
433 multi_point_tag, segment_tag,
434 strategy_tag_distance_point_segment, false
435 > : detail::distance::geometry_to_segment_or_box
437 MultiPoint, Segment, Strategy
442 template <typename MultiPoint, typename Box, typename Strategy>
445 MultiPoint, Box, Strategy,
446 multi_point_tag, box_tag,
447 strategy_tag_distance_point_box, false
448 > : detail::distance::geometry_to_segment_or_box
450 MultiPoint, Box, Strategy
455 } // namespace dispatch
456 #endif // DOXYGEN_NO_DISPATCH
459 }} // namespace boost::geometry
462 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP