centroid_weighted_length.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  3. // Copyright (c) 2009-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  5. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_WEIGHTED_LENGTH_HPP
  10. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_WEIGHTED_LENGTH_HPP
  11. #include <boost/geometry/algorithms/distance.hpp>
  12. #include <boost/geometry/arithmetic/arithmetic.hpp>
  13. #include <boost/geometry/util/select_most_precise.hpp>
  14. #include <boost/geometry/strategies/centroid.hpp>
  15. #include <boost/geometry/strategies/default_distance_result.hpp>
  16. // Helper geometry
  17. #include <boost/geometry/geometries/point.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. namespace strategy { namespace centroid
  21. {
  22. namespace detail
  23. {
  24. template <typename Type, std::size_t DimensionCount>
  25. struct weighted_length_sums
  26. {
  27. typedef typename geometry::model::point
  28. <
  29. Type, DimensionCount,
  30. cs::cartesian
  31. > work_point;
  32. Type length;
  33. work_point average_sum;
  34. inline weighted_length_sums()
  35. : length(Type())
  36. {
  37. geometry::assign_zero(average_sum);
  38. }
  39. };
  40. }
  41. template
  42. <
  43. typename Point,
  44. typename PointOfSegment = Point
  45. >
  46. class weighted_length
  47. {
  48. private :
  49. typedef typename select_most_precise
  50. <
  51. typename default_distance_result<Point>::type,
  52. typename default_distance_result<PointOfSegment>::type
  53. >::type distance_type;
  54. public :
  55. typedef detail::weighted_length_sums
  56. <
  57. distance_type,
  58. geometry::dimension<Point>::type::value
  59. > state_type;
  60. static inline void apply(PointOfSegment const& p1,
  61. PointOfSegment const& p2, state_type& state)
  62. {
  63. distance_type const d = geometry::distance(p1, p2);
  64. state.length += d;
  65. typename state_type::work_point weighted_median;
  66. geometry::assign_zero(weighted_median);
  67. geometry::add_point(weighted_median, p1);
  68. geometry::add_point(weighted_median, p2);
  69. geometry::multiply_value(weighted_median, d/2);
  70. geometry::add_point(state.average_sum, weighted_median);
  71. }
  72. static inline bool result(state_type const& state, Point& centroid)
  73. {
  74. distance_type const zero = distance_type();
  75. if (! geometry::math::equals(state.length, zero))
  76. {
  77. assign_zero(centroid);
  78. add_point(centroid, state.average_sum);
  79. divide_value(centroid, state.length);
  80. return true;
  81. }
  82. return false;
  83. }
  84. };
  85. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  86. namespace services
  87. {
  88. // Register this strategy for linear geometries, in all dimensions
  89. template <std::size_t N, typename Point, typename Geometry>
  90. struct default_strategy
  91. <
  92. cartesian_tag,
  93. linear_tag,
  94. N,
  95. Point,
  96. Geometry
  97. >
  98. {
  99. typedef weighted_length
  100. <
  101. Point,
  102. typename point_type<Geometry>::type
  103. > type;
  104. };
  105. } // namespace services
  106. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  107. }} // namespace strategy::centroid
  108. }} // namespace boost::geometry
  109. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_WEIGHTED_LENGTH_HPP