for_each_range.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2020.
  6. // Modifications copyright (c) 2020, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_FOR_EACH_RANGE_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_FOR_EACH_RANGE_HPP
  15. #include <type_traits>
  16. #include <utility>
  17. #include <boost/concept/requires.hpp>
  18. #include <boost/core/addressof.hpp>
  19. #include <boost/range/begin.hpp>
  20. #include <boost/range/end.hpp>
  21. #include <boost/geometry/core/static_assert.hpp>
  22. #include <boost/geometry/core/tag.hpp>
  23. #include <boost/geometry/core/tag_cast.hpp>
  24. #include <boost/geometry/core/tags.hpp>
  25. #include <boost/geometry/util/type_traits.hpp>
  26. #include <boost/geometry/views/box_view.hpp>
  27. #include <boost/geometry/views/segment_view.hpp>
  28. namespace boost { namespace geometry
  29. {
  30. #ifndef DOXYGEN_NO_DETAIL
  31. namespace detail { namespace for_each
  32. {
  33. template <typename Point>
  34. struct fe_range_point
  35. {
  36. template <typename Functor>
  37. static inline bool apply(Point& point, Functor&& f)
  38. {
  39. Point* ptr = boost::addressof(point);
  40. return f(std::pair<Point*, Point*>(ptr, ptr + 1));
  41. }
  42. };
  43. template <typename Segment>
  44. struct fe_range_segment
  45. {
  46. template <typename Functor>
  47. static inline bool apply(Segment& segment, Functor&& f)
  48. {
  49. return f(segment_view<typename std::remove_const<Segment>::type>(segment));
  50. }
  51. };
  52. template <typename Range>
  53. struct fe_range_range
  54. {
  55. template <typename Functor>
  56. static inline bool apply(Range& range, Functor&& f)
  57. {
  58. return f(range);
  59. }
  60. };
  61. template <typename Polygon>
  62. struct fe_range_polygon
  63. {
  64. template <typename Functor>
  65. static inline bool apply(Polygon& polygon, Functor&& f)
  66. {
  67. return f(exterior_ring(polygon));
  68. // TODO: If some flag says true, also do the inner rings.
  69. // for convex hull, it's not necessary
  70. }
  71. };
  72. template <typename Box>
  73. struct fe_range_box
  74. {
  75. template <typename Functor>
  76. static inline bool apply(Box& box, Functor&& f)
  77. {
  78. return f(box_view<typename std::remove_const<Box>::type>(box));
  79. }
  80. };
  81. template <typename Multi, typename SinglePolicy>
  82. struct fe_range_multi
  83. {
  84. template <typename Functor>
  85. static inline bool apply(Multi& multi, Functor&& f)
  86. {
  87. auto const end = boost::end(multi);
  88. for (auto it = boost::begin(multi); it != end; ++it)
  89. {
  90. if (! SinglePolicy::apply(*it, f))
  91. {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. };
  98. }} // namespace detail::for_each
  99. #endif // DOXYGEN_NO_DETAIL
  100. #ifndef DOXYGEN_NO_DISPATCH
  101. namespace dispatch
  102. {
  103. template
  104. <
  105. typename Geometry,
  106. typename Tag = typename tag<Geometry>::type
  107. >
  108. struct for_each_range
  109. {
  110. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  111. "Not or not yet implemented for this Geometry type.",
  112. Geometry, Tag);
  113. };
  114. template <typename Point>
  115. struct for_each_range<Point, point_tag>
  116. : detail::for_each::fe_range_point<Point>
  117. {};
  118. template <typename Segment>
  119. struct for_each_range<Segment, segment_tag>
  120. : detail::for_each::fe_range_segment<Segment>
  121. {};
  122. template <typename Linestring>
  123. struct for_each_range<Linestring, linestring_tag>
  124. : detail::for_each::fe_range_range<Linestring>
  125. {};
  126. template <typename Ring>
  127. struct for_each_range<Ring, ring_tag>
  128. : detail::for_each::fe_range_range<Ring>
  129. {};
  130. template <typename Polygon>
  131. struct for_each_range<Polygon, polygon_tag>
  132. : detail::for_each::fe_range_polygon<Polygon>
  133. {};
  134. template <typename Box>
  135. struct for_each_range<Box, box_tag>
  136. : detail::for_each::fe_range_box<Box>
  137. {};
  138. template <typename MultiPoint>
  139. struct for_each_range<MultiPoint, multi_point_tag>
  140. : detail::for_each::fe_range_range<MultiPoint>
  141. {};
  142. template <typename Geometry>
  143. struct for_each_range<Geometry, multi_linestring_tag>
  144. : detail::for_each::fe_range_multi
  145. <
  146. Geometry,
  147. detail::for_each::fe_range_range
  148. <
  149. util::transcribe_const_t
  150. <
  151. Geometry,
  152. typename boost::range_value<Geometry>::type
  153. >
  154. >
  155. >
  156. {};
  157. template <typename Geometry>
  158. struct for_each_range<Geometry, multi_polygon_tag>
  159. : detail::for_each::fe_range_multi
  160. <
  161. Geometry,
  162. detail::for_each::fe_range_polygon
  163. <
  164. util::transcribe_const_t
  165. <
  166. Geometry,
  167. typename boost::range_value<Geometry>::type
  168. >
  169. >
  170. >
  171. {};
  172. } // namespace dispatch
  173. #endif // DOXYGEN_NO_DISPATCH
  174. namespace detail
  175. {
  176. // Currently for Polygons p is checked only for exterior ring
  177. // Should this function be renamed?
  178. template <typename Geometry, typename UnaryPredicate>
  179. inline bool all_ranges_of(Geometry const& geometry, UnaryPredicate p)
  180. {
  181. return dispatch::for_each_range<Geometry const>::apply(geometry, p);
  182. }
  183. // Currently for Polygons p is checked only for exterior ring
  184. // Should this function be renamed?
  185. template <typename Geometry, typename UnaryPredicate>
  186. inline bool any_range_of(Geometry const& geometry, UnaryPredicate p)
  187. {
  188. return ! dispatch::for_each_range<Geometry const>::apply(geometry,
  189. [&](auto&& range)
  190. {
  191. return ! p(range);
  192. });
  193. }
  194. // Currently for Polygons p is checked only for exterior ring
  195. // Should this function be renamed?
  196. template <typename Geometry, typename UnaryPredicate>
  197. inline bool none_range_of(Geometry const& geometry, UnaryPredicate p)
  198. {
  199. return dispatch::for_each_range<Geometry const>::apply(geometry,
  200. [&](auto&& range)
  201. {
  202. return ! p(range);
  203. });
  204. }
  205. // Currently for Polygons f is called only for exterior ring
  206. // Should this function be renamed?
  207. template <typename Geometry, typename Functor>
  208. inline Functor for_each_range(Geometry const& geometry, Functor f)
  209. {
  210. dispatch::for_each_range<Geometry const>::apply(geometry,
  211. [&](auto&& range)
  212. {
  213. f(range);
  214. // TODO: Implement separate function?
  215. return true;
  216. });
  217. return f;
  218. }
  219. }
  220. }} // namespace boost::geometry
  221. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_FOR_EACH_RANGE_HPP