num_points.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 = typename tag_cast
  68. <
  69. typename tag<Geometry>::type, multi_tag
  70. >::type
  71. >
  72. struct num_points: not_implemented<Tag>
  73. {};
  74. template <typename Geometry, bool AddForOpen>
  75. struct num_points<Geometry, AddForOpen, point_tag>
  76. : detail::counting::other_count<1>
  77. {};
  78. template <typename Geometry, bool AddForOpen>
  79. struct num_points<Geometry, AddForOpen, box_tag>
  80. : detail::counting::other_count<(1 << geometry::dimension<Geometry>::value)>
  81. {};
  82. template <typename Geometry, bool AddForOpen>
  83. struct num_points<Geometry, AddForOpen, segment_tag>
  84. : detail::counting::other_count<2>
  85. {};
  86. template <typename Geometry, bool AddForOpen>
  87. struct num_points<Geometry, AddForOpen, linestring_tag>
  88. : detail::num_points::range_count<AddForOpen>
  89. {};
  90. template <typename Geometry, bool AddForOpen>
  91. struct num_points<Geometry, AddForOpen, ring_tag>
  92. : detail::num_points::range_count<AddForOpen>
  93. {};
  94. template <typename Geometry, bool AddForOpen>
  95. struct num_points<Geometry, AddForOpen, polygon_tag>
  96. : detail::counting::polygon_count
  97. <
  98. detail::num_points::range_count<AddForOpen>
  99. >
  100. {};
  101. template <typename Geometry, bool AddForOpen>
  102. struct num_points<Geometry, AddForOpen, multi_tag>
  103. : detail::counting::multi_count
  104. <
  105. num_points<typename boost::range_value<Geometry>::type, AddForOpen>
  106. >
  107. {};
  108. } // namespace dispatch
  109. #endif
  110. namespace resolve_dynamic
  111. {
  112. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  113. struct num_points
  114. {
  115. static inline std::size_t apply(Geometry const& geometry, 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 <typename Geometry>
  124. struct num_points<Geometry, dynamic_geometry_tag>
  125. {
  126. static inline std::size_t apply(Geometry const& geometry, bool add_for_open)
  127. {
  128. std::size_t result = 0;
  129. traits::visit<Geometry>::apply([&](auto const& g)
  130. {
  131. result = num_points<util::remove_cref_t<decltype(g)>>::apply(g, add_for_open);
  132. }, geometry);
  133. return result;
  134. }
  135. };
  136. template <typename Geometry>
  137. struct num_points<Geometry, geometry_collection_tag>
  138. {
  139. static inline std::size_t apply(Geometry const& geometry, bool add_for_open)
  140. {
  141. std::size_t result = 0;
  142. detail::visit_breadth_first([&](auto const& g)
  143. {
  144. result += num_points<util::remove_cref_t<decltype(g)>>::apply(g, add_for_open);
  145. return true;
  146. }, geometry);
  147. return result;
  148. }
  149. };
  150. } // namespace resolve_dynamic
  151. /*!
  152. \brief \brief_calc{number of points}
  153. \ingroup num_points
  154. \details \details_calc{num_points, number of points}.
  155. \tparam Geometry \tparam_geometry
  156. \param geometry \param_geometry
  157. \param add_for_open add one for open geometries (i.e. polygon types which are not closed)
  158. \return \return_calc{number of points}
  159. \qbk{[include reference/algorithms/num_points.qbk]}
  160. */
  161. template <typename Geometry>
  162. inline std::size_t num_points(Geometry const& geometry, bool add_for_open = false)
  163. {
  164. return resolve_dynamic::num_points<Geometry>::apply(geometry, add_for_open);
  165. }
  166. #if defined(_MSC_VER)
  167. #pragma warning(pop)
  168. #endif
  169. }} // namespace boost::geometry
  170. #endif // BOOST_GEOMETRY_ALGORITHMS_NUM_POINTS_HPP