is_convex.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2017.
  4. // Modifications copyright (c) 2017 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP
  11. #include <boost/variant/apply_visitor.hpp>
  12. #include <boost/variant/static_visitor.hpp>
  13. #include <boost/variant/variant_fwd.hpp>
  14. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  15. #include <boost/geometry/core/access.hpp>
  16. #include <boost/geometry/core/closure.hpp>
  17. #include <boost/geometry/core/cs.hpp>
  18. #include <boost/geometry/core/coordinate_dimension.hpp>
  19. #include <boost/geometry/core/point_type.hpp>
  20. #include <boost/geometry/geometries/concepts/check.hpp>
  21. #include <boost/geometry/iterators/ever_circling_iterator.hpp>
  22. #include <boost/geometry/strategies/default_strategy.hpp>
  23. #include <boost/geometry/strategies/side.hpp>
  24. #include <boost/geometry/views/detail/normalized_view.hpp>
  25. namespace boost { namespace geometry
  26. {
  27. #ifndef DOXYGEN_NO_DETAIL
  28. namespace detail { namespace is_convex
  29. {
  30. struct ring_is_convex
  31. {
  32. template <typename Ring, typename SideStrategy>
  33. static inline bool apply(Ring const& ring, SideStrategy const& strategy)
  34. {
  35. std::size_t n = boost::size(ring);
  36. if (boost::size(ring) < core_detail::closure::minimum_ring_size
  37. <
  38. geometry::closure<Ring>::value
  39. >::value)
  40. {
  41. // (Too) small rings are considered as non-concave, is convex
  42. return true;
  43. }
  44. // Walk in clockwise direction, consider ring as closed
  45. // (though closure is not important in this algorithm - any dupped
  46. // point is skipped)
  47. typedef detail::normalized_view<Ring const> view_type;
  48. view_type view(ring);
  49. typedef geometry::ever_circling_range_iterator<view_type const> it_type;
  50. it_type previous(view);
  51. it_type current(view);
  52. current++;
  53. std::size_t index = 1;
  54. while (equals::equals_point_point(*current, *previous) && index < n)
  55. {
  56. current++;
  57. index++;
  58. }
  59. if (index == n)
  60. {
  61. // All points are apparently equal
  62. return true;
  63. }
  64. it_type next = current;
  65. next++;
  66. while (equals::equals_point_point(*current, *next))
  67. {
  68. next++;
  69. }
  70. // We have now three different points on the ring
  71. // Walk through all points, use a counter because of the ever-circling
  72. // iterator
  73. for (std::size_t i = 0; i < n; i++)
  74. {
  75. int const side = strategy.apply(*previous, *current, *next);
  76. if (side == 1)
  77. {
  78. // Next is on the left side of clockwise ring:
  79. // the piece is not convex
  80. return false;
  81. }
  82. previous = current;
  83. current = next;
  84. // Advance next to next different point
  85. // (because there are non-equal points, this loop is not infinite)
  86. next++;
  87. while (equals::equals_point_point(*current, *next))
  88. {
  89. next++;
  90. }
  91. }
  92. return true;
  93. }
  94. };
  95. }} // namespace detail::is_convex
  96. #endif // DOXYGEN_NO_DETAIL
  97. #ifndef DOXYGEN_NO_DISPATCH
  98. namespace dispatch
  99. {
  100. template
  101. <
  102. typename Geometry,
  103. typename Tag = typename tag<Geometry>::type
  104. >
  105. struct is_convex : not_implemented<Tag>
  106. {};
  107. template <typename Box>
  108. struct is_convex<Box, box_tag>
  109. {
  110. template <typename Strategy>
  111. static inline bool apply(Box const& , Strategy const& )
  112. {
  113. // Any box is convex (TODO: consider spherical boxes)
  114. return true;
  115. }
  116. };
  117. template <typename Box>
  118. struct is_convex<Box, ring_tag> : detail::is_convex::ring_is_convex
  119. {};
  120. } // namespace dispatch
  121. #endif // DOXYGEN_NO_DISPATCH
  122. namespace resolve_variant {
  123. template <typename Geometry>
  124. struct is_convex
  125. {
  126. template <typename Strategy>
  127. static bool apply(Geometry const& geometry, Strategy const& strategy)
  128. {
  129. concepts::check<Geometry>();
  130. return dispatch::is_convex<Geometry>::apply(geometry, strategy);
  131. }
  132. static bool apply(Geometry const& geometry, geometry::default_strategy const&)
  133. {
  134. typedef typename strategy::side::services::default_strategy
  135. <
  136. typename cs_tag<Geometry>::type
  137. >::type side_strategy;
  138. return apply(geometry, side_strategy());
  139. }
  140. };
  141. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  142. struct is_convex<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  143. {
  144. template <typename Strategy>
  145. struct visitor: boost::static_visitor<bool>
  146. {
  147. Strategy const& m_strategy;
  148. visitor(Strategy const& strategy) : m_strategy(strategy) {}
  149. template <typename Geometry>
  150. bool operator()(Geometry const& geometry) const
  151. {
  152. return is_convex<Geometry>::apply(geometry, m_strategy);
  153. }
  154. };
  155. template <typename Strategy>
  156. static inline bool apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  157. Strategy const& strategy)
  158. {
  159. return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  160. }
  161. };
  162. } // namespace resolve_variant
  163. // TODO: documentation / qbk
  164. template<typename Geometry>
  165. inline bool is_convex(Geometry const& geometry)
  166. {
  167. return resolve_variant::is_convex
  168. <
  169. Geometry
  170. >::apply(geometry, geometry::default_strategy());
  171. }
  172. // TODO: documentation / qbk
  173. template<typename Geometry, typename Strategy>
  174. inline bool is_convex(Geometry const& geometry, Strategy const& strategy)
  175. {
  176. return resolve_variant::is_convex<Geometry>::apply(geometry, strategy);
  177. }
  178. }} // namespace boost::geometry
  179. #endif // BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP