length.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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-2021.
  6. // Modifications copyright (c) 2014-2021, 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/core/ignore_unused.hpp>
  18. #include <boost/range/begin.hpp>
  19. #include <boost/range/end.hpp>
  20. #include <boost/range/iterator.hpp>
  21. #include <boost/range/value_type.hpp>
  22. #include <boost/geometry/algorithms/assign.hpp>
  23. #include <boost/geometry/algorithms/detail/calculate_null.hpp>
  24. #include <boost/geometry/algorithms/detail/dummy_geometries.hpp>
  25. #include <boost/geometry/algorithms/detail/multi_sum.hpp>
  26. // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
  27. #include <boost/geometry/algorithms/detail/visit.hpp>
  28. #include <boost/geometry/core/cs.hpp>
  29. #include <boost/geometry/core/closure.hpp>
  30. #include <boost/geometry/core/tag.hpp>
  31. #include <boost/geometry/core/tags.hpp>
  32. #include <boost/geometry/core/visit.hpp>
  33. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
  34. #include <boost/geometry/geometries/concepts/check.hpp>
  35. #include <boost/geometry/strategies/default_strategy.hpp>
  36. #include <boost/geometry/strategies/default_length_result.hpp> // TODO: Move to algorithms
  37. #include <boost/geometry/strategies/detail.hpp>
  38. #include <boost/geometry/strategies/length/cartesian.hpp>
  39. #include <boost/geometry/strategies/length/geographic.hpp>
  40. #include <boost/geometry/strategies/length/spherical.hpp>
  41. #include <boost/geometry/views/closeable_view.hpp>
  42. namespace boost { namespace geometry
  43. {
  44. #ifndef DOXYGEN_NO_DETAIL
  45. namespace detail { namespace length
  46. {
  47. template<typename Segment>
  48. struct segment_length
  49. {
  50. template <typename Strategies>
  51. static inline typename default_length_result<Segment>::type
  52. apply(Segment const& segment, Strategies const& strategies)
  53. {
  54. typedef typename point_type<Segment>::type point_type;
  55. point_type p1, p2;
  56. geometry::detail::assign_point_from_index<0>(segment, p1);
  57. geometry::detail::assign_point_from_index<1>(segment, p2);
  58. return strategies.distance(p1, p2).apply(p1, p2);
  59. }
  60. };
  61. /*!
  62. \brief Internal, calculates length of a linestring using iterator pairs and
  63. specified strategy
  64. \note for_each could be used here, now that point_type is changed by boost
  65. range iterator
  66. */
  67. template<typename Range, closure_selector Closure>
  68. struct range_length
  69. {
  70. typedef typename default_length_result<Range>::type return_type;
  71. template <typename Strategies>
  72. static inline return_type
  73. apply(Range const& range, Strategies const& strategies)
  74. {
  75. return_type sum = return_type();
  76. detail::closed_view<Range const> const view(range);
  77. auto it = boost::begin(view);
  78. auto const end = boost::end(view);
  79. if (it != end)
  80. {
  81. auto const strategy = strategies.distance(dummy_point(), dummy_point());
  82. for(auto previous = it++; it != end; ++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 MultiLinestring>
  117. struct length<MultiLinestring, multi_linestring_tag> : detail::multi_sum
  118. {
  119. template <typename Strategy>
  120. static inline typename default_length_result<MultiLinestring>::type
  121. apply(MultiLinestring const& multi, Strategy const& strategy)
  122. {
  123. return multi_sum::apply
  124. <
  125. typename default_length_result<MultiLinestring>::type,
  126. detail::length::range_length
  127. <
  128. typename boost::range_value<MultiLinestring>::type,
  129. closed // no need to close it explicitly
  130. >
  131. >(multi, strategy);
  132. }
  133. };
  134. } // namespace dispatch
  135. #endif // DOXYGEN_NO_DISPATCH
  136. namespace resolve_strategy {
  137. template
  138. <
  139. typename Strategies,
  140. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  141. >
  142. struct length
  143. {
  144. template <typename Geometry>
  145. static inline typename default_length_result<Geometry>::type
  146. apply(Geometry const& geometry, Strategies const& strategies)
  147. {
  148. return dispatch::length<Geometry>::apply(geometry, strategies);
  149. }
  150. };
  151. template <typename Strategy>
  152. struct length<Strategy, false>
  153. {
  154. template <typename Geometry>
  155. static inline typename default_length_result<Geometry>::type
  156. apply(Geometry const& geometry, Strategy const& strategy)
  157. {
  158. using strategies::length::services::strategy_converter;
  159. return dispatch::length<Geometry>::apply(
  160. geometry, strategy_converter<Strategy>::get(strategy));
  161. }
  162. };
  163. template <>
  164. struct length<default_strategy, false>
  165. {
  166. template <typename Geometry>
  167. static inline typename default_length_result<Geometry>::type
  168. apply(Geometry const& geometry, default_strategy const&)
  169. {
  170. typedef typename strategies::length::services::default_strategy
  171. <
  172. Geometry
  173. >::type strategies_type;
  174. return dispatch::length<Geometry>::apply(geometry, strategies_type());
  175. }
  176. };
  177. } // namespace resolve_strategy
  178. namespace resolve_dynamic {
  179. template <typename Geometry, typename Tag = typename geometry::tag<Geometry>::type>
  180. struct length
  181. {
  182. template <typename Strategy>
  183. static inline typename default_length_result<Geometry>::type
  184. apply(Geometry const& geometry, Strategy const& strategy)
  185. {
  186. return resolve_strategy::length<Strategy>::apply(geometry, strategy);
  187. }
  188. };
  189. template <typename Geometry>
  190. struct length<Geometry, dynamic_geometry_tag>
  191. {
  192. template <typename Strategy>
  193. static inline typename default_length_result<Geometry>::type
  194. apply(Geometry const& geometry, Strategy const& strategy)
  195. {
  196. typename default_length_result<Geometry>::type result = 0;
  197. traits::visit<Geometry>::apply([&](auto const& g)
  198. {
  199. result = length<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  200. }, geometry);
  201. return result;
  202. }
  203. };
  204. template <typename Geometry>
  205. struct length<Geometry, geometry_collection_tag>
  206. {
  207. template <typename Strategy>
  208. static inline typename default_length_result<Geometry>::type
  209. apply(Geometry const& geometry, Strategy const& strategy)
  210. {
  211. typename default_length_result<Geometry>::type result = 0;
  212. detail::visit_breadth_first([&](auto const& g)
  213. {
  214. result += length<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  215. return true;
  216. }, geometry);
  217. return result;
  218. }
  219. };
  220. } // namespace resolve_dynamic
  221. /*!
  222. \brief \brief_calc{length}
  223. \ingroup length
  224. \details \details_calc{length, length (the sum of distances between consecutive points)}. \details_default_strategy
  225. \tparam Geometry \tparam_geometry
  226. \param geometry \param_geometry
  227. \return \return_calc{length}
  228. \qbk{[include reference/algorithms/length.qbk]}
  229. \qbk{[length] [length_output]}
  230. */
  231. template<typename Geometry>
  232. inline typename default_length_result<Geometry>::type
  233. length(Geometry const& geometry)
  234. {
  235. concepts::check<Geometry const>();
  236. // detail::throw_on_empty_input(geometry);
  237. return resolve_dynamic::length<Geometry>::apply(geometry, default_strategy());
  238. }
  239. /*!
  240. \brief \brief_calc{length} \brief_strategy
  241. \ingroup length
  242. \details \details_calc{length, length (the sum of distances between consecutive points)} \brief_strategy. \details_strategy_reasons
  243. \tparam Geometry \tparam_geometry
  244. \tparam Strategy \tparam_strategy{distance}
  245. \param geometry \param_geometry
  246. \param strategy \param_strategy{distance}
  247. \return \return_calc{length}
  248. \qbk{distinguish,with strategy}
  249. \qbk{[include reference/algorithms/length.qbk]}
  250. \qbk{[length_with_strategy] [length_with_strategy_output]}
  251. */
  252. template<typename Geometry, typename Strategy>
  253. inline typename default_length_result<Geometry>::type
  254. length(Geometry const& geometry, Strategy const& strategy)
  255. {
  256. concepts::check<Geometry const>();
  257. // detail::throw_on_empty_input(geometry);
  258. return resolve_dynamic::length<Geometry>::apply(geometry, strategy);
  259. }
  260. }} // namespace boost::geometry
  261. #endif // BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP