length.hpp 7.9 KB

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