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