]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | ||
3 | // Copyright (c) 2012-2014 Barend Gehrels, Amsterdam, the Netherlands. | |
4 | ||
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) | |
8 | ||
9 | #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP | |
10 | #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP | |
11 | ||
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> | |
17 | ||
18 | #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN | |
19 | #include <boost/geometry/io/wkt/wkt.hpp> | |
20 | #endif | |
21 | ||
22 | namespace boost { namespace geometry | |
23 | { | |
24 | ||
25 | ||
26 | namespace strategy { namespace buffer | |
27 | { | |
28 | ||
29 | ||
30 | class join_round_by_divide | |
31 | { | |
32 | public : | |
33 | ||
34 | inline join_round_by_divide(std::size_t max_level = 4) | |
35 | : m_max_level(max_level) | |
36 | {} | |
37 | ||
38 | template | |
39 | < | |
40 | typename PromotedType, | |
41 | typename Point, | |
42 | typename DistanceType, | |
43 | typename RangeOut | |
44 | > | |
45 | inline void mid_points(Point const& vertex, | |
46 | Point const& p1, Point const& p2, | |
47 | DistanceType const& buffer_distance, | |
48 | RangeOut& range_out, | |
49 | std::size_t level = 1) const | |
50 | { | |
51 | typedef typename coordinate_type<Point>::type coordinate_type; | |
52 | ||
53 | // Generate 'vectors' | |
54 | coordinate_type const vp1_x = get<0>(p1) - get<0>(vertex); | |
55 | coordinate_type const vp1_y = get<1>(p1) - get<1>(vertex); | |
56 | ||
57 | coordinate_type const vp2_x = (get<0>(p2) - get<0>(vertex)); | |
58 | coordinate_type const vp2_y = (get<1>(p2) - get<1>(vertex)); | |
59 | ||
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; | |
64 | ||
65 | PromotedType const length2 = geometry::math::sqrt(v_x * v_x + v_y * v_y); | |
66 | ||
67 | PromotedType prop = buffer_distance / length2; | |
68 | ||
69 | Point mid_point; | |
70 | set<0>(mid_point, get<0>(vertex) + v_x * prop); | |
71 | set<1>(mid_point, get<1>(vertex) + v_y * prop); | |
72 | ||
73 | if (level < m_max_level) | |
74 | { | |
75 | mid_points<PromotedType>(vertex, p1, mid_point, buffer_distance, range_out, level + 1); | |
76 | } | |
77 | range_out.push_back(mid_point); | |
78 | if (level < m_max_level) | |
79 | { | |
80 | mid_points<PromotedType>(vertex, mid_point, p2, buffer_distance, range_out, level + 1); | |
81 | } | |
82 | } | |
83 | ||
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 | |
89 | { | |
90 | typedef typename coordinate_type<Point>::type coordinate_type; | |
91 | ||
92 | typedef typename geometry::select_most_precise | |
93 | < | |
94 | coordinate_type, | |
95 | double | |
96 | >::type promoted_type; | |
97 | ||
98 | geometry::equal_to<Point> equals; | |
99 | ||
100 | if (equals(perp1, perp2)) | |
101 | { | |
102 | #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN | |
103 | std::cout << "Corner for equal points " << geometry::wkt(ip) << " " << geometry::wkt(perp1) << std::endl; | |
104 | #endif | |
105 | return false; | |
106 | } | |
107 | ||
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)); | |
111 | ||
112 | promoted_type const length_i = geometry::math::sqrt(vix * vix + viy * viy); | |
113 | ||
114 | promoted_type const bd = geometry::math::abs(buffer_distance); | |
115 | promoted_type prop = bd / length_i; | |
116 | ||
117 | Point bp; | |
118 | set<0>(bp, get<0>(vertex) + vix * prop); | |
119 | set<1>(bp, get<1>(vertex) + viy * prop); | |
120 | ||
121 | range_out.push_back(perp1); | |
122 | ||
123 | if (m_max_level > 1) | |
124 | { | |
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); | |
128 | } | |
129 | else if (m_max_level == 1) | |
130 | { | |
131 | range_out.push_back(bp); | |
132 | } | |
133 | ||
134 | range_out.push_back(perp2); | |
135 | return true; | |
136 | } | |
137 | ||
138 | template <typename NumericType> | |
139 | static inline NumericType max_distance(NumericType const& distance) | |
140 | { | |
141 | return distance; | |
142 | } | |
143 | ||
144 | private : | |
145 | std::size_t m_max_level; | |
146 | }; | |
147 | ||
148 | ||
149 | }} // namespace strategy::buffer | |
150 | ||
151 | ||
152 | }} // namespace boost::geometry | |
153 | ||
154 | #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_BY_DIVIDE_HPP |