centroid_average.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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-2023 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2015-2021.
  7. // Modifications copyright (c) 2015-2021 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 Ignored1 = void,
  34. typename Ignored2 = void
  35. >
  36. class average
  37. {
  38. private :
  39. /*! subclass to keep state */
  40. template <typename GeometryPoint, typename ResultPoint>
  41. class sum
  42. {
  43. friend class average;
  44. signed_size_type count;
  45. ResultPoint centroid;
  46. public :
  47. inline sum()
  48. : count(0)
  49. {
  50. assign_zero(centroid);
  51. }
  52. };
  53. public :
  54. template <typename GeometryPoint, typename ResultPoint>
  55. struct state_type
  56. {
  57. typedef sum<GeometryPoint, ResultPoint> type;
  58. };
  59. template <typename GeometryPoint, typename ResultPoint>
  60. static inline void apply(GeometryPoint const& p,
  61. sum<GeometryPoint, ResultPoint>& state)
  62. {
  63. add_point(state.centroid, p);
  64. state.count++;
  65. }
  66. template <typename GeometryPoint, typename ResultPoint>
  67. static inline bool result(sum<GeometryPoint, ResultPoint> const& state,
  68. ResultPoint& centroid)
  69. {
  70. centroid = state.centroid;
  71. if ( state.count > 0 )
  72. {
  73. using coord_t = typename coordinate_type<ResultPoint>::type;
  74. divide_value(centroid, static_cast<coord_t>(state.count));
  75. return true;
  76. }
  77. return false;
  78. }
  79. };
  80. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  81. namespace services
  82. {
  83. template <typename Point, std::size_t DimensionCount, typename Geometry>
  84. struct default_strategy
  85. <
  86. cartesian_tag,
  87. pointlike_tag,
  88. DimensionCount,
  89. Point,
  90. Geometry
  91. >
  92. {
  93. typedef average
  94. <
  95. Point,
  96. typename point_type<Geometry>::type
  97. > type;
  98. };
  99. } // namespace services
  100. #endif
  101. }} // namespace strategy::centroid
  102. }} // namespace boost::geometry
  103. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_AVERAGE_HPP