remove_spikes.hpp 9.0 KB

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