-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathClipperBase.cpp
More file actions
330 lines (252 loc) · 9.57 KB
/
ClipperBase.cpp
File metadata and controls
330 lines (252 loc) · 9.57 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
329
330
/*******************************************************************************
* *
* 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 *
* *
* This is a translation of the Delphi Clipper library and the naming style *
* used has retained a Delphi flavour. *
******************************************************************************/
#include "ClipperBase.h"
#include "Clipper.h"
using namespace ClipperLib;
ClipperBase::ClipperBase() {
m_MinimaList = 0;
m_CurrentLM = 0;
m_UseFullRange = true;
}
ClipperBase::~ClipperBase() {Clear();}
bool ClipperBase::AddPolygon(const Polygon &pg, PolyType polyType) {
int len = (int)pg.size();
if (len < 3) return false;
int64_t maxVal;
if (m_UseFullRange) maxVal = CLIPPER_HI; else maxVal = CLIPPER_LO;
pg[0].RangeTest(maxVal);
Polygon p(len);
p[0] = pg[0];
int j = 0;
for (int i = 0; i < len; i++) {
pg[i].RangeTest(maxVal);
if (i == 0 || p[j].Equal(pg[i])) continue;
else if (j > 0 && SlopesEqual(p[j - 1], p[j], pg[i], m_UseFullRange)) {
if (p[j - 1].Equal(pg[i])) j--;
} else j++;
p[j] = pg[i];
}
if (j < 2) return false;
len = j + 1;
while (len > 2) {
// nb: test for point equality before testing slopes
if (p[j].Equal(p[0])) j--;
else if (p[0].Equal(p[1]) ||
SlopesEqual(p[j], p[0], p[1], m_UseFullRange))
p[0] = p[j--];
else if (SlopesEqual(p[j - 1], p[j], p[0], m_UseFullRange)) j--;
else if (SlopesEqual(p[0], p[1], p[2], m_UseFullRange)) {
for (int i = 2; i <= j; i++) p[i - 1] = p[i];
j--;
} else break;
len--;
}
if (len < 3) return false;
// create a new edge array
TEdge *edges = new TEdge[len];
m_edges.push_back(edges);
// convert vertices to a double-linked-list of edges and initialize
edges[0].xcurr = p[0].X;
edges[0].ycurr = p[0].Y;
edges[len - 1] = TEdge(&edges[0], &edges[len - 2], p[len - 1], polyType);
for (int i = len - 2; i > 0; i--)
edges[i] = TEdge(&edges[i + 1], &edges[i - 1], p[i], polyType);
edges[0] = TEdge(&edges[1], &edges[len - 1], p[0], polyType);
// reset xcurr & ycurr and find 'eHighest' (given the Y axis coordinates
// increase downward so the 'highest' edge will have the smallest ytop)
TEdge *e = &edges[0];
TEdge *eHighest = e;
do {
e->xcurr = e->xbot;
e->ycurr = e->ybot;
if (e->ytop < eHighest->ytop) eHighest = e;
e = e->next;
} while (e != &edges[0]);
// make sure eHighest is positioned so the following loop works safely
if (eHighest->windDelta > 0) eHighest = eHighest->next;
if (CLIPPER_NEAR_EQUAL(eHighest->dx, CLIPPER_HORIZONTAL))
eHighest = eHighest->next;
// finally insert each local minima
e = eHighest;
do {
e = AddBoundsToLML(e);
} while (e != eHighest);
return true;
}
void ClipperBase::InsertLocalMinima(LocalMinima *newLm) {
if (!m_MinimaList) m_MinimaList = newLm;
else if (newLm->Y >= m_MinimaList->Y) {
newLm->next = m_MinimaList;
m_MinimaList = newLm;
} else {
LocalMinima *tmpLm = m_MinimaList;
while (tmpLm->next && (newLm->Y < tmpLm->next->Y))
tmpLm = tmpLm->next;
newLm->next = tmpLm->next;
tmpLm->next = newLm;
}
}
TEdge *ClipperBase::AddBoundsToLML(TEdge *e) {
// Starting at the top of one bound we progress to the bottom where there's
// a local minima. We then go to the top of the next bound. These two bounds
// form the left and right (or right and left) bounds of the local minima.
e->nextInLML = 0;
e = e->next;
while (true) {
if (CLIPPER_NEAR_EQUAL(e->dx, CLIPPER_HORIZONTAL)) {
// nb: proceed through horizontals when approaching from their right,
// but break on horizontal minima if approaching from their left.
// This ensures 'local minima' are always on the left of horizontals.
if (e->next->ytop < e->ytop && e->next->xbot > e->prev->xbot) break;
if (e->xtop != e->prev->xbot) e->SwapX();
e->nextInLML = e->prev;
} else if (e->ycurr == e->prev->ycurr) break;
else e->nextInLML = e->prev;
e = e->next;
}
// e and e.prev are now at a local minima
LocalMinima *newLm = new LocalMinima;
newLm->next = 0;
newLm->Y = e->prev->ybot;
// horizontal edges never start a left bound
if (CLIPPER_NEAR_EQUAL(e->dx, CLIPPER_HORIZONTAL)) {
if (e->xbot != e->prev->xbot) e->SwapX();
newLm->leftBound = e->prev;
newLm->rightBound = e;
} else if (e->dx < e->prev->dx) {
newLm->leftBound = e->prev;
newLm->rightBound = e;
} else {
newLm->leftBound = e;
newLm->rightBound = e->prev;
}
newLm->leftBound->side = esLeft;
newLm->rightBound->side = esRight;
InsertLocalMinima(newLm);
while (true) {
if (e->next->ytop == e->ytop &&
!CLIPPER_NEAR_EQUAL(e->next->dx, CLIPPER_HORIZONTAL))
break;
e->nextInLML = e->next;
e = e->next;
if (CLIPPER_NEAR_EQUAL(e->dx, CLIPPER_HORIZONTAL) &&
e->xbot != e->prev->xtop) e->SwapX();
}
return e->next;
}
bool ClipperBase::AddPolygons(const Polygons &ppg, PolyType polyType) {
bool result = false;
for (Polygons::size_type i = 0; i < ppg.size(); i++)
if (AddPolygon(ppg[i], polyType)) result = true;
return result;
}
void ClipperBase::Clear() {
DisposeLocalMinimaList();
for (EdgeList::size_type i = 0; i < m_edges.size(); i++)
delete [] m_edges[i];
m_edges.clear();
m_UseFullRange = false;
}
void ClipperBase::Reset() {
m_CurrentLM = m_MinimaList;
if (!m_CurrentLM) return; // ie nothing to process
// reset all edges
LocalMinima *lm = m_MinimaList;
while (lm) {
TEdge *e = lm->leftBound;
while (e) {
e->xcurr = e->xbot;
e->ycurr = e->ybot;
e->side = esLeft;
e->outIdx = -1;
e = e->nextInLML;
}
e = lm->rightBound;
while (e) {
e->xcurr = e->xbot;
e->ycurr = e->ybot;
e->side = esRight;
e->outIdx = -1;
e = e->nextInLML;
}
lm = lm->next;
}
}
void ClipperBase::DisposeLocalMinimaList() {
while (m_MinimaList) {
LocalMinima *tmpLm = m_MinimaList->next;
delete m_MinimaList;
m_MinimaList = tmpLm;
}
m_CurrentLM = 0;
}
void ClipperBase::PopLocalMinima() {
if (!m_CurrentLM) return;
m_CurrentLM = m_CurrentLM->next;
}
Bounds ClipperBase::GetBounds() {
Bounds result;
LocalMinima *lm = m_MinimaList;
if (!lm) {
result.left = result.top = result.right = result.bottom = 0;
return result;
}
result.left = lm->leftBound->xbot;
result.top = lm->leftBound->ybot;
result.right = lm->leftBound->xbot;
result.bottom = lm->leftBound->ybot;
while (lm) {
if (lm->leftBound->ybot > result.bottom)
result.bottom = lm->leftBound->ybot;
TEdge *e = lm->leftBound;
while (true) {
TEdge *bottomE = e;
while (e->nextInLML) {
if (e->xbot < result.left) result.left = e->xbot;
if (e->xbot > result.right) result.right = e->xbot;
e = e->nextInLML;
}
if (e->xbot < result.left) result.left = e->xbot;
if (e->xbot > result.right) result.right = e->xbot;
if (e->xtop < result.left) result.left = e->xtop;
if (e->xtop > result.right) result.right = e->xtop;
if (e->ytop < result.top) result.top = e->ytop;
if (bottomE == lm->leftBound) e = lm->rightBound;
else break;
}
lm = lm->next;
}
return result;
}