hull_graham_andrew.hpp 11 KB

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