0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
#include "oldtessellator.h"
|
|
42 |
#include <QPointF>
|
|
43 |
#include <QVector>
|
|
44 |
#include <QList>
|
|
45 |
#include <QVariant>
|
|
46 |
#include <QVarLengthArray>
|
|
47 |
#include <qdebug.h>
|
|
48 |
|
|
49 |
#include "limits.h"
|
|
50 |
#include "utils.h"
|
|
51 |
|
|
52 |
#include "qnum.h"
|
|
53 |
#include "XrenderFake.h"
|
|
54 |
|
|
55 |
/*
|
|
56 |
* Polygon tesselator - can probably be optimized a bit more
|
|
57 |
*/
|
|
58 |
|
|
59 |
//#define QT_DEBUG_TESSELATOR
|
|
60 |
#define FloatToXFixed(i) (int)((i) * 65536)
|
|
61 |
#define IntToXFixed(i) ((i) << 16)
|
|
62 |
|
|
63 |
//inline int qrealToXFixed(qreal f)
|
|
64 |
//{ return f << 8; }
|
|
65 |
|
|
66 |
struct QEdge {
|
|
67 |
inline QEdge()
|
|
68 |
{}
|
|
69 |
inline QEdge(const QPointF &pt1,
|
|
70 |
const QPointF &pt2)
|
|
71 |
{
|
|
72 |
p1.x = XDoubleToFixed(pt1.x());
|
|
73 |
p1.y = XDoubleToFixed(pt1.y());
|
|
74 |
p2.x = XDoubleToFixed(pt2.x());
|
|
75 |
p2.y = XDoubleToFixed(pt2.y());
|
|
76 |
m = (pt1.x() - pt2.x()) ? (pt1.y() - pt2.y()) / (pt1.x() - pt2.x()) : 0;
|
|
77 |
im = m ? 1/m : 0;
|
|
78 |
b = pt1.y() - m * pt1.x();
|
|
79 |
vertical = p1.x == p2.x;
|
|
80 |
horizontal = p1.y == p2.y;
|
|
81 |
}
|
|
82 |
|
|
83 |
inline qreal xAt(const qreal &y) const
|
|
84 |
{
|
|
85 |
Q_ASSERT(p1.y != p2.y);
|
|
86 |
XFixed yf = XDoubleToFixed(y);
|
|
87 |
|
|
88 |
if (yf == p1.y)
|
|
89 |
return XFixedToDouble(p1.x);
|
|
90 |
else if (yf == p2.y)
|
|
91 |
return XFixedToDouble(p2.x);
|
|
92 |
|
|
93 |
return (!vertical) ? (((y - b)*im)) : pf1.x();
|
|
94 |
}
|
|
95 |
|
|
96 |
QPointF pf1, pf2;
|
|
97 |
XPointFixed p1, p2;
|
|
98 |
qreal m;
|
|
99 |
qreal im;
|
|
100 |
qreal b;
|
|
101 |
qreal intersection;
|
|
102 |
signed short winding;
|
|
103 |
bool vertical;
|
|
104 |
bool horizontal;
|
|
105 |
};
|
|
106 |
|
|
107 |
struct QVrtx {
|
|
108 |
typedef QList<QEdge> Edges;
|
|
109 |
XPointFixed coords;
|
|
110 |
Edges startingEdges;
|
|
111 |
Edges endingEdges;
|
|
112 |
Edges intersectingEdges;
|
|
113 |
};
|
|
114 |
|
|
115 |
struct QIntersectionPoint {
|
|
116 |
qreal x;
|
|
117 |
const QEdge *edge;
|
|
118 |
};
|
|
119 |
|
|
120 |
QT_BEGIN_NAMESPACE
|
|
121 |
Q_DECLARE_TYPEINFO(QEdge, Q_PRIMITIVE_TYPE);
|
|
122 |
Q_DECLARE_TYPEINFO(QVrtx, Q_PRIMITIVE_TYPE);
|
|
123 |
Q_DECLARE_TYPEINFO(QIntersectionPoint, Q_PRIMITIVE_TYPE);
|
|
124 |
QT_END_NAMESPACE
|
|
125 |
|
|
126 |
|
|
127 |
// used by the edge point sort algorithm
|
|
128 |
static qreal currentY = 0.f;
|
|
129 |
|
|
130 |
static inline bool compareEdges(const QEdge *e1, const QEdge *e2)
|
|
131 |
{
|
|
132 |
return e1->p1.y < e2->p1.y;
|
|
133 |
}
|
|
134 |
|
|
135 |
static inline bool isEqual(const XPointFixed &p1, const XPointFixed &p2)
|
|
136 |
{
|
|
137 |
return ((p1.x == p2.x) && (p1.y == p2.y));
|
|
138 |
}
|
|
139 |
|
|
140 |
static inline bool compareIntersections(const QIntersectionPoint &i1, const QIntersectionPoint &i2)
|
|
141 |
{
|
|
142 |
if (qAbs(i1.x - i2.x) > 0.01) { // x != other.x in 99% of the cases
|
|
143 |
return i1.x < i2.x;
|
|
144 |
} else {
|
|
145 |
qreal x1 = (i1.edge->p1.x != i1.edge->p2.x) ?
|
|
146 |
((currentY+1 - i1.edge->b)*i1.edge->m) : XFixedToDouble(i1.edge->p1.x);
|
|
147 |
qreal x2 = (i2.edge->p1.x != i2.edge->p2.x) ?
|
|
148 |
((currentY+1 - i2.edge->b)*i2.edge->m) : XFixedToDouble(i2.edge->p1.x);
|
|
149 |
// qDebug() << ">>>" << currentY << i1.edge << i2.edge << x1 << x2;
|
|
150 |
return x1 < x2;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
#ifdef QT_USE_FIXED_POINT
|
|
155 |
inline int qrealToXFixed(qreal f)
|
|
156 |
{ return f.value() << 8; }
|
|
157 |
#else
|
|
158 |
#define qrealToXFixed FloatToXFixed
|
|
159 |
#endif
|
|
160 |
|
|
161 |
static XTrapezoid QT_FASTCALL toXTrapezoid(XFixed y1, XFixed y2, const QEdge &left, const QEdge &right)
|
|
162 |
{
|
|
163 |
XTrapezoid trap;
|
|
164 |
trap.top = y1;
|
|
165 |
trap.bottom = y2;
|
|
166 |
trap.left.p1.y = left.p1.y;
|
|
167 |
trap.left.p2.y = left.p2.y;
|
|
168 |
trap.right.p1.y = right.p1.y;
|
|
169 |
trap.right.p2.y = right.p2.y;
|
|
170 |
trap.left.p1.x = left.p1.x;
|
|
171 |
trap.left.p2.x = left.p2.x;
|
|
172 |
trap.right.p1.x = right.p1.x;
|
|
173 |
trap.right.p2.x = right.p2.x;
|
|
174 |
return trap;
|
|
175 |
}
|
|
176 |
|
|
177 |
#ifdef QT_DEBUG_TESSELATOR
|
|
178 |
static void dump_edges(const QList<const QEdge *> &et)
|
|
179 |
{
|
|
180 |
for (int x = 0; x < et.size(); ++x) {
|
|
181 |
qDebug() << "edge#" << x << et.at(x) << "("
|
|
182 |
<< XFixedToDouble(et.at(x)->p1.x)
|
|
183 |
<< XFixedToDouble(et.at(x)->p1.y)
|
|
184 |
<< ") ("
|
|
185 |
<< XFixedToDouble(et.at(x)->p2.x)
|
|
186 |
<< XFixedToDouble(et.at(x)->p2.y)
|
|
187 |
<< ") b: " << et.at(x)->b << "m:" << et.at(x)->m;
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
static void dump_trap(const XTrapezoid &t)
|
|
192 |
{
|
|
193 |
qDebug() << "trap# t=" << t.top/65536.0 << "b=" << t.bottom/65536.0 << "h="
|
|
194 |
<< XFixedToDouble(t.bottom - t.top) << "\tleft p1: ("
|
|
195 |
<< XFixedToDouble(t.left.p1.x) << ","<< XFixedToDouble(t.left.p1.y)
|
|
196 |
<< ")" << "\tleft p2: (" << XFixedToDouble(t.left.p2.x) << ","
|
|
197 |
<< XFixedToDouble(t.left.p2.y) << ")" << "\n\t\t\t\tright p1:("
|
|
198 |
<< XFixedToDouble(t.right.p1.x) << "," << XFixedToDouble(t.right.p1.y) << ")"
|
|
199 |
<< "\tright p2:(" << XFixedToDouble(t.right.p2.x) << ","
|
|
200 |
<< XFixedToDouble(t.right.p2.y) << ")";
|
|
201 |
}
|
|
202 |
#endif
|
|
203 |
|
|
204 |
|
|
205 |
typedef int Q27Dot5;
|
|
206 |
#define Q27Dot5ToDouble(i) (i/32.)
|
|
207 |
#define FloatToQ27Dot5(i) (int)((i) * 32)
|
|
208 |
#define IntToQ27Dot5(i) ((i) << 5)
|
|
209 |
#define Q27Dot5ToXFixed(i) ((i) << 11)
|
|
210 |
#define Q27Dot5Factor 32
|
|
211 |
|
|
212 |
void old_tesselate_polygon(QVector<XTrapezoid> *traps, const QPointF *pg, int pgSize,
|
|
213 |
bool winding)
|
|
214 |
{
|
|
215 |
QVector<QEdge> edges;
|
|
216 |
edges.reserve(128);
|
|
217 |
qreal ymin(INT_MAX/256);
|
|
218 |
qreal ymax(INT_MIN/256);
|
|
219 |
|
|
220 |
//painter.begin(pg, pgSize);
|
|
221 |
Q_ASSERT(pg[0] == pg[pgSize-1]);
|
|
222 |
// generate edge table
|
|
223 |
// qDebug() << "POINTS:";
|
|
224 |
for (int x = 0; x < pgSize-1; ++x) {
|
|
225 |
QEdge edge;
|
|
226 |
QPointF p1(Q27Dot5ToDouble(FloatToQ27Dot5(pg[x].x())),
|
|
227 |
Q27Dot5ToDouble(FloatToQ27Dot5(pg[x].y())));
|
|
228 |
QPointF p2(Q27Dot5ToDouble(FloatToQ27Dot5(pg[x+1].x())),
|
|
229 |
Q27Dot5ToDouble(FloatToQ27Dot5(pg[x+1].y())));
|
|
230 |
|
|
231 |
// qDebug() << " "
|
|
232 |
// << p1;
|
|
233 |
edge.winding = p1.y() > p2.y() ? 1 : -1;
|
|
234 |
if (edge.winding > 0)
|
|
235 |
qSwap(p1, p2);
|
|
236 |
edge.p1.x = XDoubleToFixed(p1.x());
|
|
237 |
edge.p1.y = XDoubleToFixed(p1.y());
|
|
238 |
edge.p2.x = XDoubleToFixed(p2.x());
|
|
239 |
edge.p2.y = XDoubleToFixed(p2.y());
|
|
240 |
|
|
241 |
edge.m = (p1.y() - p2.y()) / (p1.x() - p2.x()); // line derivative
|
|
242 |
edge.b = p1.y() - edge.m * p1.x(); // intersection with y axis
|
|
243 |
edge.m = edge.m != 0.0 ? 1.0 / edge.m : 0.0; // inverted derivative
|
|
244 |
edges.append(edge);
|
|
245 |
ymin = qMin(ymin, qreal(XFixedToDouble(edge.p1.y)));
|
|
246 |
ymax = qMax(ymax, qreal(XFixedToDouble(edge.p2.y)));
|
|
247 |
}
|
|
248 |
|
|
249 |
QList<const QEdge *> et; // edge list
|
|
250 |
for (int i = 0; i < edges.size(); ++i)
|
|
251 |
et.append(&edges.at(i));
|
|
252 |
|
|
253 |
// sort edge table by min y value
|
|
254 |
qSort(et.begin(), et.end(), compareEdges);
|
|
255 |
|
|
256 |
// eliminate shared edges
|
|
257 |
for (int i = 0; i < et.size(); ++i) {
|
|
258 |
for (int k = i+1; k < et.size(); ++k) {
|
|
259 |
const QEdge *edgeI = et.at(i);
|
|
260 |
const QEdge *edgeK = et.at(k);
|
|
261 |
if (edgeK->p1.y > edgeI->p1.y)
|
|
262 |
break;
|
|
263 |
if (edgeI->winding != edgeK->winding &&
|
|
264 |
isEqual(edgeI->p1, edgeK->p1) && isEqual(edgeI->p2, edgeK->p2)
|
|
265 |
) {
|
|
266 |
et.removeAt(k);
|
|
267 |
et.removeAt(i);
|
|
268 |
--i;
|
|
269 |
break;
|
|
270 |
}
|
|
271 |
}
|
|
272 |
}
|
|
273 |
|
|
274 |
if (ymax <= ymin)
|
|
275 |
return;
|
|
276 |
QList<const QEdge *> aet; // edges that intersects the current scanline
|
|
277 |
|
|
278 |
// if (ymin < 0)
|
|
279 |
// ymin = 0;
|
|
280 |
// if (paintEventClipRegion) // don't scan more lines than we have to
|
|
281 |
// ymax = paintEventClipRegion->boundingRect().height();
|
|
282 |
|
|
283 |
#ifdef QT_DEBUG_TESSELATOR
|
|
284 |
qDebug("==> ymin = %f, ymax = %f", ymin, ymax);
|
|
285 |
#endif // QT_DEBUG_TESSELATOR
|
|
286 |
|
|
287 |
currentY = ymin; // used by the less than op
|
|
288 |
for (qreal y = ymin; y < ymax;) {
|
|
289 |
// fill active edge table with edges that intersect the current line
|
|
290 |
for (int i = 0; i < et.size(); ++i) {
|
|
291 |
const QEdge *edge = et.at(i);
|
|
292 |
if (edge->p1.y > XDoubleToFixed(y))
|
|
293 |
break;
|
|
294 |
aet.append(edge);
|
|
295 |
et.removeAt(i);
|
|
296 |
--i;
|
|
297 |
}
|
|
298 |
|
|
299 |
// remove processed edges from active edge table
|
|
300 |
for (int i = 0; i < aet.size(); ++i) {
|
|
301 |
if (aet.at(i)->p2.y <= XDoubleToFixed(y)) {
|
|
302 |
aet.removeAt(i);
|
|
303 |
--i;
|
|
304 |
}
|
|
305 |
}
|
|
306 |
if (aet.size()%2 != 0) {
|
|
307 |
#ifndef QT_NO_DEBUG
|
|
308 |
qWarning("QX11PaintEngine: aet out of sync - this should not happen.");
|
|
309 |
#endif
|
|
310 |
return;
|
|
311 |
}
|
|
312 |
|
|
313 |
// done?
|
|
314 |
if (!aet.size()) {
|
|
315 |
if (!et.size()) {
|
|
316 |
break;
|
|
317 |
} else {
|
|
318 |
y = currentY = XFixedToDouble(et.at(0)->p1.y);
|
|
319 |
continue;
|
|
320 |
}
|
|
321 |
}
|
|
322 |
|
|
323 |
// calculate the next y where we have to start a new set of trapezoids
|
|
324 |
qreal next_y(INT_MAX/256);
|
|
325 |
for (int i = 0; i < aet.size(); ++i) {
|
|
326 |
const QEdge *edge = aet.at(i);
|
|
327 |
if (XFixedToDouble(edge->p2.y) < next_y)
|
|
328 |
next_y = XFixedToDouble(edge->p2.y);
|
|
329 |
}
|
|
330 |
|
|
331 |
if (et.size() && next_y > XFixedToDouble(et.at(0)->p1.y))
|
|
332 |
next_y = XFixedToDouble(et.at(0)->p1.y);
|
|
333 |
|
|
334 |
int aetSize = aet.size();
|
|
335 |
for (int i = 0; i < aetSize; ++i) {
|
|
336 |
for (int k = i+1; k < aetSize; ++k) {
|
|
337 |
const QEdge *edgeI = aet.at(i);
|
|
338 |
const QEdge *edgeK = aet.at(k);
|
|
339 |
qreal m1 = edgeI->m;
|
|
340 |
qreal b1 = edgeI->b;
|
|
341 |
qreal m2 = edgeK->m;
|
|
342 |
qreal b2 = edgeK->b;
|
|
343 |
|
|
344 |
if (qAbs(m1 - m2) < 0.001)
|
|
345 |
continue;
|
|
346 |
|
|
347 |
// ### intersect is not calculated correctly when optimized with -O2 (gcc)
|
|
348 |
volatile qreal intersect = 0;
|
|
349 |
if (!qIsFinite(b1))
|
|
350 |
intersect = (1.f / m2) * XFixedToDouble(edgeI->p1.x) + b2;
|
|
351 |
else if (!qIsFinite(b2))
|
|
352 |
intersect = (1.f / m1) * XFixedToDouble(edgeK->p1.x) + b1;
|
|
353 |
else
|
|
354 |
intersect = (b1*m1 - b2*m2) / (m1 - m2);
|
|
355 |
|
|
356 |
if (intersect > y && intersect < next_y)
|
|
357 |
next_y = intersect;
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
XFixed yf, next_yf;
|
|
362 |
yf = qrealToXFixed(y);
|
|
363 |
next_yf = qrealToXFixed(next_y);
|
|
364 |
|
|
365 |
if (yf == next_yf) {
|
|
366 |
y = currentY = next_y;
|
|
367 |
continue;
|
|
368 |
}
|
|
369 |
|
|
370 |
#ifdef QT_DEBUG_TESSELATOR
|
|
371 |
qDebug("###> y = %f, next_y = %f, %d active edges", y, next_y, aet.size());
|
|
372 |
qDebug("===> edges");
|
|
373 |
dump_edges(et);
|
|
374 |
qDebug("===> active edges");
|
|
375 |
dump_edges(aet);
|
|
376 |
#endif
|
|
377 |
// calc intersection points
|
|
378 |
QVarLengthArray<QIntersectionPoint> isects(aet.size()+1);
|
|
379 |
for (int i = 0; i < isects.size()-1; ++i) {
|
|
380 |
const QEdge *edge = aet.at(i);
|
|
381 |
isects[i].x = (edge->p1.x != edge->p2.x) ?
|
|
382 |
((y - edge->b)*edge->m) : XFixedToDouble(edge->p1.x);
|
|
383 |
isects[i].edge = edge;
|
|
384 |
}
|
|
385 |
|
|
386 |
Q_ASSERT(isects.size()%2 == 1);
|
|
387 |
|
|
388 |
// sort intersection points
|
|
389 |
qSort(&isects[0], &isects[isects.size()-1], compareIntersections);
|
|
390 |
// qDebug() << "INTERSECTION_POINTS:";
|
|
391 |
// for (int i = 0; i < isects.size(); ++i)
|
|
392 |
// qDebug() << isects[i].edge << isects[i].x;
|
|
393 |
|
|
394 |
if (winding) {
|
|
395 |
// winding fill rule
|
|
396 |
for (int i = 0; i < isects.size()-1;) {
|
|
397 |
int winding = 0;
|
|
398 |
const QEdge *left = isects[i].edge;
|
|
399 |
const QEdge *right = 0;
|
|
400 |
winding += isects[i].edge->winding;
|
|
401 |
for (++i; i < isects.size()-1 && winding != 0; ++i) {
|
|
402 |
winding += isects[i].edge->winding;
|
|
403 |
right = isects[i].edge;
|
|
404 |
}
|
|
405 |
if (!left || !right)
|
|
406 |
break;
|
|
407 |
//painter.addTrapezoid(&toXTrapezoid(yf, next_yf, *left, *right));
|
|
408 |
traps->append(toXTrapezoid(yf, next_yf, *left, *right));
|
|
409 |
}
|
|
410 |
} else {
|
|
411 |
// odd-even fill rule
|
|
412 |
for (int i = 0; i < isects.size()-2; i += 2) {
|
|
413 |
//painter.addTrapezoid(&toXTrapezoid(yf, next_yf, *isects[i].edge, *isects[i+1].edge));
|
|
414 |
traps->append(toXTrapezoid(yf, next_yf, *isects[i].edge, *isects[i+1].edge));
|
|
415 |
}
|
|
416 |
}
|
|
417 |
y = currentY = next_y;
|
|
418 |
}
|
|
419 |
|
|
420 |
#ifdef QT_DEBUG_TESSELATOR
|
|
421 |
qDebug("==> number of trapezoids: %d - edge table size: %d\n", traps->size(), et.size());
|
|
422 |
|
|
423 |
for (int i = 0; i < traps->size(); ++i)
|
|
424 |
dump_trap(traps->at(i));
|
|
425 |
#endif
|
|
426 |
|
|
427 |
// optimize by unifying trapezoids that share left/right lines
|
|
428 |
// and have a common top/bottom edge
|
|
429 |
// for (int i = 0; i < tps.size(); ++i) {
|
|
430 |
// for (int k = i+1; k < tps.size(); ++k) {
|
|
431 |
// if (i != k && tps.at(i).right == tps.at(k).right
|
|
432 |
// && tps.at(i).left == tps.at(k).left
|
|
433 |
// && (tps.at(i).top == tps.at(k).bottom
|
|
434 |
// || tps.at(i).bottom == tps.at(k).top))
|
|
435 |
// {
|
|
436 |
// tps[i].bottom = tps.at(k).bottom;
|
|
437 |
// tps.removeAt(k);
|
|
438 |
// i = 0;
|
|
439 |
// break;
|
|
440 |
// }
|
|
441 |
// }
|
|
442 |
// }
|
|
443 |
//static int i = 0;
|
|
444 |
//QImage img = painter.end();
|
|
445 |
//img.save(QString("res%1.png").arg(i++), "PNG");
|
|
446 |
}
|