remove_spikes.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2013 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2013 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013-2023 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2017-2023.
  7. // Modifications copyright (c) 2017-2023 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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_REMOVE_SPIKES_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_REMOVE_SPIKES_HPP
  15. #include <boost/range/begin.hpp>
  16. #include <boost/range/end.hpp>
  17. #include <boost/range/size.hpp>
  18. #include <boost/variant/apply_visitor.hpp>
  19. #include <boost/variant/static_visitor.hpp>
  20. #include <boost/variant/variant_fwd.hpp>
  21. #include <boost/geometry/core/closure.hpp>
  22. #include <boost/geometry/core/cs.hpp>
  23. #include <boost/geometry/core/interior_rings.hpp>
  24. #include <boost/geometry/core/tags.hpp>
  25. #include <boost/geometry/geometries/concepts/check.hpp>
  26. #include <boost/geometry/algorithms/detail/point_is_spike_or_equal.hpp>
  27. #include <boost/geometry/algorithms/clear.hpp>
  28. #include <boost/geometry/strategies/default_strategy.hpp>
  29. #include <boost/geometry/util/constexpr.hpp>
  30. #include <boost/geometry/util/range.hpp>
  31. /*
  32. Remove spikes from a ring/polygon.
  33. Ring (having 8 vertices, including closing vertex)
  34. +------+
  35. | |
  36. | +--+
  37. | | ^this "spike" is removed, can be located outside/inside the ring
  38. +------+
  39. (the actualy determination if it is removed is done by a strategy)
  40. */
  41. namespace boost { namespace geometry
  42. {
  43. #ifndef DOXYGEN_NO_DETAIL
  44. namespace detail { namespace remove_spikes
  45. {
  46. struct range_remove_spikes
  47. {
  48. template <typename Range, typename SideStrategy>
  49. static inline void apply(Range& range, SideStrategy const& strategy)
  50. {
  51. typedef typename point_type<Range>::type point_type;
  52. std::size_t n = boost::size(range);
  53. std::size_t const min_num_points = core_detail::closure::minimum_ring_size
  54. <
  55. geometry::closure<Range>::value
  56. >::value - 1; // subtract one: a polygon with only one spike should result into one point
  57. if (n < min_num_points)
  58. {
  59. return;
  60. }
  61. std::vector<point_type> cleaned;
  62. cleaned.reserve(n);
  63. for (auto const& p : range)
  64. {
  65. // Add point
  66. cleaned.push_back(p);
  67. while(cleaned.size() >= 3
  68. && detail::is_spike_or_equal(range::at(cleaned, cleaned.size() - 3),
  69. range::at(cleaned, cleaned.size() - 2),
  70. range::back(cleaned),
  71. strategy))
  72. {
  73. // Remove pen-ultimate point causing the spike (or which was equal)
  74. cleaned.erase(cleaned.end() - 2);
  75. }
  76. }
  77. auto cleaned_b = cleaned.begin();
  78. auto cleaned_e = cleaned.end();
  79. std::size_t cleaned_count = cleaned.size();
  80. // For a closed-polygon, remove closing point, this makes checking first point(s) easier and consistent
  81. if BOOST_GEOMETRY_CONSTEXPR (geometry::closure<Range>::value == geometry::closed)
  82. {
  83. --cleaned_e;
  84. --cleaned_count;
  85. }
  86. bool found = false;
  87. do
  88. {
  89. found = false;
  90. // Check for spike in first point
  91. while(cleaned_count >= 3
  92. && detail::is_spike_or_equal(*(cleaned_e - 2), // prev
  93. *(cleaned_e - 1), // back
  94. *(cleaned_b), // front
  95. strategy))
  96. {
  97. --cleaned_e;
  98. --cleaned_count;
  99. found = true;
  100. }
  101. // Check for spike in second point
  102. while(cleaned_count >= 3
  103. && detail::is_spike_or_equal(*(cleaned_e - 1), // back
  104. *(cleaned_b), // front
  105. *(cleaned_b + 1), // next
  106. strategy))
  107. {
  108. ++cleaned_b;
  109. --cleaned_count;
  110. found = true;
  111. }
  112. }
  113. while (found);
  114. if (cleaned_count == 2)
  115. {
  116. // Ticket #9871: open polygon with only two points.
  117. // the second point forms, by definition, a spike
  118. --cleaned_e;
  119. //--cleaned_count;
  120. }
  121. // Close if necessary
  122. if BOOST_GEOMETRY_CONSTEXPR (geometry::closure<Range>::value == geometry::closed)
  123. {
  124. BOOST_GEOMETRY_ASSERT(cleaned_e != cleaned.end());
  125. *cleaned_e = *cleaned_b;
  126. ++cleaned_e;
  127. //++cleaned_count;
  128. }
  129. // Copy output
  130. geometry::clear(range);
  131. std::copy(cleaned_b, cleaned_e, range::back_inserter(range));
  132. }
  133. };
  134. struct polygon_remove_spikes
  135. {
  136. template <typename Polygon, typename SideStrategy>
  137. static inline void apply(Polygon& polygon, SideStrategy const& strategy)
  138. {
  139. typedef range_remove_spikes per_range;
  140. per_range::apply(exterior_ring(polygon), strategy);
  141. auto&& rings = interior_rings(polygon);
  142. for (auto it = boost::begin(rings); it != boost::end(rings); ++it)
  143. {
  144. per_range::apply(*it, strategy);
  145. }
  146. }
  147. };
  148. template <typename SingleVersion>
  149. struct multi_remove_spikes
  150. {
  151. template <typename MultiGeometry, typename SideStrategy>
  152. static inline void apply(MultiGeometry& multi, SideStrategy const& strategy)
  153. {
  154. for (auto it = boost::begin(multi); it != boost::end(multi); ++it)
  155. {
  156. SingleVersion::apply(*it, strategy);
  157. }
  158. }
  159. };
  160. }} // namespace detail::remove_spikes
  161. #endif // DOXYGEN_NO_DETAIL
  162. #ifndef DOXYGEN_NO_DISPATCH
  163. namespace dispatch
  164. {
  165. template
  166. <
  167. typename Geometry,
  168. typename Tag = typename tag<Geometry>::type
  169. >
  170. struct remove_spikes
  171. {
  172. template <typename SideStrategy>
  173. static inline void apply(Geometry&, SideStrategy const&)
  174. {}
  175. };
  176. template <typename Ring>
  177. struct remove_spikes<Ring, ring_tag>
  178. : detail::remove_spikes::range_remove_spikes
  179. {};
  180. template <typename Polygon>
  181. struct remove_spikes<Polygon, polygon_tag>
  182. : detail::remove_spikes::polygon_remove_spikes
  183. {};
  184. template <typename MultiPolygon>
  185. struct remove_spikes<MultiPolygon, multi_polygon_tag>
  186. : detail::remove_spikes::multi_remove_spikes
  187. <
  188. detail::remove_spikes::polygon_remove_spikes
  189. >
  190. {};
  191. } // namespace dispatch
  192. #endif
  193. namespace resolve_variant {
  194. template <typename Geometry>
  195. struct remove_spikes
  196. {
  197. template <typename Strategy>
  198. static void apply(Geometry& geometry, Strategy const& strategy)
  199. {
  200. concepts::check<Geometry>();
  201. dispatch::remove_spikes<Geometry>::apply(geometry, strategy);
  202. }
  203. static void apply(Geometry& geometry, geometry::default_strategy const&)
  204. {
  205. typedef typename strategy::side::services::default_strategy
  206. <
  207. typename cs_tag<Geometry>::type
  208. >::type side_strategy;
  209. apply(geometry, side_strategy());
  210. }
  211. };
  212. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  213. struct remove_spikes<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  214. {
  215. template <typename Strategy>
  216. struct visitor: boost::static_visitor<void>
  217. {
  218. Strategy const& m_strategy;
  219. visitor(Strategy const& strategy) : m_strategy(strategy) {}
  220. template <typename Geometry>
  221. void operator()(Geometry& geometry) const
  222. {
  223. remove_spikes<Geometry>::apply(geometry, m_strategy);
  224. }
  225. };
  226. template <typename Strategy>
  227. static inline void apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry,
  228. Strategy const& strategy)
  229. {
  230. boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  231. }
  232. };
  233. } // namespace resolve_variant
  234. /*!
  235. \ingroup remove_spikes
  236. \tparam Geometry geometry type
  237. \param geometry the geometry to make remove_spikes
  238. */
  239. template <typename Geometry>
  240. inline void remove_spikes(Geometry& geometry)
  241. {
  242. resolve_variant::remove_spikes<Geometry>::apply(geometry, geometry::default_strategy());
  243. }
  244. /*!
  245. \ingroup remove_spikes
  246. \tparam Geometry geometry type
  247. \tparam Strategy side strategy type
  248. \param geometry the geometry to make remove_spikes
  249. \param strategy the side strategy used by the algorithm
  250. */
  251. template <typename Geometry, typename Strategy>
  252. inline void remove_spikes(Geometry& geometry, Strategy const& strategy)
  253. {
  254. resolve_variant::remove_spikes<Geometry>::apply(geometry, strategy);
  255. }
  256. }} // namespace boost::geometry
  257. #endif // BOOST_GEOMETRY_ALGORITHMS_REMOVE_SPIKES_HPP