-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgorithm.cpp
299 lines (276 loc) · 8.63 KB
/
algorithm.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "algorithm.h"
namespace nesting {
namespace geo {
FT pwh_area(const Polygon_with_holes_2& p) {
FT area = 0;
auto& outer = p.outer_boundary();
area += outer.area();
for (auto& hole : p.holes()) {
area -= hole.area();
}
return area;
}
Point_2 find_bottom_left_vertex(const Polygon_2& p) {
FT ymin = INF;
FT xmin = INF;
Point_2 target;
for (auto v = p.vertices_begin(); v != p.vertices_end(); v++) {
auto vy = v->y();
auto vx = v->x();
if (vy < ymin) {
ymin = vy;
xmin = vx;
target = *v;
} else if (vy == ymin) {
if (vx < xmin) {
xmin = vx;
target = *v;
}
}
}
return target;
}
void simplify_polygon(Polygon_2& p) {
auto i = p.begin();
auto j = i + 1;
auto k = j + 1;
while (j != p.end()) {
if (CGAL::collinear(*i, *j, *k)) {
p.erase(j);
} else {
i++;
j++;
k++;
}
if (k == p.end()) {
k = p.begin();
}
}
}
void strict_simplify(Polygon_with_holes_2& p) {
simplify_polygon(p.outer_boundary());
for (auto& h : p.holes()) {
simplify_polygon(h);
}
}
bool is_translated(const Polygon_2& A, const Polygon_2& B) {
if (A == B) {
return true;
}
FT ax = 0, ay = 0, bx = 0, by = 0;
for (auto v = A.vertices_begin(); v != A.vertices_end(); v++) {
ax += v->x();
ay += v->y();
}
for (auto v = B.vertices_begin(); v != B.vertices_end(); v++) {
bx += v->x();
by += v->y();
}
FT size((int)A.size());
Vector_2 v((ax - bx) / size, (ay - by) / size);
Transformation t(CGAL::TRANSLATION, v);
auto translated = CGAL::transform(t, B);
return A == translated;
}
bool is_translated(const Polygon_with_holes_2& A,
const Polygon_with_holes_2& B) {
if (A == B) {
return true;
}
// 只判断A和B的边界,因为B由A旋转得到,内部孔洞均一致
return is_translated(A.outer_boundary(), B.outer_boundary());
}
Polygon_with_holes_2 transform_polygon_with_holes(
const Transformation transformation,
const Polygon_with_holes_2& pgn) {
// Step1 对边界做变形
auto boundary = transform(transformation, pgn.outer_boundary());
std::vector<Polygon_2> holes;
// Step2 对孔做变形
for (auto p = pgn.holes_begin(); p != pgn.holes_end(); ++p) {
holes.push_back(transform(transformation, *p));
}
// 返回变形好的带孔多边形
return Polygon_with_holes_2(boundary, holes.begin(), holes.end());
}
bool is_valid_polygon(const Polygon_2& pgn) {
return CGAL::is_valid_polygon(pgn, traits);
}
bool is_valid_polygon_with_holes(const Polygon_with_holes_2& pwh) {
return CGAL::is_valid_polygon_with_holes(pwh, traits);
}
FT comp_pd(const Polygon_with_holes_2& nfp, const Point_2& p) {
auto& outer = nfp.outer_boundary();
auto res = CGAL::bounded_side_2(outer.begin(), outer.end(), p);
// 在nfp外部或边界上
if (res <= CGAL::ON_BOUNDARY) {
return FT_ZERO;
}
auto& outer_bound = nfp.outer_boundary();
FT pd = INF;
auto holes_end = nfp.holes_end();
for (auto h = nfp.holes_begin(); h != holes_end; h++) {
CGAL::Bounded_side r = CGAL::bounded_side_2(h->begin(), h->end(), p);
// 在nfp孔内
if (r == CGAL::ON_BOUNDED_SIDE) {
return FT_ZERO;
}
// 不在nfp孔内
auto edges_end = h->edges_end();
for (auto e = h->edges_begin(); e != edges_end; e++) {
auto distance = CGAL::squared_distance(*e, p);
pd = distance < pd ? distance : pd;
}
}
auto edges_end = outer_bound.edges_end();
// There is no better way to compute the nearest edge so far.
// Have Tried: Voronoi Diagram in CGAL
for (auto e = outer_bound.edges_begin(); e != edges_end; e++) {
auto distance = CGAL::squared_distance(*e, p);
pd = distance < pd ? distance : pd;
}
return pd;
}
Polygon_2 comp_ifr(const Polygon_2 poly_A, const Polygon_2 poly_B) {
auto bbox_A = poly_A.bbox();
auto first_vertex = poly_B.vertices_begin();
Transformation translate(CGAL::TRANSLATION,
Vector_2(-first_vertex->x(), -first_vertex->y()));
auto shifted_B = transform(translate, poly_B);
auto bbox_B = shifted_B.bbox();
// Sheet装不下
if ((bbox_A.x_span()) < (bbox_B.x_span()) ||
(bbox_A.y_span()) < (bbox_B.y_span())) {
return Polygon_2();
}
Polygon_2 outer_boundary;
auto x1 = bbox_A.xmin() - bbox_B.xmin();
auto x2 = bbox_A.xmax() - bbox_B.xmax();
auto y1 = bbox_A.ymin() - bbox_B.ymin();
auto y2 = bbox_A.ymax() - bbox_B.ymax();
// 去除重复的点,IFR可能退化成线或点
if (x1 == x2) {
outer_boundary.push_back(Point_2(x1, y1));
if (y1 != y2) {
outer_boundary.push_back(Point_2(x1, y2));
}
} else {
outer_boundary.push_back(Point_2(x1, y1));
outer_boundary.push_back(Point_2(x2, y1));
if (y1 != y2) {
outer_boundary.push_back(Point_2(x2, y2));
outer_boundary.push_back(Point_2(x1, y2));
}
}
return outer_boundary;
}
Polygon_2 comp_ifr(const Polygon_with_holes_2 poly_A,
const Polygon_with_holes_2 poly_B) {
auto bbox_A = poly_A.bbox();
auto first_vertex = poly_B.outer_boundary().vertices_begin();
Transformation translate(CGAL::TRANSLATION,
Vector_2(-first_vertex->x(), -first_vertex->y()));
auto shifted_B = transform(translate, poly_B.outer_boundary());
auto bbox_B = shifted_B.bbox();
// Sheet装不下
if ((bbox_A.x_span()) < (bbox_B.x_span()) ||
(bbox_A.y_span()) < (bbox_B.y_span())) {
return Polygon_2();
}
Polygon_2 outer_boundary;
auto x1 = bbox_A.xmin() - bbox_B.xmin();
auto x2 = bbox_A.xmax() - bbox_B.xmax();
auto y1 = bbox_A.ymin() - bbox_B.ymin();
auto y2 = bbox_A.ymax() - bbox_B.ymax();
// 去除重复的点,IFR可能退化成线或点
if (x1 == x2) {
outer_boundary.push_back(Point_2(x1, y1));
if (y1 != y2) {
outer_boundary.push_back(Point_2(x1, y2));
}
} else {
outer_boundary.push_back(Point_2(x1, y1));
outer_boundary.push_back(Point_2(x2, y1));
if (y1 != y2) {
outer_boundary.push_back(Point_2(x2, y2));
outer_boundary.push_back(Point_2(x1, y2));
}
}
return outer_boundary;
}
void offset_polygon(Polygon_with_holes_2& p, double offset) {
if (offset == 0) {
return;
}
// 只对外边界做膨胀/收缩
// 合法的outer是逆时针
auto& outer = p.outer_boundary();
if (offset > 0) {
auto ps = CGAL::create_exterior_skeleton_and_offset_polygons_2(offset,
outer, K());
if (ps.size() != 2) {
throw std::runtime_error("Error offset_polygon(): ps.size() != 2");
}
outer = *(ps[1].get());
} else {
auto ps = CGAL::create_interior_skeleton_and_offset_polygons_2(-offset,
outer, K());
if (ps.size() != 1) {
throw std::runtime_error("Error offset_polygon(): ps.size() != 1");
}
outer = *(ps[0].get());
}
// 保持outer为逆时针
if (outer.is_clockwise_oriented()) {
outer.reverse_orientation();
}
}
bool simplify(Polygon_2& p) {
if (p.is_convex()) {
return false;
}
double factor = 0.02;
double minimum = INF;
for (auto e = p.edges_begin(); e != p.edges_end(); e++) {
minimum = (std::min)(minimum, CGAL::to_double(e->squared_length()));
}
Simplification::Convex_squared_distance_cost cost;
Simplification::Stop_above_cost_threshold stop(factor * minimum);
Polygon_2 q(p);
for (size_t i = 0; i < 10; i++) {
q = Simplification::simplify(q, cost, stop);
std::vector<Polygon_with_holes_2> t;
CGAL::intersection(p, q, std::back_inserter(t));
// It should be only one polygon
size_t size = t.size();
if (size != 1) {
throw std::runtime_error("Error simplify(): size != 1");
}
auto& m = t[0];
// p intersects q should equal to p
if (m != p) {
CGAL::draw(p);
CGAL::draw(m);
throw std::runtime_error("Error simplify(): p intersects q != p");
}
}
if (p.size() != q.size()) {
std::clog << "Polygon Simplify: " << std::endl;
std::clog << "Before: \n" << p << std::endl;
std::clog << "After: \n" << q << "\n" << std::endl;
p = q;
return true;
}
return false;
}
bool simplify(Polygon_with_holes_2& p) {
bool flag = false;
auto& outer = p.outer_boundary();
flag |= simplify(outer);
for (auto& pgn : p.holes()) {
flag |= simplify(pgn);
}
return flag;
}
} // namespace geo
} // namespace nesting