1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2013 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2013 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
6 // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2017.
9 // Modifications copyright (c) 2017 Oracle and/or its affiliates.
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
13 // Use, modification and distribution is subject to the Boost Software License,
14 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 #ifndef BOOST_GEOMETRY_ALGORITHMS_REMOVE_SPIKES_HPP
18 #define BOOST_GEOMETRY_ALGORITHMS_REMOVE_SPIKES_HPP
22 #include <boost/range.hpp>
23 #include <boost/type_traits/remove_reference.hpp>
25 #include <boost/variant/apply_visitor.hpp>
26 #include <boost/variant/static_visitor.hpp>
27 #include <boost/variant/variant_fwd.hpp>
29 #include <boost/geometry/core/closure.hpp>
30 #include <boost/geometry/core/coordinate_type.hpp>
31 #include <boost/geometry/core/cs.hpp>
32 #include <boost/geometry/core/interior_rings.hpp>
33 #include <boost/geometry/core/point_order.hpp>
34 #include <boost/geometry/core/tags.hpp>
36 #include <boost/geometry/geometries/concepts/check.hpp>
38 #include <boost/geometry/algorithms/detail/point_is_spike_or_equal.hpp>
39 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
40 #include <boost/geometry/algorithms/clear.hpp>
42 #include <boost/geometry/strategies/default_strategy.hpp>
44 #include <boost/geometry/util/condition.hpp>
48 Remove spikes from a ring/polygon.
49 Ring (having 8 vertices, including closing vertex)
53 | | ^this "spike" is removed, can be located outside/inside the ring
55 (the actualy determination if it is removed is done by a strategy)
60 namespace boost { namespace geometry
64 #ifndef DOXYGEN_NO_DETAIL
65 namespace detail { namespace remove_spikes
69 struct range_remove_spikes
71 template <typename Range, typename SideStrategy>
72 static inline void apply(Range& range, SideStrategy const& strategy)
74 typedef typename point_type<Range>::type point_type;
76 std::size_t n = boost::size(range);
77 std::size_t const min_num_points = core_detail::closure::minimum_ring_size
79 geometry::closure<Range>::value
80 >::value - 1; // subtract one: a polygon with only one spike should result into one point
81 if (n < min_num_points)
86 std::deque<point_type> cleaned;
87 for (typename boost::range_iterator<Range const>::type it = boost::begin(range);
88 it != boost::end(range); ++it)
91 cleaned.push_back(*it);
93 while(cleaned.size() >= 3
94 && detail::point_is_spike_or_equal(cleaned.back(),
99 // Remove pen-ultimate point causing the spike (or which was equal)
100 cleaned.erase(cleaned.end() - 2);
104 // For a closed-polygon, remove closing point, this makes checking first point(s) easier and consistent
105 if ( BOOST_GEOMETRY_CONDITION(geometry::closure<Range>::value == geometry::closed) )
114 // Check for spike in first point
115 int const penultimate = 2;
116 while(cleaned.size() >= 3
117 && detail::point_is_spike_or_equal(cleaned.front(),
118 *(cleaned.end() - penultimate),
125 // Check for spike in second point
126 while(cleaned.size() >= 3
127 && detail::point_is_spike_or_equal(*(cleaned.begin() + 1),
138 if (cleaned.size() == 2)
140 // Ticket #9871: open polygon with only two points.
141 // the second point forms, by definition, a spike
145 // Close if necessary
146 if ( BOOST_GEOMETRY_CONDITION(geometry::closure<Range>::value == geometry::closed) )
148 cleaned.push_back(cleaned.front());
152 geometry::clear(range);
153 std::copy(cleaned.begin(), cleaned.end(), range::back_inserter(range));
158 struct polygon_remove_spikes
160 template <typename Polygon, typename SideStrategy>
161 static inline void apply(Polygon& polygon, SideStrategy const& strategy)
163 typedef range_remove_spikes per_range;
164 per_range::apply(exterior_ring(polygon), strategy);
166 typename interior_return_type<Polygon>::type
167 rings = interior_rings(polygon);
169 for (typename detail::interior_iterator<Polygon>::type
170 it = boost::begin(rings); it != boost::end(rings); ++it)
172 per_range::apply(*it, strategy);
178 template <typename SingleVersion>
179 struct multi_remove_spikes
181 template <typename MultiGeometry, typename SideStrategy>
182 static inline void apply(MultiGeometry& multi, SideStrategy const& strategy)
184 for (typename boost::range_iterator<MultiGeometry>::type
185 it = boost::begin(multi);
186 it != boost::end(multi);
189 SingleVersion::apply(*it, strategy);
195 }} // namespace detail::remove_spikes
196 #endif // DOXYGEN_NO_DETAIL
200 #ifndef DOXYGEN_NO_DISPATCH
208 typename Tag = typename tag<Geometry>::type
212 template <typename SideStrategy>
213 static inline void apply(Geometry&, SideStrategy const&)
218 template <typename Ring>
219 struct remove_spikes<Ring, ring_tag>
220 : detail::remove_spikes::range_remove_spikes
225 template <typename Polygon>
226 struct remove_spikes<Polygon, polygon_tag>
227 : detail::remove_spikes::polygon_remove_spikes
231 template <typename MultiPolygon>
232 struct remove_spikes<MultiPolygon, multi_polygon_tag>
233 : detail::remove_spikes::multi_remove_spikes
235 detail::remove_spikes::polygon_remove_spikes
240 } // namespace dispatch
244 namespace resolve_variant {
246 template <typename Geometry>
249 template <typename Strategy>
250 static void apply(Geometry& geometry, Strategy const& strategy)
252 concepts::check<Geometry>();
253 dispatch::remove_spikes<Geometry>::apply(geometry, strategy);
256 static void apply(Geometry& geometry, geometry::default_strategy const&)
258 typedef typename strategy::side::services::default_strategy
260 typename cs_tag<Geometry>::type
261 >::type side_strategy;
263 apply(geometry, side_strategy());
267 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
268 struct remove_spikes<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
270 template <typename Strategy>
271 struct visitor: boost::static_visitor<void>
273 Strategy const& m_strategy;
275 visitor(Strategy const& strategy) : m_strategy(strategy) {}
277 template <typename Geometry>
278 void operator()(Geometry& geometry) const
280 remove_spikes<Geometry>::apply(geometry, m_strategy);
284 template <typename Strategy>
285 static inline void apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry,
286 Strategy const& strategy)
288 boost::apply_visitor(visitor<Strategy>(strategy), geometry);
292 } // namespace resolve_variant
296 \ingroup remove_spikes
297 \tparam Geometry geometry type
298 \param geometry the geometry to make remove_spikes
300 template <typename Geometry>
301 inline void remove_spikes(Geometry& geometry)
303 resolve_variant::remove_spikes<Geometry>::apply(geometry, geometry::default_strategy());
307 \ingroup remove_spikes
308 \tparam Geometry geometry type
309 \tparam Strategy side strategy type
310 \param geometry the geometry to make remove_spikes
311 \param strategy the side strategy used by the algorithm
313 template <typename Geometry, typename Strategy>
314 inline void remove_spikes(Geometry& geometry, Strategy const& strategy)
316 resolve_variant::remove_spikes<Geometry>::apply(geometry, strategy);
320 }} // namespace boost::geometry
323 #endif // BOOST_GEOMETRY_ALGORITHMS_REMOVE_SPIKES_HPP