num_points.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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-2021.
  7. // Modifications copyright (c) 2014-2021, 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/geometry/algorithms/detail/counting.hpp>
  21. #include <boost/geometry/algorithms/detail/visit.hpp>
  22. #include <boost/geometry/algorithms/not_implemented.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/core/visit.hpp>
  28. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
  29. #include <boost/geometry/geometries/concepts/check.hpp>
  30. #include <boost/geometry/util/type_traits_std.hpp>
  31. namespace boost { namespace geometry
  32. {
  33. // Silence warning C4127: conditional expression is constant
  34. #if defined(_MSC_VER)
  35. #pragma warning(push)
  36. #pragma warning(disable : 4127)
  37. #endif
  38. #ifndef DOXYGEN_NO_DETAIL
  39. namespace detail { namespace num_points
  40. {
  41. template <bool AddForOpen>
  42. struct range_count
  43. {
  44. template <typename Range>
  45. static inline std::size_t apply(Range const& range)
  46. {
  47. std::size_t n = boost::size(range);
  48. if (AddForOpen
  49. && n > 0
  50. && geometry::closure<Range>::value == open
  51. )
  52. {
  53. return n + 1;
  54. }
  55. return n;
  56. }
  57. };
  58. }} // namespace detail::num_points
  59. #endif // DOXYGEN_NO_DETAIL
  60. #ifndef DOXYGEN_NO_DISPATCH
  61. namespace dispatch
  62. {
  63. template
  64. <
  65. typename Geometry,
  66. bool AddForOpen,
  67. typename Tag = tag_cast_t<tag_t<Geometry>, multi_tag>
  68. >
  69. struct num_points: not_implemented<Tag>
  70. {};
  71. template <typename Geometry, bool AddForOpen>
  72. struct num_points<Geometry, AddForOpen, point_tag>
  73. : detail::counting::other_count<1>
  74. {};
  75. template <typename Geometry, bool AddForOpen>
  76. struct num_points<Geometry, AddForOpen, box_tag>
  77. : detail::counting::other_count<(1 << geometry::dimension<Geometry>::value)>
  78. {};
  79. template <typename Geometry, bool AddForOpen>
  80. struct num_points<Geometry, AddForOpen, segment_tag>
  81. : detail::counting::other_count<2>
  82. {};
  83. template <typename Geometry, bool AddForOpen>
  84. struct num_points<Geometry, AddForOpen, linestring_tag>
  85. : detail::num_points::range_count<AddForOpen>
  86. {};
  87. template <typename Geometry, bool AddForOpen>
  88. struct num_points<Geometry, AddForOpen, ring_tag>
  89. : detail::num_points::range_count<AddForOpen>
  90. {};
  91. template <typename Geometry, bool AddForOpen>
  92. struct num_points<Geometry, AddForOpen, polygon_tag>
  93. : detail::counting::polygon_count
  94. <
  95. detail::num_points::range_count<AddForOpen>
  96. >
  97. {};
  98. template <typename Geometry, bool AddForOpen>
  99. struct num_points<Geometry, AddForOpen, multi_tag>
  100. : detail::counting::multi_count
  101. <
  102. num_points<typename boost::range_value<Geometry>::type, AddForOpen>
  103. >
  104. {};
  105. } // namespace dispatch
  106. #endif
  107. namespace resolve_dynamic
  108. {
  109. template <typename Geometry, typename Tag = tag_t<Geometry>>
  110. struct num_points
  111. {
  112. static inline std::size_t apply(Geometry const& geometry, bool add_for_open)
  113. {
  114. concepts::check<Geometry const>();
  115. return add_for_open
  116. ? dispatch::num_points<Geometry, true>::apply(geometry)
  117. : dispatch::num_points<Geometry, false>::apply(geometry);
  118. }
  119. };
  120. template <typename Geometry>
  121. struct num_points<Geometry, dynamic_geometry_tag>
  122. {
  123. static inline std::size_t apply(Geometry const& geometry, bool add_for_open)
  124. {
  125. std::size_t result = 0;
  126. traits::visit<Geometry>::apply([&](auto const& g)
  127. {
  128. result = num_points<util::remove_cref_t<decltype(g)>>::apply(g, add_for_open);
  129. }, geometry);
  130. return result;
  131. }
  132. };
  133. template <typename Geometry>
  134. struct num_points<Geometry, geometry_collection_tag>
  135. {
  136. static inline std::size_t apply(Geometry const& geometry, bool add_for_open)
  137. {
  138. std::size_t result = 0;
  139. detail::visit_breadth_first([&](auto const& g)
  140. {
  141. result += num_points<util::remove_cref_t<decltype(g)>>::apply(g, add_for_open);
  142. return true;
  143. }, geometry);
  144. return result;
  145. }
  146. };
  147. } // namespace resolve_dynamic
  148. /*!
  149. \brief \brief_calc{number of points}
  150. \ingroup num_points
  151. \details \details_calc{num_points, number of points}.
  152. \tparam Geometry \tparam_geometry
  153. \param geometry \param_geometry
  154. \param add_for_open add one for open geometries (i.e. polygon types which are not closed)
  155. \return \return_calc{number of points}
  156. \qbk{[include reference/algorithms/num_points.qbk]}
  157. */
  158. template <typename Geometry>
  159. inline std::size_t num_points(Geometry const& geometry, bool add_for_open = false)
  160. {
  161. return resolve_dynamic::num_points<Geometry>::apply(geometry, add_for_open);
  162. }
  163. #if defined(_MSC_VER)
  164. #pragma warning(pop)
  165. #endif
  166. }} // namespace boost::geometry
  167. #endif // BOOST_GEOMETRY_ALGORITHMS_NUM_POINTS_HPP