num_points.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014-2020.
  7. // Modifications copyright (c) 2014-2020, Oracle and/or its affiliates.
  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_NUM_POINTS_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_NUM_POINTS_HPP
  17. #include <cstddef>
  18. #include <boost/range/size.hpp>
  19. #include <boost/range/value_type.hpp>
  20. #include <boost/variant/apply_visitor.hpp>
  21. #include <boost/variant/static_visitor.hpp>
  22. #include <boost/variant/variant_fwd.hpp>
  23. #include <boost/geometry/core/closure.hpp>
  24. #include <boost/geometry/core/coordinate_dimension.hpp>
  25. #include <boost/geometry/core/tag_cast.hpp>
  26. #include <boost/geometry/core/tags.hpp>
  27. #include <boost/geometry/algorithms/not_implemented.hpp>
  28. #include <boost/geometry/algorithms/detail/counting.hpp>
  29. #include <boost/geometry/geometries/concepts/check.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. // Silence warning C4127: conditional expression is constant
  33. #if defined(_MSC_VER)
  34. #pragma warning(push)
  35. #pragma warning(disable : 4127)
  36. #endif
  37. #ifndef DOXYGEN_NO_DETAIL
  38. namespace detail { namespace num_points
  39. {
  40. template <bool AddForOpen>
  41. struct range_count
  42. {
  43. template <typename Range>
  44. static inline std::size_t apply(Range const& range)
  45. {
  46. std::size_t n = boost::size(range);
  47. if (AddForOpen
  48. && n > 0
  49. && geometry::closure<Range>::value == open
  50. )
  51. {
  52. return n + 1;
  53. }
  54. return n;
  55. }
  56. };
  57. }} // namespace detail::num_points
  58. #endif // DOXYGEN_NO_DETAIL
  59. #ifndef DOXYGEN_NO_DISPATCH
  60. namespace dispatch
  61. {
  62. template
  63. <
  64. typename Geometry,
  65. bool AddForOpen,
  66. typename Tag = typename tag_cast
  67. <
  68. typename tag<Geometry>::type, multi_tag
  69. >::type
  70. >
  71. struct num_points: not_implemented<Tag>
  72. {};
  73. template <typename Geometry, bool AddForOpen>
  74. struct num_points<Geometry, AddForOpen, point_tag>
  75. : detail::counting::other_count<1>
  76. {};
  77. template <typename Geometry, bool AddForOpen>
  78. struct num_points<Geometry, AddForOpen, box_tag>
  79. : detail::counting::other_count<(1 << geometry::dimension<Geometry>::value)>
  80. {};
  81. template <typename Geometry, bool AddForOpen>
  82. struct num_points<Geometry, AddForOpen, segment_tag>
  83. : detail::counting::other_count<2>
  84. {};
  85. template <typename Geometry, bool AddForOpen>
  86. struct num_points<Geometry, AddForOpen, linestring_tag>
  87. : detail::num_points::range_count<AddForOpen>
  88. {};
  89. template <typename Geometry, bool AddForOpen>
  90. struct num_points<Geometry, AddForOpen, ring_tag>
  91. : detail::num_points::range_count<AddForOpen>
  92. {};
  93. template <typename Geometry, bool AddForOpen>
  94. struct num_points<Geometry, AddForOpen, polygon_tag>
  95. : detail::counting::polygon_count
  96. <
  97. detail::num_points::range_count<AddForOpen>
  98. >
  99. {};
  100. template <typename Geometry, bool AddForOpen>
  101. struct num_points<Geometry, AddForOpen, multi_tag>
  102. : detail::counting::multi_count
  103. <
  104. num_points<typename boost::range_value<Geometry>::type, AddForOpen>
  105. >
  106. {};
  107. } // namespace dispatch
  108. #endif
  109. namespace resolve_variant
  110. {
  111. template <typename Geometry>
  112. struct num_points
  113. {
  114. static inline std::size_t apply(Geometry const& geometry,
  115. bool add_for_open)
  116. {
  117. concepts::check<Geometry const>();
  118. return add_for_open
  119. ? dispatch::num_points<Geometry, true>::apply(geometry)
  120. : dispatch::num_points<Geometry, false>::apply(geometry);
  121. }
  122. };
  123. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  124. struct num_points<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  125. {
  126. struct visitor: boost::static_visitor<std::size_t>
  127. {
  128. bool m_add_for_open;
  129. visitor(bool add_for_open): m_add_for_open(add_for_open) {}
  130. template <typename Geometry>
  131. inline std::size_t operator()(Geometry const& geometry) const
  132. {
  133. return num_points<Geometry>::apply(geometry, m_add_for_open);
  134. }
  135. };
  136. static inline std::size_t
  137. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  138. bool add_for_open)
  139. {
  140. return boost::apply_visitor(visitor(add_for_open), geometry);
  141. }
  142. };
  143. } // namespace resolve_variant
  144. /*!
  145. \brief \brief_calc{number of points}
  146. \ingroup num_points
  147. \details \details_calc{num_points, number of points}.
  148. \tparam Geometry \tparam_geometry
  149. \param geometry \param_geometry
  150. \param add_for_open add one for open geometries (i.e. polygon types which are not closed)
  151. \return \return_calc{number of points}
  152. \qbk{[include reference/algorithms/num_points.qbk]}
  153. */
  154. template <typename Geometry>
  155. inline std::size_t num_points(Geometry const& geometry, bool add_for_open = false)
  156. {
  157. return resolve_variant::num_points<Geometry>::apply(geometry, add_for_open);
  158. }
  159. #if defined(_MSC_VER)
  160. #pragma warning(pop)
  161. #endif
  162. }} // namespace boost::geometry
  163. #endif // BOOST_GEOMETRY_ALGORITHMS_NUM_POINTS_HPP