perimeter.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_PERIMETER_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
  16. #include <boost/range/value_type.hpp>
  17. #include <boost/variant/apply_visitor.hpp>
  18. #include <boost/variant/static_visitor.hpp>
  19. #include <boost/variant/variant_fwd.hpp>
  20. #include <boost/geometry/algorithms/length.hpp>
  21. #include <boost/geometry/algorithms/detail/calculate_null.hpp>
  22. #include <boost/geometry/algorithms/detail/calculate_sum.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/core/cs.hpp>
  26. #include <boost/geometry/core/closure.hpp>
  27. #include <boost/geometry/core/tags.hpp>
  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. namespace boost { namespace geometry
  32. {
  33. #ifndef DOXYGEN_NO_DISPATCH
  34. namespace dispatch
  35. {
  36. // Default perimeter is 0.0, specializations implement calculated values
  37. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  38. struct perimeter : detail::calculate_null
  39. {
  40. typedef typename default_length_result<Geometry>::type return_type;
  41. template <typename Strategy>
  42. static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
  43. {
  44. return calculate_null::apply<return_type>(geometry, strategy);
  45. }
  46. };
  47. template <typename Geometry>
  48. struct perimeter<Geometry, ring_tag>
  49. : detail::length::range_length
  50. <
  51. Geometry,
  52. closure<Geometry>::value
  53. >
  54. {};
  55. template <typename Polygon>
  56. struct perimeter<Polygon, polygon_tag> : detail::calculate_polygon_sum
  57. {
  58. typedef typename default_length_result<Polygon>::type return_type;
  59. typedef detail::length::range_length
  60. <
  61. typename ring_type<Polygon>::type,
  62. closure<Polygon>::value
  63. > policy;
  64. template <typename Strategy>
  65. static inline return_type apply(Polygon const& polygon, Strategy const& strategy)
  66. {
  67. return calculate_polygon_sum::apply<return_type, policy>(polygon, strategy);
  68. }
  69. };
  70. template <typename MultiPolygon>
  71. struct perimeter<MultiPolygon, multi_polygon_tag> : detail::multi_sum
  72. {
  73. typedef typename default_length_result<MultiPolygon>::type return_type;
  74. template <typename Strategy>
  75. static inline return_type apply(MultiPolygon const& multi, Strategy const& strategy)
  76. {
  77. return multi_sum::apply
  78. <
  79. return_type,
  80. perimeter<typename boost::range_value<MultiPolygon>::type>
  81. >(multi, strategy);
  82. }
  83. };
  84. // box,n-sphere: to be implemented
  85. } // namespace dispatch
  86. #endif // DOXYGEN_NO_DISPATCH
  87. namespace resolve_strategy {
  88. struct perimeter
  89. {
  90. template <typename Geometry, typename Strategy>
  91. static inline typename default_length_result<Geometry>::type
  92. apply(Geometry const& geometry, Strategy const& strategy)
  93. {
  94. return dispatch::perimeter<Geometry>::apply(geometry, strategy);
  95. }
  96. template <typename Geometry>
  97. static inline typename default_length_result<Geometry>::type
  98. apply(Geometry const& geometry, default_strategy)
  99. {
  100. typedef typename strategy::distance::services::default_strategy
  101. <
  102. point_tag, point_tag, typename point_type<Geometry>::type
  103. >::type strategy_type;
  104. return dispatch::perimeter<Geometry>::apply(geometry, strategy_type());
  105. }
  106. };
  107. } // namespace resolve_strategy
  108. namespace resolve_variant {
  109. template <typename Geometry>
  110. struct perimeter
  111. {
  112. template <typename Strategy>
  113. static inline typename default_length_result<Geometry>::type
  114. apply(Geometry const& geometry, Strategy const& strategy)
  115. {
  116. concepts::check<Geometry const>();
  117. return resolve_strategy::perimeter::apply(geometry, strategy);
  118. }
  119. };
  120. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  121. struct perimeter<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  122. {
  123. typedef typename default_length_result
  124. <
  125. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
  126. >::type result_type;
  127. template <typename Strategy>
  128. struct visitor: boost::static_visitor<result_type>
  129. {
  130. Strategy const& m_strategy;
  131. visitor(Strategy const& strategy): m_strategy(strategy) {}
  132. template <typename Geometry>
  133. typename default_length_result<Geometry>::type
  134. operator()(Geometry const& geometry) const
  135. {
  136. return perimeter<Geometry>::apply(geometry, m_strategy);
  137. }
  138. };
  139. template <typename Strategy>
  140. static inline result_type
  141. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  142. Strategy const& strategy)
  143. {
  144. return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  145. }
  146. };
  147. } // namespace resolve_variant
  148. /*!
  149. \brief \brief_calc{perimeter}
  150. \ingroup perimeter
  151. \details The function perimeter returns the perimeter of a geometry,
  152. using the default distance-calculation-strategy
  153. \tparam Geometry \tparam_geometry
  154. \param geometry \param_geometry
  155. \return \return_calc{perimeter}
  156. \qbk{[include reference/algorithms/perimeter.qbk]}
  157. \qbk{
  158. [heading Example]
  159. [perimeter]
  160. [perimeter_output]
  161. }
  162. */
  163. template<typename Geometry>
  164. inline typename default_length_result<Geometry>::type perimeter(
  165. Geometry const& geometry)
  166. {
  167. // detail::throw_on_empty_input(geometry);
  168. return resolve_variant::perimeter<Geometry>::apply(geometry, default_strategy());
  169. }
  170. /*!
  171. \brief \brief_calc{perimeter} \brief_strategy
  172. \ingroup perimeter
  173. \details The function perimeter returns the perimeter of a geometry,
  174. using specified strategy
  175. \tparam Geometry \tparam_geometry
  176. \tparam Strategy \tparam_strategy{distance}
  177. \param geometry \param_geometry
  178. \param strategy strategy to be used for distance calculations.
  179. \return \return_calc{perimeter}
  180. \qbk{distinguish,with strategy}
  181. \qbk{[include reference/algorithms/perimeter.qbk]}
  182. */
  183. template<typename Geometry, typename Strategy>
  184. inline typename default_length_result<Geometry>::type perimeter(
  185. Geometry const& geometry, Strategy const& strategy)
  186. {
  187. // detail::throw_on_empty_input(geometry);
  188. return resolve_variant::perimeter<Geometry>::apply(geometry, strategy);
  189. }
  190. }} // namespace boost::geometry
  191. #endif // BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP