-
Notifications
You must be signed in to change notification settings - Fork 18
/
AreaClipper.cpp
479 lines (409 loc) · 11.7 KB
/
AreaClipper.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// AreaClipper.cpp
// implements CArea methods using Angus Johnson's "Clipper"
#include "Area.h"
#include "clipper.hpp"
using namespace clipper;
#define TPolygon Polygon
#define TPolyPolygon Polygons
bool CArea::HolesLinked(){ return false; }
static const double PI = 3.1415926535897932;
static double Clipper4Factor = 10000.0;
class DoublePoint
{
public:
double X, Y;
DoublePoint(double x, double y){X = x; Y = y;}
DoublePoint(const IntPoint& p){X = (double)(p.X) / Clipper4Factor; Y = (double)(p.Y) / Clipper4Factor;}
IntPoint int_point(){return IntPoint((long64)(X * Clipper4Factor), (long64)(Y * Clipper4Factor));}
};
static std::list<DoublePoint> pts_for_AddVertex;
static void AddPoint(const DoublePoint& p)
{
pts_for_AddVertex.push_back(p);
}
static void AddVertex(const CVertex& vertex, const CVertex* prev_vertex)
{
if(vertex.m_type == 0 || prev_vertex == NULL)
{
AddPoint(DoublePoint(vertex.m_p.x * CArea::m_units, vertex.m_p.y * CArea::m_units));
}
else
{
if(vertex.m_p != prev_vertex->m_p)
{
double phi,dphi,dx,dy;
int Segments;
int i;
double ang1,ang2,phit;
dx = (prev_vertex->m_p.x - vertex.m_c.x) * CArea::m_units;
dy = (prev_vertex->m_p.y - vertex.m_c.y) * CArea::m_units;
ang1=atan2(dy,dx);
if (ang1<0) ang1+=2.0*PI;
dx = (vertex.m_p.x - vertex.m_c.x) * CArea::m_units;
dy = (vertex.m_p.y - vertex.m_c.y) * CArea::m_units;
ang2=atan2(dy,dx);
if (ang2<0) ang2+=2.0*PI;
if (vertex.m_type == -1)
{ //clockwise
if (ang2 > ang1)
phit=2.0*PI-ang2+ ang1;
else
phit=ang1-ang2;
}
else
{ //counter_clockwise
if (ang1 > ang2)
phit=-(2.0*PI-ang1+ ang2);
else
phit=-(ang2-ang1);
}
//what is the delta phi to get an accurancy of aber
double radius = sqrt(dx*dx + dy*dy);
dphi=2*acos((radius-CArea::m_accuracy)/radius);
//set the number of segments
if (phit > 0)
Segments=(int)ceil(phit/dphi);
else
Segments=(int)ceil(-phit/dphi);
if (Segments < 1)
Segments=1;
if (Segments > 100)
Segments=100;
dphi=phit/(Segments);
double px = prev_vertex->m_p.x * CArea::m_units;
double py = prev_vertex->m_p.y * CArea::m_units;
for (i=1; i<=Segments; i++)
{
dx = px - vertex.m_c.x * CArea::m_units;
dy = py - vertex.m_c.y * CArea::m_units;
phi=atan2(dy,dx);
double nx = vertex.m_c.x * CArea::m_units + radius * cos(phi-dphi);
double ny = vertex.m_c.y * CArea::m_units + radius * sin(phi-dphi);
AddPoint(DoublePoint(nx, ny));
px = nx;
py = ny;
}
}
}
}
static bool IsPolygonClockwise(const TPolygon& p)
{
#if 1
double area = 0.0;
unsigned int s = p.size();
for(unsigned int i = 0; i<s; i++)
{
int im1 = i-1;
if(im1 < 0)im1 += s;
DoublePoint pt0(p[im1]);
DoublePoint pt1(p[i]);
area += 0.5 * (pt1.X - pt0.X) * (pt0.Y + pt1.Y);
}
return area > 0.0;
#else
return IsClockwise(p);
#endif
}
static void MakeLoop(const DoublePoint &pt0, const DoublePoint &pt1, const DoublePoint &pt2, double radius)
{
Point p0(pt0.X, pt0.Y);
Point p1(pt1.X, pt1.Y);
Point p2(pt2.X, pt2.Y);
Point forward0 = p1 - p0;
Point right0(forward0.y, -forward0.x);
right0.normalize();
Point forward1 = p2 - p1;
Point right1(forward1.y, -forward1.x);
right1.normalize();
int arc_dir = (radius > 0) ? 1 : -1;
CVertex v0(0, p1 + right0 * radius, Point(0, 0));
CVertex v1(arc_dir, p1 + right1 * radius, p1);
CVertex v2(0, p2 + right1 * radius, Point(0, 0));
double save_units = CArea::m_units;
CArea::m_units = 1.0;
AddVertex(v1, &v0);
AddVertex(v2, &v1);
CArea::m_units = save_units;
}
static void OffsetWithLoops(const TPolyPolygon &pp, TPolyPolygon &pp_new, double inwards_value)
{
Clipper c;
bool inwards = (inwards_value > 0);
bool reverse = false;
double radius = -fabs(inwards_value);
if(inwards)
{
// add a large square on the outside, to be removed later
TPolygon p;
p.push_back(DoublePoint(-10000.0, -10000.0).int_point());
p.push_back(DoublePoint(-10000.0, 10000.0).int_point());
p.push_back(DoublePoint(10000.0, 10000.0).int_point());
p.push_back(DoublePoint(10000.0, -10000.0).int_point());
c.AddPolygon(p, ptSubject);
}
else
{
reverse = true;
}
for(unsigned int i = 0; i < pp.size(); i++)
{
const TPolygon& p = pp[i];
pts_for_AddVertex.clear();
if(p.size() > 2)
{
if(reverse)
{
for(unsigned int j = p.size()-1; j > 1; j--)MakeLoop(p[j], p[j-1], p[j-2], radius);
MakeLoop(p[1], p[0], p[p.size()-1], radius);
MakeLoop(p[0], p[p.size()-1], p[p.size()-2], radius);
}
else
{
MakeLoop(p[p.size()-2], p[p.size()-1], p[0], radius);
MakeLoop(p[p.size()-1], p[0], p[1], radius);
for(unsigned int j = 2; j < p.size(); j++)MakeLoop(p[j-2], p[j-1], p[j], radius);
}
TPolygon loopy_polygon;
loopy_polygon.reserve(pts_for_AddVertex.size());
for(std::list<DoublePoint>::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++)
{
loopy_polygon.push_back(It->int_point());
}
c.AddPolygon(loopy_polygon, ptSubject);
pts_for_AddVertex.clear();
}
}
//c.ForceOrientation(false);
c.Execute(ctUnion, pp_new, pftNonZero, pftNonZero);
if(inwards)
{
// remove the large square
if(pp_new.size() > 0)
{
pp_new.erase(pp_new.begin());
}
}
else
{
// reverse all the resulting polygons
TPolyPolygon copy = pp_new;
pp_new.clear();
pp_new.resize(copy.size());
for(unsigned int i = 0; i < copy.size(); i++)
{
const TPolygon& p = copy[i];
TPolygon p_new;
p_new.resize(p.size());
int size_minus_one = p.size() - 1;
for(unsigned int j = 0; j < p.size(); j++)p_new[j] = p[size_minus_one - j];
pp_new[i] = p_new;
}
}
}
static void MakeObround(const Point &pt0, const CVertex &vt1, double radius)
{
Span span(pt0, vt1);
Point forward0 = span.GetVector(0.0);
Point forward1 = span.GetVector(1.0);
Point right0(forward0.y, -forward0.x);
Point right1(forward1.y, -forward1.x);
right0.normalize();
right1.normalize();
CVertex v0(pt0 + right0 * radius);
CVertex v1(vt1.m_type, vt1.m_p + right1 * radius, vt1.m_c);
CVertex v2(1, vt1.m_p + right1 * -radius, vt1.m_p);
CVertex v3(-vt1.m_type, pt0 + right0 * -radius, vt1.m_c);
CVertex v4(1, pt0 + right0 * radius, pt0);
double save_units = CArea::m_units;
CArea::m_units = 1.0;
AddVertex(v0, NULL);
AddVertex(v1, &v0);
AddVertex(v2, &v1);
AddVertex(v3, &v2);
AddVertex(v4, &v3);
CArea::m_units = save_units;
}
static void OffsetSpansWithObrounds(const CArea& area, TPolyPolygon &pp_new, double radius)
{
Clipper c;
for(std::list<CCurve>::const_iterator It = area.m_curves.begin(); It != area.m_curves.end(); It++)
{
pts_for_AddVertex.clear();
const CCurve& curve = *It;
const CVertex* prev_vertex = NULL;
for(std::list<CVertex>::const_iterator It2 = curve.m_vertices.begin(); It2 != curve.m_vertices.end(); It2++)
{
const CVertex& vertex = *It2;
if(prev_vertex)
{
MakeObround(prev_vertex->m_p, vertex, radius);
TPolygon loopy_polygon;
loopy_polygon.reserve(pts_for_AddVertex.size());
for(std::list<DoublePoint>::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++)
{
loopy_polygon.push_back(It->int_point());
}
c.AddPolygon(loopy_polygon, ptSubject);
pts_for_AddVertex.clear();
}
prev_vertex = &vertex;
}
}
pp_new.clear();
c.Execute(ctUnion, pp_new, pftNonZero, pftNonZero);
// reverse all the resulting polygons
TPolyPolygon copy = pp_new;
pp_new.clear();
pp_new.resize(copy.size());
for(unsigned int i = 0; i < copy.size(); i++)
{
const TPolygon& p = copy[i];
TPolygon p_new;
p_new.resize(p.size());
int size_minus_one = p.size() - 1;
for(unsigned int j = 0; j < p.size(); j++)p_new[j] = p[size_minus_one - j];
pp_new[i] = p_new;
}
}
static void MakePolyPoly( const CArea& area, TPolyPolygon &pp, bool reverse = true ){
pp.clear();
for(std::list<CCurve>::const_iterator It = area.m_curves.begin(); It != area.m_curves.end(); It++)
{
pts_for_AddVertex.clear();
const CCurve& curve = *It;
const CVertex* prev_vertex = NULL;
for(std::list<CVertex>::const_iterator It2 = curve.m_vertices.begin(); It2 != curve.m_vertices.end(); It2++)
{
const CVertex& vertex = *It2;
if(prev_vertex)AddVertex(vertex, prev_vertex);
prev_vertex = &vertex;
}
TPolygon p;
p.resize(pts_for_AddVertex.size());
if(reverse)
{
unsigned int i = pts_for_AddVertex.size() - 1;// clipper wants them the opposite way to CArea
for(std::list<DoublePoint>::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++, i--)
{
p[i] = It->int_point();
}
}
else
{
unsigned int i = 0;
for(std::list<DoublePoint>::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++, i++)
{
p[i] = It->int_point();
}
}
pp.push_back(p);
}
}
static void SetFromResult( CCurve& curve, const TPolygon& p, bool reverse = true )
{
for(unsigned int j = 0; j < p.size(); j++)
{
const IntPoint &pt = p[j];
DoublePoint dp(pt);
CVertex vertex(0, Point(dp.X / CArea::m_units, dp.Y / CArea::m_units), Point(0.0, 0.0));
if(reverse)curve.m_vertices.push_front(vertex);
else curve.m_vertices.push_back(vertex);
}
// make a copy of the first point at the end
if(reverse)curve.m_vertices.push_front(curve.m_vertices.back());
else curve.m_vertices.push_back(curve.m_vertices.front());
if(CArea::m_fit_arcs)curve.FitArcs();
}
static void SetFromResult( CArea& area, const TPolyPolygon& pp, bool reverse = true )
{
// delete existing geometry
area.m_curves.clear();
for(unsigned int i = 0; i < pp.size(); i++)
{
const TPolygon& p = pp[i];
area.m_curves.push_back(CCurve());
CCurve &curve = area.m_curves.back();
SetFromResult(curve, p, reverse);
}
}
void CArea::Subtract(const CArea& a2)
{
Clipper c;
TPolyPolygon pp1, pp2;
MakePolyPoly(*this, pp1);
MakePolyPoly(a2, pp2);
c.AddPolygons(pp1, ptSubject);
c.AddPolygons(pp2, ptClip);
TPolyPolygon solution;
c.Execute(ctDifference, solution);
SetFromResult(*this, solution);
}
void CArea::Intersect(const CArea& a2)
{
Clipper c;
TPolyPolygon pp1, pp2;
MakePolyPoly(*this, pp1);
MakePolyPoly(a2, pp2);
c.AddPolygons(pp1, ptSubject);
c.AddPolygons(pp2, ptClip);
TPolyPolygon solution;
c.Execute(ctIntersection, solution);
SetFromResult(*this, solution);
}
void CArea::Union(const CArea& a2)
{
Clipper c;
TPolyPolygon pp1, pp2;
MakePolyPoly(*this, pp1);
MakePolyPoly(a2, pp2);
c.AddPolygons(pp1, ptSubject);
c.AddPolygons(pp2, ptClip);
TPolyPolygon solution;
c.Execute(ctUnion, solution);
SetFromResult(*this, solution);
}
void CArea::Xor(const CArea& a2)
{
Clipper c;
TPolyPolygon pp1, pp2;
MakePolyPoly(*this, pp1);
MakePolyPoly(a2, pp2);
c.AddPolygons(pp1, ptSubject);
c.AddPolygons(pp2, ptClip);
TPolyPolygon solution;
c.Execute(ctXor, solution);
SetFromResult(*this, solution);
}
void CArea::Offset(double inwards_value)
{
TPolyPolygon pp, pp2;
MakePolyPoly(*this, pp, false);
OffsetWithLoops(pp, pp2, inwards_value * m_units);
SetFromResult(*this, pp2, false);
this->Reorder();
}
void CArea::Thicken(double value)
{
TPolyPolygon pp;
OffsetSpansWithObrounds(*this, pp, value * m_units);
SetFromResult(*this, pp, false);
this->Reorder();
}
void UnFitArcs(CCurve &curve)
{
pts_for_AddVertex.clear();
const CVertex* prev_vertex = NULL;
for(std::list<CVertex>::const_iterator It2 = curve.m_vertices.begin(); It2 != curve.m_vertices.end(); It2++)
{
const CVertex& vertex = *It2;
AddVertex(vertex, prev_vertex);
prev_vertex = &vertex;
}
curve.m_vertices.clear();
for(std::list<DoublePoint>::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++)
{
DoublePoint &pt = *It;
CVertex vertex(0, Point(pt.X / CArea::m_units, pt.Y / CArea::m_units), Point(0.0, 0.0));
curve.m_vertices.push_back(vertex);
}
}