num_segments.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2020, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP
  9. #include <cstddef>
  10. #include <boost/range/size.hpp>
  11. #include <boost/range/value_type.hpp>
  12. #include <boost/variant/apply_visitor.hpp>
  13. #include <boost/variant/static_visitor.hpp>
  14. #include <boost/variant/variant_fwd.hpp>
  15. #include <boost/geometry/core/closure.hpp>
  16. #include <boost/geometry/core/tag.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #include <boost/geometry/util/range.hpp>
  19. #include <boost/geometry/geometries/concepts/check.hpp>
  20. #include <boost/geometry/algorithms/not_implemented.hpp>
  21. #include <boost/geometry/algorithms/detail/counting.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. #ifndef DOXYGEN_NO_DETAIL
  25. namespace detail { namespace num_segments
  26. {
  27. struct range_count
  28. {
  29. template <typename Range>
  30. static inline std::size_t apply(Range const& range)
  31. {
  32. std::size_t n = boost::size(range);
  33. if ( n <= 1 )
  34. {
  35. return 0;
  36. }
  37. return
  38. geometry::closure<Range>::value == open
  39. ?
  40. n
  41. :
  42. static_cast<std::size_t>(n - 1);
  43. }
  44. };
  45. }} // namespace detail::num_segments
  46. #endif // DOXYGEN_NO_DETAIL
  47. #ifndef DOXYGEN_NO_DISPATCH
  48. namespace dispatch
  49. {
  50. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  51. struct num_segments
  52. : not_implemented<Tag>
  53. {};
  54. template <typename Geometry>
  55. struct num_segments<Geometry, point_tag>
  56. : detail::counting::other_count<0>
  57. {};
  58. // the number of segments (1-dimensional faces) of the hypercube is
  59. // given by the formula: d * 2^(d-1), where d is the dimension of the
  60. // hypercube; see also:
  61. // http://en.wikipedia.org/wiki/Hypercube
  62. template <typename Geometry>
  63. struct num_segments<Geometry, box_tag>
  64. : detail::counting::other_count
  65. <
  66. geometry::dimension<Geometry>::value
  67. * (1 << (geometry::dimension<Geometry>::value - 1))
  68. >
  69. {};
  70. template <typename Geometry>
  71. struct num_segments<Geometry, segment_tag>
  72. : detail::counting::other_count<1>
  73. {};
  74. template <typename Geometry>
  75. struct num_segments<Geometry, linestring_tag>
  76. : detail::num_segments::range_count
  77. {};
  78. template <typename Geometry>
  79. struct num_segments<Geometry, ring_tag>
  80. : detail::num_segments::range_count
  81. {};
  82. template <typename Geometry>
  83. struct num_segments<Geometry, polygon_tag>
  84. : detail::counting::polygon_count<detail::num_segments::range_count>
  85. {};
  86. template <typename Geometry>
  87. struct num_segments<Geometry, multi_point_tag>
  88. : detail::counting::other_count<0>
  89. {};
  90. template <typename Geometry>
  91. struct num_segments<Geometry, multi_linestring_tag>
  92. : detail::counting::multi_count
  93. <
  94. num_segments< typename boost::range_value<Geometry>::type>
  95. >
  96. {};
  97. template <typename Geometry>
  98. struct num_segments<Geometry, multi_polygon_tag>
  99. : detail::counting::multi_count
  100. <
  101. num_segments< typename boost::range_value<Geometry>::type>
  102. >
  103. {};
  104. } // namespace dispatch
  105. #endif // DOXYGEN_NO_DISPATCH
  106. namespace resolve_variant
  107. {
  108. template <typename Geometry>
  109. struct num_segments
  110. {
  111. static inline std::size_t apply(Geometry const& geometry)
  112. {
  113. concepts::check<Geometry const>();
  114. return dispatch::num_segments<Geometry>::apply(geometry);
  115. }
  116. };
  117. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  118. struct num_segments<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  119. {
  120. struct visitor: boost::static_visitor<std::size_t>
  121. {
  122. template <typename Geometry>
  123. inline std::size_t operator()(Geometry const& geometry) const
  124. {
  125. return num_segments<Geometry>::apply(geometry);
  126. }
  127. };
  128. static inline std::size_t
  129. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry)
  130. {
  131. return boost::apply_visitor(visitor(), geometry);
  132. }
  133. };
  134. } // namespace resolve_variant
  135. /*!
  136. \brief \brief_calc{number of segments}
  137. \ingroup num_segments
  138. \details \details_calc{num_segments, number of segments}.
  139. \tparam Geometry \tparam_geometry
  140. \param geometry \param_geometry
  141. \return \return_calc{number of segments}
  142. \qbk{[include reference/algorithms/num_segments.qbk]}
  143. */
  144. template <typename Geometry>
  145. inline std::size_t num_segments(Geometry const& geometry)
  146. {
  147. return resolve_variant::num_segments<Geometry>::apply(geometry);
  148. }
  149. }} // namespace boost::geometry
  150. #endif // BOOST_GEOMETRY_ALGORITHMS_NUM_SEGMENTS_HPP