convex_hull.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP
  12. #include <boost/array.hpp>
  13. #include <boost/geometry/core/cs.hpp>
  14. #include <boost/geometry/core/point_order.hpp>
  15. #include <boost/geometry/core/exterior_ring.hpp>
  16. #include <boost/geometry/geometries/concepts/check.hpp>
  17. #include <boost/geometry/strategies/convex_hull.hpp>
  18. #include <boost/geometry/strategies/concepts/convex_hull_concept.hpp>
  19. #include <boost/geometry/views/detail/range_type.hpp>
  20. #include <boost/geometry/algorithms/num_points.hpp>
  21. #include <boost/geometry/algorithms/detail/as_range.hpp>
  22. #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail { namespace convex_hull
  27. {
  28. template <order_selector Order>
  29. struct hull_insert
  30. {
  31. // Member template function (to avoid inconvenient declaration
  32. // of output-iterator-type, from hull_to_geometry)
  33. template <typename Geometry, typename OutputIterator, typename Strategy>
  34. static inline OutputIterator apply(Geometry const& geometry,
  35. OutputIterator out, Strategy const& strategy)
  36. {
  37. typename Strategy::state_type state;
  38. strategy.apply(geometry, state);
  39. strategy.result(state, out, Order == clockwise);
  40. return out;
  41. }
  42. };
  43. struct hull_to_geometry
  44. {
  45. template <typename Geometry, typename OutputGeometry, typename Strategy>
  46. static inline void apply(Geometry const& geometry, OutputGeometry& out,
  47. Strategy const& strategy)
  48. {
  49. hull_insert
  50. <
  51. geometry::point_order<OutputGeometry>::value
  52. >::apply(geometry,
  53. std::back_inserter(
  54. // Handle linestring, ring and polygon the same:
  55. detail::as_range
  56. <
  57. typename range_type<OutputGeometry>::type
  58. >(out)), strategy);
  59. }
  60. };
  61. // Helper metafunction for default strategy retrieval
  62. template <typename Geometry>
  63. struct default_strategy
  64. : strategy_convex_hull
  65. <
  66. Geometry,
  67. typename point_type<Geometry>::type
  68. >
  69. {};
  70. }} // namespace detail::convex_hull
  71. #endif // DOXYGEN_NO_DETAIL
  72. #ifndef DOXYGEN_NO_DISPATCH
  73. namespace dispatch
  74. {
  75. template
  76. <
  77. typename Geometry,
  78. typename Tag = typename tag<Geometry>::type
  79. >
  80. struct convex_hull
  81. : detail::convex_hull::hull_to_geometry
  82. {};
  83. template <typename Box>
  84. struct convex_hull<Box, box_tag>
  85. {
  86. template <typename OutputGeometry, typename Strategy>
  87. static inline void apply(Box const& box, OutputGeometry& out,
  88. Strategy const& )
  89. {
  90. static bool const Close
  91. = geometry::closure<OutputGeometry>::value == closed;
  92. static bool const Reverse
  93. = geometry::point_order<OutputGeometry>::value == counterclockwise;
  94. // A hull for boxes is trivial. Any strategy is (currently) skipped.
  95. boost::array<typename point_type<Box>::type, 4> range;
  96. geometry::detail::assign_box_corners_oriented<Reverse>(box, range);
  97. geometry::append(out, range);
  98. if (Close)
  99. {
  100. geometry::append(out, *boost::begin(range));
  101. }
  102. }
  103. };
  104. template <order_selector Order>
  105. struct convex_hull_insert
  106. : detail::convex_hull::hull_insert<Order>
  107. {};
  108. } // namespace dispatch
  109. #endif // DOXYGEN_NO_DISPATCH
  110. template<typename Geometry, typename OutputGeometry, typename Strategy>
  111. inline void convex_hull(Geometry const& geometry,
  112. OutputGeometry& out, Strategy const& strategy)
  113. {
  114. concept::check_concepts_and_equal_dimensions
  115. <
  116. const Geometry,
  117. OutputGeometry
  118. >();
  119. BOOST_CONCEPT_ASSERT( (geometry::concept::ConvexHullStrategy<Strategy>) );
  120. if (geometry::num_points(geometry) == 0)
  121. {
  122. // Leave output empty
  123. return;
  124. }
  125. dispatch::convex_hull<Geometry>::apply(geometry, out, strategy);
  126. }
  127. /*!
  128. \brief \brief_calc{convex hull}
  129. \ingroup convex_hull
  130. \details \details_calc{convex_hull,convex hull}.
  131. \tparam Geometry the input geometry type
  132. \tparam OutputGeometry the output geometry type
  133. \param geometry \param_geometry, input geometry
  134. \param hull \param_geometry \param_set{convex hull}
  135. \qbk{[include reference/algorithms/convex_hull.qbk]}
  136. */
  137. template<typename Geometry, typename OutputGeometry>
  138. inline void convex_hull(Geometry const& geometry,
  139. OutputGeometry& hull)
  140. {
  141. concept::check_concepts_and_equal_dimensions
  142. <
  143. const Geometry,
  144. OutputGeometry
  145. >();
  146. typedef typename detail::convex_hull::default_strategy<Geometry>::type strategy_type;
  147. convex_hull(geometry, hull, strategy_type());
  148. }
  149. #ifndef DOXYGEN_NO_DETAIL
  150. namespace detail { namespace convex_hull
  151. {
  152. template<typename Geometry, typename OutputIterator, typename Strategy>
  153. inline OutputIterator convex_hull_insert(Geometry const& geometry,
  154. OutputIterator out, Strategy const& strategy)
  155. {
  156. // Concept: output point type = point type of input geometry
  157. concept::check<Geometry const>();
  158. concept::check<typename point_type<Geometry>::type>();
  159. BOOST_CONCEPT_ASSERT( (geometry::concept::ConvexHullStrategy<Strategy>) );
  160. return dispatch::convex_hull_insert
  161. <
  162. geometry::point_order<Geometry>::value
  163. >::apply(geometry, out, strategy);
  164. }
  165. /*!
  166. \brief Calculate the convex hull of a geometry, output-iterator version
  167. \ingroup convex_hull
  168. \tparam Geometry the input geometry type
  169. \tparam OutputIterator: an output-iterator
  170. \param geometry the geometry to calculate convex hull from
  171. \param out an output iterator outputing points of the convex hull
  172. \note This overloaded version outputs to an output iterator.
  173. In this case, nothing is known about its point-type or
  174. about its clockwise order. Therefore, the input point-type
  175. and order are copied
  176. */
  177. template<typename Geometry, typename OutputIterator>
  178. inline OutputIterator convex_hull_insert(Geometry const& geometry,
  179. OutputIterator out)
  180. {
  181. // Concept: output point type = point type of input geometry
  182. concept::check<Geometry const>();
  183. concept::check<typename point_type<Geometry>::type>();
  184. typedef typename detail::convex_hull::default_strategy<Geometry>::type strategy_type;
  185. return convex_hull_insert(geometry, out, strategy_type());
  186. }
  187. }} // namespace detail::convex_hull
  188. #endif // DOXYGEN_NO_DETAIL
  189. }} // namespace boost::geometry
  190. #endif // BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP