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/range.hpp>
26 namespace boost { namespace geometry
30 #ifndef DOXYGEN_NO_DETAIL
31 namespace detail { namespace overlay
34 // TODO: move this / rename this
35 template <typename Point1, typename Point2, typename RobustPolicy>
36 inline bool points_equal_or_close(Point1 const& point1,
38 RobustPolicy const& robust_policy)
40 if (detail::equals::equals_point_point(point1, point2))
45 if (! RobustPolicy::enabled)
50 // Try using specified robust policy
51 typedef typename geometry::robust_point_type
55 >::type robust_point_type;
57 robust_point_type point1_rob, point2_rob;
58 geometry::recalculate(point1_rob, point1, robust_policy);
59 geometry::recalculate(point2_rob, point2, robust_policy);
61 return detail::equals::equals_point_point(point1_rob, point2_rob);
65 template <typename Range, typename Point, typename RobustPolicy>
66 inline void append_no_dups_or_spikes(Range& range, Point const& point,
67 RobustPolicy const& robust_policy)
69 #ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
70 std::cout << " add: ("
71 << geometry::get<0>(point) << ", " << geometry::get<1>(point) << ")"
74 // The code below thies condition checks all spikes/dups
75 // for geometries >= 3 points.
76 // So we have to check the first potential duplicate differently
77 if (boost::size(range) == 1
78 && points_equal_or_close(*(boost::begin(range)), point, robust_policy))
83 traits::push_back<Range>::apply(range, point);
85 // If a point is equal, or forming a spike, remove the pen-ultimate point
86 // because this one caused the spike.
87 // If so, the now-new-pen-ultimate point can again cause a spike
88 // (possibly at a corner). So keep doing this.
89 // Besides spikes it will also avoid adding duplicates.
90 while(boost::size(range) >= 3
91 && point_is_spike_or_equal(point,
92 *(boost::end(range) - 3),
93 *(boost::end(range) - 2),
96 // Use the Concept/traits, so resize and append again
97 traits::resize<Range>::apply(range, boost::size(range) - 2);
98 traits::push_back<Range>::apply(range, point);
102 template <typename Range, typename RobustPolicy>
103 inline void clean_closing_dups_and_spikes(Range& range,
104 RobustPolicy const& robust_policy)
106 std::size_t const minsize
107 = core_detail::closure::minimum_ring_size
109 geometry::closure<Range>::value
112 if (boost::size(range) <= minsize)
117 typedef typename boost::range_iterator<Range>::type iterator_type;
118 static bool const closed = geometry::closure<Range>::value == geometry::closed;
120 // TODO: the following algorithm could be rewritten to first look for spikes
121 // and then erase some number of points from the beginning of the Range
127 iterator_type first = boost::begin(range);
128 iterator_type second = first + 1;
129 iterator_type ultimate = boost::end(range) - 1;
135 // Check if closing point is a spike (this is so if the second point is
136 // considered as a spike w.r.t. the last segment)
137 if (point_is_spike_or_equal(*second, *ultimate, *first, robust_policy))
139 range::erase(range, first);
142 // Remove closing last point
143 range::resize(range, boost::size(range) - 1);
144 // Add new closing point
145 range::push_back(range, range::front(range));
149 } while(found && boost::size(range) > minsize);
153 }} // namespace detail::overlay
154 #endif // DOXYGEN_NO_DETAIL
157 }} // namespace boost::geometry
160 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP