reverse.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2020.
  7. // Modifications copyright (c) 2020 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, 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_REVERSE_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_REVERSE_HPP
  16. #include <algorithm>
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/variant/apply_visitor.hpp>
  20. #include <boost/variant/static_visitor.hpp>
  21. #include <boost/variant/variant_fwd.hpp>
  22. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  23. #include <boost/geometry/algorithms/detail/multi_modify.hpp>
  24. #include <boost/geometry/core/interior_rings.hpp>
  25. #include <boost/geometry/core/tags.hpp>
  26. #include <boost/geometry/geometries/concepts/check.hpp>
  27. namespace boost { namespace geometry
  28. {
  29. #ifndef DOXYGEN_NO_DETAIL
  30. namespace detail { namespace reverse
  31. {
  32. struct range_reverse
  33. {
  34. template <typename Range>
  35. static inline void apply(Range& range)
  36. {
  37. std::reverse(boost::begin(range), boost::end(range));
  38. }
  39. };
  40. struct polygon_reverse: private range_reverse
  41. {
  42. template <typename Polygon>
  43. static inline void apply(Polygon& polygon)
  44. {
  45. range_reverse::apply(exterior_ring(polygon));
  46. typename interior_return_type<Polygon>::type
  47. rings = interior_rings(polygon);
  48. for (typename detail::interior_iterator<Polygon>::type
  49. it = boost::begin(rings); it != boost::end(rings); ++it)
  50. {
  51. range_reverse::apply(*it);
  52. }
  53. }
  54. };
  55. }} // namespace detail::reverse
  56. #endif // DOXYGEN_NO_DETAIL
  57. #ifndef DOXYGEN_NO_DISPATCH
  58. namespace dispatch
  59. {
  60. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  61. struct reverse
  62. {
  63. static inline void apply(Geometry&)
  64. {}
  65. };
  66. template <typename Ring>
  67. struct reverse<Ring, ring_tag>
  68. : detail::reverse::range_reverse
  69. {};
  70. template <typename LineString>
  71. struct reverse<LineString, linestring_tag>
  72. : detail::reverse::range_reverse
  73. {};
  74. template <typename Polygon>
  75. struct reverse<Polygon, polygon_tag>
  76. : detail::reverse::polygon_reverse
  77. {};
  78. template <typename Geometry>
  79. struct reverse<Geometry, multi_linestring_tag>
  80. : detail::multi_modify
  81. <
  82. Geometry,
  83. detail::reverse::range_reverse
  84. >
  85. {};
  86. template <typename Geometry>
  87. struct reverse<Geometry, multi_polygon_tag>
  88. : detail::multi_modify
  89. <
  90. Geometry,
  91. detail::reverse::polygon_reverse
  92. >
  93. {};
  94. } // namespace dispatch
  95. #endif
  96. namespace resolve_variant
  97. {
  98. template <typename Geometry>
  99. struct reverse
  100. {
  101. static void apply(Geometry& geometry)
  102. {
  103. concepts::check<Geometry>();
  104. dispatch::reverse<Geometry>::apply(geometry);
  105. }
  106. };
  107. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  108. struct reverse<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  109. {
  110. struct visitor: boost::static_visitor<void>
  111. {
  112. template <typename Geometry>
  113. void operator()(Geometry& geometry) const
  114. {
  115. reverse<Geometry>::apply(geometry);
  116. }
  117. };
  118. static inline void apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry)
  119. {
  120. boost::apply_visitor(visitor(), geometry);
  121. }
  122. };
  123. } // namespace resolve_variant
  124. /*!
  125. \brief Reverses the points within a geometry
  126. \details Generic function to reverse a geometry. It resembles the std::reverse
  127. functionality, but it takes the geometry type into account. Only for a ring
  128. or for a linestring it is the same as the std::reverse.
  129. \ingroup reverse
  130. \tparam Geometry \tparam_geometry
  131. \param geometry \param_geometry which will be reversed
  132. \qbk{[include reference/algorithms/reverse.qbk]}
  133. */
  134. template <typename Geometry>
  135. inline void reverse(Geometry& geometry)
  136. {
  137. resolve_variant::reverse<Geometry>::apply(geometry);
  138. }
  139. }} // namespace boost::geometry
  140. #endif // BOOST_GEOMETRY_ALGORITHMS_REVERSE_HPP