perimeter.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_PERIMETER_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
  17. #include <boost/range/value_type.hpp>
  18. #include <boost/geometry/algorithms/length.hpp>
  19. #include <boost/geometry/algorithms/detail/calculate_null.hpp>
  20. #include <boost/geometry/algorithms/detail/calculate_sum.hpp>
  21. #include <boost/geometry/algorithms/detail/multi_sum.hpp>
  22. // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
  23. #include <boost/geometry/algorithms/detail/visit.hpp>
  24. #include <boost/geometry/core/closure.hpp>
  25. #include <boost/geometry/core/tags.hpp>
  26. #include <boost/geometry/core/visit.hpp>
  27. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
  28. #include <boost/geometry/geometries/concepts/check.hpp>
  29. #include <boost/geometry/strategies/default_length_result.hpp>
  30. #include <boost/geometry/strategies/default_strategy.hpp>
  31. #include <boost/geometry/strategies/detail.hpp>
  32. #include <boost/geometry/strategies/length/cartesian.hpp>
  33. #include <boost/geometry/strategies/length/geographic.hpp>
  34. #include <boost/geometry/strategies/length/spherical.hpp>
  35. namespace boost { namespace geometry
  36. {
  37. #ifndef DOXYGEN_NO_DISPATCH
  38. namespace dispatch
  39. {
  40. // Default perimeter is 0.0, specializations implement calculated values
  41. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  42. struct perimeter : detail::calculate_null
  43. {
  44. typedef typename default_length_result<Geometry>::type return_type;
  45. template <typename Strategy>
  46. static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
  47. {
  48. return calculate_null::apply<return_type>(geometry, strategy);
  49. }
  50. };
  51. template <typename Geometry>
  52. struct perimeter<Geometry, ring_tag>
  53. : detail::length::range_length
  54. <
  55. Geometry,
  56. closure<Geometry>::value
  57. >
  58. {};
  59. template <typename Polygon>
  60. struct perimeter<Polygon, polygon_tag> : detail::calculate_polygon_sum
  61. {
  62. typedef typename default_length_result<Polygon>::type return_type;
  63. typedef detail::length::range_length
  64. <
  65. typename ring_type<Polygon>::type,
  66. closure<Polygon>::value
  67. > policy;
  68. template <typename Strategy>
  69. static inline return_type apply(Polygon const& polygon, Strategy const& strategy)
  70. {
  71. return calculate_polygon_sum::apply<return_type, policy>(polygon, strategy);
  72. }
  73. };
  74. template <typename MultiPolygon>
  75. struct perimeter<MultiPolygon, multi_polygon_tag> : detail::multi_sum
  76. {
  77. typedef typename default_length_result<MultiPolygon>::type return_type;
  78. template <typename Strategy>
  79. static inline return_type apply(MultiPolygon const& multi, Strategy const& strategy)
  80. {
  81. return multi_sum::apply
  82. <
  83. return_type,
  84. perimeter<typename boost::range_value<MultiPolygon>::type>
  85. >(multi, strategy);
  86. }
  87. };
  88. // box,n-sphere: to be implemented
  89. } // namespace dispatch
  90. #endif // DOXYGEN_NO_DISPATCH
  91. namespace resolve_strategy {
  92. template
  93. <
  94. typename Strategies,
  95. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  96. >
  97. struct perimeter
  98. {
  99. template <typename Geometry>
  100. static inline typename default_length_result<Geometry>::type
  101. apply(Geometry const& geometry, Strategies const& strategies)
  102. {
  103. return dispatch::perimeter<Geometry>::apply(geometry, strategies);
  104. }
  105. };
  106. template <typename Strategy>
  107. struct perimeter<Strategy, false>
  108. {
  109. template <typename Geometry>
  110. static inline typename default_length_result<Geometry>::type
  111. apply(Geometry const& geometry, Strategy const& strategy)
  112. {
  113. using strategies::length::services::strategy_converter;
  114. return dispatch::perimeter<Geometry>::apply(
  115. geometry, strategy_converter<Strategy>::get(strategy));
  116. }
  117. };
  118. template <>
  119. struct perimeter<default_strategy, false>
  120. {
  121. template <typename Geometry>
  122. static inline typename default_length_result<Geometry>::type
  123. apply(Geometry const& geometry, default_strategy const&)
  124. {
  125. typedef typename strategies::length::services::default_strategy
  126. <
  127. Geometry
  128. >::type strategies_type;
  129. return dispatch::perimeter<Geometry>::apply(geometry, strategies_type());
  130. }
  131. };
  132. } // namespace resolve_strategy
  133. namespace resolve_dynamic {
  134. template <typename Geometry, typename Tag = typename geometry::tag<Geometry>::type>
  135. struct perimeter
  136. {
  137. template <typename Strategy>
  138. static inline typename default_length_result<Geometry>::type
  139. apply(Geometry const& geometry, Strategy const& strategy)
  140. {
  141. concepts::check<Geometry const>();
  142. return resolve_strategy::perimeter<Strategy>::apply(geometry, strategy);
  143. }
  144. };
  145. template <typename Geometry>
  146. struct perimeter<Geometry, dynamic_geometry_tag>
  147. {
  148. template <typename Strategy>
  149. static inline typename default_length_result<Geometry>::type
  150. apply(Geometry const& geometry, Strategy const& strategy)
  151. {
  152. typename default_length_result<Geometry>::type result = 0;
  153. traits::visit<Geometry>::apply([&](auto const& g)
  154. {
  155. result = perimeter<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  156. }, geometry);
  157. return result;
  158. }
  159. };
  160. template <typename Geometry>
  161. struct perimeter<Geometry, geometry_collection_tag>
  162. {
  163. template <typename Strategy>
  164. static inline typename default_length_result<Geometry>::type
  165. apply(Geometry const& geometry, Strategy const& strategy)
  166. {
  167. typename default_length_result<Geometry>::type result = 0;
  168. detail::visit_breadth_first([&](auto const& g)
  169. {
  170. result += perimeter<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
  171. return true;
  172. }, geometry);
  173. return result;
  174. }
  175. };
  176. } // namespace resolve_dynamic
  177. /*!
  178. \brief \brief_calc{perimeter}
  179. \ingroup perimeter
  180. \details The function perimeter returns the perimeter of a geometry,
  181. using the default distance-calculation-strategy
  182. \tparam Geometry \tparam_geometry
  183. \param geometry \param_geometry
  184. \return \return_calc{perimeter}
  185. \qbk{[include reference/algorithms/perimeter.qbk]}
  186. \qbk{
  187. [heading Example]
  188. [perimeter]
  189. [perimeter_output]
  190. }
  191. */
  192. template<typename Geometry>
  193. inline typename default_length_result<Geometry>::type perimeter(
  194. Geometry const& geometry)
  195. {
  196. // detail::throw_on_empty_input(geometry);
  197. return resolve_dynamic::perimeter<Geometry>::apply(geometry, default_strategy());
  198. }
  199. /*!
  200. \brief \brief_calc{perimeter} \brief_strategy
  201. \ingroup perimeter
  202. \details The function perimeter returns the perimeter of a geometry,
  203. using specified strategy
  204. \tparam Geometry \tparam_geometry
  205. \tparam Strategy \tparam_strategy{distance}
  206. \param geometry \param_geometry
  207. \param strategy strategy to be used for distance calculations.
  208. \return \return_calc{perimeter}
  209. \qbk{distinguish,with strategy}
  210. \qbk{[include reference/algorithms/perimeter.qbk]}
  211. */
  212. template<typename Geometry, typename Strategy>
  213. inline typename default_length_result<Geometry>::type perimeter(
  214. Geometry const& geometry, Strategy const& strategy)
  215. {
  216. // detail::throw_on_empty_input(geometry);
  217. return resolve_dynamic::perimeter<Geometry>::apply(geometry, strategy);
  218. }
  219. }} // namespace boost::geometry
  220. #endif // BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP