123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- #ifndef BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_GRAHAM_ANDREW_HPP
- #define BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_GRAHAM_ANDREW_HPP
- #include <cstddef>
- #include <algorithm>
- #include <vector>
- #include <boost/range/size.hpp>
- #include <boost/geometry/algorithms/detail/for_each_range.hpp>
- #include <boost/geometry/core/assert.hpp>
- #include <boost/geometry/core/closure.hpp>
- #include <boost/geometry/core/cs.hpp>
- #include <boost/geometry/core/point_type.hpp>
- #include <boost/geometry/core/point_order.hpp>
- #include <boost/geometry/policies/compare.hpp>
- #include <boost/geometry/strategies/convex_hull/cartesian.hpp>
- #include <boost/geometry/strategies/convex_hull/geographic.hpp>
- #include <boost/geometry/strategies/convex_hull/spherical.hpp>
- #include <boost/geometry/util/range.hpp>
- namespace boost { namespace geometry
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace convex_hull
- {
- template <typename InputProxy, typename Point, typename Less>
- inline void get_extremes(InputProxy const& in_proxy,
- Point& left, Point& right,
- Less const& less)
- {
- bool first = true;
- in_proxy.for_each_range([&](auto const& range)
- {
- if (boost::empty(range))
- {
- return;
- }
-
-
-
-
-
- auto left_it = boost::begin(range);
- auto right_it = boost::begin(range);
- auto it = boost::begin(range);
- for (++it; it != boost::end(range); ++it)
- {
- if (less(*it, *left_it))
- {
- left_it = it;
- }
- if (less(*right_it, *it))
- {
- right_it = it;
- }
- }
-
- if (first)
- {
-
- left = *left_it;
- right = *right_it;
- first = false;
- }
- else
- {
-
-
- if (less(*left_it, left))
- {
- left = *left_it;
- }
- if (less(right, *right_it))
- {
- right = *right_it;
- }
- }
- });
- }
- template <typename InputProxy, typename Point, typename Container, typename SideStrategy>
- inline void assign_ranges(InputProxy const& in_proxy,
- Point const& most_left, Point const& most_right,
- Container& lower_points, Container& upper_points,
- SideStrategy const& side)
- {
- in_proxy.for_each_range([&](auto const& range)
- {
-
- for (auto it = boost::begin(range); it != boost::end(range); ++it)
- {
-
- int dir = side.apply(most_left, most_right, *it);
- switch(dir)
- {
- case 1 :
- upper_points.push_back(*it);
- break;
- case -1 :
- lower_points.push_back(*it);
- break;
-
-
-
- }
- }
- });
- }
- template <typename InputPoint>
- class graham_andrew
- {
- typedef InputPoint point_type;
- typedef typename std::vector<point_type> container_type;
- class partitions
- {
- friend class graham_andrew;
- container_type m_lower_hull;
- container_type m_upper_hull;
- container_type m_copied_input;
- };
- public:
- template <typename InputProxy, typename OutputRing, typename Strategy>
- static void apply(InputProxy const& in_proxy, OutputRing & out_ring, Strategy& strategy)
- {
- partitions state;
- apply(in_proxy, state, strategy);
- result(state,
- range::back_inserter(out_ring),
- geometry::point_order<OutputRing>::value == clockwise,
- geometry::closure<OutputRing>::value != open);
- }
- private:
- template <typename InputProxy, typename Strategy>
- static void apply(InputProxy const& in_proxy, partitions& state, Strategy& strategy)
- {
-
-
-
-
-
-
-
-
-
-
- point_type most_left, most_right;
- geometry::less_exact<point_type, -1, Strategy> less;
- detail::convex_hull::get_extremes(in_proxy, most_left, most_right, less);
- container_type lower_points, upper_points;
- auto const side_strategy = strategy.side();
-
-
-
- detail::convex_hull::assign_ranges(in_proxy, most_left, most_right,
- lower_points, upper_points,
- side_strategy);
-
- std::sort(boost::begin(lower_points), boost::end(lower_points), less);
- std::sort(boost::begin(upper_points), boost::end(upper_points), less);
-
- build_half_hull<-1>(lower_points, state.m_lower_hull,
- most_left, most_right,
- side_strategy);
- build_half_hull<1>(upper_points, state.m_upper_hull,
- most_left, most_right,
- side_strategy);
- }
- template <int Factor, typename SideStrategy>
- static inline void build_half_hull(container_type const& input,
- container_type& output,
- point_type const& left, point_type const& right,
- SideStrategy const& side)
- {
- output.push_back(left);
- for (auto const& i : input)
- {
- add_to_hull<Factor>(i, output, side);
- }
- add_to_hull<Factor>(right, output, side);
- }
- template <int Factor, typename SideStrategy>
- static inline void add_to_hull(point_type const& p, container_type& output,
- SideStrategy const& side)
- {
- output.push_back(p);
- std::size_t output_size = output.size();
- while (output_size >= 3)
- {
- auto rit = output.rbegin();
- point_type const last = *rit++;
- point_type const& last2 = *rit++;
- if (Factor * side.apply(*rit, last, last2) <= 0)
- {
-
-
- output.pop_back();
- output.pop_back();
- output.push_back(last);
- output_size--;
- }
- else
- {
- return;
- }
- }
- }
- template <typename OutputIterator>
- static void result(partitions const& state, OutputIterator out, bool clockwise, bool closed)
- {
- if (clockwise)
- {
- output_ranges(state.m_upper_hull, state.m_lower_hull, out, closed);
- }
- else
- {
- output_ranges(state.m_lower_hull, state.m_upper_hull, out, closed);
- }
- }
- template <typename OutputIterator>
- static inline void output_ranges(container_type const& first,
- container_type const& second,
- OutputIterator out,
- bool closed)
- {
- std::copy(boost::begin(first), boost::end(first), out);
- BOOST_GEOMETRY_ASSERT(closed ? !boost::empty(second) : boost::size(second) > 1);
- std::copy(++boost::rbegin(second),
- closed ? boost::rend(second) : --boost::rend(second),
- out);
- typedef typename boost::range_size<container_type>::type size_type;
- size_type const count = boost::size(first) + boost::size(second) - 1;
-
-
-
- if (count < 4)
- {
-
- *out++ = *boost::begin(first);
- }
- }
- };
- }}
- #endif
- }}
- #endif
|