]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | ||
1e59de90 | 3 | // Copyright (c) 2014-2021, Oracle and/or its affiliates. |
7c673cae FG |
4 | |
5 | // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle | |
20effc67 | 6 | // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle |
7c673cae FG |
7 | |
8 | // Licensed under the Boost Software License version 1.0. | |
9 | // http://www.boost.org/users/license.html | |
10 | ||
11 | #ifndef BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP | |
12 | #define BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP | |
13 | ||
14 | #include <cstddef> | |
15 | ||
20effc67 TL |
16 | #include <boost/range/size.hpp> |
17 | #include <boost/range/value_type.hpp> | |
7c673cae | 18 | |
1e59de90 TL |
19 | #include <boost/geometry/algorithms/detail/counting.hpp> |
20 | #include <boost/geometry/algorithms/detail/visit.hpp> | |
21 | #include <boost/geometry/algorithms/not_implemented.hpp> | |
7c673cae FG |
22 | |
23 | #include <boost/geometry/core/closure.hpp> | |
24 | #include <boost/geometry/core/tag.hpp> | |
25 | #include <boost/geometry/core/tags.hpp> | |
1e59de90 | 26 | #include <boost/geometry/core/visit.hpp> |
7c673cae | 27 | |
1e59de90 | 28 | #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility |
7c673cae FG |
29 | #include <boost/geometry/geometries/concepts/check.hpp> |
30 | ||
1e59de90 | 31 | #include <boost/geometry/util/range.hpp> |
7c673cae FG |
32 | |
33 | namespace boost { namespace geometry | |
34 | { | |
35 | ||
36 | #ifndef DOXYGEN_NO_DETAIL | |
37 | namespace detail { namespace num_segments | |
38 | { | |
39 | ||
40 | ||
41 | struct range_count | |
42 | { | |
43 | template <typename Range> | |
44 | static inline std::size_t apply(Range const& range) | |
45 | { | |
46 | std::size_t n = boost::size(range); | |
47 | if ( n <= 1 ) | |
48 | { | |
49 | return 0; | |
50 | } | |
51 | ||
52 | return | |
53 | geometry::closure<Range>::value == open | |
54 | ? | |
55 | n | |
56 | : | |
57 | static_cast<std::size_t>(n - 1); | |
58 | } | |
59 | }; | |
60 | ||
61 | }} // namespace detail::num_segments | |
62 | #endif // DOXYGEN_NO_DETAIL | |
63 | ||
64 | ||
65 | ||
66 | #ifndef DOXYGEN_NO_DISPATCH | |
67 | namespace dispatch | |
68 | { | |
69 | ||
70 | template <typename Geometry, typename Tag = typename tag<Geometry>::type> | |
71 | struct num_segments | |
72 | : not_implemented<Tag> | |
73 | {}; | |
74 | ||
75 | template <typename Geometry> | |
76 | struct num_segments<Geometry, point_tag> | |
77 | : detail::counting::other_count<0> | |
78 | {}; | |
79 | ||
80 | // the number of segments (1-dimensional faces) of the hypercube is | |
81 | // given by the formula: d * 2^(d-1), where d is the dimension of the | |
82 | // hypercube; see also: | |
83 | // http://en.wikipedia.org/wiki/Hypercube | |
84 | template <typename Geometry> | |
85 | struct num_segments<Geometry, box_tag> | |
86 | : detail::counting::other_count | |
87 | < | |
88 | geometry::dimension<Geometry>::value | |
89 | * (1 << (geometry::dimension<Geometry>::value - 1)) | |
90 | > | |
91 | {}; | |
92 | ||
93 | template <typename Geometry> | |
94 | struct num_segments<Geometry, segment_tag> | |
95 | : detail::counting::other_count<1> | |
96 | {}; | |
97 | ||
98 | template <typename Geometry> | |
99 | struct num_segments<Geometry, linestring_tag> | |
100 | : detail::num_segments::range_count | |
101 | {}; | |
102 | ||
103 | template <typename Geometry> | |
104 | struct num_segments<Geometry, ring_tag> | |
105 | : detail::num_segments::range_count | |
106 | {}; | |
107 | ||
108 | template <typename Geometry> | |
109 | struct num_segments<Geometry, polygon_tag> | |
110 | : detail::counting::polygon_count<detail::num_segments::range_count> | |
111 | {}; | |
112 | ||
113 | template <typename Geometry> | |
114 | struct num_segments<Geometry, multi_point_tag> | |
115 | : detail::counting::other_count<0> | |
116 | {}; | |
117 | ||
118 | template <typename Geometry> | |
119 | struct num_segments<Geometry, multi_linestring_tag> | |
120 | : detail::counting::multi_count | |
121 | < | |
122 | num_segments< typename boost::range_value<Geometry>::type> | |
123 | > | |
124 | {}; | |
125 | ||
126 | template <typename Geometry> | |
127 | struct num_segments<Geometry, multi_polygon_tag> | |
128 | : detail::counting::multi_count | |
129 | < | |
130 | num_segments< typename boost::range_value<Geometry>::type> | |
131 | > | |
132 | {}; | |
133 | ||
134 | ||
135 | } // namespace dispatch | |
136 | #endif // DOXYGEN_NO_DISPATCH | |
137 | ||
138 | ||
139 | ||
1e59de90 | 140 | namespace resolve_dynamic |
7c673cae FG |
141 | { |
142 | ||
143 | ||
1e59de90 | 144 | template <typename Geometry, typename Tag = typename tag<Geometry>::type> |
7c673cae FG |
145 | struct num_segments |
146 | { | |
147 | static inline std::size_t apply(Geometry const& geometry) | |
148 | { | |
149 | concepts::check<Geometry const>(); | |
150 | ||
151 | return dispatch::num_segments<Geometry>::apply(geometry); | |
152 | } | |
153 | }; | |
154 | ||
155 | ||
1e59de90 TL |
156 | template <typename Geometry> |
157 | struct num_segments<Geometry, dynamic_geometry_tag> | |
7c673cae | 158 | { |
1e59de90 | 159 | static inline std::size_t apply(Geometry const& geometry) |
7c673cae | 160 | { |
1e59de90 TL |
161 | std::size_t result = 0; |
162 | traits::visit<Geometry>::apply([&](auto const& g) | |
7c673cae | 163 | { |
1e59de90 TL |
164 | result = num_segments<util::remove_cref_t<decltype(g)>>::apply(g); |
165 | }, geometry); | |
166 | return result; | |
167 | } | |
168 | }; | |
7c673cae | 169 | |
1e59de90 TL |
170 | |
171 | template <typename Geometry> | |
172 | struct num_segments<Geometry, geometry_collection_tag> | |
173 | { | |
174 | static inline std::size_t apply(Geometry const& geometry) | |
7c673cae | 175 | { |
1e59de90 TL |
176 | std::size_t result = 0; |
177 | detail::visit_breadth_first([&](auto const& g) | |
178 | { | |
179 | result += num_segments<util::remove_cref_t<decltype(g)>>::apply(g); | |
180 | return true; | |
181 | }, geometry); | |
182 | return result; | |
7c673cae FG |
183 | } |
184 | }; | |
185 | ||
186 | ||
1e59de90 | 187 | } // namespace resolve_dynamic |
7c673cae FG |
188 | |
189 | ||
190 | ||
191 | /*! | |
192 | \brief \brief_calc{number of segments} | |
193 | \ingroup num_segments | |
194 | \details \details_calc{num_segments, number of segments}. | |
195 | \tparam Geometry \tparam_geometry | |
196 | \param geometry \param_geometry | |
197 | \return \return_calc{number of segments} | |
198 | ||
199 | \qbk{[include reference/algorithms/num_segments.qbk]} | |
200 | */ | |
201 | template <typename Geometry> | |
202 | inline std::size_t num_segments(Geometry const& geometry) | |
203 | { | |
1e59de90 | 204 | return resolve_dynamic::num_segments<Geometry>::apply(geometry); |
7c673cae FG |
205 | } |
206 | ||
207 | ||
208 | ||
209 | }} // namespace boost::geometry | |
210 | ||
211 | #endif // BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP |