1 // Boost.Geometry (aka GGL, Generic Geometry Library)
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.
7 // This file was modified by Oracle on 2014, 2015.
8 // Modifications copyright (c) 2014-2015, Oracle and/or its affiliates.
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
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)
20 #ifndef BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
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>
43 #include <boost/geometry/core/cs.hpp>
44 #include <boost/geometry/core/closure.hpp>
45 #include <boost/geometry/core/tags.hpp>
47 #include <boost/geometry/geometries/concepts/check.hpp>
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>
59 namespace boost { namespace geometry
63 #ifndef DOXYGEN_NO_DETAIL
64 namespace detail { namespace length
68 template<typename Segment>
71 template <typename Strategy>
72 static inline typename default_length_result<Segment>::type apply(
73 Segment const& segment, Strategy const& strategy)
75 boost::ignore_unused(strategy);
76 typedef typename point_type<Segment>::type point_type;
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);
85 \brief Internal, calculates length of a linestring using iterator pairs and
87 \note for_each could be used here, now that point_type is changed by boost
90 template<typename Range, closure_selector Closure>
93 typedef typename default_length_result<Range>::type return_type;
95 template <typename Strategy>
96 static inline return_type apply(
97 Range const& range, Strategy const& strategy)
99 boost::ignore_unused(strategy);
100 typedef typename closeable_view<Range const, Closure>::type view_type;
101 typedef typename boost::range_iterator
104 >::type iterator_type;
106 return_type sum = return_type();
107 view_type view(range);
108 iterator_type it = boost::begin(view), end = boost::end(view);
111 for(iterator_type previous = it++;
115 // Add point-point distance using the return type belonging
117 sum += strategy.apply(*previous, *it);
126 }} // namespace detail::length
127 #endif // DOXYGEN_NO_DETAIL
130 #ifndef DOXYGEN_NO_DISPATCH
135 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
136 struct length : detail::calculate_null
138 typedef typename default_length_result<Geometry>::type return_type;
140 template <typename Strategy>
141 static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
143 return calculate_null::apply<return_type>(geometry, strategy);
148 template <typename Geometry>
149 struct length<Geometry, linestring_tag>
150 : detail::length::range_length<Geometry, closed>
154 // RING: length is currently 0; it might be argued that it is the "perimeter"
157 template <typename Geometry>
158 struct length<Geometry, segment_tag>
159 : detail::length::segment_length<Geometry>
163 template <typename MultiLinestring>
164 struct length<MultiLinestring, multi_linestring_tag> : detail::multi_sum
166 template <typename Strategy>
167 static inline typename default_length_result<MultiLinestring>::type
168 apply(MultiLinestring const& multi, Strategy const& strategy)
170 return multi_sum::apply
172 typename default_length_result<MultiLinestring>::type,
173 detail::length::range_length
175 typename boost::range_value<MultiLinestring>::type,
176 closed // no need to close it explicitly
184 } // namespace dispatch
185 #endif // DOXYGEN_NO_DISPATCH
188 namespace resolve_strategy {
192 template <typename Geometry, typename Strategy>
193 static inline typename default_length_result<Geometry>::type
194 apply(Geometry const& geometry, Strategy const& strategy)
196 return dispatch::length<Geometry>::apply(geometry, strategy);
199 template <typename Geometry>
200 static inline typename default_length_result<Geometry>::type
201 apply(Geometry const& geometry, default_strategy)
203 typedef typename strategy::distance::services::default_strategy
205 point_tag, point_tag, typename point_type<Geometry>::type
206 >::type strategy_type;
208 return dispatch::length<Geometry>::apply(geometry, strategy_type());
212 } // namespace resolve_strategy
215 namespace resolve_variant {
217 template <typename Geometry>
220 template <typename Strategy>
221 static inline typename default_length_result<Geometry>::type
222 apply(Geometry const& geometry, Strategy const& strategy)
224 return resolve_strategy::length::apply(geometry, strategy);
228 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
229 struct length<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
231 typedef typename default_length_result
233 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
236 template <typename Strategy>
238 : static_visitor<result_type>
240 Strategy const& m_strategy;
242 visitor(Strategy const& strategy)
243 : m_strategy(strategy)
246 template <typename Geometry>
247 inline typename default_length_result<Geometry>::type
248 operator()(Geometry const& geometry) const
250 return length<Geometry>::apply(geometry, m_strategy);
254 template <typename Strategy>
255 static inline result_type apply(
256 variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
257 Strategy const& strategy
260 return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
264 } // namespace resolve_variant
268 \brief \brief_calc{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}
275 \qbk{[include reference/algorithms/length.qbk]}
276 \qbk{[length] [length_output]}
278 template<typename Geometry>
279 inline typename default_length_result<Geometry>::type
280 length(Geometry const& geometry)
282 concepts::check<Geometry const>();
284 // detail::throw_on_empty_input(geometry);
286 return resolve_variant::length<Geometry>::apply(geometry, default_strategy());
291 \brief \brief_calc{length} \brief_strategy
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}
300 \qbk{distinguish,with strategy}
301 \qbk{[include reference/algorithms/length.qbk]}
302 \qbk{[length_with_strategy] [length_with_strategy_output]}
304 template<typename Geometry, typename Strategy>
305 inline typename default_length_result<Geometry>::type
306 length(Geometry const& geometry, Strategy const& strategy)
308 concepts::check<Geometry const>();
310 // detail::throw_on_empty_input(geometry);
312 return resolve_variant::length<Geometry>::apply(geometry, strategy);
316 }} // namespace boost::geometry
318 #endif // BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP