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, 2017.
6 // Modifications copyright (c) 2014-2017 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/condition.hpp>
24 #include <boost/geometry/util/range.hpp>
25 
26 
27 namespace boost { namespace geometry
28 {
29 
30 
31 #ifndef DOXYGEN_NO_DETAIL
32 namespace detail { namespace overlay
33 {
34 
35 // TODO: move this / rename this
36 template <typename Point1, typename Point2, typename RobustPolicy>
points_equal_or_close(Point1 const & point1,Point2 const & point2,RobustPolicy const & robust_policy)37 inline bool points_equal_or_close(Point1 const& point1,
38         Point2 const& point2,
39         RobustPolicy const& robust_policy)
40 {
41     if (detail::equals::equals_point_point(point1, point2))
42     {
43         return true;
44     }
45 
46     if (BOOST_GEOMETRY_CONDITION(! RobustPolicy::enabled))
47     {
48         return false;
49     }
50 
51     // Try using specified robust policy
52     typedef typename geometry::robust_point_type
53     <
54         Point1,
55         RobustPolicy
56     >::type robust_point_type;
57 
58     robust_point_type point1_rob, point2_rob;
59     geometry::recalculate(point1_rob, point1, robust_policy);
60     geometry::recalculate(point2_rob, point2, robust_policy);
61 
62     return detail::equals::equals_point_point(point1_rob, point2_rob);
63 }
64 
65 
66 template <typename Range, typename Point, typename SideStrategy, typename RobustPolicy>
append_no_dups_or_spikes(Range & range,Point const & point,SideStrategy const & strategy,RobustPolicy const & robust_policy)67 inline void append_no_dups_or_spikes(Range& range, Point const& point,
68         SideStrategy const& strategy,
69         RobustPolicy const& robust_policy)
70 {
71 #ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
72     std::cout << "  add: ("
73         << geometry::get<0>(point) << ", " << geometry::get<1>(point) << ")"
74         << std::endl;
75 #endif
76     // The code below this condition checks all spikes/dups
77     // for geometries >= 3 points.
78     // So we have to check the first potential duplicate differently
79     if (boost::size(range) == 1
80         && points_equal_or_close(*(boost::begin(range)), point, robust_policy))
81     {
82         return;
83     }
84 
85     traits::push_back<Range>::apply(range, point);
86 
87     // If a point is equal, or forming a spike, remove the pen-ultimate point
88     // because this one caused the spike.
89     // If so, the now-new-pen-ultimate point can again cause a spike
90     // (possibly at a corner). So keep doing this.
91     // Besides spikes it will also avoid adding duplicates.
92     while(boost::size(range) >= 3
93             && point_is_spike_or_equal(point,
94                 *(boost::end(range) - 3),
95                 *(boost::end(range) - 2),
96                 strategy,
97                 robust_policy))
98     {
99         // Use the Concept/traits, so resize and append again
100         traits::resize<Range>::apply(range, boost::size(range) - 2);
101         traits::push_back<Range>::apply(range, point);
102     }
103 }
104 
105 template <typename Range, typename SideStrategy, typename RobustPolicy>
clean_closing_dups_and_spikes(Range & range,SideStrategy const & strategy,RobustPolicy const & robust_policy)106 inline void clean_closing_dups_and_spikes(Range& range,
107                 SideStrategy const& strategy,
108                 RobustPolicy const& robust_policy)
109 {
110     std::size_t const minsize
111         = core_detail::closure::minimum_ring_size
112             <
113                 geometry::closure<Range>::value
114             >::value;
115 
116     if (boost::size(range) <= minsize)
117     {
118         return;
119     }
120 
121     typedef typename boost::range_iterator<Range>::type iterator_type;
122     static bool const closed = geometry::closure<Range>::value == geometry::closed;
123 
124 // TODO: the following algorithm could be rewritten to first look for spikes
125 // and then erase some number of points from the beginning of the Range
126 
127     bool found = false;
128     do
129     {
130         found = false;
131         iterator_type first = boost::begin(range);
132         iterator_type second = first + 1;
133         iterator_type ultimate = boost::end(range) - 1;
134         if (BOOST_GEOMETRY_CONDITION(closed))
135         {
136             ultimate--;
137         }
138 
139         // Check if closing point is a spike (this is so if the second point is
140         // considered as a spike w.r.t. the last segment)
141         if (point_is_spike_or_equal(*second, *ultimate, *first, strategy, robust_policy))
142         {
143             range::erase(range, first);
144             if (BOOST_GEOMETRY_CONDITION(closed))
145             {
146                 // Remove closing last point
147                 range::resize(range, boost::size(range) - 1);
148                 // Add new closing point
149                 range::push_back(range, range::front(range));
150             }
151             found = true;
152         }
153     } while(found && boost::size(range) > minsize);
154 }
155 
156 
157 }} // namespace detail::overlay
158 #endif // DOXYGEN_NO_DETAIL
159 
160 
161 }} // namespace boost::geometry
162 
163 
164 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP
165