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