clear.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_CLEAR_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_CLEAR_HPP
  12. #include <boost/type_traits/remove_const.hpp>
  13. #include <boost/variant/apply_visitor.hpp>
  14. #include <boost/variant/static_visitor.hpp>
  15. #include <boost/variant/variant_fwd.hpp>
  16. #include <boost/geometry/algorithms/not_implemented.hpp>
  17. #include <boost/geometry/core/access.hpp>
  18. #include <boost/geometry/core/exterior_ring.hpp>
  19. #include <boost/geometry/core/interior_rings.hpp>
  20. #include <boost/geometry/core/mutable_range.hpp>
  21. #include <boost/geometry/core/tag_cast.hpp>
  22. #include <boost/geometry/core/tags.hpp>
  23. #include <boost/geometry/geometries/concepts/check.hpp>
  24. namespace boost { namespace geometry
  25. {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail { namespace clear
  28. {
  29. template <typename Geometry>
  30. struct collection_clear
  31. {
  32. static inline void apply(Geometry& geometry)
  33. {
  34. traits::clear<Geometry>::apply(geometry);
  35. }
  36. };
  37. template <typename Polygon>
  38. struct polygon_clear
  39. {
  40. static inline void apply(Polygon& polygon)
  41. {
  42. traits::clear
  43. <
  44. typename boost::remove_reference
  45. <
  46. typename traits::interior_mutable_type<Polygon>::type
  47. >::type
  48. >::apply(interior_rings(polygon));
  49. traits::clear
  50. <
  51. typename boost::remove_reference
  52. <
  53. typename traits::ring_mutable_type<Polygon>::type
  54. >::type
  55. >::apply(exterior_ring(polygon));
  56. }
  57. };
  58. template <typename Geometry>
  59. struct no_action
  60. {
  61. static inline void apply(Geometry& )
  62. {
  63. }
  64. };
  65. }} // namespace detail::clear
  66. #endif // DOXYGEN_NO_DETAIL
  67. #ifndef DOXYGEN_NO_DISPATCH
  68. namespace dispatch
  69. {
  70. template
  71. <
  72. typename Geometry,
  73. typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
  74. >
  75. struct clear: not_implemented<Tag>
  76. {};
  77. // Point/box/segment do not have clear. So specialize to do nothing.
  78. template <typename Geometry>
  79. struct clear<Geometry, point_tag>
  80. : detail::clear::no_action<Geometry>
  81. {};
  82. template <typename Geometry>
  83. struct clear<Geometry, box_tag>
  84. : detail::clear::no_action<Geometry>
  85. {};
  86. template <typename Geometry>
  87. struct clear<Geometry, segment_tag>
  88. : detail::clear::no_action<Geometry>
  89. {};
  90. template <typename Geometry>
  91. struct clear<Geometry, linestring_tag>
  92. : detail::clear::collection_clear<Geometry>
  93. {};
  94. template <typename Geometry>
  95. struct clear<Geometry, ring_tag>
  96. : detail::clear::collection_clear<Geometry>
  97. {};
  98. // Polygon can (indirectly) use std for clear
  99. template <typename Polygon>
  100. struct clear<Polygon, polygon_tag>
  101. : detail::clear::polygon_clear<Polygon>
  102. {};
  103. template <typename Geometry>
  104. struct clear<Geometry, multi_tag>
  105. : detail::clear::collection_clear<Geometry>
  106. {};
  107. } // namespace dispatch
  108. #endif // DOXYGEN_NO_DISPATCH
  109. namespace resolve_variant {
  110. template <typename Geometry>
  111. struct clear
  112. {
  113. static inline void apply(Geometry& geometry)
  114. {
  115. dispatch::clear<Geometry>::apply(geometry);
  116. }
  117. };
  118. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  119. struct clear<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  120. {
  121. struct visitor: static_visitor<void>
  122. {
  123. template <typename Geometry>
  124. inline void operator()(Geometry& geometry) const
  125. {
  126. clear<Geometry>::apply(geometry);
  127. }
  128. };
  129. static inline void apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry)
  130. {
  131. boost::apply_visitor(visitor(), geometry);
  132. }
  133. };
  134. } // namespace resolve_variant
  135. /*!
  136. \brief Clears a linestring, ring or polygon (exterior+interiors) or multi*
  137. \details Generic function to clear a geometry. All points will be removed from the collection or collections
  138. making up the geometry. In most cases this is equivalent to the .clear() method of a std::vector<...>. In
  139. the case of a polygon, this clear functionality is automatically called for the exterior ring, and for the
  140. interior ring collection. In the case of a point, boxes and segments, nothing will happen.
  141. \ingroup clear
  142. \tparam Geometry \tparam_geometry
  143. \param geometry \param_geometry which will be cleared
  144. \note points and boxes cannot be cleared, instead they can be set to zero by "assign_zero"
  145. \qbk{[include reference/algorithms/clear.qbk]}
  146. */
  147. template <typename Geometry>
  148. inline void clear(Geometry& geometry)
  149. {
  150. concepts::check<Geometry>();
  151. resolve_variant::clear<Geometry>::apply(geometry);
  152. }
  153. }} // namespace boost::geometry
  154. #endif // BOOST_GEOMETRY_ALGORITHMS_CLEAR_HPP