Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / geometry / algorithms / detail / overlay / append_no_dups_or_spikes.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4
5 // This file was modified by Oracle on 2014.
6 // Modifications copyright (c) 2014 Oracle and/or its affiliates.
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
17 #include <boost/range.hpp>
18
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>
22
23 #include <boost/geometry/util/range.hpp>
24
25
26 namespace boost { namespace geometry
27 {
28
29
30 #ifndef DOXYGEN_NO_DETAIL
31 namespace detail { namespace overlay
32 {
33
34 // TODO: move this / rename this
35 template <typename Point1, typename Point2, typename RobustPolicy>
36 inline bool points_equal_or_close(Point1 const& point1,
37         Point2 const& point2,
38         RobustPolicy const& robust_policy)
39 {
40     if (detail::equals::equals_point_point(point1, point2))
41     {
42         return true;
43     }
44
45     if (! RobustPolicy::enabled)
46     {
47         return false;
48     }
49
50     // Try using specified robust policy
51     typedef typename geometry::robust_point_type
52     <
53         Point1,
54         RobustPolicy
55     >::type robust_point_type;
56
57     robust_point_type point1_rob, point2_rob;
58     geometry::recalculate(point1_rob, point1, robust_policy);
59     geometry::recalculate(point2_rob, point2, robust_policy);
60
61     return detail::equals::equals_point_point(point1_rob, point2_rob);
62 }
63
64
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)
68 {
69 #ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
70     std::cout << "  add: ("
71         << geometry::get<0>(point) << ", " << geometry::get<1>(point) << ")"
72         << std::endl;
73 #endif
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))
79     {
80         return;
81     }
82
83     traits::push_back<Range>::apply(range, point);
84
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),
94                 robust_policy))
95     {
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);
99     }
100 }
101
102 template <typename Range, typename RobustPolicy>
103 inline void clean_closing_dups_and_spikes(Range& range,
104                 RobustPolicy const& robust_policy)
105 {
106     std::size_t const minsize
107         = core_detail::closure::minimum_ring_size
108             <
109                 geometry::closure<Range>::value
110             >::value;
111
112     if (boost::size(range) <= minsize)
113     {
114         return;
115     }
116
117     typedef typename boost::range_iterator<Range>::type iterator_type;
118     static bool const closed = geometry::closure<Range>::value == geometry::closed;
119
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
122
123     bool found = false;
124     do
125     {
126         found = false;
127         iterator_type first = boost::begin(range);
128         iterator_type second = first + 1;
129         iterator_type ultimate = boost::end(range) - 1;
130         if (closed)
131         {
132             ultimate--;
133         }
134
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))
138         {
139             range::erase(range, first);
140             if (closed)
141             {
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));
146             }
147             found = true;
148         }
149     } while(found && boost::size(range) > minsize);
150 }
151
152
153 }} // namespace detail::overlay
154 #endif // DOXYGEN_NO_DETAIL
155
156
157 }} // namespace boost::geometry
158
159
160 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP