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