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.
9 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
16 // Use, modification and distribution is subject to the Boost Software License,
17 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
20 #ifndef BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
26 #include <boost/range.hpp>
27 #include <boost/type_traits/is_const.hpp>
28 #include <boost/type_traits/remove_reference.hpp>
30 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
31 #include <boost/geometry/algorithms/not_implemented.hpp>
32 #include <boost/geometry/core/closure.hpp>
33 #include <boost/geometry/core/exterior_ring.hpp>
34 #include <boost/geometry/core/interior_rings.hpp>
35 #include <boost/geometry/core/point_type.hpp>
36 #include <boost/geometry/core/tag_cast.hpp>
37 #include <boost/geometry/core/tags.hpp>
39 #include <boost/geometry/geometries/concepts/check.hpp>
41 #include <boost/geometry/geometries/segment.hpp>
43 #include <boost/geometry/util/add_const_if_c.hpp>
44 #include <boost/geometry/util/range.hpp>
47 namespace boost { namespace geometry
50 #ifndef DOXYGEN_NO_DETAIL
51 namespace detail { namespace for_each
55 struct fe_point_per_point
57 template <typename Point, typename Functor>
58 static inline void apply(Point& point, Functor& f)
65 struct fe_point_per_segment
67 template <typename Point, typename Functor>
68 static inline void apply(Point& , Functor& /*f*/)
70 // TODO: if non-const, we should extract the points from the segment
71 // and call the functor on those two points
76 struct fe_range_per_point
78 template <typename Range, typename Functor>
79 static inline void apply(Range& range, Functor& f)
81 // The previous implementation called the std library:
82 // return (std::for_each(boost::begin(range), boost::end(range), f));
83 // But that is not accepted for capturing lambda's.
84 // It needs to do it like that to return the state of Functor f (f is passed by value in std::for_each).
86 // So we now loop manually.
88 for (typename boost::range_iterator<Range>::type
89 it = boost::begin(range); it != boost::end(range); ++it)
97 template <closure_selector Closure>
98 struct fe_range_per_segment_with_closure
100 template <typename Range, typename Functor>
101 static inline void apply(Range& range, Functor& f)
103 typedef typename add_const_if_c
105 is_const<Range>::value,
106 typename point_type<Range>::type
109 typedef typename boost::range_iterator<Range>::type iterator_type;
111 iterator_type it = boost::begin(range);
112 if (it == boost::end(range))
117 iterator_type previous = it++;
118 while(it != boost::end(range))
120 model::referring_segment<point_type> s(*previous, *it);
129 struct fe_range_per_segment_with_closure<open>
131 template <typename Range, typename Functor>
132 static inline void apply(Range& range, Functor& f)
134 fe_range_per_segment_with_closure<closed>::apply(range, f);
136 model::referring_segment
138 typename add_const_if_c
140 is_const<Range>::value,
141 typename point_type<Range>::type
143 > s(range::back(range), range::front(range));
150 struct fe_range_per_segment
152 template <typename Range, typename Functor>
153 static inline void apply(Range& range, Functor& f)
155 fe_range_per_segment_with_closure
157 closure<Range>::value
163 struct fe_polygon_per_point
165 template <typename Polygon, typename Functor>
166 static inline void apply(Polygon& poly, Functor& f)
168 fe_range_per_point::apply(exterior_ring(poly), f);
170 typename interior_return_type<Polygon>::type
171 rings = interior_rings(poly);
173 for (typename detail::interior_iterator<Polygon>::type
174 it = boost::begin(rings); it != boost::end(rings); ++it)
176 fe_range_per_point::apply(*it, f);
182 struct fe_polygon_per_segment
184 template <typename Polygon, typename Functor>
185 static inline void apply(Polygon& poly, Functor& f)
187 fe_range_per_segment::apply(exterior_ring(poly), f);
189 typename interior_return_type<Polygon>::type
190 rings = interior_rings(poly);
192 for (typename detail::interior_iterator<Polygon>::type
193 it = boost::begin(rings); it != boost::end(rings); ++it)
195 fe_range_per_segment::apply(*it, f);
201 // Implementation of multi, for both point and segment,
202 // just calling the single version.
203 template <typename Policy>
204 struct for_each_multi
206 template <typename MultiGeometry, typename Functor>
207 static inline void apply(MultiGeometry& multi, Functor& f)
209 for (typename boost::range_iterator<MultiGeometry>::type
210 it = boost::begin(multi); it != boost::end(multi); ++it)
212 Policy::apply(*it, f);
217 }} // namespace detail::for_each
218 #endif // DOXYGEN_NO_DETAIL
221 #ifndef DOXYGEN_NO_DISPATCH
228 typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
230 struct for_each_point: not_implemented<Tag>
234 template <typename Point>
235 struct for_each_point<Point, point_tag>
236 : detail::for_each::fe_point_per_point
240 template <typename Linestring>
241 struct for_each_point<Linestring, linestring_tag>
242 : detail::for_each::fe_range_per_point
246 template <typename Ring>
247 struct for_each_point<Ring, ring_tag>
248 : detail::for_each::fe_range_per_point
252 template <typename Polygon>
253 struct for_each_point<Polygon, polygon_tag>
254 : detail::for_each::fe_polygon_per_point
261 typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
263 struct for_each_segment: not_implemented<Tag>
266 template <typename Point>
267 struct for_each_segment<Point, point_tag>
268 : detail::for_each::fe_point_per_segment
272 template <typename Linestring>
273 struct for_each_segment<Linestring, linestring_tag>
274 : detail::for_each::fe_range_per_segment
278 template <typename Ring>
279 struct for_each_segment<Ring, ring_tag>
280 : detail::for_each::fe_range_per_segment
284 template <typename Polygon>
285 struct for_each_segment<Polygon, polygon_tag>
286 : detail::for_each::fe_polygon_per_segment
290 template <typename MultiGeometry>
291 struct for_each_point<MultiGeometry, multi_tag>
292 : detail::for_each::for_each_multi
294 // Specify the dispatch of the single-version as policy
297 typename add_const_if_c
299 is_const<MultiGeometry>::value,
300 typename boost::range_value<MultiGeometry>::type
307 template <typename MultiGeometry>
308 struct for_each_segment<MultiGeometry, multi_tag>
309 : detail::for_each::for_each_multi
311 // Specify the dispatch of the single-version as policy
314 typename add_const_if_c
316 is_const<MultiGeometry>::value,
317 typename boost::range_value<MultiGeometry>::type
324 } // namespace dispatch
325 #endif // DOXYGEN_NO_DISPATCH
329 \brief \brf_for_each{point}
330 \details \det_for_each{point}
332 \param geometry \param_geometry
333 \param f \par_for_each_f{point}
334 \tparam Geometry \tparam_geometry
335 \tparam Functor \tparam_functor
337 \qbk{[include reference/algorithms/for_each_point.qbk]}
338 \qbk{[heading Example]}
339 \qbk{[for_each_point] [for_each_point_output]}
340 \qbk{[for_each_point_const] [for_each_point_const_output]}
342 template<typename Geometry, typename Functor>
343 inline Functor for_each_point(Geometry& geometry, Functor f)
345 concepts::check<Geometry>();
347 dispatch::for_each_point<Geometry>::apply(geometry, f);
353 \brief \brf_for_each{segment}
354 \details \det_for_each{segment}
356 \param geometry \param_geometry
357 \param f \par_for_each_f{segment}
358 \tparam Geometry \tparam_geometry
359 \tparam Functor \tparam_functor
361 \qbk{[include reference/algorithms/for_each_segment.qbk]}
362 \qbk{[heading Example]}
363 \qbk{[for_each_segment_const] [for_each_segment_const_output]}
365 template<typename Geometry, typename Functor>
366 inline Functor for_each_segment(Geometry& geometry, Functor f)
368 concepts::check<Geometry>();
370 dispatch::for_each_segment<Geometry>::apply(geometry, f);
375 }} // namespace boost::geometry
378 #endif // BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP