buffer_join_round_by_divide.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2012-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP
  8. #include <boost/geometry/core/cs.hpp>
  9. #include <boost/geometry/policies/compare.hpp>
  10. #include <boost/geometry/strategies/buffer.hpp>
  11. #include <boost/geometry/util/math.hpp>
  12. #include <boost/geometry/util/select_most_precise.hpp>
  13. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  14. #include <boost/geometry/io/wkt/wkt.hpp>
  15. #endif
  16. namespace boost { namespace geometry
  17. {
  18. namespace strategy { namespace buffer
  19. {
  20. class join_round_by_divide
  21. {
  22. public :
  23. inline join_round_by_divide(std::size_t max_level = 4)
  24. : m_max_level(max_level)
  25. {}
  26. template
  27. <
  28. typename PromotedType,
  29. typename Point,
  30. typename DistanceType,
  31. typename RangeOut
  32. >
  33. inline void mid_points(Point const& vertex,
  34. Point const& p1, Point const& p2,
  35. DistanceType const& buffer_distance,
  36. RangeOut& range_out,
  37. std::size_t level = 1) const
  38. {
  39. typedef typename coordinate_type<Point>::type coordinate_type;
  40. // Generate 'vectors'
  41. coordinate_type const vp1_x = get<0>(p1) - get<0>(vertex);
  42. coordinate_type const vp1_y = get<1>(p1) - get<1>(vertex);
  43. coordinate_type const vp2_x = (get<0>(p2) - get<0>(vertex));
  44. coordinate_type const vp2_y = (get<1>(p2) - get<1>(vertex));
  45. // Average them to generate vector in between
  46. coordinate_type const two = 2;
  47. coordinate_type const v_x = (vp1_x + vp2_x) / two;
  48. coordinate_type const v_y = (vp1_y + vp2_y) / two;
  49. PromotedType const length2 = geometry::math::sqrt(v_x * v_x + v_y * v_y);
  50. PromotedType prop = buffer_distance / length2;
  51. Point mid_point;
  52. set<0>(mid_point, get<0>(vertex) + v_x * prop);
  53. set<1>(mid_point, get<1>(vertex) + v_y * prop);
  54. if (level < m_max_level)
  55. {
  56. mid_points<PromotedType>(vertex, p1, mid_point, buffer_distance, range_out, level + 1);
  57. }
  58. range_out.push_back(mid_point);
  59. if (level < m_max_level)
  60. {
  61. mid_points<PromotedType>(vertex, mid_point, p2, buffer_distance, range_out, level + 1);
  62. }
  63. }
  64. template <typename Point, typename DistanceType, typename RangeOut>
  65. inline bool apply(Point const& ip, Point const& vertex,
  66. Point const& perp1, Point const& perp2,
  67. DistanceType const& buffer_distance,
  68. RangeOut& range_out) const
  69. {
  70. typedef typename coordinate_type<Point>::type coordinate_type;
  71. typedef typename geometry::select_most_precise
  72. <
  73. coordinate_type,
  74. double
  75. >::type promoted_type;
  76. geometry::equal_to<Point> equals;
  77. if (equals(perp1, perp2))
  78. {
  79. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  80. std::cout << "Corner for equal points " << geometry::wkt(ip) << " " << geometry::wkt(perp1) << std::endl;
  81. #endif
  82. return false;
  83. }
  84. // Generate 'vectors'
  85. coordinate_type const vix = (get<0>(ip) - get<0>(vertex));
  86. coordinate_type const viy = (get<1>(ip) - get<1>(vertex));
  87. promoted_type const length_i = geometry::math::sqrt(vix * vix + viy * viy);
  88. promoted_type const bd = geometry::math::abs(buffer_distance);
  89. promoted_type prop = bd / length_i;
  90. Point bp;
  91. set<0>(bp, get<0>(vertex) + vix * prop);
  92. set<1>(bp, get<1>(vertex) + viy * prop);
  93. range_out.push_back(perp1);
  94. if (m_max_level > 1)
  95. {
  96. mid_points<promoted_type>(vertex, perp1, bp, bd, range_out);
  97. range_out.push_back(bp);
  98. mid_points<promoted_type>(vertex, bp, perp2, bd, range_out);
  99. }
  100. else if (m_max_level == 1)
  101. {
  102. range_out.push_back(bp);
  103. }
  104. range_out.push_back(perp2);
  105. return true;
  106. }
  107. template <typename NumericType>
  108. static inline NumericType max_distance(NumericType const& distance)
  109. {
  110. return distance;
  111. }
  112. private :
  113. std::size_t m_max_level;
  114. };
  115. }} // namespace strategy::buffer
  116. }} // namespace boost::geometry
  117. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP