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
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QPOLYGONCLIPPER_P_H
#define QPOLYGONCLIPPER_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include "private/qdatabuffer_p.h"
QT_BEGIN_NAMESPACE
/* based on sutherland-hodgman line-by-line clipping, as described in
Computer Graphics and Principles */
template <typename InType, typename OutType, typename CastType> class QPolygonClipper
{
public:
QPolygonClipper() :
buffer1(0), buffer2(0)
{
x1 = y1 = x2 = y2 = 0;
}
~QPolygonClipper()
{
}
void setBoundingRect(const QRect bounds)
{
x1 = bounds.x();
x2 = bounds.x() + bounds.width();
y1 = bounds.y();
y2 = bounds.y() + bounds.height();
}
QRect boundingRect()
{
return QRect(QPoint(x1, y1), QPoint(x2, y2));
}
inline OutType intersectLeft(const OutType &p1, const OutType &p2)
{
OutType t;
qreal dy = (p1.y - p2.y) / qreal(p1.x - p2.x);
t.x = x1;
t.y = static_cast<CastType>(p2.y + (x1 - p2.x) * dy);
return t;
}
inline OutType intersectRight(const OutType &p1, const OutType &p2)
{
OutType t;
qreal dy = (p1.y - p2.y) / qreal(p1.x - p2.x);
t.x = x2;
t.y = static_cast<CastType>(p2.y + (x2 - p2.x) * dy);
return t;
}
inline OutType intersectTop(const OutType &p1, const OutType &p2)
{
OutType t;
qreal dx = (p1.x - p2.x) / qreal(p1.y - p2.y);
t.x = static_cast<CastType>(p2.x + (y1 - p2.y) * dx);
t.y = y1;
return t;
}
inline OutType intersectBottom(const OutType &p1, const OutType &p2)
{
OutType t;
qreal dx = (p1.x - p2.x) / qreal(p1.y - p2.y);
t.x = static_cast<CastType>(p2.x + (y2 - p2.y) * dx);
t.y = y2;
return t;
}
void clipPolygon(const InType *inPoints, int inCount, OutType **outPoints, int *outCount,
bool closePolygon = true)
{
Q_ASSERT(outPoints);
Q_ASSERT(outCount);
if (inCount < 2) {
*outCount = 0;
return;
}
buffer1.reset();
buffer2.reset();
QDataBuffer<OutType> *source = &buffer1;
QDataBuffer<OutType> *clipped = &buffer2;
// Gather some info since we are iterating through the points anyway..
bool doLeft = false, doRight = false, doTop = false, doBottom = false;
OutType ot;
for (int i=0; i<inCount; ++i) {
ot = inPoints[i];
clipped->add(ot);
if (ot.x < x1)
doLeft = true;
else if (ot.x > x2)
doRight = true;
if (ot.y < y1)
doTop = true;
else if (ot.y > y2)
doBottom = true;
}
if (doLeft && clipped->size() > 1) {
QDataBuffer<OutType> *tmp = source;
source = clipped;
clipped = tmp;
clipped->reset();
int lastPos, start;
if (closePolygon) {
lastPos = source->size() - 1;
start = 0;
} else {
lastPos = 0;
start = 1;
if (source->at(0).x >= x1)
clipped->add(source->at(0));
}
for (int i=start; i<inCount; ++i) {
const OutType &cpt = source->at(i);
const OutType &ppt = source->at(lastPos);
if (cpt.x >= x1) {
if (ppt.x >= x1) {
clipped->add(cpt);
} else {
clipped->add(intersectLeft(cpt, ppt));
clipped->add(cpt);
}
} else if (ppt.x >= x1) {
clipped->add(intersectLeft(cpt, ppt));
}
lastPos = i;
}
}
if (doRight && clipped->size() > 1) {
QDataBuffer<OutType> *tmp = source;
source = clipped;
clipped = tmp;
clipped->reset();
int lastPos, start;
if (closePolygon) {
lastPos = source->size() - 1;
start = 0;
} else {
lastPos = 0;
start = 1;
if (source->at(0).x <= x2)
clipped->add(source->at(0));
}
for (int i=start; i<source->size(); ++i) {
const OutType &cpt = source->at(i);
const OutType &ppt = source->at(lastPos);
if (cpt.x <= x2) {
if (ppt.x <= x2) {
clipped->add(cpt);
} else {
clipped->add(intersectRight(cpt, ppt));
clipped->add(cpt);
}
} else if (ppt.x <= x2) {
clipped->add(intersectRight(cpt, ppt));
}
lastPos = i;
}
}
if (doTop && clipped->size() > 1) {
QDataBuffer<OutType> *tmp = source;
source = clipped;
clipped = tmp;
clipped->reset();
int lastPos, start;
if (closePolygon) {
lastPos = source->size() - 1;
start = 0;
} else {
lastPos = 0;
start = 1;
if (source->at(0).y >= y1)
clipped->add(source->at(0));
}
for (int i=start; i<source->size(); ++i) {
const OutType &cpt = source->at(i);
const OutType &ppt = source->at(lastPos);
if (cpt.y >= y1) {
if (ppt.y >= y1) {
clipped->add(cpt);
} else {
clipped->add(intersectTop(cpt, ppt));
clipped->add(cpt);
}
} else if (ppt.y >= y1) {
clipped->add(intersectTop(cpt, ppt));
}
lastPos = i;
}
}
if (doBottom && clipped->size() > 1) {
QDataBuffer<OutType> *tmp = source;
source = clipped;
clipped = tmp;
clipped->reset();
int lastPos, start;
if (closePolygon) {
lastPos = source->size() - 1;
start = 0;
} else {
lastPos = 0;
start = 1;
if (source->at(0).y <= y2)
clipped->add(source->at(0));
}
for (int i=start; i<source->size(); ++i) {
const OutType &cpt = source->at(i);
const OutType &ppt = source->at(lastPos);
if (cpt.y <= y2) {
if (ppt.y <= y2) {
clipped->add(cpt);
} else {
clipped->add(intersectBottom(cpt, ppt));
clipped->add(cpt);
}
} else if (ppt.y <= y2) {
clipped->add(intersectBottom(cpt, ppt));
}
lastPos = i;
}
}
if (closePolygon && clipped->size() > 0) {
// close clipped polygon
if (clipped->at(0).x != clipped->at(clipped->size()-1).x ||
clipped->at(0).y != clipped->at(clipped->size()-1).y) {
OutType ot = clipped->at(0);
clipped->add(ot);
}
}
*outCount = clipped->size();
*outPoints = clipped->data();
}
private:
int x1, x2, y1, y2;
QDataBuffer<OutType> buffer1;
QDataBuffer<OutType> buffer2;
};
QT_END_NAMESPACE
#endif // QPOLYGONCLIPPER_P_H
|