is_convex.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, 2018.
  4. // Modifications copyright (c) 2017-2018 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. typename SideStrategy::equals_point_point_strategy_type
  36. eq_pp_strategy = strategy.get_equals_point_point_strategy();
  37. std::size_t n = boost::size(ring);
  38. if (boost::size(ring) < core_detail::closure::minimum_ring_size
  39. <
  40. geometry::closure<Ring>::value
  41. >::value)
  42. {
  43. // (Too) small rings are considered as non-concave, is convex
  44. return true;
  45. }
  46. // Walk in clockwise direction, consider ring as closed
  47. // (though closure is not important in this algorithm - any dupped
  48. // point is skipped)
  49. typedef detail::normalized_view<Ring const> view_type;
  50. view_type view(ring);
  51. typedef geometry::ever_circling_range_iterator<view_type const> it_type;
  52. it_type previous(view);
  53. it_type current(view);
  54. current++;
  55. std::size_t index = 1;
  56. while (equals::equals_point_point(*current, *previous, eq_pp_strategy)
  57. && index < n)
  58. {
  59. current++;
  60. index++;
  61. }
  62. if (index == n)
  63. {
  64. // All points are apparently equal
  65. return true;
  66. }
  67. it_type next = current;
  68. next++;
  69. while (equals::equals_point_point(*current, *next, eq_pp_strategy))
  70. {
  71. next++;
  72. }
  73. // We have now three different points on the ring
  74. // Walk through all points, use a counter because of the ever-circling
  75. // iterator
  76. for (std::size_t i = 0; i < n; i++)
  77. {
  78. int const side = strategy.apply(*previous, *current, *next);
  79. if (side == 1)
  80. {
  81. // Next is on the left side of clockwise ring:
  82. // the piece is not convex
  83. return false;
  84. }
  85. previous = current;
  86. current = next;
  87. // Advance next to next different point
  88. // (because there are non-equal points, this loop is not infinite)
  89. next++;
  90. while (equals::equals_point_point(*current, *next, eq_pp_strategy))
  91. {
  92. next++;
  93. }
  94. }
  95. return true;
  96. }
  97. };
  98. }} // namespace detail::is_convex
  99. #endif // DOXYGEN_NO_DETAIL
  100. #ifndef DOXYGEN_NO_DISPATCH
  101. namespace dispatch
  102. {
  103. template
  104. <
  105. typename Geometry,
  106. typename Tag = typename tag<Geometry>::type
  107. >
  108. struct is_convex : not_implemented<Tag>
  109. {};
  110. template <typename Box>
  111. struct is_convex<Box, box_tag>
  112. {
  113. template <typename Strategy>
  114. static inline bool apply(Box const& , Strategy const& )
  115. {
  116. // Any box is convex (TODO: consider spherical boxes)
  117. return true;
  118. }
  119. };
  120. template <typename Box>
  121. struct is_convex<Box, ring_tag> : detail::is_convex::ring_is_convex
  122. {};
  123. } // namespace dispatch
  124. #endif // DOXYGEN_NO_DISPATCH
  125. namespace resolve_variant {
  126. template <typename Geometry>
  127. struct is_convex
  128. {
  129. template <typename Strategy>
  130. static bool apply(Geometry const& geometry, Strategy const& strategy)
  131. {
  132. concepts::check<Geometry>();
  133. return dispatch::is_convex<Geometry>::apply(geometry, strategy);
  134. }
  135. static bool apply(Geometry const& geometry, geometry::default_strategy const&)
  136. {
  137. typedef typename strategy::side::services::default_strategy
  138. <
  139. typename cs_tag<Geometry>::type
  140. >::type side_strategy;
  141. return apply(geometry, side_strategy());
  142. }
  143. };
  144. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  145. struct is_convex<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  146. {
  147. template <typename Strategy>
  148. struct visitor: boost::static_visitor<bool>
  149. {
  150. Strategy const& m_strategy;
  151. visitor(Strategy const& strategy) : m_strategy(strategy) {}
  152. template <typename Geometry>
  153. bool operator()(Geometry const& geometry) const
  154. {
  155. return is_convex<Geometry>::apply(geometry, m_strategy);
  156. }
  157. };
  158. template <typename Strategy>
  159. static inline bool apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  160. Strategy const& strategy)
  161. {
  162. return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  163. }
  164. };
  165. } // namespace resolve_variant
  166. // TODO: documentation / qbk
  167. template<typename Geometry>
  168. inline bool is_convex(Geometry const& geometry)
  169. {
  170. return resolve_variant::is_convex
  171. <
  172. Geometry
  173. >::apply(geometry, geometry::default_strategy());
  174. }
  175. // TODO: documentation / qbk
  176. template<typename Geometry, typename Strategy>
  177. inline bool is_convex(Geometry const& geometry, Strategy const& strategy)
  178. {
  179. return resolve_variant::is_convex<Geometry>::apply(geometry, strategy);
  180. }
  181. }} // namespace boost::geometry
  182. #endif // BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP