is_convex.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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-2023.
  4. // Modifications copyright (c) 2017-2023 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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_IS_CONVEX_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP
  12. #include <boost/range/empty.hpp>
  13. #include <boost/range/size.hpp>
  14. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  15. #include <boost/geometry/algorithms/detail/dummy_geometries.hpp>
  16. #include <boost/geometry/algorithms/detail/visit.hpp>
  17. #include <boost/geometry/core/closure.hpp>
  18. #include <boost/geometry/core/exterior_ring.hpp>
  19. #include <boost/geometry/core/interior_rings.hpp>
  20. #include <boost/geometry/core/visit.hpp>
  21. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
  22. #include <boost/geometry/geometries/concepts/check.hpp>
  23. #include <boost/geometry/iterators/ever_circling_iterator.hpp>
  24. #include <boost/geometry/strategies/default_strategy.hpp>
  25. #include <boost/geometry/strategies/is_convex/cartesian.hpp>
  26. #include <boost/geometry/strategies/is_convex/geographic.hpp>
  27. #include <boost/geometry/strategies/is_convex/spherical.hpp>
  28. #include <boost/geometry/views/detail/closed_clockwise_view.hpp>
  29. namespace boost { namespace geometry
  30. {
  31. #ifndef DOXYGEN_NO_DETAIL
  32. namespace detail { namespace is_convex
  33. {
  34. struct ring_is_convex
  35. {
  36. template <typename Ring, typename Strategies>
  37. static inline bool apply(Ring const& ring, Strategies const& strategies)
  38. {
  39. std::size_t n = boost::size(ring);
  40. if (n < detail::minimum_ring_size<Ring>::value)
  41. {
  42. // (Too) small rings are considered as non-concave, is convex
  43. return true;
  44. }
  45. // Walk in clockwise direction, consider ring as closed
  46. // (though closure is not important in this algorithm - any dupped
  47. // point is skipped)
  48. using view_type = detail::closed_clockwise_view<Ring const>;
  49. view_type const view(ring);
  50. using it_type = geometry::ever_circling_range_iterator<view_type const>;
  51. it_type previous(view);
  52. it_type current(view);
  53. current++;
  54. auto const equals_strategy = strategies.relate(dummy_point(), dummy_point());
  55. std::size_t index = 1;
  56. while (equals::equals_point_point(*current, *previous, equals_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, equals_strategy))
  70. {
  71. next++;
  72. }
  73. auto const side_strategy = strategies.side();
  74. // We have now three different points on the ring
  75. // Walk through all points, use a counter because of the ever-circling
  76. // iterator
  77. for (std::size_t i = 0; i < n; i++)
  78. {
  79. int const side = side_strategy.apply(*previous, *current, *next);
  80. if (side == 1)
  81. {
  82. // Next is on the left side of clockwise ring:
  83. // the piece is not convex
  84. return false;
  85. }
  86. previous = current;
  87. current = next;
  88. // Advance next to next different point
  89. // (because there are non-equal points, this loop is not infinite)
  90. next++;
  91. while (equals::equals_point_point(*current, *next, equals_strategy))
  92. {
  93. next++;
  94. }
  95. }
  96. return true;
  97. }
  98. };
  99. struct polygon_is_convex
  100. {
  101. template <typename Polygon, typename Strategies>
  102. static inline bool apply(Polygon const& polygon, Strategies const& strategies)
  103. {
  104. return boost::empty(interior_rings(polygon))
  105. && ring_is_convex::apply(exterior_ring(polygon), strategies);
  106. }
  107. };
  108. struct multi_polygon_is_convex
  109. {
  110. template <typename MultiPolygon, typename Strategies>
  111. static inline bool apply(MultiPolygon const& multi_polygon, Strategies const& strategies)
  112. {
  113. auto const size = boost::size(multi_polygon);
  114. // TODO: this looks wrong, it should only return convex if all its rings are convex
  115. return size == 0 // For consistency with ring_is_convex
  116. || (size == 1 && polygon_is_convex::apply(range::front(multi_polygon), strategies));
  117. }
  118. };
  119. }} // namespace detail::is_convex
  120. #endif // DOXYGEN_NO_DETAIL
  121. #ifndef DOXYGEN_NO_DISPATCH
  122. namespace dispatch
  123. {
  124. template
  125. <
  126. typename Geometry,
  127. typename Tag = typename tag<Geometry>::type
  128. >
  129. struct is_convex
  130. {
  131. template <typename Strategies>
  132. static inline bool apply(Geometry const&, Strategies const&)
  133. {
  134. // Convexity is not defined for PointLike and Linear geometries.
  135. // We could implement this because the following definitions would work:
  136. // - no line segment between two points on the interior or boundary ever goes outside.
  137. // - convex_hull of geometry is equal to the original geometry, this implies equal
  138. // topological dimension.
  139. // For MultiPoint we'd have to check whether or not an arbitrary number of equal points
  140. // is stored.
  141. // MultiPolygon we'd have to check for continuous chain of Linestrings which would require
  142. // the use of relate(pt, seg) or distance(pt, pt) strategy.
  143. return false;
  144. }
  145. };
  146. template <typename Box>
  147. struct is_convex<Box, box_tag>
  148. {
  149. template <typename Strategies>
  150. static inline bool apply(Box const& , Strategies const& )
  151. {
  152. // Any box is convex (TODO: consider spherical boxes)
  153. // TODO: in spherical and geographic the answer would be "false" most of the time.
  154. // Assuming that:
  155. // - it even makes sense to consider Box in spherical and geographic in this context
  156. // because it's not a Polygon, e.g. it can degenerate to a Point.
  157. // - line segments are defined by geodesics and box edges by parallels and meridians
  158. // - we use this definition: A convex polygon is a simple polygon (not self-intersecting)
  159. // in which no line segment between two points on the boundary ever goes outside the
  160. // polygon.
  161. // Then a geodesic segment would go into the exterior of a Box for all horizontal edges
  162. // of a Box unless it was one of the poles (edge degenerated to a point) or equator and
  163. // longitude difference was lesser than 360 (otherwise depending on the CS there would be
  164. // no solution or there would be two possible solutions - segment going through one of
  165. // the poles, at least in case of oblate spheroid, either way the answer would probably
  166. // be "false").
  167. return true;
  168. }
  169. };
  170. template <typename Ring>
  171. struct is_convex<Ring, ring_tag> : detail::is_convex::ring_is_convex
  172. {};
  173. template <typename Polygon>
  174. struct is_convex<Polygon, polygon_tag> : detail::is_convex::polygon_is_convex
  175. {};
  176. template <typename MultiPolygon>
  177. struct is_convex<MultiPolygon, multi_polygon_tag> : detail::is_convex::multi_polygon_is_convex
  178. {};
  179. } // namespace dispatch
  180. #endif // DOXYGEN_NO_DISPATCH
  181. namespace resolve_strategy {
  182. template
  183. <
  184. typename Strategies,
  185. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  186. >
  187. struct is_convex
  188. {
  189. template <typename Geometry>
  190. static bool apply(Geometry const& geometry, Strategies const& strategies)
  191. {
  192. return dispatch::is_convex<Geometry>::apply(geometry, strategies);
  193. }
  194. };
  195. template <typename Strategy>
  196. struct is_convex<Strategy, false>
  197. {
  198. template <typename Geometry>
  199. static bool apply(Geometry const& geometry, Strategy const& strategy)
  200. {
  201. using strategies::is_convex::services::strategy_converter;
  202. return dispatch::is_convex
  203. <
  204. Geometry
  205. >::apply(geometry, strategy_converter<Strategy>::get(strategy));
  206. }
  207. };
  208. template <>
  209. struct is_convex<default_strategy, false>
  210. {
  211. template <typename Geometry>
  212. static bool apply(Geometry const& geometry, default_strategy const& )
  213. {
  214. typedef typename strategies::is_convex::services::default_strategy
  215. <
  216. Geometry
  217. >::type strategy_type;
  218. return dispatch::is_convex<Geometry>::apply(geometry, strategy_type());
  219. }
  220. };
  221. } // namespace resolve_strategy
  222. namespace resolve_dynamic {
  223. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  224. struct is_convex
  225. {
  226. template <typename Strategy>
  227. static bool apply(Geometry const& geometry, Strategy const& strategy)
  228. {
  229. concepts::check<Geometry const>();
  230. return resolve_strategy::is_convex<Strategy>::apply(geometry, strategy);
  231. }
  232. };
  233. template <typename Geometry>
  234. struct is_convex<Geometry, dynamic_geometry_tag>
  235. {
  236. template <typename Strategy>
  237. static inline bool apply(Geometry const& geometry, Strategy const& strategy)
  238. {
  239. bool result = false;
  240. traits::visit<Geometry>::apply([&](auto const& g)
  241. {
  242. result = is_convex<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  243. }, geometry);
  244. return result;
  245. }
  246. };
  247. // NOTE: This is a simple implementation checking if a GC contains single convex geometry.
  248. // Technically a GC could store e.g. polygons touching with edges and together creating a convex
  249. // region. To check this we'd require relate() strategy and the algorithm would be quite complex.
  250. template <typename Geometry>
  251. struct is_convex<Geometry, geometry_collection_tag>
  252. {
  253. template <typename Strategy>
  254. static inline bool apply(Geometry const& geometry, Strategy const& strategy)
  255. {
  256. bool result = false;
  257. bool is_first = true;
  258. detail::visit_breadth_first([&](auto const& g)
  259. {
  260. result = is_first
  261. && is_convex<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  262. is_first = false;
  263. return result;
  264. }, geometry);
  265. return result;
  266. }
  267. };
  268. } // namespace resolve_dynamic
  269. // TODO: documentation / qbk
  270. template<typename Geometry>
  271. inline bool is_convex(Geometry const& geometry)
  272. {
  273. return resolve_dynamic::is_convex
  274. <
  275. Geometry
  276. >::apply(geometry, geometry::default_strategy());
  277. }
  278. // TODO: documentation / qbk
  279. template<typename Geometry, typename Strategy>
  280. inline bool is_convex(Geometry const& geometry, Strategy const& strategy)
  281. {
  282. return resolve_dynamic::is_convex<Geometry>::apply(geometry, strategy);
  283. }
  284. }} // namespace boost::geometry
  285. #endif // BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP