123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- #ifndef BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP
- #define BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP
- #include <boost/range/empty.hpp>
- #include <boost/range/size.hpp>
- #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
- #include <boost/geometry/algorithms/detail/dummy_geometries.hpp>
- #include <boost/geometry/algorithms/detail/visit.hpp>
- #include <boost/geometry/core/closure.hpp>
- #include <boost/geometry/core/exterior_ring.hpp>
- #include <boost/geometry/core/interior_rings.hpp>
- #include <boost/geometry/core/visit.hpp>
- #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
- #include <boost/geometry/geometries/concepts/check.hpp>
- #include <boost/geometry/iterators/ever_circling_iterator.hpp>
- #include <boost/geometry/strategies/default_strategy.hpp>
- #include <boost/geometry/strategies/is_convex/cartesian.hpp>
- #include <boost/geometry/strategies/is_convex/geographic.hpp>
- #include <boost/geometry/strategies/is_convex/spherical.hpp>
- #include <boost/geometry/views/detail/closed_clockwise_view.hpp>
- namespace boost { namespace geometry
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace is_convex
- {
- struct ring_is_convex
- {
- template <typename Ring, typename Strategies>
- static inline bool apply(Ring const& ring, Strategies const& strategies)
- {
- std::size_t n = boost::size(ring);
- if (n < detail::minimum_ring_size<Ring>::value)
- {
-
- return true;
- }
-
-
-
- using view_type = detail::closed_clockwise_view<Ring const>;
- view_type const view(ring);
- using it_type = geometry::ever_circling_range_iterator<view_type const>;
- it_type previous(view);
- it_type current(view);
- current++;
- auto const equals_strategy = strategies.relate(dummy_point(), dummy_point());
- std::size_t index = 1;
- while (equals::equals_point_point(*current, *previous, equals_strategy)
- && index < n)
- {
- current++;
- index++;
- }
- if (index == n)
- {
-
- return true;
- }
- it_type next = current;
- next++;
- while (equals::equals_point_point(*current, *next, equals_strategy))
- {
- next++;
- }
- auto const side_strategy = strategies.side();
-
-
-
- for (std::size_t i = 0; i < n; i++)
- {
- int const side = side_strategy.apply(*previous, *current, *next);
- if (side == 1)
- {
-
-
- return false;
- }
- previous = current;
- current = next;
-
-
- next++;
- while (equals::equals_point_point(*current, *next, equals_strategy))
- {
- next++;
- }
- }
- return true;
- }
- };
- struct polygon_is_convex
- {
- template <typename Polygon, typename Strategies>
- static inline bool apply(Polygon const& polygon, Strategies const& strategies)
- {
- return boost::empty(interior_rings(polygon))
- && ring_is_convex::apply(exterior_ring(polygon), strategies);
- }
- };
- struct multi_polygon_is_convex
- {
- template <typename MultiPolygon, typename Strategies>
- static inline bool apply(MultiPolygon const& multi_polygon, Strategies const& strategies)
- {
- auto const size = boost::size(multi_polygon);
-
- return size == 0
- || (size == 1 && polygon_is_convex::apply(range::front(multi_polygon), strategies));
- }
- };
- }}
- #endif
- #ifndef DOXYGEN_NO_DISPATCH
- namespace dispatch
- {
- template
- <
- typename Geometry,
- typename Tag = typename tag<Geometry>::type
- >
- struct is_convex
- {
- template <typename Strategies>
- static inline bool apply(Geometry const&, Strategies const&)
- {
-
-
-
-
-
-
-
-
-
- return false;
- }
- };
- template <typename Box>
- struct is_convex<Box, box_tag>
- {
- template <typename Strategies>
- static inline bool apply(Box const& , Strategies const& )
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return true;
- }
- };
- template <typename Ring>
- struct is_convex<Ring, ring_tag> : detail::is_convex::ring_is_convex
- {};
- template <typename Polygon>
- struct is_convex<Polygon, polygon_tag> : detail::is_convex::polygon_is_convex
- {};
- template <typename MultiPolygon>
- struct is_convex<MultiPolygon, multi_polygon_tag> : detail::is_convex::multi_polygon_is_convex
- {};
- }
- #endif
- namespace resolve_strategy {
- template
- <
- typename Strategies,
- bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
- >
- struct is_convex
- {
- template <typename Geometry>
- static bool apply(Geometry const& geometry, Strategies const& strategies)
- {
- return dispatch::is_convex<Geometry>::apply(geometry, strategies);
- }
- };
- template <typename Strategy>
- struct is_convex<Strategy, false>
- {
- template <typename Geometry>
- static bool apply(Geometry const& geometry, Strategy const& strategy)
- {
- using strategies::is_convex::services::strategy_converter;
- return dispatch::is_convex
- <
- Geometry
- >::apply(geometry, strategy_converter<Strategy>::get(strategy));
- }
- };
- template <>
- struct is_convex<default_strategy, false>
- {
- template <typename Geometry>
- static bool apply(Geometry const& geometry, default_strategy const& )
- {
- typedef typename strategies::is_convex::services::default_strategy
- <
- Geometry
- >::type strategy_type;
- return dispatch::is_convex<Geometry>::apply(geometry, strategy_type());
- }
- };
- }
- namespace resolve_dynamic {
- template <typename Geometry, typename Tag = typename tag<Geometry>::type>
- struct is_convex
- {
- template <typename Strategy>
- static bool apply(Geometry const& geometry, Strategy const& strategy)
- {
- concepts::check<Geometry const>();
- return resolve_strategy::is_convex<Strategy>::apply(geometry, strategy);
- }
- };
- template <typename Geometry>
- struct is_convex<Geometry, dynamic_geometry_tag>
- {
- template <typename Strategy>
- static inline bool apply(Geometry const& geometry, Strategy const& strategy)
- {
- bool result = false;
- traits::visit<Geometry>::apply([&](auto const& g)
- {
- result = is_convex<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
- }, geometry);
- return result;
- }
- };
- template <typename Geometry>
- struct is_convex<Geometry, geometry_collection_tag>
- {
- template <typename Strategy>
- static inline bool apply(Geometry const& geometry, Strategy const& strategy)
- {
- bool result = false;
- bool is_first = true;
- detail::visit_breadth_first([&](auto const& g)
- {
- result = is_first
- && is_convex<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
- is_first = false;
- return result;
- }, geometry);
- return result;
- }
- };
- }
- template<typename Geometry>
- inline bool is_convex(Geometry const& geometry)
- {
- return resolve_dynamic::is_convex
- <
- Geometry
- >::apply(geometry, geometry::default_strategy());
- }
- template<typename Geometry, typename Strategy>
- inline bool is_convex(Geometry const& geometry, Strategy const& strategy)
- {
- return resolve_dynamic::is_convex<Geometry>::apply(geometry, strategy);
- }
- }}
- #endif
|