centroid.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_MULTI_ALGORITHMS_CENTROID_HPP
  11. #define BOOST_GEOMETRY_MULTI_ALGORITHMS_CENTROID_HPP
  12. #include <boost/range.hpp>
  13. #include <boost/geometry/algorithms/centroid.hpp>
  14. #include <boost/geometry/multi/core/tags.hpp>
  15. #include <boost/geometry/multi/core/point_type.hpp>
  16. #include <boost/geometry/multi/geometries/concepts/check.hpp>
  17. #include <boost/geometry/multi/algorithms/num_points.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. #ifndef DOXYGEN_NO_DETAIL
  21. namespace detail { namespace centroid
  22. {
  23. /*!
  24. \brief Building block of a multi-point, to be used as Policy in the
  25. more generec centroid_multi
  26. */
  27. struct centroid_multi_point_state
  28. {
  29. template <typename Point, typename Strategy>
  30. static inline void apply(Point const& point,
  31. Strategy const& strategy, typename Strategy::state_type& state)
  32. {
  33. strategy.apply(point, state);
  34. }
  35. };
  36. /*!
  37. \brief Generic implementation which calls a policy to calculate the
  38. centroid of the total of its single-geometries
  39. \details The Policy is, in general, the single-version, with state. So
  40. detail::centroid::centroid_polygon_state is used as a policy for this
  41. detail::centroid::centroid_multi
  42. */
  43. template <typename Policy>
  44. struct centroid_multi
  45. {
  46. template <typename Multi, typename Point, typename Strategy>
  47. static inline void apply(Multi const& multi, Point& centroid,
  48. Strategy const& strategy)
  49. {
  50. #if ! defined(BOOST_GEOMETRY_CENTROID_NO_THROW)
  51. // If there is nothing in any of the ranges, it is not possible
  52. // to calculate the centroid
  53. if (geometry::num_points(multi) == 0)
  54. {
  55. throw centroid_exception();
  56. }
  57. #endif
  58. typename Strategy::state_type state;
  59. for (typename boost::range_iterator<Multi const>::type
  60. it = boost::begin(multi);
  61. it != boost::end(multi);
  62. ++it)
  63. {
  64. Policy::apply(*it, strategy, state);
  65. }
  66. Strategy::result(state, centroid);
  67. }
  68. };
  69. }} // namespace detail::centroid
  70. #endif // DOXYGEN_NO_DETAIL
  71. #ifndef DOXYGEN_NO_DISPATCH
  72. namespace dispatch
  73. {
  74. template <typename MultiLinestring>
  75. struct centroid<MultiLinestring, multi_linestring_tag>
  76. : detail::centroid::centroid_multi
  77. <
  78. detail::centroid::centroid_range_state<closed>
  79. >
  80. {};
  81. template <typename MultiPolygon>
  82. struct centroid<MultiPolygon, multi_polygon_tag>
  83. : detail::centroid::centroid_multi
  84. <
  85. detail::centroid::centroid_polygon_state
  86. >
  87. {};
  88. template <typename MultiPoint>
  89. struct centroid<MultiPoint, multi_point_tag>
  90. : detail::centroid::centroid_multi
  91. <
  92. detail::centroid::centroid_multi_point_state
  93. >
  94. {};
  95. } // namespace dispatch
  96. #endif
  97. }} // namespace boost::geometry
  98. #endif // BOOST_GEOMETRY_MULTI_ALGORITHMS_CENTROID_HPP