centroid_weighted_length.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  3. // Copyright (c) 2009-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2015-2023.
  5. // Modifications copyright (c) 2015-2023, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_WEIGHTED_LENGTH_HPP
  14. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_WEIGHTED_LENGTH_HPP
  15. #include <boost/math/special_functions/fpclassify.hpp>
  16. #include <boost/geometry/algorithms/assign.hpp>
  17. #include <boost/geometry/arithmetic/arithmetic.hpp>
  18. // Helper geometry
  19. #include <boost/geometry/geometries/point.hpp>
  20. #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
  21. #include <boost/geometry/strategies/centroid.hpp>
  22. #include <boost/geometry/util/algorithm.hpp>
  23. #include <boost/geometry/util/numeric_cast.hpp>
  24. #include <boost/geometry/util/select_most_precise.hpp>
  25. namespace boost { namespace geometry
  26. {
  27. namespace strategy { namespace centroid
  28. {
  29. template
  30. <
  31. typename Ignored1 = void,
  32. typename Ignored2 = void,
  33. typename CalculationType = void
  34. >
  35. class weighted_length
  36. {
  37. private :
  38. typedef geometry::strategy::distance::pythagoras<CalculationType> pythagoras_strategy;
  39. template <typename GeometryPoint, typename ResultPoint>
  40. struct calculation_type
  41. {
  42. // Below the distance between two GeometryPoints is calculated.
  43. // ResultPoint is taken into account by passing them together here.
  44. typedef typename pythagoras_strategy::template calculation_type
  45. <
  46. GeometryPoint, ResultPoint
  47. >::type type;
  48. };
  49. template <typename GeometryPoint, typename ResultPoint>
  50. class sums
  51. {
  52. friend class weighted_length;
  53. template <typename, typename> friend struct set_sum_div_length;
  54. typedef typename calculation_type<GeometryPoint, ResultPoint>::type calc_type;
  55. typedef typename geometry::model::point
  56. <
  57. calc_type,
  58. geometry::dimension<ResultPoint>::value,
  59. cs::cartesian
  60. > work_point;
  61. calc_type length;
  62. work_point average_sum;
  63. public:
  64. inline sums()
  65. : length(calc_type())
  66. {
  67. geometry::assign_zero(average_sum);
  68. }
  69. };
  70. public :
  71. template <typename GeometryPoint, typename ResultPoint>
  72. struct state_type
  73. {
  74. typedef sums<GeometryPoint, ResultPoint> type;
  75. };
  76. template <typename GeometryPoint, typename ResultPoint>
  77. static inline void apply(GeometryPoint const& p1, GeometryPoint const& p2,
  78. sums<GeometryPoint, ResultPoint>& state)
  79. {
  80. typedef typename calculation_type<GeometryPoint, ResultPoint>::type distance_type;
  81. distance_type const d = pythagoras_strategy::apply(p1, p2);
  82. state.length += d;
  83. distance_type const d_half = d / distance_type(2);
  84. geometry::detail::for_each_dimension<ResultPoint>([&](auto dimension)
  85. {
  86. distance_type const coord1 = get<dimension>(p1);
  87. distance_type const coord2 = get<dimension>(p2);
  88. distance_type const wm = (coord1 + coord2) * d_half; // weighted median
  89. set<dimension>(state.average_sum, get<dimension>(state.average_sum) + wm);
  90. });
  91. }
  92. template <typename GeometryPoint, typename ResultPoint>
  93. static inline bool result(sums<GeometryPoint, ResultPoint> const& state,
  94. ResultPoint& centroid)
  95. {
  96. typedef typename calculation_type<GeometryPoint, ResultPoint>::type distance_type;
  97. distance_type const zero = distance_type();
  98. if (! geometry::math::equals(state.length, zero)
  99. && boost::math::isfinite(state.length)) // Prevent NaN centroid coordinates
  100. {
  101. // NOTE: above distance_type is checked, not the centroid coordinate_type
  102. // which means that the centroid can still be filled with INF
  103. // if e.g. distance_type is double and centroid contains floats
  104. geometry::detail::for_each_dimension<ResultPoint>([&](auto dimension)
  105. {
  106. typedef typename geometry::coordinate_type<ResultPoint>::type coordinate_type;
  107. geometry::set<dimension>(
  108. centroid,
  109. util::numeric_cast<coordinate_type>(
  110. geometry::get<dimension>(state.average_sum) / state.length
  111. )
  112. );
  113. });
  114. return true;
  115. }
  116. return false;
  117. }
  118. };
  119. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  120. namespace services
  121. {
  122. // Register this strategy for linear geometries, in all dimensions
  123. template <std::size_t N, typename Point, typename Geometry>
  124. struct default_strategy
  125. <
  126. cartesian_tag,
  127. linear_tag,
  128. N,
  129. Point,
  130. Geometry
  131. >
  132. {
  133. typedef weighted_length
  134. <
  135. Point,
  136. typename point_type<Geometry>::type
  137. > type;
  138. };
  139. } // namespace services
  140. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  141. }} // namespace strategy::centroid
  142. }} // namespace boost::geometry
  143. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_WEIGHTED_LENGTH_HPP