]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/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> | |
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 RobustPolicy> | |
67 | inline void append_no_dups_or_spikes(Range& range, Point const& point, | |
68 | RobustPolicy const& robust_policy) | |
69 | { | |
70 | #ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION | |
71 | std::cout << " add: (" | |
72 | << geometry::get<0>(point) << ", " << geometry::get<1>(point) << ")" | |
73 | << std::endl; | |
74 | #endif | |
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)) | |
80 | { | |
81 | return; | |
82 | } | |
83 | ||
84 | traits::push_back<Range>::apply(range, point); | |
85 | ||
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), | |
95 | robust_policy)) | |
96 | { | |
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); | |
100 | } | |
101 | } | |
102 | ||
103 | template <typename Range, typename RobustPolicy> | |
104 | inline void clean_closing_dups_and_spikes(Range& range, | |
105 | RobustPolicy const& robust_policy) | |
106 | { | |
107 | std::size_t const minsize | |
108 | = core_detail::closure::minimum_ring_size | |
109 | < | |
110 | geometry::closure<Range>::value | |
111 | >::value; | |
112 | ||
113 | if (boost::size(range) <= minsize) | |
114 | { | |
115 | return; | |
116 | } | |
117 | ||
118 | typedef typename boost::range_iterator<Range>::type iterator_type; | |
119 | static bool const closed = geometry::closure<Range>::value == geometry::closed; | |
120 | ||
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 | |
123 | ||
124 | bool found = false; | |
125 | do | |
126 | { | |
127 | found = false; | |
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)) | |
132 | { | |
133 | ultimate--; | |
134 | } | |
135 | ||
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)) | |
139 | { | |
140 | range::erase(range, first); | |
141 | if (BOOST_GEOMETRY_CONDITION(closed)) | |
142 | { | |
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)); | |
147 | } | |
148 | found = true; | |
149 | } | |
150 | } while(found && boost::size(range) > minsize); | |
151 | } | |
152 | ||
153 | ||
154 | }} // namespace detail::overlay | |
155 | #endif // DOXYGEN_NO_DETAIL | |
156 | ||
157 | ||
158 | }} // namespace boost::geometry | |
159 | ||
160 | ||
161 | #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP |