perimeter.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.
  6. // Modifications copyright (c) 2014, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
  15. #include <boost/range/metafunctions.hpp>
  16. #include <boost/variant/apply_visitor.hpp>
  17. #include <boost/variant/static_visitor.hpp>
  18. #include <boost/variant/variant_fwd.hpp>
  19. #include <boost/geometry/algorithms/length.hpp>
  20. #include <boost/geometry/algorithms/detail/calculate_null.hpp>
  21. #include <boost/geometry/algorithms/detail/calculate_sum.hpp>
  22. #include <boost/geometry/algorithms/detail/multi_sum.hpp>
  23. // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
  24. #include <boost/geometry/core/cs.hpp>
  25. #include <boost/geometry/core/closure.hpp>
  26. #include <boost/geometry/core/tags.hpp>
  27. #include <boost/geometry/geometries/concepts/check.hpp>
  28. #include <boost/geometry/strategies/default_length_result.hpp>
  29. #include <boost/geometry/strategies/default_strategy.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DISPATCH
  33. namespace dispatch
  34. {
  35. // Default perimeter is 0.0, specializations implement calculated values
  36. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  37. struct perimeter : detail::calculate_null
  38. {
  39. typedef typename default_length_result<Geometry>::type return_type;
  40. template <typename Strategy>
  41. static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
  42. {
  43. return calculate_null::apply<return_type>(geometry, strategy);
  44. }
  45. };
  46. template <typename Geometry>
  47. struct perimeter<Geometry, ring_tag>
  48. : detail::length::range_length
  49. <
  50. Geometry,
  51. closure<Geometry>::value
  52. >
  53. {};
  54. template <typename Polygon>
  55. struct perimeter<Polygon, polygon_tag> : detail::calculate_polygon_sum
  56. {
  57. typedef typename default_length_result<Polygon>::type return_type;
  58. typedef detail::length::range_length
  59. <
  60. typename ring_type<Polygon>::type,
  61. closure<Polygon>::value
  62. > policy;
  63. template <typename Strategy>
  64. static inline return_type apply(Polygon const& polygon, Strategy const& strategy)
  65. {
  66. return calculate_polygon_sum::apply<return_type, policy>(polygon, strategy);
  67. }
  68. };
  69. template <typename MultiPolygon>
  70. struct perimeter<MultiPolygon, multi_polygon_tag> : detail::multi_sum
  71. {
  72. typedef typename default_length_result<MultiPolygon>::type return_type;
  73. template <typename Strategy>
  74. static inline return_type apply(MultiPolygon const& multi, Strategy const& strategy)
  75. {
  76. return multi_sum::apply
  77. <
  78. return_type,
  79. perimeter<typename boost::range_value<MultiPolygon>::type>
  80. >(multi, strategy);
  81. }
  82. };
  83. // box,n-sphere: to be implemented
  84. } // namespace dispatch
  85. #endif // DOXYGEN_NO_DISPATCH
  86. namespace resolve_strategy {
  87. struct perimeter
  88. {
  89. template <typename Geometry, typename Strategy>
  90. static inline typename default_length_result<Geometry>::type
  91. apply(Geometry const& geometry, Strategy const& strategy)
  92. {
  93. return dispatch::perimeter<Geometry>::apply(geometry, strategy);
  94. }
  95. template <typename Geometry>
  96. static inline typename default_length_result<Geometry>::type
  97. apply(Geometry const& geometry, default_strategy)
  98. {
  99. typedef typename strategy::distance::services::default_strategy
  100. <
  101. point_tag, point_tag, typename point_type<Geometry>::type
  102. >::type strategy_type;
  103. return dispatch::perimeter<Geometry>::apply(geometry, strategy_type());
  104. }
  105. };
  106. } // namespace resolve_strategy
  107. namespace resolve_variant {
  108. template <typename Geometry>
  109. struct perimeter
  110. {
  111. template <typename Strategy>
  112. static inline typename default_length_result<Geometry>::type
  113. apply(Geometry const& geometry, Strategy const& strategy)
  114. {
  115. concepts::check<Geometry const>();
  116. return resolve_strategy::perimeter::apply(geometry, strategy);
  117. }
  118. };
  119. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  120. struct perimeter<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  121. {
  122. typedef typename default_length_result
  123. <
  124. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
  125. >::type result_type;
  126. template <typename Strategy>
  127. struct visitor: boost::static_visitor<result_type>
  128. {
  129. Strategy const& m_strategy;
  130. visitor(Strategy const& strategy): m_strategy(strategy) {}
  131. template <typename Geometry>
  132. typename default_length_result<Geometry>::type
  133. operator()(Geometry const& geometry) const
  134. {
  135. return perimeter<Geometry>::apply(geometry, m_strategy);
  136. }
  137. };
  138. template <typename Strategy>
  139. static inline result_type
  140. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  141. Strategy const& strategy)
  142. {
  143. return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  144. }
  145. };
  146. } // namespace resolve_variant
  147. /*!
  148. \brief \brief_calc{perimeter}
  149. \ingroup perimeter
  150. \details The function perimeter returns the perimeter of a geometry,
  151. using the default distance-calculation-strategy
  152. \tparam Geometry \tparam_geometry
  153. \param geometry \param_geometry
  154. \return \return_calc{perimeter}
  155. \qbk{[include reference/algorithms/perimeter.qbk]}
  156. */
  157. template<typename Geometry>
  158. inline typename default_length_result<Geometry>::type perimeter(
  159. Geometry const& geometry)
  160. {
  161. // detail::throw_on_empty_input(geometry);
  162. return resolve_variant::perimeter<Geometry>::apply(geometry, default_strategy());
  163. }
  164. /*!
  165. \brief \brief_calc{perimeter} \brief_strategy
  166. \ingroup perimeter
  167. \details The function perimeter returns the perimeter of a geometry,
  168. using specified strategy
  169. \tparam Geometry \tparam_geometry
  170. \tparam Strategy \tparam_strategy{distance}
  171. \param geometry \param_geometry
  172. \param strategy strategy to be used for distance calculations.
  173. \return \return_calc{perimeter}
  174. \qbk{distinguish,with strategy}
  175. \qbk{[include reference/algorithms/perimeter.qbk]}
  176. */
  177. template<typename Geometry, typename Strategy>
  178. inline typename default_length_result<Geometry>::type perimeter(
  179. Geometry const& geometry, Strategy const& strategy)
  180. {
  181. // detail::throw_on_empty_input(geometry);
  182. return resolve_variant::perimeter<Geometry>::apply(geometry, strategy);
  183. }
  184. }} // namespace boost::geometry
  185. #endif // BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP