hull_graham_andrew.hpp 11 KB

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