convex_hull.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2014, 2015.
  6. // Modifications copyright (c) 2014-2015 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP
  16. #include <boost/array.hpp>
  17. #include <boost/variant/apply_visitor.hpp>
  18. #include <boost/variant/static_visitor.hpp>
  19. #include <boost/variant/variant_fwd.hpp>
  20. #include <boost/geometry/core/cs.hpp>
  21. #include <boost/geometry/core/point_order.hpp>
  22. #include <boost/geometry/core/closure.hpp>
  23. #include <boost/geometry/core/exterior_ring.hpp>
  24. #include <boost/geometry/geometries/concepts/check.hpp>
  25. #include <boost/geometry/strategies/convex_hull.hpp>
  26. #include <boost/geometry/strategies/concepts/convex_hull_concept.hpp>
  27. #include <boost/geometry/strategies/default_strategy.hpp>
  28. #include <boost/geometry/util/condition.hpp>
  29. #include <boost/geometry/views/detail/range_type.hpp>
  30. #include <boost/geometry/algorithms/is_empty.hpp>
  31. #include <boost/geometry/algorithms/detail/as_range.hpp>
  32. #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
  33. namespace boost { namespace geometry
  34. {
  35. #ifndef DOXYGEN_NO_DETAIL
  36. namespace detail { namespace convex_hull
  37. {
  38. template <order_selector Order, closure_selector Closure>
  39. struct hull_insert
  40. {
  41. // Member template function (to avoid inconvenient declaration
  42. // of output-iterator-type, from hull_to_geometry)
  43. template <typename Geometry, typename OutputIterator, typename Strategy>
  44. static inline OutputIterator apply(Geometry const& geometry,
  45. OutputIterator out, Strategy const& strategy)
  46. {
  47. typename Strategy::state_type state;
  48. strategy.apply(geometry, state);
  49. strategy.result(state, out, Order == clockwise, Closure != open);
  50. return out;
  51. }
  52. };
  53. struct hull_to_geometry
  54. {
  55. template <typename Geometry, typename OutputGeometry, typename Strategy>
  56. static inline void apply(Geometry const& geometry, OutputGeometry& out,
  57. Strategy const& strategy)
  58. {
  59. hull_insert
  60. <
  61. geometry::point_order<OutputGeometry>::value,
  62. geometry::closure<OutputGeometry>::value
  63. >::apply(geometry,
  64. range::back_inserter(
  65. // Handle linestring, ring and polygon the same:
  66. detail::as_range
  67. <
  68. typename range_type<OutputGeometry>::type
  69. >(out)), strategy);
  70. }
  71. };
  72. }} // namespace detail::convex_hull
  73. #endif // DOXYGEN_NO_DETAIL
  74. #ifndef DOXYGEN_NO_DISPATCH
  75. namespace dispatch
  76. {
  77. template
  78. <
  79. typename Geometry,
  80. typename Tag = typename tag<Geometry>::type
  81. >
  82. struct convex_hull
  83. : detail::convex_hull::hull_to_geometry
  84. {};
  85. template <typename Box>
  86. struct convex_hull<Box, box_tag>
  87. {
  88. template <typename OutputGeometry, typename Strategy>
  89. static inline void apply(Box const& box, OutputGeometry& out,
  90. Strategy const& )
  91. {
  92. static bool const Close
  93. = geometry::closure<OutputGeometry>::value == closed;
  94. static bool const Reverse
  95. = geometry::point_order<OutputGeometry>::value == counterclockwise;
  96. // A hull for boxes is trivial. Any strategy is (currently) skipped.
  97. boost::array<typename point_type<Box>::type, 4> range;
  98. geometry::detail::assign_box_corners_oriented<Reverse>(box, range);
  99. geometry::append(out, range);
  100. if (BOOST_GEOMETRY_CONDITION(Close))
  101. {
  102. geometry::append(out, *boost::begin(range));
  103. }
  104. }
  105. };
  106. template <order_selector Order, closure_selector Closure>
  107. struct convex_hull_insert
  108. : detail::convex_hull::hull_insert<Order, Closure>
  109. {};
  110. } // namespace dispatch
  111. #endif // DOXYGEN_NO_DISPATCH
  112. namespace resolve_strategy {
  113. struct convex_hull
  114. {
  115. template <typename Geometry, typename OutputGeometry, typename Strategy>
  116. static inline void apply(Geometry const& geometry,
  117. OutputGeometry& out,
  118. Strategy const& strategy)
  119. {
  120. BOOST_CONCEPT_ASSERT( (geometry::concepts::ConvexHullStrategy<Strategy>) );
  121. dispatch::convex_hull<Geometry>::apply(geometry, out, strategy);
  122. }
  123. template <typename Geometry, typename OutputGeometry>
  124. static inline void apply(Geometry const& geometry,
  125. OutputGeometry& out,
  126. default_strategy)
  127. {
  128. typedef typename strategy_convex_hull<
  129. Geometry,
  130. typename point_type<Geometry>::type
  131. >::type strategy_type;
  132. apply(geometry, out, strategy_type());
  133. }
  134. };
  135. struct convex_hull_insert
  136. {
  137. template <typename Geometry, typename OutputIterator, typename Strategy>
  138. static inline OutputIterator apply(Geometry const& geometry,
  139. OutputIterator& out,
  140. Strategy const& strategy)
  141. {
  142. BOOST_CONCEPT_ASSERT( (geometry::concepts::ConvexHullStrategy<Strategy>) );
  143. return dispatch::convex_hull_insert<
  144. geometry::point_order<Geometry>::value,
  145. geometry::closure<Geometry>::value
  146. >::apply(geometry, out, strategy);
  147. }
  148. template <typename Geometry, typename OutputIterator>
  149. static inline OutputIterator apply(Geometry const& geometry,
  150. OutputIterator& out,
  151. default_strategy)
  152. {
  153. typedef typename strategy_convex_hull<
  154. Geometry,
  155. typename point_type<Geometry>::type
  156. >::type strategy_type;
  157. return apply(geometry, out, strategy_type());
  158. }
  159. };
  160. } // namespace resolve_strategy
  161. namespace resolve_variant {
  162. template <typename Geometry>
  163. struct convex_hull
  164. {
  165. template <typename OutputGeometry, typename Strategy>
  166. static inline void apply(Geometry const& geometry, OutputGeometry& out, Strategy const& strategy)
  167. {
  168. concepts::check_concepts_and_equal_dimensions<
  169. const Geometry,
  170. OutputGeometry
  171. >();
  172. resolve_strategy::convex_hull::apply(geometry, out, strategy);
  173. }
  174. };
  175. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  176. struct convex_hull<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  177. {
  178. template <typename OutputGeometry, typename Strategy>
  179. struct visitor: boost::static_visitor<void>
  180. {
  181. OutputGeometry& m_out;
  182. Strategy const& m_strategy;
  183. visitor(OutputGeometry& out, Strategy const& strategy)
  184. : m_out(out), m_strategy(strategy)
  185. {}
  186. template <typename Geometry>
  187. void operator()(Geometry const& geometry) const
  188. {
  189. convex_hull<Geometry>::apply(geometry, m_out, m_strategy);
  190. }
  191. };
  192. template <typename OutputGeometry, typename Strategy>
  193. static inline void
  194. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  195. OutputGeometry& out,
  196. Strategy const& strategy)
  197. {
  198. boost::apply_visitor(visitor<OutputGeometry, Strategy>(out, strategy), geometry);
  199. }
  200. };
  201. template <typename Geometry>
  202. struct convex_hull_insert
  203. {
  204. template <typename OutputIterator, typename Strategy>
  205. static inline OutputIterator apply(Geometry const& geometry, OutputIterator& out, Strategy const& strategy)
  206. {
  207. // Concept: output point type = point type of input geometry
  208. concepts::check<Geometry const>();
  209. concepts::check<typename point_type<Geometry>::type>();
  210. return resolve_strategy::convex_hull_insert::apply(geometry, out, strategy);
  211. }
  212. };
  213. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  214. struct convex_hull_insert<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  215. {
  216. template <typename OutputIterator, typename Strategy>
  217. struct visitor: boost::static_visitor<OutputIterator>
  218. {
  219. OutputIterator& m_out;
  220. Strategy const& m_strategy;
  221. visitor(OutputIterator& out, Strategy const& strategy)
  222. : m_out(out), m_strategy(strategy)
  223. {}
  224. template <typename Geometry>
  225. OutputIterator operator()(Geometry const& geometry) const
  226. {
  227. return convex_hull_insert<Geometry>::apply(geometry, m_out, m_strategy);
  228. }
  229. };
  230. template <typename OutputIterator, typename Strategy>
  231. static inline OutputIterator
  232. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  233. OutputIterator& out,
  234. Strategy const& strategy)
  235. {
  236. return boost::apply_visitor(visitor<OutputIterator, Strategy>(out, strategy), geometry);
  237. }
  238. };
  239. } // namespace resolve_variant
  240. template<typename Geometry, typename OutputGeometry, typename Strategy>
  241. inline void convex_hull(Geometry const& geometry,
  242. OutputGeometry& out, Strategy const& strategy)
  243. {
  244. if (geometry::is_empty(geometry))
  245. {
  246. // Leave output empty
  247. return;
  248. }
  249. resolve_variant::convex_hull<Geometry>::apply(geometry, out, strategy);
  250. }
  251. /*!
  252. \brief \brief_calc{convex hull}
  253. \ingroup convex_hull
  254. \details \details_calc{convex_hull,convex hull}.
  255. \tparam Geometry the input geometry type
  256. \tparam OutputGeometry the output geometry type
  257. \param geometry \param_geometry, input geometry
  258. \param hull \param_geometry \param_set{convex hull}
  259. \qbk{[include reference/algorithms/convex_hull.qbk]}
  260. */
  261. template<typename Geometry, typename OutputGeometry>
  262. inline void convex_hull(Geometry const& geometry,
  263. OutputGeometry& hull)
  264. {
  265. geometry::convex_hull(geometry, hull, default_strategy());
  266. }
  267. #ifndef DOXYGEN_NO_DETAIL
  268. namespace detail { namespace convex_hull
  269. {
  270. template<typename Geometry, typename OutputIterator, typename Strategy>
  271. inline OutputIterator convex_hull_insert(Geometry const& geometry,
  272. OutputIterator out, Strategy const& strategy)
  273. {
  274. return resolve_variant::convex_hull_insert<Geometry>
  275. ::apply(geometry, out, strategy);
  276. }
  277. /*!
  278. \brief Calculate the convex hull of a geometry, output-iterator version
  279. \ingroup convex_hull
  280. \tparam Geometry the input geometry type
  281. \tparam OutputIterator: an output-iterator
  282. \param geometry the geometry to calculate convex hull from
  283. \param out an output iterator outputing points of the convex hull
  284. \note This overloaded version outputs to an output iterator.
  285. In this case, nothing is known about its point-type or
  286. about its clockwise order. Therefore, the input point-type
  287. and order are copied
  288. */
  289. template<typename Geometry, typename OutputIterator>
  290. inline OutputIterator convex_hull_insert(Geometry const& geometry,
  291. OutputIterator out)
  292. {
  293. return convex_hull_insert(geometry, out, default_strategy());
  294. }
  295. }} // namespace detail::convex_hull
  296. #endif // DOXYGEN_NO_DETAIL
  297. }} // namespace boost::geometry
  298. #endif // BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP