remove_spikes.hpp 9.5 KB

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