centroid_average.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2015.
  7. // Modifications copyright (c) 2015 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, 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_STRATEGIES_CARTESIAN_CENTROID_AVERAGE_HPP
  15. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_AVERAGE_HPP
  16. #include <cstddef>
  17. #include <boost/geometry/algorithms/assign.hpp>
  18. #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
  19. #include <boost/geometry/arithmetic/arithmetic.hpp>
  20. #include <boost/geometry/core/coordinate_type.hpp>
  21. #include <boost/geometry/core/point_type.hpp>
  22. #include <boost/geometry/strategies/centroid.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. namespace strategy { namespace centroid
  26. {
  27. /*!
  28. \brief Centroid calculation taking average of points
  29. \ingroup strategies
  30. */
  31. template
  32. <
  33. typename PointCentroid,
  34. typename Point = PointCentroid
  35. >
  36. class average
  37. {
  38. private :
  39. /*! subclass to keep state */
  40. class sum
  41. {
  42. friend class average;
  43. signed_size_type count;
  44. PointCentroid centroid;
  45. public :
  46. inline sum()
  47. : count(0)
  48. {
  49. assign_zero(centroid);
  50. }
  51. };
  52. public :
  53. typedef sum state_type;
  54. typedef PointCentroid centroid_point_type;
  55. typedef Point point_type;
  56. static inline void apply(Point const& p, sum& state)
  57. {
  58. add_point(state.centroid, p);
  59. state.count++;
  60. }
  61. static inline bool result(sum const& state, PointCentroid& centroid)
  62. {
  63. centroid = state.centroid;
  64. if ( state.count > 0 )
  65. {
  66. divide_value(centroid, state.count);
  67. return true;
  68. }
  69. return false;
  70. }
  71. };
  72. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  73. namespace services
  74. {
  75. template <typename Point, std::size_t DimensionCount, typename Geometry>
  76. struct default_strategy
  77. <
  78. cartesian_tag,
  79. pointlike_tag,
  80. DimensionCount,
  81. Point,
  82. Geometry
  83. >
  84. {
  85. typedef average
  86. <
  87. Point,
  88. typename point_type<Geometry>::type
  89. > type;
  90. };
  91. } // namespace services
  92. #endif
  93. }} // namespace strategy::centroid
  94. }} // namespace boost::geometry
  95. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_AVERAGE_HPP