/
poly_utils.cpp
173 lines (128 loc) · 5.36 KB
/
poly_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <poly_utils.h>
#include <geom_poly_types.h>
#include <gtl_poly_types.h>
#include <gtl_poly_serialize.h>
#include <geom_poly_serialize.h>
const int points_per_circle = 36;
boost::geometry::strategy::buffer::join_round join_strategy_round(points_per_circle);
boost::geometry::strategy::buffer::end_round end_strategy_round(points_per_circle);
boost::geometry::strategy::buffer::join_miter join_strategy_miter;
boost::geometry::strategy::buffer::end_flat end_strategy_flat;
boost::geometry::strategy::buffer::point_circle circle_strategy(points_per_circle);
boost::geometry::strategy::buffer::point_square point_strategy;
boost::geometry::strategy::buffer::side_straight side_strategy;
void gtl_poly2geom_poly(const Polygon_Holes& gtlpoly, polygon& boost_poly)
{
boost_poly.clear();
for(Point pt : gtlpoly) {
boost_poly.outer().push_back(point(gtl::x(pt),gtl::y(pt)));
}
int num_holes =0;
for(auto iter = gtlpoly.begin_holes(); iter != gtlpoly.end_holes(); ++iter) {
num_holes++;
const gtl::polygon_data<int> h = *iter;
ring r;
for(Point pt : h) {
r.push_back(point(gtl::x(pt),gtl::y(pt)));
}
boost_poly.inners().push_back(r);
}
// gtl goes in the wrong direction. clockwise vs c-clockwise.
boost::geometry::correct(boost_poly);
//std::cout << "gtl2boost " << gtlpoly.size() << " holes " << num_holes << "\n";
}
void gtl_poly2geom_poly(const Polygon_NoHoles& gtlpoly, polygon& boost_poly)
{
boost_poly.clear();
for(Point pt : gtlpoly) {
boost_poly.outer().push_back(point(gtl::x(pt),gtl::y(pt)));
}
boost::geometry::correct(boost_poly);
//std::cout << "gtl2boost " << gtlpoly.size() << " holes " << num_holes << "\n";
}
void geom_poly2gtl_poly(const polygon& boost_poly, Polygon_Holes& gtlpoly)
{
//std::cout << "boost2gtl " << boost_poly.outer().size() << " holes " << boost_poly.inners().size() << "\n";
std::vector<Point> pts;
for (point pt : boost_poly.outer()) {
pts.push_back(gtl::construct<Point>(pt.get<0>(),
pt.get<1>()));
}
gtl::set_points(gtlpoly, pts.begin(), pts.end());
std::vector<Polygon_NoHoles> holes;
for (ring r: boost_poly.inners()) {
std::vector<Point> pts;
for (point pt : r) {
pts.push_back(gtl::construct<Point>(pt.get<0>(),
pt.get<1>()));
}
Polygon_NoHoles hole;
gtl::set_points(hole, pts.begin(), pts.end());
holes.push_back(hole);
}
gtl::set_holes(gtlpoly, holes.begin(), holes.end());
}
Polygon_Holes gen_capped_line(std::vector<Point> &path, int width)
{
boost::geometry::model::linestring<point> ls;
for(auto pt : path) {
boost::geometry::append(ls, point(gtl::x(pt), gtl::y(pt)));
}
multi_polygon result;
// Declare strategies
const int buffer_distance = width/2.0;
boost::geometry::strategy::buffer::distance_symmetric<double> distance_strategy(buffer_distance);
// Create the buffer of a linestring
// buffer doesn't work with int coordinate type.
boost::geometry::buffer(ls, result,
distance_strategy, side_strategy,
join_strategy_round, end_strategy_round, circle_strategy);
// we're just buffering a line. it should always be one polygon.
assert(result.size() == 1);
Polygon_Holes gtlpoly;
geom_poly2gtl_poly(result.front(), gtlpoly);
return gtlpoly;
}
PolygonSet buffer_poly(const Polygon_Holes >lpoly, std::vector<int> distances, bool flat)
{
polygon boost_poly;
gtl_poly2geom_poly(gtlpoly, boost_poly);
multi_polygon curpolys{boost_poly};
for (int distance : distances) {
boost::geometry::strategy::buffer::distance_symmetric<double> distance_strategy(distance);
multi_polygon newpolys;
for (polygon poly : curpolys) {
multi_polygon result;
polygon simplified;
// with a scale factor of inches*1000000, 1 mil is 1000.
// so simplifying by 10 eliminates 1/100 mil.
// why do I have to do this? some buffering operations yield some polys that confuse subsequent
// buffer operations.
boost::geometry::simplify(poly, simplified, 10);
// buffer doesn't work with int coordinate type.
if (flat) {
boost::geometry::buffer(simplified, result,
distance_strategy, side_strategy,
join_strategy_miter, end_strategy_flat, point_strategy);
} else {
boost::geometry::buffer(simplified, result,
distance_strategy, side_strategy,
join_strategy_round, end_strategy_round, circle_strategy);
}
newpolys.insert(newpolys.end(), result.begin(), result.end());
}
curpolys = newpolys;
}
PolygonSet retval;
for (auto poly : curpolys) {
Polygon_Holes newgtlpoly;
geom_poly2gtl_poly(poly, newgtlpoly);
retval.push_back(newgtlpoly);
}
return retval;
}
PolygonSet buffer_poly(const Polygon_Holes >lpoly, int distance, bool flat)
{
std::vector<int> distances{distance};
return buffer_poly(gtlpoly, distances, flat);
}