1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014-2020, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP
16 #include <boost/range/begin.hpp>
17 #include <boost/range/end.hpp>
19 #include <boost/geometry/core/point_type.hpp>
20 #include <boost/geometry/core/tag.hpp>
21 #include <boost/geometry/core/tags.hpp>
23 #include <boost/geometry/strategies/distance.hpp>
24 #include <boost/geometry/strategies/tags.hpp>
26 #include <boost/geometry/algorithms/assign.hpp>
27 #include <boost/geometry/algorithms/intersects.hpp>
28 #include <boost/geometry/algorithms/num_points.hpp>
30 #include <boost/geometry/iterators/point_iterator.hpp>
31 #include <boost/geometry/iterators/segment_iterator.hpp>
33 #include <boost/geometry/algorithms/dispatch/distance.hpp>
35 #include <boost/geometry/algorithms/detail/closest_feature/geometry_to_range.hpp>
36 #include <boost/geometry/algorithms/detail/closest_feature/point_to_range.hpp>
38 #include <boost/geometry/algorithms/detail/distance/is_comparable.hpp>
40 #include <boost/geometry/util/condition.hpp>
43 namespace boost { namespace geometry
46 #ifndef DOXYGEN_NO_DETAIL
47 namespace detail { namespace distance
51 // closure of segment or box point range
54 typename SegmentOrBox,
55 typename Tag = typename tag<SegmentOrBox>::type
57 struct segment_or_box_point_range_closure
58 : not_implemented<SegmentOrBox>
61 template <typename Segment>
62 struct segment_or_box_point_range_closure<Segment, segment_tag>
64 static const closure_selector value = closed;
67 template <typename Box>
68 struct segment_or_box_point_range_closure<Box, box_tag>
70 static const closure_selector value = open;
78 typename SegmentOrBox,
80 typename Tag = typename tag<Geometry>::type
82 class geometry_to_segment_or_box
85 typedef typename point_type<SegmentOrBox>::type segment_or_box_point;
87 typedef typename strategy::distance::services::comparable_type
90 >::type comparable_strategy;
92 typedef detail::closest_feature::point_to_point_range
94 typename point_type<Geometry>::type,
95 std::vector<segment_or_box_point>,
96 segment_or_box_point_range_closure<SegmentOrBox>::value,
98 > point_to_point_range;
100 typedef detail::closest_feature::geometry_to_range geometry_to_range;
102 typedef typename strategy::distance::services::return_type
105 typename point_type<Geometry>::type,
107 >::type comparable_return_type;
110 // assign the new minimum value for an iterator of the point range
111 // of a segment or a box
115 typename SegOrBoxTag = typename tag<SegOrBox>::type
117 struct assign_new_min_iterator
118 : not_implemented<SegOrBox>
121 template <typename Segment>
122 struct assign_new_min_iterator<Segment, segment_tag>
124 template <typename Iterator>
125 static inline void apply(Iterator&, Iterator)
130 template <typename Box>
131 struct assign_new_min_iterator<Box, box_tag>
133 template <typename Iterator>
134 static inline void apply(Iterator& it_min, Iterator it)
141 // assign the points of a segment or a box to a range
146 typename SegOrBoxTag = typename tag<SegOrBox>::type
148 struct assign_segment_or_box_points
151 template <typename Segment, typename PointRange>
152 struct assign_segment_or_box_points<Segment, PointRange, segment_tag>
154 static inline void apply(Segment const& segment, PointRange& range)
156 detail::assign_point_from_index<0>(segment, range[0]);
157 detail::assign_point_from_index<1>(segment, range[1]);
161 template <typename Box, typename PointRange>
162 struct assign_segment_or_box_points<Box, PointRange, box_tag>
164 static inline void apply(Box const& box, PointRange& range)
166 detail::assign_box_corners_oriented<true>(box, range);
173 typename SegOrBoxTag = typename tag<SegOrBox>::type
177 static inline bool apply(Geometry const& g1, SegOrBox const& g2, Strategy const&)
179 return geometry::intersects(g1, g2);
183 template <typename SegOrBox>
184 struct intersects<SegOrBox, segment_tag>
186 static inline bool apply(Geometry const& g1, SegOrBox const& g2, Strategy const& s)
188 return geometry::intersects(g1, g2, s.get_relate_segment_segment_strategy());
193 typedef typename strategy::distance::services::return_type
196 typename point_type<Geometry>::type,
200 static inline return_type apply(Geometry const& geometry,
201 SegmentOrBox const& segment_or_box,
202 Strategy const& strategy,
203 bool check_intersection = true)
205 typedef geometry::point_iterator<Geometry const> point_iterator_type;
206 typedef geometry::segment_iterator
209 > segment_iterator_type;
211 typedef typename boost::range_const_iterator
213 std::vector<segment_or_box_point>
214 >::type seg_or_box_const_iterator;
216 typedef assign_new_min_iterator<SegmentOrBox> assign_new_value;
219 if (check_intersection
220 && intersects<SegmentOrBox>::apply(geometry, segment_or_box, strategy))
225 comparable_strategy cstrategy =
226 strategy::distance::services::get_comparable
231 // get all points of the segment or the box
232 std::vector<segment_or_box_point>
233 seg_or_box_points(geometry::num_points(segment_or_box));
235 assign_segment_or_box_points
238 std::vector<segment_or_box_point>
239 >::apply(segment_or_box, seg_or_box_points);
241 // consider all distances of the points in the geometry to the
243 comparable_return_type cd_min1(0);
244 point_iterator_type pit_min;
245 seg_or_box_const_iterator it_min1 = boost::const_begin(seg_or_box_points);
246 seg_or_box_const_iterator it_min2 = it_min1;
250 for (point_iterator_type pit = points_begin(geometry);
251 pit != points_end(geometry); ++pit, first = false)
253 comparable_return_type cd;
256 seg_or_box_const_iterator, seg_or_box_const_iterator
258 = point_to_point_range::apply(*pit,
259 boost::const_begin(seg_or_box_points),
260 boost::const_end(seg_or_box_points),
264 if (first || cd < cd_min1)
268 assign_new_value::apply(it_min1, it_pair.first);
269 assign_new_value::apply(it_min2, it_pair.second);
273 // consider all distances of the points in the segment or box to the
274 // segments of the geometry
275 comparable_return_type cd_min2(0);
276 segment_iterator_type sit_min;
277 seg_or_box_const_iterator it_min;
280 for (seg_or_box_const_iterator it = boost::const_begin(seg_or_box_points);
281 it != boost::const_end(seg_or_box_points); ++it, first = false)
283 comparable_return_type cd;
284 segment_iterator_type sit
285 = geometry_to_range::apply(*it,
286 segments_begin(geometry),
287 segments_end(geometry),
291 if (first || cd < cd_min2)
299 if (BOOST_GEOMETRY_CONDITION(is_comparable<Strategy>::value))
301 return (std::min)(cd_min1, cd_min2);
304 if (cd_min1 < cd_min2)
306 return strategy.apply(*pit_min, *it_min1, *it_min2);
310 return dispatch::distance
312 segment_or_box_point,
313 typename std::iterator_traits
315 segment_iterator_type
318 >::apply(*it_min, *sit_min, strategy);
323 static inline return_type
324 apply(SegmentOrBox const& segment_or_box, Geometry const& geometry,
325 Strategy const& strategy, bool check_intersection = true)
327 return apply(geometry, segment_or_box, strategy, check_intersection);
333 template <typename MultiPoint, typename SegmentOrBox, typename Strategy>
334 class geometry_to_segment_or_box
336 MultiPoint, SegmentOrBox, Strategy, multi_point_tag
340 typedef detail::closest_feature::geometry_to_range base_type;
342 typedef typename boost::range_iterator
345 >::type iterator_type;
347 typedef detail::closest_feature::geometry_to_range geometry_to_range;
350 typedef typename strategy::distance::services::return_type
353 typename point_type<SegmentOrBox>::type,
354 typename point_type<MultiPoint>::type
357 static inline return_type apply(MultiPoint const& multipoint,
358 SegmentOrBox const& segment_or_box,
359 Strategy const& strategy)
361 namespace sds = strategy::distance::services;
363 typename sds::return_type
365 typename sds::comparable_type<Strategy>::type,
366 typename point_type<SegmentOrBox>::type,
367 typename point_type<MultiPoint>::type
371 = geometry_to_range::apply(segment_or_box,
372 boost::begin(multipoint),
373 boost::end(multipoint),
381 is_comparable<Strategy>::value
387 typename point_type<MultiPoint>::type,
390 >::apply(*it_min, segment_or_box, strategy);
396 }} // namespace detail::distance
397 #endif // DOXYGEN_NO_DETAIL
401 #ifndef DOXYGEN_NO_DISPATCH
406 template <typename Linear, typename Segment, typename Strategy>
409 Linear, Segment, Strategy, linear_tag, segment_tag,
410 strategy_tag_distance_point_segment, false
411 > : detail::distance::geometry_to_segment_or_box<Linear, Segment, Strategy>
415 template <typename Areal, typename Segment, typename Strategy>
418 Areal, Segment, Strategy, areal_tag, segment_tag,
419 strategy_tag_distance_point_segment, false
420 > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
424 template <typename Segment, typename Areal, typename Strategy>
427 Segment, Areal, Strategy, segment_tag, areal_tag,
428 strategy_tag_distance_point_segment, false
429 > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
433 template <typename Linear, typename Box, typename Strategy>
436 Linear, Box, Strategy, linear_tag, box_tag,
437 strategy_tag_distance_point_segment, false
438 > : detail::distance::geometry_to_segment_or_box
440 Linear, Box, Strategy
445 template <typename Areal, typename Box, typename Strategy>
448 Areal, Box, Strategy, areal_tag, box_tag,
449 strategy_tag_distance_point_segment, false
450 > : detail::distance::geometry_to_segment_or_box<Areal, Box, Strategy>
454 template <typename MultiPoint, typename Segment, typename Strategy>
457 MultiPoint, Segment, Strategy,
458 multi_point_tag, segment_tag,
459 strategy_tag_distance_point_segment, false
460 > : detail::distance::geometry_to_segment_or_box
462 MultiPoint, Segment, Strategy
467 template <typename MultiPoint, typename Box, typename Strategy>
470 MultiPoint, Box, Strategy,
471 multi_point_tag, box_tag,
472 strategy_tag_distance_point_box, false
473 > : detail::distance::geometry_to_segment_or_box
475 MultiPoint, Box, Strategy
480 } // namespace dispatch
481 #endif // DOXYGEN_NO_DISPATCH
484 }} // namespace boost::geometry
487 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP