1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
6
7 // This file was modified by Oracle on 2014-2020.
8 // Modifications copyright (c) 2014-2020, Oracle and/or its affiliates.
9
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15
16 // Use, modification and distribution is subject to the Boost Software License,
17 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19
20 #ifndef BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
22
23 #include <iterator>
24
25 #include <boost/concept_check.hpp>
26 #include <boost/core/ignore_unused.hpp>
27 #include <boost/range/begin.hpp>
28 #include <boost/range/end.hpp>
29 #include <boost/range/iterator.hpp>
30 #include <boost/range/value_type.hpp>
31 #include <boost/variant/apply_visitor.hpp>
32 #include <boost/variant/static_visitor.hpp>
33 #include <boost/variant/variant_fwd.hpp>
34
35 #include <boost/geometry/core/cs.hpp>
36 #include <boost/geometry/core/closure.hpp>
37 #include <boost/geometry/core/tags.hpp>
38
39 #include <boost/geometry/geometries/concepts/check.hpp>
40
41 #include <boost/geometry/algorithms/assign.hpp>
42 #include <boost/geometry/algorithms/detail/calculate_null.hpp>
43 #include <boost/geometry/algorithms/detail/multi_sum.hpp>
44 // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
45 #include <boost/geometry/views/closeable_view.hpp>
46 #include <boost/geometry/strategies/default_strategy.hpp>
47 #include <boost/geometry/strategies/distance.hpp>
48 #include <boost/geometry/strategies/default_length_result.hpp>
49
50
51 namespace boost { namespace geometry
52 {
53
54
55 #ifndef DOXYGEN_NO_DETAIL
56 namespace detail { namespace length
57 {
58
59
60 template<typename Segment>
61 struct segment_length
62 {
63 template <typename Strategy>
applyboost::geometry::detail::length::segment_length64 static inline typename default_length_result<Segment>::type apply(
65 Segment const& segment, Strategy const& strategy)
66 {
67 boost::ignore_unused(strategy);
68 typedef typename point_type<Segment>::type point_type;
69 point_type p1, p2;
70 geometry::detail::assign_point_from_index<0>(segment, p1);
71 geometry::detail::assign_point_from_index<1>(segment, p2);
72 return strategy.apply(p1, p2);
73 }
74 };
75
76 /*!
77 \brief Internal, calculates length of a linestring using iterator pairs and
78 specified strategy
79 \note for_each could be used here, now that point_type is changed by boost
80 range iterator
81 */
82 template<typename Range, closure_selector Closure>
83 struct range_length
84 {
85 typedef typename default_length_result<Range>::type return_type;
86
87 template <typename Strategy>
applyboost::geometry::detail::length::range_length88 static inline return_type apply(
89 Range const& range, Strategy const& strategy)
90 {
91 boost::ignore_unused(strategy);
92 typedef typename closeable_view<Range const, Closure>::type view_type;
93 typedef typename boost::range_iterator
94 <
95 view_type const
96 >::type iterator_type;
97
98 return_type sum = return_type();
99 view_type view(range);
100 iterator_type it = boost::begin(view), end = boost::end(view);
101 if(it != end)
102 {
103 for(iterator_type previous = it++;
104 it != end;
105 ++previous, ++it)
106 {
107 // Add point-point distance using the return type belonging
108 // to strategy
109 sum += strategy.apply(*previous, *it);
110 }
111 }
112
113 return sum;
114 }
115 };
116
117
118 }} // namespace detail::length
119 #endif // DOXYGEN_NO_DETAIL
120
121
122 #ifndef DOXYGEN_NO_DISPATCH
123 namespace dispatch
124 {
125
126
127 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
128 struct length : detail::calculate_null
129 {
130 typedef typename default_length_result<Geometry>::type return_type;
131
132 template <typename Strategy>
applyboost::geometry::dispatch::length133 static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
134 {
135 return calculate_null::apply<return_type>(geometry, strategy);
136 }
137 };
138
139
140 template <typename Geometry>
141 struct length<Geometry, linestring_tag>
142 : detail::length::range_length<Geometry, closed>
143 {};
144
145
146 // RING: length is currently 0; it might be argued that it is the "perimeter"
147
148
149 template <typename Geometry>
150 struct length<Geometry, segment_tag>
151 : detail::length::segment_length<Geometry>
152 {};
153
154
155 template <typename MultiLinestring>
156 struct length<MultiLinestring, multi_linestring_tag> : detail::multi_sum
157 {
158 template <typename Strategy>
159 static inline typename default_length_result<MultiLinestring>::type
applyboost::geometry::dispatch::length160 apply(MultiLinestring const& multi, Strategy const& strategy)
161 {
162 return multi_sum::apply
163 <
164 typename default_length_result<MultiLinestring>::type,
165 detail::length::range_length
166 <
167 typename boost::range_value<MultiLinestring>::type,
168 closed // no need to close it explicitly
169 >
170 >(multi, strategy);
171
172 }
173 };
174
175
176 } // namespace dispatch
177 #endif // DOXYGEN_NO_DISPATCH
178
179
180 namespace resolve_strategy {
181
182 struct length
183 {
184 template <typename Geometry, typename Strategy>
185 static inline typename default_length_result<Geometry>::type
applyboost::geometry::resolve_strategy::length186 apply(Geometry const& geometry, Strategy const& strategy)
187 {
188 return dispatch::length<Geometry>::apply(geometry, strategy);
189 }
190
191 template <typename Geometry>
192 static inline typename default_length_result<Geometry>::type
applyboost::geometry::resolve_strategy::length193 apply(Geometry const& geometry, default_strategy)
194 {
195 typedef typename strategy::distance::services::default_strategy
196 <
197 point_tag, point_tag, typename point_type<Geometry>::type
198 >::type strategy_type;
199
200 return dispatch::length<Geometry>::apply(geometry, strategy_type());
201 }
202 };
203
204 } // namespace resolve_strategy
205
206
207 namespace resolve_variant {
208
209 template <typename Geometry>
210 struct length
211 {
212 template <typename Strategy>
213 static inline typename default_length_result<Geometry>::type
applyboost::geometry::resolve_variant::length214 apply(Geometry const& geometry, Strategy const& strategy)
215 {
216 return resolve_strategy::length::apply(geometry, strategy);
217 }
218 };
219
220 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
221 struct length<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
222 {
223 typedef typename default_length_result
224 <
225 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
226 >::type result_type;
227
228 template <typename Strategy>
229 struct visitor
230 : static_visitor<result_type>
231 {
232 Strategy const& m_strategy;
233
visitorboost::geometry::resolve_variant::length::visitor234 visitor(Strategy const& strategy)
235 : m_strategy(strategy)
236 {}
237
238 template <typename Geometry>
239 inline typename default_length_result<Geometry>::type
operator ()boost::geometry::resolve_variant::length::visitor240 operator()(Geometry const& geometry) const
241 {
242 return length<Geometry>::apply(geometry, m_strategy);
243 }
244 };
245
246 template <typename Strategy>
applyboost::geometry::resolve_variant::length247 static inline result_type apply(
248 variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
249 Strategy const& strategy
250 )
251 {
252 return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
253 }
254 };
255
256 } // namespace resolve_variant
257
258
259 /*!
260 \brief \brief_calc{length}
261 \ingroup length
262 \details \details_calc{length, length (the sum of distances between consecutive points)}. \details_default_strategy
263 \tparam Geometry \tparam_geometry
264 \param geometry \param_geometry
265 \return \return_calc{length}
266
267 \qbk{[include reference/algorithms/length.qbk]}
268 \qbk{[length] [length_output]}
269 */
270 template<typename Geometry>
271 inline typename default_length_result<Geometry>::type
length(Geometry const & geometry)272 length(Geometry const& geometry)
273 {
274 concepts::check<Geometry const>();
275
276 // detail::throw_on_empty_input(geometry);
277
278 return resolve_variant::length<Geometry>::apply(geometry, default_strategy());
279 }
280
281
282 /*!
283 \brief \brief_calc{length} \brief_strategy
284 \ingroup length
285 \details \details_calc{length, length (the sum of distances between consecutive points)} \brief_strategy. \details_strategy_reasons
286 \tparam Geometry \tparam_geometry
287 \tparam Strategy \tparam_strategy{distance}
288 \param geometry \param_geometry
289 \param strategy \param_strategy{distance}
290 \return \return_calc{length}
291
292 \qbk{distinguish,with strategy}
293 \qbk{[include reference/algorithms/length.qbk]}
294 \qbk{[length_with_strategy] [length_with_strategy_output]}
295 */
296 template<typename Geometry, typename Strategy>
297 inline typename default_length_result<Geometry>::type
length(Geometry const & geometry,Strategy const & strategy)298 length(Geometry const& geometry, Strategy const& strategy)
299 {
300 concepts::check<Geometry const>();
301
302 // detail::throw_on_empty_input(geometry);
303
304 return resolve_variant::length<Geometry>::apply(geometry, strategy);
305 }
306
307
308 }} // namespace boost::geometry
309
310 #endif // BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
311