for_each.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. if (it == boost::end(range))
  85. {
  86. return;
  87. }
  88. iterator_type previous = it++;
  89. while(it != boost::end(range))
  90. {
  91. model::referring_segment<point_type> s(*previous, *it);
  92. f(s);
  93. previous = it++;
  94. }
  95. }
  96. };
  97. template <>
  98. struct fe_range_per_segment_with_closure<open>
  99. {
  100. template <typename Range, typename Functor>
  101. static inline void apply(Range& range, Functor& f)
  102. {
  103. fe_range_per_segment_with_closure<closed>::apply(range, f);
  104. model::referring_segment
  105. <
  106. typename add_const_if_c
  107. <
  108. is_const<Range>::value,
  109. typename point_type<Range>::type
  110. >::type
  111. > s(range::back(range), range::front(range));
  112. f(s);
  113. }
  114. };
  115. struct fe_range_per_segment
  116. {
  117. template <typename Range, typename Functor>
  118. static inline void apply(Range& range, Functor& f)
  119. {
  120. fe_range_per_segment_with_closure
  121. <
  122. closure<Range>::value
  123. >::apply(range, f);
  124. }
  125. };
  126. struct fe_polygon_per_point
  127. {
  128. template <typename Polygon, typename Functor>
  129. static inline void apply(Polygon& poly, Functor& f)
  130. {
  131. fe_range_per_point::apply(exterior_ring(poly), f);
  132. typename interior_return_type<Polygon>::type
  133. rings = interior_rings(poly);
  134. for (typename detail::interior_iterator<Polygon>::type
  135. it = boost::begin(rings); it != boost::end(rings); ++it)
  136. {
  137. fe_range_per_point::apply(*it, f);
  138. }
  139. }
  140. };
  141. struct fe_polygon_per_segment
  142. {
  143. template <typename Polygon, typename Functor>
  144. static inline void apply(Polygon& poly, Functor& f)
  145. {
  146. fe_range_per_segment::apply(exterior_ring(poly), f);
  147. typename interior_return_type<Polygon>::type
  148. rings = interior_rings(poly);
  149. for (typename detail::interior_iterator<Polygon>::type
  150. it = boost::begin(rings); it != boost::end(rings); ++it)
  151. {
  152. fe_range_per_segment::apply(*it, f);
  153. }
  154. }
  155. };
  156. // Implementation of multi, for both point and segment,
  157. // just calling the single version.
  158. template <typename Policy>
  159. struct for_each_multi
  160. {
  161. template <typename MultiGeometry, typename Functor>
  162. static inline void apply(MultiGeometry& multi, Functor& f)
  163. {
  164. for (typename boost::range_iterator<MultiGeometry>::type
  165. it = boost::begin(multi); it != boost::end(multi); ++it)
  166. {
  167. Policy::apply(*it, f);
  168. }
  169. }
  170. };
  171. }} // namespace detail::for_each
  172. #endif // DOXYGEN_NO_DETAIL
  173. #ifndef DOXYGEN_NO_DISPATCH
  174. namespace dispatch
  175. {
  176. template
  177. <
  178. typename Geometry,
  179. typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
  180. >
  181. struct for_each_point: not_implemented<Tag>
  182. {};
  183. template <typename Point>
  184. struct for_each_point<Point, point_tag>
  185. : detail::for_each::fe_point_per_point
  186. {};
  187. template <typename Linestring>
  188. struct for_each_point<Linestring, linestring_tag>
  189. : detail::for_each::fe_range_per_point
  190. {};
  191. template <typename Ring>
  192. struct for_each_point<Ring, ring_tag>
  193. : detail::for_each::fe_range_per_point
  194. {};
  195. template <typename Polygon>
  196. struct for_each_point<Polygon, polygon_tag>
  197. : detail::for_each::fe_polygon_per_point
  198. {};
  199. template
  200. <
  201. typename Geometry,
  202. typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
  203. >
  204. struct for_each_segment: not_implemented<Tag>
  205. {};
  206. template <typename Point>
  207. struct for_each_segment<Point, point_tag>
  208. : detail::for_each::fe_point_per_segment
  209. {};
  210. template <typename Linestring>
  211. struct for_each_segment<Linestring, linestring_tag>
  212. : detail::for_each::fe_range_per_segment
  213. {};
  214. template <typename Ring>
  215. struct for_each_segment<Ring, ring_tag>
  216. : detail::for_each::fe_range_per_segment
  217. {};
  218. template <typename Polygon>
  219. struct for_each_segment<Polygon, polygon_tag>
  220. : detail::for_each::fe_polygon_per_segment
  221. {};
  222. template <typename MultiGeometry>
  223. struct for_each_point<MultiGeometry, multi_tag>
  224. : detail::for_each::for_each_multi
  225. <
  226. // Specify the dispatch of the single-version as policy
  227. for_each_point
  228. <
  229. typename add_const_if_c
  230. <
  231. is_const<MultiGeometry>::value,
  232. typename boost::range_value<MultiGeometry>::type
  233. >::type
  234. >
  235. >
  236. {};
  237. template <typename MultiGeometry>
  238. struct for_each_segment<MultiGeometry, multi_tag>
  239. : detail::for_each::for_each_multi
  240. <
  241. // Specify the dispatch of the single-version as policy
  242. for_each_segment
  243. <
  244. typename add_const_if_c
  245. <
  246. is_const<MultiGeometry>::value,
  247. typename boost::range_value<MultiGeometry>::type
  248. >::type
  249. >
  250. >
  251. {};
  252. } // namespace dispatch
  253. #endif // DOXYGEN_NO_DISPATCH
  254. /*!
  255. \brief \brf_for_each{point}
  256. \details \det_for_each{point}
  257. \ingroup for_each
  258. \param geometry \param_geometry
  259. \param f \par_for_each_f{point}
  260. \tparam Geometry \tparam_geometry
  261. \tparam Functor \tparam_functor
  262. \qbk{[include reference/algorithms/for_each_point.qbk]}
  263. \qbk{[heading Example]}
  264. \qbk{[for_each_point] [for_each_point_output]}
  265. \qbk{[for_each_point_const] [for_each_point_const_output]}
  266. */
  267. template<typename Geometry, typename Functor>
  268. inline Functor for_each_point(Geometry& geometry, Functor f)
  269. {
  270. concepts::check<Geometry>();
  271. dispatch::for_each_point<Geometry>::apply(geometry, f);
  272. return f;
  273. }
  274. /*!
  275. \brief \brf_for_each{segment}
  276. \details \det_for_each{segment}
  277. \ingroup for_each
  278. \param geometry \param_geometry
  279. \param f \par_for_each_f{segment}
  280. \tparam Geometry \tparam_geometry
  281. \tparam Functor \tparam_functor
  282. \qbk{[include reference/algorithms/for_each_segment.qbk]}
  283. \qbk{[heading Example]}
  284. \qbk{[for_each_segment_const] [for_each_segment_const_output]}
  285. */
  286. template<typename Geometry, typename Functor>
  287. inline Functor for_each_segment(Geometry& geometry, Functor f)
  288. {
  289. concepts::check<Geometry>();
  290. dispatch::for_each_segment<Geometry>::apply(geometry, f);
  291. return f;
  292. }
  293. }} // namespace boost::geometry
  294. #endif // BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP