length.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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-2020.
  6. // Modifications copyright (c) 2014-2020, 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/range/begin.hpp>
  20. #include <boost/range/end.hpp>
  21. #include <boost/range/iterator.hpp>
  22. #include <boost/range/value_type.hpp>
  23. #include <boost/variant/apply_visitor.hpp>
  24. #include <boost/variant/static_visitor.hpp>
  25. #include <boost/variant/variant_fwd.hpp>
  26. #include <boost/geometry/core/cs.hpp>
  27. #include <boost/geometry/core/closure.hpp>
  28. #include <boost/geometry/core/tags.hpp>
  29. #include <boost/geometry/geometries/concepts/check.hpp>
  30. #include <boost/geometry/algorithms/assign.hpp>
  31. #include <boost/geometry/algorithms/detail/calculate_null.hpp>
  32. #include <boost/geometry/algorithms/detail/multi_sum.hpp>
  33. // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
  34. #include <boost/geometry/views/closeable_view.hpp>
  35. #include <boost/geometry/strategies/default_strategy.hpp>
  36. #include <boost/geometry/strategies/distance.hpp>
  37. #include <boost/geometry/strategies/default_length_result.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. #ifndef DOXYGEN_NO_DETAIL
  41. namespace detail { namespace length
  42. {
  43. template<typename Segment>
  44. struct segment_length
  45. {
  46. template <typename Strategy>
  47. static inline typename default_length_result<Segment>::type apply(
  48. Segment const& segment, Strategy const& strategy)
  49. {
  50. boost::ignore_unused(strategy);
  51. typedef typename point_type<Segment>::type point_type;
  52. point_type p1, p2;
  53. geometry::detail::assign_point_from_index<0>(segment, p1);
  54. geometry::detail::assign_point_from_index<1>(segment, p2);
  55. return strategy.apply(p1, p2);
  56. }
  57. };
  58. /*!
  59. \brief Internal, calculates length of a linestring using iterator pairs and
  60. specified strategy
  61. \note for_each could be used here, now that point_type is changed by boost
  62. range iterator
  63. */
  64. template<typename Range, closure_selector Closure>
  65. struct range_length
  66. {
  67. typedef typename default_length_result<Range>::type return_type;
  68. template <typename Strategy>
  69. static inline return_type apply(
  70. Range const& range, Strategy const& strategy)
  71. {
  72. boost::ignore_unused(strategy);
  73. typedef typename closeable_view<Range const, Closure>::type view_type;
  74. typedef typename boost::range_iterator
  75. <
  76. view_type const
  77. >::type iterator_type;
  78. return_type sum = return_type();
  79. view_type view(range);
  80. iterator_type it = boost::begin(view), end = boost::end(view);
  81. if(it != end)
  82. {
  83. for(iterator_type previous = it++;
  84. it != end;
  85. ++previous, ++it)
  86. {
  87. // Add point-point distance using the return type belonging
  88. // to strategy
  89. sum += strategy.apply(*previous, *it);
  90. }
  91. }
  92. return sum;
  93. }
  94. };
  95. }} // namespace detail::length
  96. #endif // DOXYGEN_NO_DETAIL
  97. #ifndef DOXYGEN_NO_DISPATCH
  98. namespace dispatch
  99. {
  100. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  101. struct length : detail::calculate_null
  102. {
  103. typedef typename default_length_result<Geometry>::type return_type;
  104. template <typename Strategy>
  105. static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
  106. {
  107. return calculate_null::apply<return_type>(geometry, strategy);
  108. }
  109. };
  110. template <typename Geometry>
  111. struct length<Geometry, linestring_tag>
  112. : detail::length::range_length<Geometry, closed>
  113. {};
  114. // RING: length is currently 0; it might be argued that it is the "perimeter"
  115. template <typename Geometry>
  116. struct length<Geometry, segment_tag>
  117. : detail::length::segment_length<Geometry>
  118. {};
  119. template <typename MultiLinestring>
  120. struct length<MultiLinestring, multi_linestring_tag> : detail::multi_sum
  121. {
  122. template <typename Strategy>
  123. static inline typename default_length_result<MultiLinestring>::type
  124. apply(MultiLinestring const& multi, Strategy const& strategy)
  125. {
  126. return multi_sum::apply
  127. <
  128. typename default_length_result<MultiLinestring>::type,
  129. detail::length::range_length
  130. <
  131. typename boost::range_value<MultiLinestring>::type,
  132. closed // no need to close it explicitly
  133. >
  134. >(multi, strategy);
  135. }
  136. };
  137. } // namespace dispatch
  138. #endif // DOXYGEN_NO_DISPATCH
  139. namespace resolve_strategy {
  140. struct length
  141. {
  142. template <typename Geometry, typename Strategy>
  143. static inline typename default_length_result<Geometry>::type
  144. apply(Geometry const& geometry, Strategy const& strategy)
  145. {
  146. return dispatch::length<Geometry>::apply(geometry, strategy);
  147. }
  148. template <typename Geometry>
  149. static inline typename default_length_result<Geometry>::type
  150. apply(Geometry const& geometry, default_strategy)
  151. {
  152. typedef typename strategy::distance::services::default_strategy
  153. <
  154. point_tag, point_tag, typename point_type<Geometry>::type
  155. >::type strategy_type;
  156. return dispatch::length<Geometry>::apply(geometry, strategy_type());
  157. }
  158. };
  159. } // namespace resolve_strategy
  160. namespace resolve_variant {
  161. template <typename Geometry>
  162. struct length
  163. {
  164. template <typename Strategy>
  165. static inline typename default_length_result<Geometry>::type
  166. apply(Geometry const& geometry, Strategy const& strategy)
  167. {
  168. return resolve_strategy::length::apply(geometry, strategy);
  169. }
  170. };
  171. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  172. struct length<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  173. {
  174. typedef typename default_length_result
  175. <
  176. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
  177. >::type result_type;
  178. template <typename Strategy>
  179. struct visitor
  180. : static_visitor<result_type>
  181. {
  182. Strategy const& m_strategy;
  183. visitor(Strategy const& strategy)
  184. : m_strategy(strategy)
  185. {}
  186. template <typename Geometry>
  187. inline typename default_length_result<Geometry>::type
  188. operator()(Geometry const& geometry) const
  189. {
  190. return length<Geometry>::apply(geometry, m_strategy);
  191. }
  192. };
  193. template <typename Strategy>
  194. static inline result_type apply(
  195. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  196. Strategy const& strategy
  197. )
  198. {
  199. return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  200. }
  201. };
  202. } // namespace resolve_variant
  203. /*!
  204. \brief \brief_calc{length}
  205. \ingroup length
  206. \details \details_calc{length, length (the sum of distances between consecutive points)}. \details_default_strategy
  207. \tparam Geometry \tparam_geometry
  208. \param geometry \param_geometry
  209. \return \return_calc{length}
  210. \qbk{[include reference/algorithms/length.qbk]}
  211. \qbk{[length] [length_output]}
  212. */
  213. template<typename Geometry>
  214. inline typename default_length_result<Geometry>::type
  215. length(Geometry const& geometry)
  216. {
  217. concepts::check<Geometry const>();
  218. // detail::throw_on_empty_input(geometry);
  219. return resolve_variant::length<Geometry>::apply(geometry, default_strategy());
  220. }
  221. /*!
  222. \brief \brief_calc{length} \brief_strategy
  223. \ingroup length
  224. \details \details_calc{length, length (the sum of distances between consecutive points)} \brief_strategy. \details_strategy_reasons
  225. \tparam Geometry \tparam_geometry
  226. \tparam Strategy \tparam_strategy{distance}
  227. \param geometry \param_geometry
  228. \param strategy \param_strategy{distance}
  229. \return \return_calc{length}
  230. \qbk{distinguish,with strategy}
  231. \qbk{[include reference/algorithms/length.qbk]}
  232. \qbk{[length_with_strategy] [length_with_strategy_output]}
  233. */
  234. template<typename Geometry, typename Strategy>
  235. inline typename default_length_result<Geometry>::type
  236. length(Geometry const& geometry, Strategy const& strategy)
  237. {
  238. concepts::check<Geometry const>();
  239. // detail::throw_on_empty_input(geometry);
  240. return resolve_variant::length<Geometry>::apply(geometry, strategy);
  241. }
  242. }} // namespace boost::geometry
  243. #endif // BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP