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