graham_andrew.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014-2023.
  4. // Modifications copyright (c) 2014-2023 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  8. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_GRAHAM_ANDREW_HPP
  13. #define BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_GRAHAM_ANDREW_HPP
  14. #include <cstddef>
  15. #include <algorithm>
  16. #include <vector>
  17. #include <boost/range/size.hpp>
  18. #include <boost/geometry/algorithms/detail/for_each_range.hpp>
  19. #include <boost/geometry/core/assert.hpp>
  20. #include <boost/geometry/core/closure.hpp>
  21. #include <boost/geometry/core/cs.hpp>
  22. #include <boost/geometry/core/point_type.hpp>
  23. #include <boost/geometry/core/point_order.hpp>
  24. #include <boost/geometry/policies/compare.hpp>
  25. #include <boost/geometry/strategies/convex_hull/cartesian.hpp>
  26. #include <boost/geometry/strategies/convex_hull/geographic.hpp>
  27. #include <boost/geometry/strategies/convex_hull/spherical.hpp>
  28. #include <boost/geometry/util/range.hpp>
  29. namespace boost { namespace geometry
  30. {
  31. #ifndef DOXYGEN_NO_DETAIL
  32. namespace detail { namespace convex_hull
  33. {
  34. // TODO: All of the copies could be avoided if this function stored pointers to points.
  35. // But would it be possible considering that a range can return proxy reference?
  36. template <typename InputProxy, typename Point, typename Less>
  37. inline void get_extremes(InputProxy const& in_proxy,
  38. Point& left, Point& right,
  39. Less const& less)
  40. {
  41. bool first = true;
  42. in_proxy.for_each_range([&](auto const& range)
  43. {
  44. if (boost::empty(range))
  45. {
  46. return;
  47. }
  48. // First iterate through this range
  49. // (this two-stage approach avoids many point copies,
  50. // because iterators are kept in memory. Because iterators are
  51. // not persistent (in MSVC) this approach is not applicable
  52. // for more ranges together)
  53. auto left_it = boost::begin(range);
  54. auto right_it = boost::begin(range);
  55. auto it = boost::begin(range);
  56. for (++it; it != boost::end(range); ++it)
  57. {
  58. if (less(*it, *left_it))
  59. {
  60. left_it = it;
  61. }
  62. if (less(*right_it, *it))
  63. {
  64. right_it = it;
  65. }
  66. }
  67. // Then compare with earlier
  68. if (first)
  69. {
  70. // First time, assign left/right
  71. left = *left_it;
  72. right = *right_it;
  73. first = false;
  74. }
  75. else
  76. {
  77. // Next time, check if this range was left/right from
  78. // the extremes already collected
  79. if (less(*left_it, left))
  80. {
  81. left = *left_it;
  82. }
  83. if (less(right, *right_it))
  84. {
  85. right = *right_it;
  86. }
  87. }
  88. });
  89. }
  90. template <typename InputProxy, typename Point, typename Container, typename SideStrategy>
  91. inline void assign_ranges(InputProxy const& in_proxy,
  92. Point const& most_left, Point const& most_right,
  93. Container& lower_points, Container& upper_points,
  94. SideStrategy const& side)
  95. {
  96. in_proxy.for_each_range([&](auto const& range)
  97. {
  98. // Put points in one of the two output sequences
  99. for (auto it = boost::begin(range); it != boost::end(range); ++it)
  100. {
  101. // check if it is lying most_left or most_right from the line
  102. int dir = side.apply(most_left, most_right, *it);
  103. switch(dir)
  104. {
  105. case 1 : // left side
  106. upper_points.push_back(*it);
  107. break;
  108. case -1 : // right side
  109. lower_points.push_back(*it);
  110. break;
  111. // 0: on line most_left-most_right,
  112. // or most_left, or most_right,
  113. // -> all never part of hull
  114. }
  115. }
  116. });
  117. }
  118. /*!
  119. \brief Graham scan algorithm to calculate convex hull
  120. */
  121. template <typename InputPoint>
  122. class graham_andrew
  123. {
  124. typedef InputPoint point_type;
  125. typedef typename std::vector<point_type> container_type;
  126. class partitions
  127. {
  128. friend class graham_andrew;
  129. container_type m_lower_hull;
  130. container_type m_upper_hull;
  131. container_type m_copied_input;
  132. };
  133. public:
  134. template <typename InputProxy, typename OutputRing, typename Strategy>
  135. static void apply(InputProxy const& in_proxy, OutputRing & out_ring, Strategy& strategy)
  136. {
  137. partitions state;
  138. apply(in_proxy, state, strategy);
  139. result(state,
  140. range::back_inserter(out_ring),
  141. geometry::point_order<OutputRing>::value == clockwise,
  142. geometry::closure<OutputRing>::value != open);
  143. }
  144. private:
  145. template <typename InputProxy, typename Strategy>
  146. static void apply(InputProxy const& in_proxy, partitions& state, Strategy& strategy)
  147. {
  148. // First pass.
  149. // Get min/max (in most cases left / right) points
  150. // This makes use of the geometry::less/greater predicates
  151. // For the left boundary it is important that multiple points
  152. // are sorted from bottom to top. Therefore the less predicate
  153. // does not take the x-only template parameter (this fixes ticket #6019.
  154. // For the right boundary it is not necessary (though also not harmful),
  155. // because points are sorted from bottom to top in a later stage.
  156. // For symmetry and to get often more balanced lower/upper halves
  157. // we keep it.
  158. point_type most_left, most_right;
  159. geometry::less_exact<point_type, -1, Strategy> less;
  160. detail::convex_hull::get_extremes(in_proxy, most_left, most_right, less);
  161. container_type lower_points, upper_points;
  162. auto const side_strategy = strategy.side();
  163. // Bounding left/right points
  164. // Second pass, now that extremes are found, assign all points
  165. // in either lower, either upper
  166. detail::convex_hull::assign_ranges(in_proxy, most_left, most_right,
  167. lower_points, upper_points,
  168. side_strategy);
  169. // Sort both collections, first on x(, then on y)
  170. std::sort(boost::begin(lower_points), boost::end(lower_points), less);
  171. std::sort(boost::begin(upper_points), boost::end(upper_points), less);
  172. // And decide which point should be in the final hull
  173. build_half_hull<-1>(lower_points, state.m_lower_hull,
  174. most_left, most_right,
  175. side_strategy);
  176. build_half_hull<1>(upper_points, state.m_upper_hull,
  177. most_left, most_right,
  178. side_strategy);
  179. }
  180. template <int Factor, typename SideStrategy>
  181. static inline void build_half_hull(container_type const& input,
  182. container_type& output,
  183. point_type const& left, point_type const& right,
  184. SideStrategy const& side)
  185. {
  186. output.push_back(left);
  187. for (auto const& i : input)
  188. {
  189. add_to_hull<Factor>(i, output, side);
  190. }
  191. add_to_hull<Factor>(right, output, side);
  192. }
  193. template <int Factor, typename SideStrategy>
  194. static inline void add_to_hull(point_type const& p, container_type& output,
  195. SideStrategy const& side)
  196. {
  197. output.push_back(p);
  198. std::size_t output_size = output.size();
  199. while (output_size >= 3)
  200. {
  201. auto rit = output.rbegin();
  202. point_type const last = *rit++;
  203. point_type const& last2 = *rit++;
  204. if (Factor * side.apply(*rit, last, last2) <= 0)
  205. {
  206. // Remove last two points from stack, and add last again
  207. // This is much faster then erasing the one but last.
  208. output.pop_back();
  209. output.pop_back();
  210. output.push_back(last);
  211. output_size--;
  212. }
  213. else
  214. {
  215. return;
  216. }
  217. }
  218. }
  219. template <typename OutputIterator>
  220. static void result(partitions const& state, OutputIterator out, bool clockwise, bool closed)
  221. {
  222. if (clockwise)
  223. {
  224. output_ranges(state.m_upper_hull, state.m_lower_hull, out, closed);
  225. }
  226. else
  227. {
  228. output_ranges(state.m_lower_hull, state.m_upper_hull, out, closed);
  229. }
  230. }
  231. template <typename OutputIterator>
  232. static inline void output_ranges(container_type const& first,
  233. container_type const& second,
  234. OutputIterator out,
  235. bool closed)
  236. {
  237. std::copy(boost::begin(first), boost::end(first), out);
  238. BOOST_GEOMETRY_ASSERT(closed ? !boost::empty(second) : boost::size(second) > 1);
  239. std::copy(++boost::rbegin(second), // skip the first Point
  240. closed ? boost::rend(second) : --boost::rend(second), // skip the last Point if open
  241. out);
  242. typedef typename boost::range_size<container_type>::type size_type;
  243. size_type const count = boost::size(first) + boost::size(second) - 1;
  244. // count describes a closed case but comparison with min size of closed
  245. // gives the result compatible also with open
  246. // here core_detail::closure::minimum_ring_size<closed> could be used
  247. if (count < 4)
  248. {
  249. // there should be only one missing
  250. *out++ = *boost::begin(first);
  251. }
  252. }
  253. };
  254. }} // namespace detail::convex_hull
  255. #endif // DOXYGEN_NO_DETAIL
  256. }} // namespace boost::geometry
  257. #endif // BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_GRAHAM_ANDREW_HPP