1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2014.
6 // Modifications copyright (c) 2014 Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP
17 #include <boost/range.hpp>
19 #include <boost/geometry/algorithms/append.hpp>
20 #include <boost/geometry/algorithms/detail/point_is_spike_or_equal.hpp>
21 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
23 #include <boost/geometry/util/condition.hpp>
24 #include <boost/geometry/util/range.hpp>
27 namespace boost { namespace geometry
31 #ifndef DOXYGEN_NO_DETAIL
32 namespace detail { namespace overlay
35 // TODO: move this / rename this
36 template <typename Point1, typename Point2, typename RobustPolicy>
37 inline bool points_equal_or_close(Point1 const& point1,
39 RobustPolicy const& robust_policy)
41 if (detail::equals::equals_point_point(point1, point2))
46 if (BOOST_GEOMETRY_CONDITION(! RobustPolicy::enabled))
51 // Try using specified robust policy
52 typedef typename geometry::robust_point_type
56 >::type robust_point_type;
58 robust_point_type point1_rob, point2_rob;
59 geometry::recalculate(point1_rob, point1, robust_policy);
60 geometry::recalculate(point2_rob, point2, robust_policy);
62 return detail::equals::equals_point_point(point1_rob, point2_rob);
66 template <typename Range, typename Point, typename RobustPolicy>
67 inline void append_no_dups_or_spikes(Range& range, Point const& point,
68 RobustPolicy const& robust_policy)
70 #ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
71 std::cout << " add: ("
72 << geometry::get<0>(point) << ", " << geometry::get<1>(point) << ")"
75 // The code below this condition checks all spikes/dups
76 // for geometries >= 3 points.
77 // So we have to check the first potential duplicate differently
78 if (boost::size(range) == 1
79 && points_equal_or_close(*(boost::begin(range)), point, robust_policy))
84 traits::push_back<Range>::apply(range, point);
86 // If a point is equal, or forming a spike, remove the pen-ultimate point
87 // because this one caused the spike.
88 // If so, the now-new-pen-ultimate point can again cause a spike
89 // (possibly at a corner). So keep doing this.
90 // Besides spikes it will also avoid adding duplicates.
91 while(boost::size(range) >= 3
92 && point_is_spike_or_equal(point,
93 *(boost::end(range) - 3),
94 *(boost::end(range) - 2),
97 // Use the Concept/traits, so resize and append again
98 traits::resize<Range>::apply(range, boost::size(range) - 2);
99 traits::push_back<Range>::apply(range, point);
103 template <typename Range, typename RobustPolicy>
104 inline void clean_closing_dups_and_spikes(Range& range,
105 RobustPolicy const& robust_policy)
107 std::size_t const minsize
108 = core_detail::closure::minimum_ring_size
110 geometry::closure<Range>::value
113 if (boost::size(range) <= minsize)
118 typedef typename boost::range_iterator<Range>::type iterator_type;
119 static bool const closed = geometry::closure<Range>::value == geometry::closed;
121 // TODO: the following algorithm could be rewritten to first look for spikes
122 // and then erase some number of points from the beginning of the Range
128 iterator_type first = boost::begin(range);
129 iterator_type second = first + 1;
130 iterator_type ultimate = boost::end(range) - 1;
131 if (BOOST_GEOMETRY_CONDITION(closed))
136 // Check if closing point is a spike (this is so if the second point is
137 // considered as a spike w.r.t. the last segment)
138 if (point_is_spike_or_equal(*second, *ultimate, *first, robust_policy))
140 range::erase(range, first);
141 if (BOOST_GEOMETRY_CONDITION(closed))
143 // Remove closing last point
144 range::resize(range, boost::size(range) - 1);
145 // Add new closing point
146 range::push_back(range, range::front(range));
150 } while(found && boost::size(range) > minsize);
154 }} // namespace detail::overlay
155 #endif // DOXYGEN_NO_DETAIL
158 }} // namespace boost::geometry
161 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP