for_each.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014.
  7. // Modifications copyright (c) 2014, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
  16. #include <algorithm>
  17. #include <boost/range.hpp>
  18. #include <boost/type_traits/is_const.hpp>
  19. #include <boost/type_traits/remove_reference.hpp>
  20. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  21. #include <boost/geometry/algorithms/not_implemented.hpp>
  22. #include <boost/geometry/core/closure.hpp>
  23. #include <boost/geometry/core/exterior_ring.hpp>
  24. #include <boost/geometry/core/interior_rings.hpp>
  25. #include <boost/geometry/core/point_type.hpp>
  26. #include <boost/geometry/core/tag_cast.hpp>
  27. #include <boost/geometry/core/tags.hpp>
  28. #include <boost/geometry/geometries/concepts/check.hpp>
  29. #include <boost/geometry/geometries/segment.hpp>
  30. #include <boost/geometry/util/add_const_if_c.hpp>
  31. #include <boost/geometry/util/range.hpp>
  32. namespace boost { namespace geometry
  33. {
  34. #ifndef DOXYGEN_NO_DETAIL
  35. namespace detail { namespace for_each
  36. {
  37. struct fe_point_per_point
  38. {
  39. template <typename Point, typename Functor>
  40. static inline void apply(Point& point, Functor& f)
  41. {
  42. f(point);
  43. }
  44. };
  45. struct fe_point_per_segment
  46. {
  47. template <typename Point, typename Functor>
  48. static inline void apply(Point& , Functor& /*f*/)
  49. {
  50. // TODO: if non-const, we should extract the points from the segment
  51. // and call the functor on those two points
  52. }
  53. };
  54. struct fe_range_per_point
  55. {
  56. template <typename Range, typename Functor>
  57. static inline void apply(Range& range, Functor& f)
  58. {
  59. // The previous implementation called the std library:
  60. // return (std::for_each(boost::begin(range), boost::end(range), f));
  61. // But that is not accepted for capturing lambda's.
  62. // It needs to do it like that to return the state of Functor f (f is passed by value in std::for_each).
  63. // So we now loop manually.
  64. for (typename boost::range_iterator<Range>::type
  65. it = boost::begin(range); it != boost::end(range); ++it)
  66. {
  67. f(*it);
  68. }
  69. }
  70. };
  71. template <closure_selector Closure>
  72. struct fe_range_per_segment_with_closure
  73. {
  74. template <typename Range, typename Functor>
  75. static inline void apply(Range& range, Functor& f)
  76. {
  77. typedef typename add_const_if_c
  78. <
  79. is_const<Range>::value,
  80. typename point_type<Range>::type
  81. >::type point_type;
  82. typedef typename boost::range_iterator<Range>::type iterator_type;
  83. iterator_type it = boost::begin(range);
  84. iterator_type previous = it++;
  85. while(it != boost::end(range))
  86. {
  87. model::referring_segment<point_type> s(*previous, *it);
  88. f(s);
  89. previous = it++;
  90. }
  91. }
  92. };
  93. template <>
  94. struct fe_range_per_segment_with_closure<open>
  95. {
  96. template <typename Range, typename Functor>
  97. static inline void apply(Range& range, Functor& f)
  98. {
  99. fe_range_per_segment_with_closure<closed>::apply(range, f);
  100. model::referring_segment
  101. <
  102. typename add_const_if_c
  103. <
  104. is_const<Range>::value,
  105. typename point_type<Range>::type
  106. >::type
  107. > s(range::back(range), range::front(range));
  108. f(s);
  109. }
  110. };
  111. struct fe_range_per_segment
  112. {
  113. template <typename Range, typename Functor>
  114. static inline void apply(Range& range, Functor& f)
  115. {
  116. fe_range_per_segment_with_closure
  117. <
  118. closure<Range>::value
  119. >::apply(range, f);
  120. }
  121. };
  122. struct fe_polygon_per_point
  123. {
  124. template <typename Polygon, typename Functor>
  125. static inline void apply(Polygon& poly, Functor& f)
  126. {
  127. fe_range_per_point::apply(exterior_ring(poly), f);
  128. typename interior_return_type<Polygon>::type
  129. rings = interior_rings(poly);
  130. for (typename detail::interior_iterator<Polygon>::type
  131. it = boost::begin(rings); it != boost::end(rings); ++it)
  132. {
  133. fe_range_per_point::apply(*it, f);
  134. }
  135. }
  136. };
  137. struct fe_polygon_per_segment
  138. {
  139. template <typename Polygon, typename Functor>
  140. static inline void apply(Polygon& poly, Functor& f)
  141. {
  142. fe_range_per_segment::apply(exterior_ring(poly), f);
  143. typename interior_return_type<Polygon>::type
  144. rings = interior_rings(poly);
  145. for (typename detail::interior_iterator<Polygon>::type
  146. it = boost::begin(rings); it != boost::end(rings); ++it)
  147. {
  148. fe_range_per_segment::apply(*it, f);
  149. }
  150. }
  151. };
  152. // Implementation of multi, for both point and segment,
  153. // just calling the single version.
  154. template <typename Policy>
  155. struct for_each_multi
  156. {
  157. template <typename MultiGeometry, typename Functor>
  158. static inline void apply(MultiGeometry& multi, Functor& f)
  159. {
  160. for (typename boost::range_iterator<MultiGeometry>::type
  161. it = boost::begin(multi); it != boost::end(multi); ++it)
  162. {
  163. Policy::apply(*it, f);
  164. }
  165. }
  166. };
  167. }} // namespace detail::for_each
  168. #endif // DOXYGEN_NO_DETAIL
  169. #ifndef DOXYGEN_NO_DISPATCH
  170. namespace dispatch
  171. {
  172. template
  173. <
  174. typename Geometry,
  175. typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
  176. >
  177. struct for_each_point: not_implemented<Tag>
  178. {};
  179. template <typename Point>
  180. struct for_each_point<Point, point_tag>
  181. : detail::for_each::fe_point_per_point
  182. {};
  183. template <typename Linestring>
  184. struct for_each_point<Linestring, linestring_tag>
  185. : detail::for_each::fe_range_per_point
  186. {};
  187. template <typename Ring>
  188. struct for_each_point<Ring, ring_tag>
  189. : detail::for_each::fe_range_per_point
  190. {};
  191. template <typename Polygon>
  192. struct for_each_point<Polygon, polygon_tag>
  193. : detail::for_each::fe_polygon_per_point
  194. {};
  195. template
  196. <
  197. typename Geometry,
  198. typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
  199. >
  200. struct for_each_segment: not_implemented<Tag>
  201. {};
  202. template <typename Point>
  203. struct for_each_segment<Point, point_tag>
  204. : detail::for_each::fe_point_per_segment
  205. {};
  206. template <typename Linestring>
  207. struct for_each_segment<Linestring, linestring_tag>
  208. : detail::for_each::fe_range_per_segment
  209. {};
  210. template <typename Ring>
  211. struct for_each_segment<Ring, ring_tag>
  212. : detail::for_each::fe_range_per_segment
  213. {};
  214. template <typename Polygon>
  215. struct for_each_segment<Polygon, polygon_tag>
  216. : detail::for_each::fe_polygon_per_segment
  217. {};
  218. template <typename MultiGeometry>
  219. struct for_each_point<MultiGeometry, multi_tag>
  220. : detail::for_each::for_each_multi
  221. <
  222. // Specify the dispatch of the single-version as policy
  223. for_each_point
  224. <
  225. typename add_const_if_c
  226. <
  227. is_const<MultiGeometry>::value,
  228. typename boost::range_value<MultiGeometry>::type
  229. >::type
  230. >
  231. >
  232. {};
  233. template <typename MultiGeometry>
  234. struct for_each_segment<MultiGeometry, multi_tag>
  235. : detail::for_each::for_each_multi
  236. <
  237. // Specify the dispatch of the single-version as policy
  238. for_each_segment
  239. <
  240. typename add_const_if_c
  241. <
  242. is_const<MultiGeometry>::value,
  243. typename boost::range_value<MultiGeometry>::type
  244. >::type
  245. >
  246. >
  247. {};
  248. } // namespace dispatch
  249. #endif // DOXYGEN_NO_DISPATCH
  250. /*!
  251. \brief \brf_for_each{point}
  252. \details \det_for_each{point}
  253. \ingroup for_each
  254. \param geometry \param_geometry
  255. \param f \par_for_each_f{point}
  256. \tparam Geometry \tparam_geometry
  257. \tparam Functor \tparam_functor
  258. \qbk{[include reference/algorithms/for_each_point.qbk]}
  259. \qbk{[heading Example]}
  260. \qbk{[for_each_point] [for_each_point_output]}
  261. \qbk{[for_each_point_const] [for_each_point_const_output]}
  262. */
  263. template<typename Geometry, typename Functor>
  264. inline Functor for_each_point(Geometry& geometry, Functor f)
  265. {
  266. concepts::check<Geometry>();
  267. dispatch::for_each_point<Geometry>::apply(geometry, f);
  268. return f;
  269. }
  270. /*!
  271. \brief \brf_for_each{segment}
  272. \details \det_for_each{segment}
  273. \ingroup for_each
  274. \param geometry \param_geometry
  275. \param f \par_for_each_f{segment}
  276. \tparam Geometry \tparam_geometry
  277. \tparam Functor \tparam_functor
  278. \qbk{[include reference/algorithms/for_each_segment.qbk]}
  279. \qbk{[heading Example]}
  280. \qbk{[for_each_segment_const] [for_each_segment_const_output]}
  281. */
  282. template<typename Geometry, typename Functor>
  283. inline Functor for_each_segment(Geometry& geometry, Functor f)
  284. {
  285. concepts::check<Geometry>();
  286. dispatch::for_each_segment<Geometry>::apply(geometry, f);
  287. return f;
  288. }
  289. }} // namespace boost::geometry
  290. #endif // BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP