]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/geometry/algorithms/detail/overlay/append_no_dups_or_spikes.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / geometry / algorithms / detail / overlay / append_no_dups_or_spikes.hpp
CommitLineData
7c673cae
FG
1// Boost.Geometry (aka GGL, Generic Geometry Library)
2
3// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4
20effc67
TL
5// This file was modified by Oracle on 2014-2020.
6// Modifications copyright (c) 2014-2020 Oracle and/or its affiliates.
7c673cae
FG
7
8// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9
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)
13
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
16
20effc67
TL
17
18#include <type_traits>
19
20#include <boost/range/begin.hpp>
21#include <boost/range/end.hpp>
22#include <boost/range/size.hpp>
92f5a8d4 23#include <boost/static_assert.hpp>
7c673cae
FG
24
25#include <boost/geometry/algorithms/append.hpp>
26#include <boost/geometry/algorithms/detail/point_is_spike_or_equal.hpp>
27#include <boost/geometry/algorithms/detail/equals/point_point.hpp>
28
92f5a8d4
TL
29#include <boost/geometry/core/closure.hpp>
30
7c673cae
FG
31#include <boost/geometry/util/condition.hpp>
32#include <boost/geometry/util/range.hpp>
33
34
35namespace boost { namespace geometry
36{
37
38
39#ifndef DOXYGEN_NO_DETAIL
40namespace detail { namespace overlay
41{
42
43// TODO: move this / rename this
1e59de90 44template <typename Point1, typename Point2, typename Strategy, typename RobustPolicy>
7c673cae 45inline bool points_equal_or_close(Point1 const& point1,
1e59de90
TL
46 Point2 const& point2,
47 Strategy const& strategy,
48 RobustPolicy const& robust_policy)
7c673cae 49{
92f5a8d4 50 if (detail::equals::equals_point_point(point1, point2, strategy))
7c673cae
FG
51 {
52 return true;
53 }
54
55 if (BOOST_GEOMETRY_CONDITION(! RobustPolicy::enabled))
56 {
57 return false;
58 }
59
60 // Try using specified robust policy
61 typedef typename geometry::robust_point_type
62 <
63 Point1,
64 RobustPolicy
65 >::type robust_point_type;
66
67 robust_point_type point1_rob, point2_rob;
68 geometry::recalculate(point1_rob, point1, robust_policy);
69 geometry::recalculate(point2_rob, point2, robust_policy);
70
92f5a8d4 71 // Only if this is the case the same strategy can be used.
20effc67 72 BOOST_STATIC_ASSERT((std::is_same
92f5a8d4
TL
73 <
74 typename geometry::cs_tag<Point1>::type,
75 typename geometry::cs_tag<robust_point_type>::type
76 >::value));
77
78 return detail::equals::equals_point_point(point1_rob, point2_rob, strategy);
7c673cae
FG
79}
80
81
1e59de90 82template <typename Range, typename Point, typename Strategy, typename RobustPolicy>
7c673cae 83inline void append_no_dups_or_spikes(Range& range, Point const& point,
1e59de90
TL
84 Strategy const& strategy,
85 RobustPolicy const& robust_policy)
7c673cae
FG
86{
87#ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
88 std::cout << " add: ("
89 << geometry::get<0>(point) << ", " << geometry::get<1>(point) << ")"
90 << std::endl;
91#endif
92 // The code below this condition checks all spikes/dups
93 // for geometries >= 3 points.
94 // So we have to check the first potential duplicate differently
92f5a8d4 95 if ( boost::size(range) == 1
1e59de90 96 && points_equal_or_close(*(boost::begin(range)), point, strategy,
92f5a8d4 97 robust_policy) )
7c673cae
FG
98 {
99 return;
100 }
101
102 traits::push_back<Range>::apply(range, point);
103
104 // If a point is equal, or forming a spike, remove the pen-ultimate point
105 // because this one caused the spike.
106 // If so, the now-new-pen-ultimate point can again cause a spike
107 // (possibly at a corner). So keep doing this.
108 // Besides spikes it will also avoid adding duplicates.
109 while(boost::size(range) >= 3
110 && point_is_spike_or_equal(point,
111 *(boost::end(range) - 3),
112 *(boost::end(range) - 2),
1e59de90 113 strategy.side(), // TODO: Pass strategy?
7c673cae
FG
114 robust_policy))
115 {
116 // Use the Concept/traits, so resize and append again
117 traits::resize<Range>::apply(range, boost::size(range) - 2);
118 traits::push_back<Range>::apply(range, point);
119 }
120}
121
1e59de90 122template <typename Range, typename Point, typename Strategy, typename RobustPolicy>
11fdf7f2 123inline void append_no_collinear(Range& range, Point const& point,
1e59de90
TL
124 Strategy const& strategy,
125 RobustPolicy const& robust_policy)
11fdf7f2
TL
126{
127 // Stricter version, not allowing any point in a linear row
128 // (spike, continuation or same point)
129
130 // The code below this condition checks all spikes/dups
131 // for geometries >= 3 points.
132 // So we have to check the first potential duplicate differently
92f5a8d4
TL
133 if ( boost::size(range) == 1
134 && points_equal_or_close(*(boost::begin(range)), point,
1e59de90 135 strategy,
92f5a8d4 136 robust_policy) )
11fdf7f2
TL
137 {
138 return;
139 }
140
141 traits::push_back<Range>::apply(range, point);
142
143 // If a point is equal, or forming a spike, remove the pen-ultimate point
144 // because this one caused the spike.
145 // If so, the now-new-pen-ultimate point can again cause a spike
146 // (possibly at a corner). So keep doing this.
147 // Besides spikes it will also avoid adding duplicates.
148 while(boost::size(range) >= 3
149 && point_is_collinear(point,
150 *(boost::end(range) - 3),
151 *(boost::end(range) - 2),
1e59de90 152 strategy.side(), // TODO: Pass strategy?
11fdf7f2
TL
153 robust_policy))
154 {
155 // Use the Concept/traits, so resize and append again
156 traits::resize<Range>::apply(range, boost::size(range) - 2);
157 traits::push_back<Range>::apply(range, point);
158 }
159}
160
1e59de90 161template <typename Range, typename Strategy, typename RobustPolicy>
7c673cae 162inline void clean_closing_dups_and_spikes(Range& range,
1e59de90
TL
163 Strategy const& strategy,
164 RobustPolicy const& robust_policy)
7c673cae
FG
165{
166 std::size_t const minsize
167 = core_detail::closure::minimum_ring_size
168 <
169 geometry::closure<Range>::value
170 >::value;
171
172 if (boost::size(range) <= minsize)
173 {
174 return;
175 }
176
177 typedef typename boost::range_iterator<Range>::type iterator_type;
178 static bool const closed = geometry::closure<Range>::value == geometry::closed;
179
180// TODO: the following algorithm could be rewritten to first look for spikes
181// and then erase some number of points from the beginning of the Range
182
183 bool found = false;
184 do
185 {
186 found = false;
187 iterator_type first = boost::begin(range);
188 iterator_type second = first + 1;
189 iterator_type ultimate = boost::end(range) - 1;
190 if (BOOST_GEOMETRY_CONDITION(closed))
191 {
192 ultimate--;
193 }
194
195 // Check if closing point is a spike (this is so if the second point is
11fdf7f2 196 // considered as collinear w.r.t. the last segment)
1e59de90
TL
197 if (point_is_collinear(*second, *ultimate, *first,
198 strategy.side(), // TODO: Pass strategy?
199 robust_policy))
7c673cae
FG
200 {
201 range::erase(range, first);
202 if (BOOST_GEOMETRY_CONDITION(closed))
203 {
204 // Remove closing last point
205 range::resize(range, boost::size(range) - 1);
206 // Add new closing point
207 range::push_back(range, range::front(range));
208 }
209 found = true;
210 }
211 } while(found && boost::size(range) > minsize);
212}
213
214
215}} // namespace detail::overlay
216#endif // DOXYGEN_NO_DETAIL
217
218
219}} // namespace boost::geometry
220
221
222#endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP