1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2015 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP
10 #define BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP
13 #include <boost/geometry/core/access.hpp>
14 #include <boost/geometry/core/closure.hpp>
15 #include <boost/geometry/core/cs.hpp>
16 #include <boost/geometry/core/coordinate_dimension.hpp>
17 #include <boost/geometry/core/point_type.hpp>
18 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
19 #include <boost/geometry/iterators/ever_circling_iterator.hpp>
20 #include <boost/geometry/strategies/side.hpp>
21 #include <boost/geometry/strategies/cartesian/side_by_triangle.hpp>
22 #include <boost/geometry/views/detail/normalized_view.hpp>
24 namespace boost { namespace geometry
28 #ifndef DOXYGEN_NO_DETAIL
29 namespace detail { namespace is_convex
34 template <typename Ring>
35 static inline bool apply(Ring const& ring)
37 typedef typename geometry::point_type<Ring>::type point_type;
38 typedef typename strategy::side::services::default_strategy
40 typename cs_tag<point_type>::type
41 >::type side_strategy_type;
43 std::size_t n = boost::size(ring);
44 if (boost::size(ring) < core_detail::closure::minimum_ring_size
46 geometry::closure<Ring>::value
49 // (Too) small rings are considered as non-concave, is convex
53 // Walk in clockwise direction, consider ring as closed
54 // (though closure is not important in this algorithm - any dupped
56 typedef detail::normalized_view<Ring const> view_type;
59 typedef geometry::ever_circling_range_iterator<view_type const> it_type;
60 it_type previous(view);
61 it_type current(view);
64 std::size_t index = 1;
65 while (equals::equals_point_point(*current, *previous) && index < n)
73 // All points are apparently equal
77 it_type next = current;
79 while (equals::equals_point_point(*current, *next))
84 // We have now three different points on the ring
85 // Walk through all points, use a counter because of the ever-circling
87 for (std::size_t i = 0; i < n; i++)
89 int const side = side_strategy_type::apply(*previous, *current, *next);
92 // Next is on the left side of clockwise ring:
93 // the piece is not convex
100 // Advance next to next different point
101 // (because there are non-equal points, this loop is not infinite)
103 while (equals::equals_point_point(*current, *next))
113 }} // namespace detail::is_convex
114 #endif // DOXYGEN_NO_DETAIL
117 #ifndef DOXYGEN_NO_DISPATCH
124 typename Tag = typename tag<Geometry>::type
126 struct is_convex : not_implemented<Tag>
129 template <typename Box>
130 struct is_convex<Box, box_tag>
132 static inline bool apply(Box const& )
134 // Any box is convex (TODO: consider spherical boxes)
139 template <typename Box>
140 struct is_convex<Box, ring_tag> : detail::is_convex::ring_is_convex
144 } // namespace dispatch
145 #endif // DOXYGEN_NO_DISPATCH
149 // TODO: documentation / qbk
150 template<typename Geometry>
151 inline bool is_convex(Geometry const& geometry)
153 return dispatch::is_convex<Geometry>::apply(geometry);
157 }} // namespace boost::geometry
160 #endif // BOOST_GEOMETRY_ALGORITHMS_IS_CONVEX_HPP