centroid_average.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_STRATEGIES_CARTESIAN_CENTROID_AVERAGE_HPP
  11. #define BOOST_GEOMETRY_MULTI_STRATEGIES_CARTESIAN_CENTROID_AVERAGE_HPP
  12. #include <boost/numeric/conversion/cast.hpp>
  13. #include <boost/type_traits.hpp>
  14. #include <boost/geometry/core/coordinate_type.hpp>
  15. #include <boost/geometry/core/point_type.hpp>
  16. #include <boost/geometry/arithmetic/arithmetic.hpp>
  17. #include <boost/geometry/strategies/centroid.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. namespace strategy { namespace centroid
  21. {
  22. /*!
  23. \brief Centroid calculation taking average of points
  24. \ingroup strategies
  25. */
  26. template
  27. <
  28. typename PointCentroid,
  29. typename Point = PointCentroid
  30. >
  31. class average
  32. {
  33. private :
  34. /*! subclass to keep state */
  35. class sum
  36. {
  37. friend class average;
  38. int count;
  39. PointCentroid centroid;
  40. public :
  41. inline sum()
  42. : count(0)
  43. {
  44. assign_zero(centroid);
  45. }
  46. };
  47. public :
  48. typedef sum state_type;
  49. typedef PointCentroid centroid_point_type;
  50. typedef Point point_type;
  51. static inline void apply(Point const& p, sum& state)
  52. {
  53. add_point(state.centroid, p);
  54. state.count++;
  55. }
  56. static inline void result(sum const& state, PointCentroid& centroid)
  57. {
  58. centroid = state.centroid;
  59. divide_value(centroid, state.count);
  60. }
  61. };
  62. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  63. namespace services
  64. {
  65. template <typename Point, std::size_t DimensionCount, typename Geometry>
  66. struct default_strategy
  67. <
  68. cartesian_tag,
  69. pointlike_tag,
  70. DimensionCount,
  71. Point,
  72. Geometry
  73. >
  74. {
  75. typedef average
  76. <
  77. Point,
  78. typename point_type<Geometry>::type
  79. > type;
  80. };
  81. } // namespace services
  82. #endif
  83. }} // namespace strategy::centroid
  84. }} // namespace boost::geometry
  85. #endif // BOOST_GEOMETRY_MULTI_STRATEGIES_CARTESIAN_CENTROID_AVERAGE_HPP