1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2012-2014 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP
10 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP
12 #include <boost/geometry/core/cs.hpp>
13 #include <boost/geometry/policies/compare.hpp>
14 #include <boost/geometry/strategies/buffer.hpp>
15 #include <boost/geometry/util/math.hpp>
16 #include <boost/geometry/util/select_most_precise.hpp>
18 #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
19 #include <boost/geometry/io/wkt/wkt.hpp>
22 namespace boost { namespace geometry
26 namespace strategy { namespace buffer
30 class join_round_by_divide
34 inline join_round_by_divide(std::size_t max_level = 4)
35 : m_max_level(max_level)
40 typename PromotedType,
42 typename DistanceType,
45 inline void mid_points(Point const& vertex,
46 Point const& p1, Point const& p2,
47 DistanceType const& buffer_distance,
49 std::size_t level = 1) const
51 typedef typename coordinate_type<Point>::type coordinate_type;
54 coordinate_type const vp1_x = get<0>(p1) - get<0>(vertex);
55 coordinate_type const vp1_y = get<1>(p1) - get<1>(vertex);
57 coordinate_type const vp2_x = (get<0>(p2) - get<0>(vertex));
58 coordinate_type const vp2_y = (get<1>(p2) - get<1>(vertex));
60 // Average them to generate vector in between
61 coordinate_type const two = 2;
62 coordinate_type const v_x = (vp1_x + vp2_x) / two;
63 coordinate_type const v_y = (vp1_y + vp2_y) / two;
65 PromotedType const length2 = geometry::math::sqrt(v_x * v_x + v_y * v_y);
67 PromotedType prop = buffer_distance / length2;
70 set<0>(mid_point, get<0>(vertex) + v_x * prop);
71 set<1>(mid_point, get<1>(vertex) + v_y * prop);
73 if (level < m_max_level)
75 mid_points<PromotedType>(vertex, p1, mid_point, buffer_distance, range_out, level + 1);
77 range_out.push_back(mid_point);
78 if (level < m_max_level)
80 mid_points<PromotedType>(vertex, mid_point, p2, buffer_distance, range_out, level + 1);
84 template <typename Point, typename DistanceType, typename RangeOut>
85 inline bool apply(Point const& ip, Point const& vertex,
86 Point const& perp1, Point const& perp2,
87 DistanceType const& buffer_distance,
88 RangeOut& range_out) const
90 typedef typename coordinate_type<Point>::type coordinate_type;
92 typedef typename geometry::select_most_precise
96 >::type promoted_type;
98 geometry::equal_to<Point> equals;
100 if (equals(perp1, perp2))
102 #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
103 std::cout << "Corner for equal points " << geometry::wkt(ip) << " " << geometry::wkt(perp1) << std::endl;
108 // Generate 'vectors'
109 coordinate_type const vix = (get<0>(ip) - get<0>(vertex));
110 coordinate_type const viy = (get<1>(ip) - get<1>(vertex));
112 promoted_type const length_i = geometry::math::sqrt(vix * vix + viy * viy);
114 promoted_type const bd = geometry::math::abs(buffer_distance);
115 promoted_type prop = bd / length_i;
118 set<0>(bp, get<0>(vertex) + vix * prop);
119 set<1>(bp, get<1>(vertex) + viy * prop);
121 range_out.push_back(perp1);
125 mid_points<promoted_type>(vertex, perp1, bp, bd, range_out);
126 range_out.push_back(bp);
127 mid_points<promoted_type>(vertex, bp, perp2, bd, range_out);
129 else if (m_max_level == 1)
131 range_out.push_back(bp);
134 range_out.push_back(perp2);
138 template <typename NumericType>
139 static inline NumericType max_distance(NumericType const& distance)
145 std::size_t m_max_level;
149 }} // namespace strategy::buffer
152 }} // namespace boost::geometry
154 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP