1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7 // Licensed under the Boost Software License version 1.0.
8 // http://www.boost.org/users/license.html
10 #ifndef BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP
11 #define BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP
15 #include <boost/mpl/size_t.hpp>
16 #include <boost/mpl/times.hpp>
18 #include <boost/range.hpp>
20 #include <boost/variant/apply_visitor.hpp>
21 #include <boost/variant/static_visitor.hpp>
22 #include <boost/variant/variant_fwd.hpp>
24 #include <boost/geometry/core/closure.hpp>
25 #include <boost/geometry/core/tag.hpp>
26 #include <boost/geometry/core/tags.hpp>
28 #include <boost/geometry/util/range.hpp>
30 #include <boost/geometry/geometries/concepts/check.hpp>
32 #include <boost/geometry/algorithms/not_implemented.hpp>
34 #include <boost/geometry/algorithms/detail/counting.hpp>
37 namespace boost { namespace geometry
40 #ifndef DOXYGEN_NO_DETAIL
41 namespace detail { namespace num_segments
47 template <typename Range>
48 static inline std::size_t apply(Range const& range)
50 std::size_t n = boost::size(range);
57 geometry::closure<Range>::value == open
61 static_cast<std::size_t>(n - 1);
65 }} // namespace detail::num_segments
66 #endif // DOXYGEN_NO_DETAIL
70 #ifndef DOXYGEN_NO_DISPATCH
74 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
76 : not_implemented<Tag>
79 template <typename Geometry>
80 struct num_segments<Geometry, point_tag>
81 : detail::counting::other_count<0>
84 // the number of segments (1-dimensional faces) of the hypercube is
85 // given by the formula: d * 2^(d-1), where d is the dimension of the
86 // hypercube; see also:
87 // http://en.wikipedia.org/wiki/Hypercube
88 template <typename Geometry>
89 struct num_segments<Geometry, box_tag>
90 : detail::counting::other_count
92 geometry::dimension<Geometry>::value
93 * (1 << (geometry::dimension<Geometry>::value - 1))
97 template <typename Geometry>
98 struct num_segments<Geometry, segment_tag>
99 : detail::counting::other_count<1>
102 template <typename Geometry>
103 struct num_segments<Geometry, linestring_tag>
104 : detail::num_segments::range_count
107 template <typename Geometry>
108 struct num_segments<Geometry, ring_tag>
109 : detail::num_segments::range_count
112 template <typename Geometry>
113 struct num_segments<Geometry, polygon_tag>
114 : detail::counting::polygon_count<detail::num_segments::range_count>
117 template <typename Geometry>
118 struct num_segments<Geometry, multi_point_tag>
119 : detail::counting::other_count<0>
122 template <typename Geometry>
123 struct num_segments<Geometry, multi_linestring_tag>
124 : detail::counting::multi_count
126 num_segments< typename boost::range_value<Geometry>::type>
130 template <typename Geometry>
131 struct num_segments<Geometry, multi_polygon_tag>
132 : detail::counting::multi_count
134 num_segments< typename boost::range_value<Geometry>::type>
139 } // namespace dispatch
140 #endif // DOXYGEN_NO_DISPATCH
144 namespace resolve_variant
148 template <typename Geometry>
151 static inline std::size_t apply(Geometry const& geometry)
153 concepts::check<Geometry const>();
155 return dispatch::num_segments<Geometry>::apply(geometry);
160 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
161 struct num_segments<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
163 struct visitor: boost::static_visitor<std::size_t>
165 template <typename Geometry>
166 inline std::size_t operator()(Geometry const& geometry) const
168 return num_segments<Geometry>::apply(geometry);
172 static inline std::size_t
173 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry)
175 return boost::apply_visitor(visitor(), geometry);
180 } // namespace resolve_variant
185 \brief \brief_calc{number of segments}
186 \ingroup num_segments
187 \details \details_calc{num_segments, number of segments}.
188 \tparam Geometry \tparam_geometry
189 \param geometry \param_geometry
190 \return \return_calc{number of segments}
192 \qbk{[include reference/algorithms/num_segments.qbk]}
194 template <typename Geometry>
195 inline std::size_t num_segments(Geometry const& geometry)
197 return resolve_variant::num_segments<Geometry>::apply(geometry);
202 }} // namespace boost::geometry
204 #endif // BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP