1 // Boost.Geometry (aka GGL, Generic Geometry Library)
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) 2014 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2014-2020.
9 // Modifications copyright (c) 2014-2020, Oracle and/or its affiliates.
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
14 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
15 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
17 // Use, modification and distribution is subject to the Boost Software License,
18 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
21 #ifndef BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
22 #define BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
27 #include <boost/range/begin.hpp>
28 #include <boost/range/end.hpp>
29 #include <boost/range/reference.hpp>
30 #include <boost/range/value_type.hpp>
32 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
33 #include <boost/geometry/algorithms/not_implemented.hpp>
34 #include <boost/geometry/core/closure.hpp>
35 #include <boost/geometry/core/exterior_ring.hpp>
36 #include <boost/geometry/core/interior_rings.hpp>
37 #include <boost/geometry/core/point_type.hpp>
38 #include <boost/geometry/core/tag_cast.hpp>
39 #include <boost/geometry/core/tags.hpp>
41 #include <boost/geometry/geometries/concepts/check.hpp>
43 #include <boost/geometry/geometries/segment.hpp>
45 #include <boost/geometry/util/range.hpp>
46 #include <boost/geometry/util/type_traits.hpp>
48 #include <boost/geometry/views/detail/indexed_point_view.hpp>
51 namespace boost { namespace geometry
54 #ifndef DOXYGEN_NO_DETAIL
55 namespace detail { namespace for_each
61 template <typename Point, typename Functor>
62 static inline bool apply(Point& point, Functor&& f)
69 struct fe_segment_point
71 template <typename Point, typename Functor>
72 static inline bool apply(Point& , Functor&& )
74 // TODO: if non-const, we should extract the points from the segment
75 // and call the functor on those two points
77 //model::referring_segment<Point> s(point, point);
85 struct fe_point_segment
87 template <typename Segment, typename Functor>
88 static inline bool apply(Segment& s, Functor&& f)
90 // Or should we guarantee that the type of points is
91 // point_type<Segment>::type ?
92 geometry::detail::indexed_point_view<Segment, 0> p0(s);
93 geometry::detail::indexed_point_view<Segment, 1> p1(s);
94 return f(p0) && f(p1);
98 struct fe_segment_segment
100 template <typename Segment, typename Functor>
101 static inline bool apply(Segment& s, Functor&& f)
103 // Or should we guarantee that the type of segment is
104 // referring_segment<...> ?
110 template <typename Range>
111 struct fe_range_value
113 typedef util::transcribe_const_t
116 typename boost::range_value<Range>::type
120 template <typename Range>
123 typedef util::transcribe_const_t
126 typename point_type<Range>::type
131 template <typename Range>
132 struct fe_point_type_is_referencable
134 static const bool value =
135 std::is_const<Range>::value
138 typename boost::range_reference<Range>::type,
139 typename fe_point_type<Range>::type&
147 bool UseReferences = fe_point_type_is_referencable<Range>::value
149 struct fe_point_call_f
151 template <typename Iterator, typename Functor>
152 static inline bool apply(Iterator it, Functor&& f)
154 // Implementation for real references (both const and mutable)
155 // and const proxy references.
156 typedef typename fe_point_type<Range>::type point_type;
162 template <typename Range>
163 struct fe_point_call_f<Range, false>
165 template <typename Iterator, typename Functor>
166 static inline bool apply(Iterator it, Functor&& f)
168 // Implementation for proxy mutable references.
169 // Temporary point has to be created and assigned afterwards.
170 typedef typename fe_point_type<Range>::type point_type;
179 struct fe_point_range
181 template <typename Range, typename Functor>
182 static inline bool apply(Range& range, Functor&& f)
184 auto const end = boost::end(range);
185 for (auto it = boost::begin(range); it != end; ++it)
187 if (! fe_point_call_f<Range>::apply(it, f))
201 bool UseReferences = fe_point_type_is_referencable<Range>::value
203 struct fe_segment_call_f
205 template <typename Iterator, typename Functor>
206 static inline bool apply(Iterator it0, Iterator it1, Functor&& f)
208 // Implementation for real references (both const and mutable)
209 // and const proxy references.
210 // If const proxy references are returned by iterators
211 // then const real references here prevents temporary
212 // objects from being destroyed.
213 typedef typename fe_point_type<Range>::type point_type;
214 point_type& p0 = *it0;
215 point_type& p1 = *it1;
216 model::referring_segment<point_type> s(p0, p1);
221 template <typename Range>
222 struct fe_segment_call_f<Range, false>
224 template <typename Iterator, typename Functor>
225 static inline bool apply(Iterator it0, Iterator it1, Functor&& f)
227 // Mutable proxy references returned by iterators.
228 // Temporary points have to be created and assigned afterwards.
229 typedef typename fe_point_type<Range>::type point_type;
230 point_type p0 = *it0;
231 point_type p1 = *it1;
232 model::referring_segment<point_type> s(p0, p1);
241 template <closure_selector Closure>
242 struct fe_segment_range_with_closure
244 template <typename Range, typename Functor>
245 static inline bool apply(Range& range, Functor&& f)
247 auto it = boost::begin(range);
248 auto const end = boost::end(range);
254 auto previous = it++;
257 return fe_segment_call_f<Range>::apply(previous, previous, f);
262 if (! fe_segment_call_f<Range>::apply(previous, it, f))
275 struct fe_segment_range_with_closure<open>
277 template <typename Range, typename Functor>
278 static inline bool apply(Range& range, Functor&& f)
280 fe_segment_range_with_closure<closed>::apply(range, f);
282 auto const begin = boost::begin(range);
283 auto end = boost::end(range);
293 // single point ranges already handled in closed case above
297 return fe_segment_call_f<Range>::apply(end, begin, f);
302 struct fe_segment_range
304 template <typename Range, typename Functor>
305 static inline bool apply(Range& range, Functor&& f)
307 return fe_segment_range_with_closure
309 closure<Range>::value
315 template <typename RangePolicy>
316 struct for_each_polygon
318 template <typename Polygon, typename Functor>
319 static inline bool apply(Polygon& poly, Functor&& f)
321 if (! RangePolicy::apply(exterior_ring(poly), f))
326 typename interior_return_type<Polygon>::type
327 rings = interior_rings(poly);
329 auto const end = boost::end(rings);
330 for (auto it = boost::begin(rings); it != end; ++it)
332 // NOTE: Currently lvalue iterator required
333 if (! RangePolicy::apply(*it, f))
344 // Implementation of multi, for both point and segment,
345 // just calling the single version.
346 template <typename SinglePolicy>
347 struct for_each_multi
349 template <typename MultiGeometry, typename Functor>
350 static inline bool apply(MultiGeometry& multi, Functor&& f)
352 auto const end = boost::end(multi);
353 for (auto it = boost::begin(multi); it != end; ++it)
355 // NOTE: Currently lvalue iterator required
356 if (! SinglePolicy::apply(*it, f))
366 }} // namespace detail::for_each
367 #endif // DOXYGEN_NO_DETAIL
370 #ifndef DOXYGEN_NO_DISPATCH
377 typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
379 struct for_each_point: not_implemented<Tag>
383 template <typename Point>
384 struct for_each_point<Point, point_tag>
385 : detail::for_each::fe_point_point
389 template <typename Segment>
390 struct for_each_point<Segment, segment_tag>
391 : detail::for_each::fe_point_segment
395 template <typename Linestring>
396 struct for_each_point<Linestring, linestring_tag>
397 : detail::for_each::fe_point_range
401 template <typename Ring>
402 struct for_each_point<Ring, ring_tag>
403 : detail::for_each::fe_point_range
407 template <typename Polygon>
408 struct for_each_point<Polygon, polygon_tag>
409 : detail::for_each::for_each_polygon
411 detail::for_each::fe_point_range
416 template <typename MultiGeometry>
417 struct for_each_point<MultiGeometry, multi_tag>
418 : detail::for_each::for_each_multi
420 // Specify the dispatch of the single-version as policy
423 typename detail::for_each::fe_range_value
435 typename Tag = typename tag<Geometry>::type
437 struct for_each_segment: not_implemented<Tag>
440 template <typename Point>
441 struct for_each_segment<Point, point_tag>
442 : detail::for_each::fe_segment_point // empty
446 template <typename Segment>
447 struct for_each_segment<Segment, segment_tag>
448 : detail::for_each::fe_segment_segment
452 template <typename Linestring>
453 struct for_each_segment<Linestring, linestring_tag>
454 : detail::for_each::fe_segment_range
458 template <typename Ring>
459 struct for_each_segment<Ring, ring_tag>
460 : detail::for_each::fe_segment_range
464 template <typename Polygon>
465 struct for_each_segment<Polygon, polygon_tag>
466 : detail::for_each::for_each_polygon
468 detail::for_each::fe_segment_range
473 template <typename MultiPoint>
474 struct for_each_segment<MultiPoint, multi_point_tag>
475 : detail::for_each::fe_segment_point // empty
479 template <typename MultiLinestring>
480 struct for_each_segment<MultiLinestring, multi_linestring_tag>
481 : detail::for_each::for_each_multi
483 detail::for_each::fe_segment_range
487 template <typename MultiPolygon>
488 struct for_each_segment<MultiPolygon, multi_polygon_tag>
489 : detail::for_each::for_each_multi
491 detail::for_each::for_each_polygon
493 detail::for_each::fe_segment_range
499 } // namespace dispatch
500 #endif // DOXYGEN_NO_DISPATCH
503 template<typename Geometry, typename UnaryPredicate>
504 inline bool all_points_of(Geometry& geometry, UnaryPredicate p)
506 concepts::check<Geometry>();
508 return dispatch::for_each_point<Geometry>::apply(geometry, p);
512 template<typename Geometry, typename UnaryPredicate>
513 inline bool all_segments_of(Geometry const& geometry, UnaryPredicate p)
515 concepts::check<Geometry const>();
517 return dispatch::for_each_segment<Geometry const>::apply(geometry, p);
521 template<typename Geometry, typename UnaryPredicate>
522 inline bool any_point_of(Geometry& geometry, UnaryPredicate p)
524 concepts::check<Geometry>();
526 return ! dispatch::for_each_point<Geometry>::apply(geometry, [&](auto&& pt)
533 template<typename Geometry, typename UnaryPredicate>
534 inline bool any_segment_of(Geometry const& geometry, UnaryPredicate p)
536 concepts::check<Geometry const>();
538 return ! dispatch::for_each_segment<Geometry const>::apply(geometry, [&](auto&& s)
544 template<typename Geometry, typename UnaryPredicate>
545 inline bool none_point_of(Geometry& geometry, UnaryPredicate p)
547 concepts::check<Geometry>();
549 return dispatch::for_each_point<Geometry>::apply(geometry, [&](auto&& pt)
556 template<typename Geometry, typename UnaryPredicate>
557 inline bool none_segment_of(Geometry const& geometry, UnaryPredicate p)
559 concepts::check<Geometry const>();
561 return dispatch::for_each_segment<Geometry const>::apply(geometry, [&](auto&& s)
569 \brief \brf_for_each{point}
570 \details \det_for_each{point}
572 \param geometry \param_geometry
573 \param f \par_for_each_f{point}
574 \tparam Geometry \tparam_geometry
575 \tparam Functor \tparam_functor
577 \qbk{[include reference/algorithms/for_each_point.qbk]}
578 \qbk{[heading Example]}
579 \qbk{[for_each_point] [for_each_point_output]}
580 \qbk{[for_each_point_const] [for_each_point_const_output]}
582 template<typename Geometry, typename Functor>
583 inline Functor for_each_point(Geometry& geometry, Functor f)
585 concepts::check<Geometry>();
587 dispatch::for_each_point<Geometry>::apply(geometry, [&](auto&& pt)
590 // TODO: Implement separate function?
598 \brief \brf_for_each{segment}
599 \details \det_for_each{segment}
601 \param geometry \param_geometry
602 \param f \par_for_each_f{segment}
603 \tparam Geometry \tparam_geometry
604 \tparam Functor \tparam_functor
606 \qbk{[include reference/algorithms/for_each_segment.qbk]}
607 \qbk{[heading Example]}
608 \qbk{[for_each_segment_const] [for_each_segment_const_output]}
610 template<typename Geometry, typename Functor>
611 inline Functor for_each_segment(Geometry& geometry, Functor f)
613 concepts::check<Geometry>();
615 dispatch::for_each_segment<Geometry>::apply(geometry, [&](auto&& s)
618 // TODO: Implement separate function?
625 }} // namespace boost::geometry
628 #endif // BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP