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, 2015.
8 // Modifications copyright (c) 2014-2015, 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/mpl/fold.hpp>
28 #include <boost/mpl/greater.hpp>
29 #include <boost/mpl/if.hpp>
30 #include <boost/mpl/insert.hpp>
31 #include <boost/mpl/int.hpp>
32 #include <boost/mpl/set.hpp>
33 #include <boost/mpl/size.hpp>
34 #include <boost/mpl/transform.hpp>
35 #include <boost/range/begin.hpp>
36 #include <boost/range/end.hpp>
37 #include <boost/range/iterator.hpp>
38 #include <boost/range/value_type.hpp>
39 #include <boost/variant/apply_visitor.hpp>
40 #include <boost/variant/static_visitor.hpp>
41 #include <boost/variant/variant_fwd.hpp>
42 
43 #include <boost/geometry/core/cs.hpp>
44 #include <boost/geometry/core/closure.hpp>
45 #include <boost/geometry/core/tags.hpp>
46 
47 #include <boost/geometry/geometries/concepts/check.hpp>
48 
49 #include <boost/geometry/algorithms/assign.hpp>
50 #include <boost/geometry/algorithms/detail/calculate_null.hpp>
51 #include <boost/geometry/algorithms/detail/multi_sum.hpp>
52 // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
53 #include <boost/geometry/views/closeable_view.hpp>
54 #include <boost/geometry/strategies/default_strategy.hpp>
55 #include <boost/geometry/strategies/distance.hpp>
56 #include <boost/geometry/strategies/default_length_result.hpp>
57 
58 
59 namespace boost { namespace geometry
60 {
61 
62 
63 #ifndef DOXYGEN_NO_DETAIL
64 namespace detail { namespace length
65 {
66 
67 
68 template<typename Segment>
69 struct segment_length
70 {
71     template <typename Strategy>
applyboost::geometry::detail::length::segment_length72     static inline typename default_length_result<Segment>::type apply(
73             Segment const& segment, Strategy const& strategy)
74     {
75         boost::ignore_unused(strategy);
76         typedef typename point_type<Segment>::type point_type;
77         point_type p1, p2;
78         geometry::detail::assign_point_from_index<0>(segment, p1);
79         geometry::detail::assign_point_from_index<1>(segment, p2);
80         return strategy.apply(p1, p2);
81     }
82 };
83 
84 /*!
85 \brief Internal, calculates length of a linestring using iterator pairs and
86     specified strategy
87 \note for_each could be used here, now that point_type is changed by boost
88     range iterator
89 */
90 template<typename Range, closure_selector Closure>
91 struct range_length
92 {
93     typedef typename default_length_result<Range>::type return_type;
94 
95     template <typename Strategy>
applyboost::geometry::detail::length::range_length96     static inline return_type apply(
97             Range const& range, Strategy const& strategy)
98     {
99         boost::ignore_unused(strategy);
100         typedef typename closeable_view<Range const, Closure>::type view_type;
101         typedef typename boost::range_iterator
102             <
103                 view_type const
104             >::type iterator_type;
105 
106         return_type sum = return_type();
107         view_type view(range);
108         iterator_type it = boost::begin(view), end = boost::end(view);
109         if(it != end)
110         {
111             for(iterator_type previous = it++;
112                     it != end;
113                     ++previous, ++it)
114             {
115                 // Add point-point distance using the return type belonging
116                 // to strategy
117                 sum += strategy.apply(*previous, *it);
118             }
119         }
120 
121         return sum;
122     }
123 };
124 
125 
126 }} // namespace detail::length
127 #endif // DOXYGEN_NO_DETAIL
128 
129 
130 #ifndef DOXYGEN_NO_DISPATCH
131 namespace dispatch
132 {
133 
134 
135 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
136 struct length : detail::calculate_null
137 {
138     typedef typename default_length_result<Geometry>::type return_type;
139 
140     template <typename Strategy>
applyboost::geometry::dispatch::length141     static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
142     {
143         return calculate_null::apply<return_type>(geometry, strategy);
144     }
145 };
146 
147 
148 template <typename Geometry>
149 struct length<Geometry, linestring_tag>
150     : detail::length::range_length<Geometry, closed>
151 {};
152 
153 
154 // RING: length is currently 0; it might be argued that it is the "perimeter"
155 
156 
157 template <typename Geometry>
158 struct length<Geometry, segment_tag>
159     : detail::length::segment_length<Geometry>
160 {};
161 
162 
163 template <typename MultiLinestring>
164 struct length<MultiLinestring, multi_linestring_tag> : detail::multi_sum
165 {
166     template <typename Strategy>
167     static inline typename default_length_result<MultiLinestring>::type
applyboost::geometry::dispatch::length168     apply(MultiLinestring const& multi, Strategy const& strategy)
169     {
170         return multi_sum::apply
171                <
172                    typename default_length_result<MultiLinestring>::type,
173                    detail::length::range_length
174                    <
175                        typename boost::range_value<MultiLinestring>::type,
176                        closed // no need to close it explicitly
177                    >
178                >(multi, strategy);
179 
180     }
181 };
182 
183 
184 } // namespace dispatch
185 #endif // DOXYGEN_NO_DISPATCH
186 
187 
188 namespace resolve_strategy {
189 
190 struct length
191 {
192     template <typename Geometry, typename Strategy>
193     static inline typename default_length_result<Geometry>::type
applyboost::geometry::resolve_strategy::length194     apply(Geometry const& geometry, Strategy const& strategy)
195     {
196         return dispatch::length<Geometry>::apply(geometry, strategy);
197     }
198 
199     template <typename Geometry>
200     static inline typename default_length_result<Geometry>::type
applyboost::geometry::resolve_strategy::length201     apply(Geometry const& geometry, default_strategy)
202     {
203         typedef typename strategy::distance::services::default_strategy
204             <
205                 point_tag, point_tag, typename point_type<Geometry>::type
206             >::type strategy_type;
207 
208         return dispatch::length<Geometry>::apply(geometry, strategy_type());
209     }
210 };
211 
212 } // namespace resolve_strategy
213 
214 
215 namespace resolve_variant {
216 
217 template <typename Geometry>
218 struct length
219 {
220     template <typename Strategy>
221     static inline typename default_length_result<Geometry>::type
applyboost::geometry::resolve_variant::length222     apply(Geometry const& geometry, Strategy const& strategy)
223     {
224         return resolve_strategy::length::apply(geometry, strategy);
225     }
226 };
227 
228 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
229 struct length<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
230 {
231     typedef typename default_length_result
232         <
233             boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
234         >::type result_type;
235 
236     template <typename Strategy>
237     struct visitor
238         : static_visitor<result_type>
239     {
240         Strategy const& m_strategy;
241 
visitorboost::geometry::resolve_variant::length::visitor242         visitor(Strategy const& strategy)
243             : m_strategy(strategy)
244         {}
245 
246         template <typename Geometry>
247         inline typename default_length_result<Geometry>::type
operator ()boost::geometry::resolve_variant::length::visitor248         operator()(Geometry const& geometry) const
249         {
250             return length<Geometry>::apply(geometry, m_strategy);
251         }
252     };
253 
254     template <typename Strategy>
applyboost::geometry::resolve_variant::length255     static inline result_type apply(
256         variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
257         Strategy const& strategy
258     )
259     {
260         return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
261     }
262 };
263 
264 } // namespace resolve_variant
265 
266 
267 /*!
268 \brief \brief_calc{length}
269 \ingroup length
270 \details \details_calc{length, length (the sum of distances between consecutive points)}. \details_default_strategy
271 \tparam Geometry \tparam_geometry
272 \param geometry \param_geometry
273 \return \return_calc{length}
274 
275 \qbk{[include reference/algorithms/length.qbk]}
276 \qbk{[length] [length_output]}
277  */
278 template<typename Geometry>
279 inline typename default_length_result<Geometry>::type
length(Geometry const & geometry)280 length(Geometry const& geometry)
281 {
282     concepts::check<Geometry const>();
283 
284     // detail::throw_on_empty_input(geometry);
285 
286     return resolve_variant::length<Geometry>::apply(geometry, default_strategy());
287 }
288 
289 
290 /*!
291 \brief \brief_calc{length} \brief_strategy
292 \ingroup length
293 \details \details_calc{length, length (the sum of distances between consecutive points)} \brief_strategy. \details_strategy_reasons
294 \tparam Geometry \tparam_geometry
295 \tparam Strategy \tparam_strategy{distance}
296 \param geometry \param_geometry
297 \param strategy \param_strategy{distance}
298 \return \return_calc{length}
299 
300 \qbk{distinguish,with strategy}
301 \qbk{[include reference/algorithms/length.qbk]}
302 \qbk{[length_with_strategy] [length_with_strategy_output]}
303  */
304 template<typename Geometry, typename Strategy>
305 inline typename default_length_result<Geometry>::type
length(Geometry const & geometry,Strategy const & strategy)306 length(Geometry const& geometry, Strategy const& strategy)
307 {
308     concepts::check<Geometry const>();
309 
310     // detail::throw_on_empty_input(geometry);
311 
312     return resolve_variant::length<Geometry>::apply(geometry, strategy);
313 }
314 
315 
316 }} // namespace boost::geometry
317 
318 #endif // BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
319