-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathOffsetBuilder.cpp
More file actions
328 lines (270 loc) · 11.2 KB
/
OffsetBuilder.cpp
File metadata and controls
328 lines (270 loc) · 11.2 KB
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
/*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 5.1.6 *
* Date : 23 May 2013 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2013 *
* *
* License: *
* Use, modification & distribution is subject to Boost Software License Ver 1.*
* http://www.boost.org/LICENSE_1_0.txt *
* *
* Attributions: *
* The code in this library is an extension of Bala Vatti's clipping algorithm:*
* "A generic solution to polygon clipping" *
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
* http://portal.acm.org/citation.cfm?id=129906 *
* *
* Computer graphics and geometric modeling: implementation and algorithms *
* By Max K. Agoston *
* Springer; 1 edition (January 4, 2005) *
* http://books.google.com/books?q=vatti+clipping+agoston *
* *
* See also: *
* "Polygon Offsetting by Computing Winding Numbers" *
* Paper no. DETC2005-85513 pp. 565-575 *
* ASME 2005 International Design Engineering Technical Conferences *
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
* September 24-28, 2005 , Long Beach, California, USA *
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
* *
******************************************************************************/
#include "OffsetBuilder.h"
#include "Util.h"
#include "Clipper.h"
#include <cbang/Math.h>
using namespace std;
using namespace cb;
using namespace ClipperLib;
namespace {
Polygon BuildArc(const IntPoint &pt, const double a1, const double a2,
const double r, double limit) {
// see notes in clipper.pas regarding steps
double arcFrac = fabs(a2 - a1) / (2 * Math::PI);
int steps = (int)(arcFrac * Math::PI / acos(1 - limit / fabs(r)));
if (steps < 2) steps = 2;
else if (steps > (int)(222.0 * arcFrac)) steps = (int)(222.0 * arcFrac);
double x = cos(a1);
double y = sin(a1);
double c = cos((a2 - a1) / steps);
double s = sin((a2 - a1) / steps);
Polygon result(steps + 1);
for (int i = 0; i <= steps; i++) {
result[i].X = pt.X + Round(x * r);
result[i].Y = pt.Y + Round(y * r);
double x2 = x;
x = x * c - s * y; // cross product
y = x2 * s + y * c; // dot product
}
return result;
}
}
OffsetBuilder::OffsetBuilder(const Polygons &in_polys, Polygons &out_polys,
bool isPolygon, double delta, JoinType jointype,
EndType endtype, double limit) : m_p(in_polys) {
// precondition: &out_polys != &in_polys
if (CLIPPER_NEAR_ZERO(delta)) {out_polys = in_polys; return;}
m_rmin = 0.5;
m_delta = delta;
if (jointype == jtMiter) {
if (limit > 2) m_rmin = 2.0 / (limit * limit);
limit = 0.25; // just in case endtype == etRound
} else {
if (limit <= 0) limit = 0.25;
else if (limit > fabs(delta)) limit = fabs(delta);
}
out_polys.clear();
out_polys.resize(m_p.size());
for (m_i = 0; m_i < m_p.size(); m_i++) {
size_t len = m_p[m_i].size();
if (len == 0 || (len < 3 && delta <= 0)) continue;
else if (len == 1) {
out_polys[m_i] = BuildArc(m_p[m_i][0], 0, 2 * Math::PI, delta, limit);
continue;
}
bool forceClose = m_p[m_i][0].Equal(m_p[m_i][len - 1]);
if (forceClose) len--;
// build normals
normals.clear();
normals.resize(len);
for (m_j = 0; m_j < len - 1; m_j++)
normals[m_j] = m_p[m_i][m_j].GetUnitNormal(m_p[m_i][m_j + 1]);
if (isPolygon || forceClose)
normals[len - 1] = m_p[m_i][len - 1].GetUnitNormal(m_p[m_i][0]);
else normals[len - 1] = normals[len - 2]; // is open polyline
m_curr_poly = &out_polys[m_i];
m_curr_poly->reserve(len);
if (isPolygon || forceClose) {
m_k = len - 1;
for (m_j = 0; m_j < len; ++m_j)
OffsetPoint(jointype, limit);
if (!isPolygon) {
size_t j = out_polys.size();
out_polys.resize(j + 1);
m_curr_poly = &out_polys[j];
m_curr_poly->reserve(len);
m_delta = -m_delta;
m_k = len - 1;
for (m_j = 0; m_j < len; ++m_j)
OffsetPoint(jointype, limit);
m_delta = -m_delta;
m_curr_poly->reverse();
}
} else { // is open polyline
// offset the polyline going forward
m_k = 0;
for (m_j = 1; m_j < len - 1; ++m_j)
OffsetPoint(jointype, limit);
// handle the end (butt, round or square)
IntPoint pt1;
if (endtype == etButt) {
m_j = len - 1;
pt1 = IntPoint(Round(m_p[m_i][m_j].X + normals[m_j].X * m_delta),
Round(m_p[m_i][m_j].Y + normals[m_j].Y * m_delta));
AddPoint(pt1);
pt1 = IntPoint(Round(m_p[m_i][m_j].X - normals[m_j].X * m_delta),
Round(m_p[m_i][m_j].Y - normals[m_j].Y * m_delta));
AddPoint(pt1);
} else {
m_j = len - 1;
m_k = len - 2;
normals[m_j].X = -normals[m_j].X;
normals[m_j].Y = -normals[m_j].Y;
if (endtype == etSquare) DoSquare();
else DoRound(limit);
}
// re-build Normals
for (int j = len - 1; j > 0; j--) {
normals[j].X = -normals[j - 1].X;
normals[j].Y = -normals[j - 1].Y;
}
normals[0].X = -normals[1].X;
normals[0].Y = -normals[1].Y;
// offset the polyline going backward
m_k = len - 1;
for (m_j = m_k - 1; m_j > 0; m_j--)
OffsetPoint(jointype, limit);
// finally handle the start (butt, round or square)
if (endtype == etButt) {
pt1 = IntPoint(Round(m_p[m_i][0].X - normals[0].X * m_delta),
Round(m_p[m_i][0].Y - normals[0].Y * m_delta));
AddPoint(pt1);
pt1 = IntPoint(Round(m_p[m_i][0].X + normals[0].X * m_delta),
Round(m_p[m_i][0].Y + normals[0].Y * m_delta));
AddPoint(pt1);
} else {
m_k = 1;
if (endtype == etSquare) DoSquare();
else DoRound(limit);
}
}
}
// and clean up untidy corners using Clipper
Clipper clpr;
clpr.AddPolygons(out_polys, ptSubject);
if (delta > 0) {
if (!clpr.Execute(ctUnion, out_polys, pftPositive, pftPositive))
out_polys.clear();
} else {
Bounds r = clpr.GetBounds();
Polygon outer(4);
outer[0] = IntPoint(r.left - 10, r.bottom + 10);
outer[1] = IntPoint(r.right + 10, r.bottom + 10);
outer[2] = IntPoint(r.right + 10, r.top - 10);
outer[3] = IntPoint(r.left - 10, r.top - 10);
clpr.AddPolygon(outer, ptSubject);
clpr.ReverseSolution(true);
if (clpr.Execute(ctUnion, out_polys, pftNegative, pftNegative))
out_polys.erase(out_polys.begin());
else out_polys.clear();
}
}
void OffsetBuilder::OffsetPoint(JoinType jointype, double limit) {
switch (jointype) {
case jtMiter: {
m_r = 1 + (normals[m_j].X*normals[m_k].X +
normals[m_j].Y*normals[m_k].Y);
if (m_r >= m_rmin) DoMiter(); else DoSquare();
break;
}
case jtSquare: DoSquare(); break;
case jtRound: DoRound(limit); break;
}
m_k = m_j;
}
void OffsetBuilder::AddPoint(const IntPoint &pt) {
if (m_curr_poly->size() == m_curr_poly->capacity())
m_curr_poly->reserve(m_curr_poly->capacity() + buffLength);
m_curr_poly->push_back(pt);
}
void OffsetBuilder::DoSquare() {
IntPoint pt1 =
IntPoint(Round(m_p[m_i][m_j].X + normals[m_k].X * m_delta),
Round(m_p[m_i][m_j].Y + normals[m_k].Y * m_delta));
IntPoint pt2 =
IntPoint(Round(m_p[m_i][m_j].X + normals[m_j].X * m_delta),
Round(m_p[m_i][m_j].Y + normals[m_j].Y * m_delta));
if ((normals[m_k].X * normals[m_j].Y - normals[m_j].X * normals[m_k].Y) *
m_delta >= 0) {
double a1 = atan2(normals[m_k].Y, normals[m_k].X);
double a2 = atan2(-normals[m_j].Y, -normals[m_j].X);
a1 = fabs(a2 - a1);
if (a1 > Math::PI) a1 = Math::PI * 2 - a1;
double dx = tan((Math::PI - a1) / 4) * fabs(m_delta);
pt1 = IntPoint((int64_t)(pt1.X - normals[m_k].Y * dx),
(int64_t)(pt1.Y + normals[m_k].X * dx));
AddPoint(pt1);
pt2 = IntPoint((int64_t)(pt2.X + normals[m_j].Y * dx),
(int64_t)(pt2.Y - normals[m_j].X * dx));
AddPoint(pt2);
} else {
AddPoint(pt1);
AddPoint(m_p[m_i][m_j]);
AddPoint(pt2);
}
}
void OffsetBuilder::DoMiter() {
if ((normals[m_k].X * normals[m_j].Y - normals[m_j].X * normals[m_k].Y) *
m_delta >= 0) {
double q = m_delta / m_r;
AddPoint(IntPoint(Round(m_p[m_i][m_j].X +
(normals[m_k].X + normals[m_j].X) * q),
Round(m_p[m_i][m_j].Y +
(normals[m_k].Y + normals[m_j].Y) * q)));
} else {
IntPoint pt1 =
IntPoint(Round(m_p[m_i][m_j].X + normals[m_k].X * m_delta),
Round(m_p[m_i][m_j].Y + normals[m_k].Y * m_delta));
IntPoint pt2 =
IntPoint(Round(m_p[m_i][m_j].X + normals[m_j].X * m_delta),
Round(m_p[m_i][m_j].Y + normals[m_j].Y * m_delta));
AddPoint(pt1);
AddPoint(m_p[m_i][m_j]);
AddPoint(pt2);
}
}
void OffsetBuilder::DoRound(double limit) {
IntPoint pt1 =
IntPoint(Round(m_p[m_i][m_j].X + normals[m_k].X * m_delta),
Round(m_p[m_i][m_j].Y + normals[m_k].Y * m_delta));
IntPoint pt2 =
IntPoint(Round(m_p[m_i][m_j].X + normals[m_j].X * m_delta),
Round(m_p[m_i][m_j].Y + normals[m_j].Y * m_delta));
AddPoint(pt1);
// round off reflex angles (ie > 180 deg) unless almost flat (ie < ~10deg)
if ((normals[m_k].X * normals[m_j].Y - normals[m_j].X * normals[m_k].Y) *
m_delta >= 0) {
if (normals[m_j].X * normals[m_k].X + normals[m_j].Y * normals[m_k].Y <
0.985) {
double a1 = atan2(normals[m_k].Y, normals[m_k].X);
double a2 = atan2(normals[m_j].Y, normals[m_j].X);
if (m_delta > 0 && a2 < a1) a2 += Math::PI * 2;
else if (m_delta < 0 && a2 > a1) a2 -= Math::PI * 2;
Polygon arc = BuildArc(m_p[m_i][m_j], a1, a2, m_delta, limit);
for (Polygon::size_type m = 0; m < arc.size(); m++)
AddPoint(arc[m]);
}
} else AddPoint(m_p[m_i][m_j]);
AddPoint(pt2);
}