length.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-2023.
  6. // Modifications copyright (c) 2014-2023, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/range/value_type.hpp>
  20. #include "boost/geometry/algorithms/detail/assign_indexed_point.hpp"
  21. #include <boost/geometry/algorithms/detail/calculate_null.hpp>
  22. #include <boost/geometry/algorithms/detail/dummy_geometries.hpp>
  23. #include <boost/geometry/algorithms/detail/multi_sum.hpp>
  24. // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
  25. #include <boost/geometry/algorithms/detail/visit.hpp>
  26. #include <boost/geometry/core/closure.hpp>
  27. #include <boost/geometry/core/tag.hpp>
  28. #include <boost/geometry/core/tags.hpp>
  29. #include <boost/geometry/core/visit.hpp>
  30. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
  31. #include <boost/geometry/geometries/concepts/check.hpp>
  32. #include <boost/geometry/strategies/default_strategy.hpp>
  33. #include <boost/geometry/strategies/default_length_result.hpp> // TODO: Move to algorithms
  34. #include <boost/geometry/strategies/detail.hpp>
  35. #include <boost/geometry/strategies/length/cartesian.hpp>
  36. #include <boost/geometry/strategies/length/geographic.hpp>
  37. #include <boost/geometry/strategies/length/spherical.hpp>
  38. #include <boost/geometry/views/closeable_view.hpp>
  39. namespace boost { namespace geometry
  40. {
  41. #ifndef DOXYGEN_NO_DETAIL
  42. namespace detail { namespace length
  43. {
  44. template<typename Segment>
  45. struct segment_length
  46. {
  47. template <typename Strategies>
  48. static inline typename default_length_result<Segment>::type
  49. apply(Segment const& segment, Strategies const& strategies)
  50. {
  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 strategies.distance(p1, p2).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 Strategies>
  69. static inline return_type
  70. apply(Range const& range, Strategies const& strategies)
  71. {
  72. return_type sum = return_type();
  73. detail::closed_view<Range const> const view(range);
  74. auto it = boost::begin(view);
  75. auto const end = boost::end(view);
  76. if (it != end)
  77. {
  78. auto const strategy = strategies.distance(dummy_point(), dummy_point());
  79. for(auto previous = it++; it != end; ++previous, ++it)
  80. {
  81. // Add point-point distance using the return type belonging
  82. // to strategy
  83. sum += strategy.apply(*previous, *it);
  84. }
  85. }
  86. return sum;
  87. }
  88. };
  89. }} // namespace detail::length
  90. #endif // DOXYGEN_NO_DETAIL
  91. #ifndef DOXYGEN_NO_DISPATCH
  92. namespace dispatch
  93. {
  94. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  95. struct length : detail::calculate_null
  96. {
  97. typedef typename default_length_result<Geometry>::type return_type;
  98. template <typename Strategy>
  99. static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
  100. {
  101. return calculate_null::apply<return_type>(geometry, strategy);
  102. }
  103. };
  104. template <typename Geometry>
  105. struct length<Geometry, linestring_tag>
  106. : detail::length::range_length<Geometry, closed>
  107. {};
  108. // RING: length is currently 0; it might be argued that it is the "perimeter"
  109. template <typename Geometry>
  110. struct length<Geometry, segment_tag>
  111. : detail::length::segment_length<Geometry>
  112. {};
  113. template <typename MultiLinestring>
  114. struct length<MultiLinestring, multi_linestring_tag> : detail::multi_sum
  115. {
  116. template <typename Strategy>
  117. static inline typename default_length_result<MultiLinestring>::type
  118. apply(MultiLinestring const& multi, Strategy const& strategy)
  119. {
  120. return multi_sum::apply
  121. <
  122. typename default_length_result<MultiLinestring>::type,
  123. detail::length::range_length
  124. <
  125. typename boost::range_value<MultiLinestring>::type,
  126. closed // no need to close it explicitly
  127. >
  128. >(multi, strategy);
  129. }
  130. };
  131. } // namespace dispatch
  132. #endif // DOXYGEN_NO_DISPATCH
  133. namespace resolve_strategy {
  134. template
  135. <
  136. typename Strategies,
  137. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  138. >
  139. struct length
  140. {
  141. template <typename Geometry>
  142. static inline typename default_length_result<Geometry>::type
  143. apply(Geometry const& geometry, Strategies const& strategies)
  144. {
  145. return dispatch::length<Geometry>::apply(geometry, strategies);
  146. }
  147. };
  148. template <typename Strategy>
  149. struct length<Strategy, false>
  150. {
  151. template <typename Geometry>
  152. static inline typename default_length_result<Geometry>::type
  153. apply(Geometry const& geometry, Strategy const& strategy)
  154. {
  155. using strategies::length::services::strategy_converter;
  156. return dispatch::length<Geometry>::apply(
  157. geometry, strategy_converter<Strategy>::get(strategy));
  158. }
  159. };
  160. template <>
  161. struct length<default_strategy, false>
  162. {
  163. template <typename Geometry>
  164. static inline typename default_length_result<Geometry>::type
  165. apply(Geometry const& geometry, default_strategy const&)
  166. {
  167. typedef typename strategies::length::services::default_strategy
  168. <
  169. Geometry
  170. >::type strategies_type;
  171. return dispatch::length<Geometry>::apply(geometry, strategies_type());
  172. }
  173. };
  174. } // namespace resolve_strategy
  175. namespace resolve_dynamic {
  176. template <typename Geometry, typename Tag = typename geometry::tag<Geometry>::type>
  177. struct length
  178. {
  179. template <typename Strategy>
  180. static inline typename default_length_result<Geometry>::type
  181. apply(Geometry const& geometry, Strategy const& strategy)
  182. {
  183. return resolve_strategy::length<Strategy>::apply(geometry, strategy);
  184. }
  185. };
  186. template <typename Geometry>
  187. struct length<Geometry, dynamic_geometry_tag>
  188. {
  189. template <typename Strategy>
  190. static inline typename default_length_result<Geometry>::type
  191. apply(Geometry const& geometry, Strategy const& strategy)
  192. {
  193. typename default_length_result<Geometry>::type result = 0;
  194. traits::visit<Geometry>::apply([&](auto const& g)
  195. {
  196. result = length<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  197. }, geometry);
  198. return result;
  199. }
  200. };
  201. template <typename Geometry>
  202. struct length<Geometry, geometry_collection_tag>
  203. {
  204. template <typename Strategy>
  205. static inline typename default_length_result<Geometry>::type
  206. apply(Geometry const& geometry, Strategy const& strategy)
  207. {
  208. typename default_length_result<Geometry>::type result = 0;
  209. detail::visit_breadth_first([&](auto const& g)
  210. {
  211. result += length<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  212. return true;
  213. }, geometry);
  214. return result;
  215. }
  216. };
  217. } // namespace resolve_dynamic
  218. /*!
  219. \brief \brief_calc{length}
  220. \ingroup length
  221. \details \details_calc{length, length (the sum of distances between consecutive points)}. \details_default_strategy
  222. \tparam Geometry \tparam_geometry
  223. \param geometry \param_geometry
  224. \return \return_calc{length}
  225. \qbk{[include reference/algorithms/length.qbk]}
  226. \qbk{[length] [length_output]}
  227. */
  228. template<typename Geometry>
  229. inline typename default_length_result<Geometry>::type
  230. length(Geometry const& geometry)
  231. {
  232. concepts::check<Geometry const>();
  233. // detail::throw_on_empty_input(geometry);
  234. return resolve_dynamic::length<Geometry>::apply(geometry, default_strategy());
  235. }
  236. /*!
  237. \brief \brief_calc{length} \brief_strategy
  238. \ingroup length
  239. \details \details_calc{length, length (the sum of distances between consecutive points)} \brief_strategy. \details_strategy_reasons
  240. \tparam Geometry \tparam_geometry
  241. \tparam Strategy \tparam_strategy{distance}
  242. \param geometry \param_geometry
  243. \param strategy \param_strategy{distance}
  244. \return \return_calc{length}
  245. \qbk{distinguish,with strategy}
  246. \qbk{[include reference/algorithms/length.qbk]}
  247. \qbk{[length_with_strategy] [length_with_strategy_output]}
  248. */
  249. template<typename Geometry, typename Strategy>
  250. inline typename default_length_result<Geometry>::type
  251. length(Geometry const& geometry, Strategy const& strategy)
  252. {
  253. concepts::check<Geometry const>();
  254. // detail::throw_on_empty_input(geometry);
  255. return resolve_dynamic::length<Geometry>::apply(geometry, strategy);
  256. }
  257. }} // namespace boost::geometry
  258. #endif // BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP