remove_spikes.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. std::size_t const n = boost::size(range);
  52. std::size_t const min_num_points = core_detail::closure::minimum_ring_size
  53. <
  54. geometry::closure<Range>::value
  55. >::value - 1; // subtract one: a polygon with only one spike should result into one point
  56. if (n < min_num_points)
  57. {
  58. return;
  59. }
  60. std::vector<point_type_t<Range>> cleaned;
  61. cleaned.reserve(n);
  62. for (auto const& p : range)
  63. {
  64. // Add point
  65. cleaned.push_back(p);
  66. while(cleaned.size() >= 3
  67. && detail::is_spike_or_equal(range::at(cleaned, cleaned.size() - 3),
  68. range::at(cleaned, cleaned.size() - 2),
  69. range::back(cleaned),
  70. strategy))
  71. {
  72. // Remove pen-ultimate point causing the spike (or which was equal)
  73. cleaned.erase(cleaned.end() - 2);
  74. }
  75. }
  76. auto cleaned_b = cleaned.begin();
  77. auto cleaned_e = cleaned.end();
  78. std::size_t cleaned_count = cleaned.size();
  79. // For a closed-polygon, remove closing point, this makes checking first point(s) easier and consistent
  80. if BOOST_GEOMETRY_CONSTEXPR (geometry::closure<Range>::value == geometry::closed)
  81. {
  82. --cleaned_e;
  83. --cleaned_count;
  84. }
  85. bool found = false;
  86. do
  87. {
  88. found = false;
  89. // Check for spike in first point
  90. while(cleaned_count >= 3
  91. && detail::is_spike_or_equal(*(cleaned_e - 2), // prev
  92. *(cleaned_e - 1), // back
  93. *(cleaned_b), // front
  94. strategy))
  95. {
  96. --cleaned_e;
  97. --cleaned_count;
  98. found = true;
  99. }
  100. // Check for spike in second point
  101. while(cleaned_count >= 3
  102. && detail::is_spike_or_equal(*(cleaned_e - 1), // back
  103. *(cleaned_b), // front
  104. *(cleaned_b + 1), // next
  105. strategy))
  106. {
  107. ++cleaned_b;
  108. --cleaned_count;
  109. found = true;
  110. }
  111. }
  112. while (found);
  113. if (cleaned_count == 2)
  114. {
  115. // Ticket #9871: open polygon with only two points.
  116. // the second point forms, by definition, a spike
  117. --cleaned_e;
  118. //--cleaned_count;
  119. }
  120. // Close if necessary
  121. if BOOST_GEOMETRY_CONSTEXPR (geometry::closure<Range>::value == geometry::closed)
  122. {
  123. BOOST_GEOMETRY_ASSERT(cleaned_e != cleaned.end());
  124. *cleaned_e = *cleaned_b;
  125. ++cleaned_e;
  126. //++cleaned_count;
  127. }
  128. // Copy output
  129. geometry::clear(range);
  130. std::copy(cleaned_b, cleaned_e, range::back_inserter(range));
  131. }
  132. };
  133. struct polygon_remove_spikes
  134. {
  135. template <typename Polygon, typename SideStrategy>
  136. static inline void apply(Polygon& polygon, SideStrategy const& strategy)
  137. {
  138. typedef range_remove_spikes per_range;
  139. per_range::apply(exterior_ring(polygon), strategy);
  140. auto&& rings = interior_rings(polygon);
  141. for (auto it = boost::begin(rings); it != boost::end(rings); ++it)
  142. {
  143. per_range::apply(*it, strategy);
  144. }
  145. }
  146. };
  147. template <typename SingleVersion>
  148. struct multi_remove_spikes
  149. {
  150. template <typename MultiGeometry, typename SideStrategy>
  151. static inline void apply(MultiGeometry& multi, SideStrategy const& strategy)
  152. {
  153. for (auto it = boost::begin(multi); it != boost::end(multi); ++it)
  154. {
  155. SingleVersion::apply(*it, strategy);
  156. }
  157. }
  158. };
  159. }} // namespace detail::remove_spikes
  160. #endif // DOXYGEN_NO_DETAIL
  161. #ifndef DOXYGEN_NO_DISPATCH
  162. namespace dispatch
  163. {
  164. template
  165. <
  166. typename Geometry,
  167. typename Tag = tag_t<Geometry>
  168. >
  169. struct remove_spikes
  170. {
  171. template <typename SideStrategy>
  172. static inline void apply(Geometry&, SideStrategy const&)
  173. {}
  174. };
  175. template <typename Ring>
  176. struct remove_spikes<Ring, ring_tag>
  177. : detail::remove_spikes::range_remove_spikes
  178. {};
  179. template <typename Polygon>
  180. struct remove_spikes<Polygon, polygon_tag>
  181. : detail::remove_spikes::polygon_remove_spikes
  182. {};
  183. template <typename MultiPolygon>
  184. struct remove_spikes<MultiPolygon, multi_polygon_tag>
  185. : detail::remove_spikes::multi_remove_spikes
  186. <
  187. detail::remove_spikes::polygon_remove_spikes
  188. >
  189. {};
  190. } // namespace dispatch
  191. #endif
  192. namespace resolve_variant {
  193. template <typename Geometry>
  194. struct remove_spikes
  195. {
  196. template <typename Strategy>
  197. static void apply(Geometry& geometry, Strategy const& strategy)
  198. {
  199. concepts::check<Geometry>();
  200. dispatch::remove_spikes<Geometry>::apply(geometry, strategy);
  201. }
  202. static void apply(Geometry& geometry, geometry::default_strategy const&)
  203. {
  204. using side_strategy = typename strategy::side::services::default_strategy
  205. <
  206. cs_tag_t<Geometry>
  207. >::type;
  208. apply(geometry, side_strategy());
  209. }
  210. };
  211. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  212. struct remove_spikes<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  213. {
  214. template <typename Strategy>
  215. struct visitor: boost::static_visitor<void>
  216. {
  217. Strategy const& m_strategy;
  218. visitor(Strategy const& strategy) : m_strategy(strategy) {}
  219. template <typename Geometry>
  220. void operator()(Geometry& geometry) const
  221. {
  222. remove_spikes<Geometry>::apply(geometry, m_strategy);
  223. }
  224. };
  225. template <typename Strategy>
  226. static inline void apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry,
  227. Strategy const& strategy)
  228. {
  229. boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  230. }
  231. };
  232. } // namespace resolve_variant
  233. /*!
  234. \ingroup remove_spikes
  235. \tparam Geometry geometry type
  236. \param geometry the geometry to make remove_spikes
  237. */
  238. template <typename Geometry>
  239. inline void remove_spikes(Geometry& geometry)
  240. {
  241. resolve_variant::remove_spikes<Geometry>::apply(geometry, geometry::default_strategy());
  242. }
  243. /*!
  244. \ingroup remove_spikes
  245. \tparam Geometry geometry type
  246. \tparam Strategy side strategy type
  247. \param geometry the geometry to make remove_spikes
  248. \param strategy the side strategy used by the algorithm
  249. */
  250. template <typename Geometry, typename Strategy>
  251. inline void remove_spikes(Geometry& geometry, Strategy const& strategy)
  252. {
  253. resolve_variant::remove_spikes<Geometry>::apply(geometry, strategy);
  254. }
  255. }} // namespace boost::geometry
  256. #endif // BOOST_GEOMETRY_ALGORITHMS_REMOVE_SPIKES_HPP