remove_spikes.hpp 9.6 KB

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