length.hpp 9.1 KB

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