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-2020.
6 // Modifications copyright (c) 2014-2020 Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
11 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
13 // Use, modification and distribution is subject to the Boost Software License,
14 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 #ifndef BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_CONVEX_GRAHAM_ANDREW_HPP
18 #define BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_CONVEX_GRAHAM_ANDREW_HPP
25 #include <boost/range/begin.hpp>
26 #include <boost/range/empty.hpp>
27 #include <boost/range/end.hpp>
28 #include <boost/range/size.hpp>
30 #include <boost/geometry/algorithms/detail/for_each_range.hpp>
31 #include <boost/geometry/core/assert.hpp>
32 #include <boost/geometry/core/cs.hpp>
33 #include <boost/geometry/core/point_type.hpp>
34 #include <boost/geometry/policies/compare.hpp>
35 #include <boost/geometry/strategies/convex_hull.hpp>
36 #include <boost/geometry/strategies/side.hpp>
37 #include <boost/geometry/views/detail/range_type.hpp>
40 namespace boost { namespace geometry
43 namespace strategy { namespace convex_hull
46 #ifndef DOXYGEN_NO_DETAIL
51 template <typename Geometry, typename Point, typename Less>
52 inline void get_extremes(Geometry const& geometry,
53 Point& left, Point& right,
57 geometry::detail::for_each_range(geometry, [&](auto const& range)
59 if (boost::empty(range))
64 // First iterate through this range
65 // (this two-stage approach avoids many point copies,
66 // because iterators are kept in memory. Because iterators are
67 // not persistent (in MSVC) this approach is not applicable
68 // for more ranges together)
70 auto left_it = boost::begin(range);
71 auto right_it = boost::begin(range);
73 for (auto it = ++boost::begin(range); it != boost::end(range); ++it)
75 if (less(*it, *left_it))
80 if (less(*right_it, *it))
86 // Then compare with earlier
89 // First time, assign left/right
96 // Next time, check if this range was left/right from
97 // the extremes already collected
98 if (less(*left_it, left))
103 if (less(right, *right_it))
117 typename SideStrategy
119 inline void assign_ranges(Geometry const& geometry,
120 Point const& most_left, Point const& most_right,
121 Container& lower_points, Container& upper_points,
122 SideStrategy const& side)
124 geometry::detail::for_each_range(geometry, [&](auto const& range)
126 // Put points in one of the two output sequences
127 for (auto it = boost::begin(range); it != boost::end(range); ++it)
129 // check if it is lying most_left or most_right from the line
131 int dir = side.apply(most_left, most_right, *it);
134 case 1 : // left side
135 upper_points.push_back(*it);
137 case -1 : // right side
138 lower_points.push_back(*it);
141 // 0: on line most_left-most_right,
142 // or most_left, or most_right,
143 // -> all never part of hull
150 template <typename Range, typename Less>
151 inline void sort(Range& range, Less const& less)
153 std::sort(boost::begin(range), boost::end(range), less);
156 } // namespace detail
157 #endif // DOXYGEN_NO_DETAIL
161 \brief Graham scan strategy to calculate convex hull
164 template <typename InputGeometry, typename OutputPoint>
168 typedef OutputPoint point_type;
169 typedef InputGeometry geometry_type;
173 typedef typename cs_tag<point_type>::type cs_tag;
175 typedef typename std::vector<point_type> container_type;
176 typedef typename std::vector<point_type>::const_iterator iterator;
177 typedef typename std::vector<point_type>::const_reverse_iterator rev_iterator;
182 friend class graham_andrew;
184 container_type m_lower_hull;
185 container_type m_upper_hull;
186 container_type m_copied_input;
191 typedef partitions state_type;
194 inline void apply(InputGeometry const& geometry, partitions& state) const
197 // Get min/max (in most cases left / right) points
198 // This makes use of the geometry::less/greater predicates
200 // For the left boundary it is important that multiple points
201 // are sorted from bottom to top. Therefore the less predicate
202 // does not take the x-only template parameter (this fixes ticket #6019.
203 // For the right boundary it is not necessary (though also not harmful),
204 // because points are sorted from bottom to top in a later stage.
205 // For symmetry and to get often more balanced lower/upper halves
208 typedef typename geometry::point_type<InputGeometry>::type point_type;
210 point_type most_left, most_right;
212 // TODO: User-defined CS-specific less-compare
213 geometry::less<point_type> less;
215 detail::get_extremes(geometry, most_left, most_right, less);
217 container_type lower_points, upper_points;
219 // TODO: User-defiend CS-specific side strategy
220 typename strategy::side::services::default_strategy<cs_tag>::type side;
222 // Bounding left/right points
223 // Second pass, now that extremes are found, assign all points
224 // in either lower, either upper
225 detail::assign_ranges(geometry, most_left, most_right,
226 lower_points, upper_points,
229 // Sort both collections, first on x(, then on y)
230 detail::sort(lower_points, less);
231 detail::sort(upper_points, less);
233 // And decide which point should be in the final hull
234 build_half_hull<-1>(lower_points, state.m_lower_hull,
235 most_left, most_right,
237 build_half_hull<1>(upper_points, state.m_upper_hull,
238 most_left, most_right,
243 template <typename OutputIterator>
244 inline void result(partitions const& state,
251 output_ranges(state.m_upper_hull, state.m_lower_hull, out, closed);
255 output_ranges(state.m_lower_hull, state.m_upper_hull, out, closed);
262 template <int Factor, typename SideStrategy>
263 static inline void build_half_hull(container_type const& input,
264 container_type& output,
265 point_type const& left, point_type const& right,
266 SideStrategy const& side)
268 output.push_back(left);
269 for(iterator it = input.begin(); it != input.end(); ++it)
271 add_to_hull<Factor>(*it, output, side);
273 add_to_hull<Factor>(right, output, side);
277 template <int Factor, typename SideStrategy>
278 static inline void add_to_hull(point_type const& p, container_type& output,
279 SideStrategy const& side)
282 std::size_t output_size = output.size();
283 while (output_size >= 3)
285 rev_iterator rit = output.rbegin();
286 point_type const last = *rit++;
287 point_type const& last2 = *rit++;
289 if (Factor * side.apply(*rit, last, last2) <= 0)
291 // Remove last two points from stack, and add last again
292 // This is much faster then erasing the one but last.
295 output.push_back(last);
306 template <typename OutputIterator>
307 static inline void output_ranges(container_type const& first, container_type const& second,
308 OutputIterator out, bool closed)
310 std::copy(boost::begin(first), boost::end(first), out);
312 BOOST_GEOMETRY_ASSERT(closed ? !boost::empty(second) : boost::size(second) > 1);
313 std::copy(++boost::rbegin(second), // skip the first Point
314 closed ? boost::rend(second) : --boost::rend(second), // skip the last Point if open
317 typedef typename boost::range_size<container_type>::type size_type;
318 size_type const count = boost::size(first) + boost::size(second) - 1;
319 // count describes a closed case but comparison with min size of closed
320 // gives the result compatible also with open
321 // here core_detail::closure::minimum_ring_size<closed> could be used
324 // there should be only one missing
325 *out++ = *boost::begin(first);
330 }} // namespace strategy::convex_hull
333 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
334 template <typename InputGeometry, typename OutputPoint>
335 struct strategy_convex_hull<InputGeometry, OutputPoint, cartesian_tag>
337 typedef strategy::convex_hull::graham_andrew<InputGeometry, OutputPoint> type;
341 }} // namespace boost::geometry
344 #endif // BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_CONVEX_GRAHAM_ANDREW_HPP