author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module 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 |
||
42 |
#include "qbezier_p.h" |
|
43 |
#include <qdebug.h> |
|
44 |
#include <qline.h> |
|
45 |
#include <qpolygon.h> |
|
46 |
#include <qvector.h> |
|
47 |
#include <qlist.h> |
|
48 |
#include <qmath.h> |
|
49 |
||
50 |
#include <private/qnumeric_p.h> |
|
51 |
#include <private/qmath_p.h> |
|
52 |
||
53 |
QT_BEGIN_NAMESPACE |
|
54 |
||
55 |
//#define QDEBUG_BEZIER |
|
56 |
||
57 |
#ifdef FLOAT_ACCURACY |
|
58 |
#define INV_EPS (1L<<23) |
|
59 |
#else |
|
60 |
/* The value of 1.0 / (1L<<14) is enough for most applications */ |
|
61 |
#define INV_EPS (1L<<14) |
|
62 |
#endif |
|
63 |
||
64 |
#ifndef M_SQRT2 |
|
65 |
#define M_SQRT2 1.41421356237309504880 |
|
66 |
#endif |
|
67 |
||
68 |
#define log2(x) (qLn(x)/qLn(2.)) |
|
69 |
||
70 |
static inline qreal log4(qreal x) |
|
71 |
{ |
|
72 |
return qreal(0.5) * log2(x); |
|
73 |
} |
|
74 |
||
75 |
/*! |
|
76 |
\internal |
|
77 |
*/ |
|
78 |
QBezier QBezier::fromPoints(const QPointF &p1, const QPointF &p2, |
|
79 |
const QPointF &p3, const QPointF &p4) |
|
80 |
{ |
|
81 |
QBezier b; |
|
82 |
b.x1 = p1.x(); |
|
83 |
b.y1 = p1.y(); |
|
84 |
b.x2 = p2.x(); |
|
85 |
b.y2 = p2.y(); |
|
86 |
b.x3 = p3.x(); |
|
87 |
b.y3 = p3.y(); |
|
88 |
b.x4 = p4.x(); |
|
89 |
b.y4 = p4.y(); |
|
90 |
return b; |
|
91 |
} |
|
92 |
||
93 |
/*! |
|
94 |
\internal |
|
95 |
*/ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
96 |
QPolygonF QBezier::toPolygon(qreal bezier_flattening_threshold) const |
0 | 97 |
{ |
98 |
// flattening is done by splitting the bezier until we can replace the segment by a straight |
|
99 |
// line. We split further until the control points are close enough to the line connecting the |
|
100 |
// boundary points. |
|
101 |
// |
|
102 |
// the Distance of a point p from a line given by the points (a,b) is given by: |
|
103 |
// |
|
104 |
// d = abs( (bx - ax)(ay - py) - (by - ay)(ax - px) ) / line_length |
|
105 |
// |
|
106 |
// We can stop splitting if both control points are close enough to the line. |
|
107 |
// To make the algorithm faster we use the manhattan length of the line. |
|
108 |
||
109 |
QPolygonF polygon; |
|
110 |
polygon.append(QPointF(x1, y1)); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
111 |
addToPolygon(&polygon, bezier_flattening_threshold); |
0 | 112 |
return polygon; |
113 |
} |
|
114 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
115 |
QBezier QBezier::mapBy(const QTransform &transform) const |
0 | 116 |
{ |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
117 |
return QBezier::fromPoints(transform.map(pt1()), transform.map(pt2()), transform.map(pt3()), transform.map(pt4())); |
0 | 118 |
} |
119 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
120 |
QBezier QBezier::getSubRange(qreal t0, qreal t1) const |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
121 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
122 |
QBezier result; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
123 |
QBezier temp; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
124 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
125 |
// cut at t1 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
126 |
if (qFuzzyIsNull(t1 - qreal(1.))) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
127 |
result = *this; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
128 |
} else { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
129 |
temp = *this; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
130 |
temp.parameterSplitLeft(t1, &result); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
131 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
132 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
133 |
// cut at t0 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
134 |
if (!qFuzzyIsNull(t0)) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
135 |
result.parameterSplitLeft(t0 / t1, &temp); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
136 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
137 |
return result; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
138 |
} |
0 | 139 |
|
140 |
static inline int quadraticRoots(qreal a, qreal b, qreal c, |
|
141 |
qreal *x1, qreal *x2) |
|
142 |
{ |
|
143 |
if (qFuzzyIsNull(a)) { |
|
144 |
if (qFuzzyIsNull(b)) |
|
145 |
return 0; |
|
146 |
*x1 = *x2 = (-c / b); |
|
147 |
return 1; |
|
148 |
} else { |
|
149 |
const qreal det = b * b - 4 * a * c; |
|
150 |
if (qFuzzyIsNull(det)) { |
|
151 |
*x1 = *x2 = -b / (2 * a); |
|
152 |
return 1; |
|
153 |
} |
|
154 |
if (det > 0) { |
|
155 |
if (qFuzzyIsNull(b)) { |
|
156 |
*x2 = qSqrt(-c / a); |
|
157 |
*x1 = -(*x2); |
|
158 |
return 2; |
|
159 |
} |
|
160 |
const qreal stableA = b / (2 * a); |
|
161 |
const qreal stableB = c / (a * stableA * stableA); |
|
162 |
const qreal stableC = -1 - qSqrt(1 - stableB); |
|
163 |
*x2 = stableA * stableC; |
|
164 |
*x1 = (stableA * stableB) / stableC; |
|
165 |
return 2; |
|
166 |
} else |
|
167 |
return 0; |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
static inline bool findInflections(qreal a, qreal b, qreal c, |
|
172 |
qreal *t1 , qreal *t2, qreal *tCups) |
|
173 |
{ |
|
174 |
qreal r1 = 0, r2 = 0; |
|
175 |
||
176 |
short rootsCount = quadraticRoots(a, b, c, &r1, &r2); |
|
177 |
||
178 |
if (rootsCount >= 1) { |
|
179 |
if (r1 < r2) { |
|
180 |
*t1 = r1; |
|
181 |
*t2 = r2; |
|
182 |
} else { |
|
183 |
*t1 = r2; |
|
184 |
*t2 = r1; |
|
185 |
} |
|
186 |
if (!qFuzzyIsNull(a)) |
|
187 |
*tCups = 0.5 * (-b / a); |
|
188 |
else |
|
189 |
*tCups = 2; |
|
190 |
||
191 |
return true; |
|
192 |
} |
|
193 |
||
194 |
return false; |
|
195 |
} |
|
196 |
||
197 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
198 |
void QBezier::addToPolygon(QPolygonF *polygon, qreal bezier_flattening_threshold) const |
0 | 199 |
{ |
200 |
QBezier beziers[32]; |
|
201 |
beziers[0] = *this; |
|
202 |
QBezier *b = beziers; |
|
203 |
||
204 |
while (b >= beziers) { |
|
205 |
// check if we can pop the top bezier curve from the stack |
|
206 |
qreal y4y1 = b->y4 - b->y1; |
|
207 |
qreal x4x1 = b->x4 - b->x1; |
|
208 |
qreal l = qAbs(x4x1) + qAbs(y4y1); |
|
209 |
qreal d; |
|
210 |
if (l > 1.) { |
|
211 |
d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) ) |
|
212 |
+ qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) ); |
|
213 |
} else { |
|
214 |
d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) + |
|
215 |
qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3); |
|
216 |
l = 1.; |
|
217 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
218 |
if (d < bezier_flattening_threshold*l || b == beziers + 31) { |
0 | 219 |
// good enough, we pop it off and add the endpoint |
220 |
polygon->append(QPointF(b->x4, b->y4)); |
|
221 |
--b; |
|
222 |
} else { |
|
223 |
// split, second half of the polygon goes lower into the stack |
|
224 |
b->split(b+1, b); |
|
225 |
++b; |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
QRectF QBezier::bounds() const |
|
231 |
{ |
|
232 |
qreal xmin = x1; |
|
233 |
qreal xmax = x1; |
|
234 |
if (x2 < xmin) |
|
235 |
xmin = x2; |
|
236 |
else if (x2 > xmax) |
|
237 |
xmax = x2; |
|
238 |
if (x3 < xmin) |
|
239 |
xmin = x3; |
|
240 |
else if (x3 > xmax) |
|
241 |
xmax = x3; |
|
242 |
if (x4 < xmin) |
|
243 |
xmin = x4; |
|
244 |
else if (x4 > xmax) |
|
245 |
xmax = x4; |
|
246 |
||
247 |
qreal ymin = y1; |
|
248 |
qreal ymax = y1; |
|
249 |
if (y2 < ymin) |
|
250 |
ymin = y2; |
|
251 |
else if (y2 > ymax) |
|
252 |
ymax = y2; |
|
253 |
if (y3 < ymin) |
|
254 |
ymin = y3; |
|
255 |
else if (y3 > ymax) |
|
256 |
ymax = y3; |
|
257 |
if (y4 < ymin) |
|
258 |
ymin = y4; |
|
259 |
else if (y4 > ymax) |
|
260 |
ymax = y4; |
|
261 |
return QRectF(xmin, ymin, xmax-xmin, ymax-ymin); |
|
262 |
} |
|
263 |
||
264 |
||
265 |
enum ShiftResult { |
|
266 |
Ok, |
|
267 |
Discard, |
|
268 |
Split, |
|
269 |
Circle |
|
270 |
}; |
|
271 |
||
272 |
static ShiftResult good_offset(const QBezier *b1, const QBezier *b2, qreal offset, qreal threshold) |
|
273 |
{ |
|
274 |
const qreal o2 = offset*offset; |
|
275 |
const qreal max_dist_line = threshold*offset*offset; |
|
276 |
const qreal max_dist_normal = threshold*offset; |
|
277 |
const qreal spacing = 0.25; |
|
278 |
for (qreal i = spacing; i < 0.99; i += spacing) { |
|
279 |
QPointF p1 = b1->pointAt(i); |
|
280 |
QPointF p2 = b2->pointAt(i); |
|
281 |
qreal d = (p1.x() - p2.x())*(p1.x() - p2.x()) + (p1.y() - p2.y())*(p1.y() - p2.y()); |
|
282 |
if (qAbs(d - o2) > max_dist_line) |
|
283 |
return Split; |
|
284 |
||
285 |
QPointF normalPoint = b1->normalVector(i); |
|
286 |
qreal l = qAbs(normalPoint.x()) + qAbs(normalPoint.y()); |
|
287 |
if (l != 0.) { |
|
288 |
d = qAbs( normalPoint.x()*(p1.y() - p2.y()) - normalPoint.y()*(p1.x() - p2.x()) ) / l; |
|
289 |
if (d > max_dist_normal) |
|
290 |
return Split; |
|
291 |
} |
|
292 |
} |
|
293 |
return Ok; |
|
294 |
} |
|
295 |
||
296 |
static inline QLineF qline_shifted(const QPointF &p1, const QPointF &p2, qreal offset) |
|
297 |
{ |
|
298 |
QLineF l(p1, p2); |
|
299 |
QLineF ln = l.normalVector().unitVector(); |
|
300 |
l.translate(ln.dx() * offset, ln.dy() * offset); |
|
301 |
return l; |
|
302 |
} |
|
303 |
||
304 |
static bool qbezier_is_line(QPointF *points, int pointCount) |
|
305 |
{ |
|
306 |
Q_ASSERT(pointCount > 2); |
|
307 |
||
308 |
qreal dx13 = points[2].x() - points[0].x(); |
|
309 |
qreal dy13 = points[2].y() - points[0].y(); |
|
310 |
||
311 |
qreal dx12 = points[1].x() - points[0].x(); |
|
312 |
qreal dy12 = points[1].y() - points[0].y(); |
|
313 |
||
314 |
if (pointCount == 3) { |
|
315 |
return qFuzzyCompare(dx12 * dy13, dx13 * dy12); |
|
316 |
} else if (pointCount == 4) { |
|
317 |
qreal dx14 = points[3].x() - points[0].x(); |
|
318 |
qreal dy14 = points[3].y() - points[0].y(); |
|
319 |
||
320 |
return (qFuzzyCompare(dx12 * dy13, dx13 * dy12) && qFuzzyCompare(dx12 * dy14, dx14 * dy12)); |
|
321 |
} |
|
322 |
||
323 |
return false; |
|
324 |
} |
|
325 |
||
326 |
static ShiftResult shift(const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold) |
|
327 |
{ |
|
328 |
int map[4]; |
|
329 |
bool p1_p2_equal = (orig->x1 == orig->x2 && orig->y1 == orig->y2); |
|
330 |
bool p2_p3_equal = (orig->x2 == orig->x3 && orig->y2 == orig->y3); |
|
331 |
bool p3_p4_equal = (orig->x3 == orig->x4 && orig->y3 == orig->y4); |
|
332 |
||
333 |
QPointF points[4]; |
|
334 |
int np = 0; |
|
335 |
points[np] = QPointF(orig->x1, orig->y1); |
|
336 |
map[0] = 0; |
|
337 |
++np; |
|
338 |
if (!p1_p2_equal) { |
|
339 |
points[np] = QPointF(orig->x2, orig->y2); |
|
340 |
++np; |
|
341 |
} |
|
342 |
map[1] = np - 1; |
|
343 |
if (!p2_p3_equal) { |
|
344 |
points[np] = QPointF(orig->x3, orig->y3); |
|
345 |
++np; |
|
346 |
} |
|
347 |
map[2] = np - 1; |
|
348 |
if (!p3_p4_equal) { |
|
349 |
points[np] = QPointF(orig->x4, orig->y4); |
|
350 |
++np; |
|
351 |
} |
|
352 |
map[3] = np - 1; |
|
353 |
if (np == 1) |
|
354 |
return Discard; |
|
355 |
||
356 |
// We need to specialcase lines of 3 or 4 points due to numerical |
|
357 |
// instability in intersections below |
|
358 |
if (np > 2 && qbezier_is_line(points, np)) { |
|
359 |
if (points[0] == points[np-1]) |
|
360 |
return Discard; |
|
361 |
||
362 |
QLineF l = qline_shifted(points[0], points[np-1], offset); |
|
363 |
*shifted = QBezier::fromPoints(l.p1(), l.pointAt(qreal(0.33)), l.pointAt(qreal(0.66)), l.p2()); |
|
364 |
return Ok; |
|
365 |
} |
|
366 |
||
367 |
QRectF b = orig->bounds(); |
|
368 |
if (np == 4 && b.width() < .1*offset && b.height() < .1*offset) { |
|
369 |
qreal l = (orig->x1 - orig->x2)*(orig->x1 - orig->x2) + |
|
370 |
(orig->y1 - orig->y2)*(orig->y1 - orig->y1) * |
|
371 |
(orig->x3 - orig->x4)*(orig->x3 - orig->x4) + |
|
372 |
(orig->y3 - orig->y4)*(orig->y3 - orig->y4); |
|
373 |
qreal dot = (orig->x1 - orig->x2)*(orig->x3 - orig->x4) + |
|
374 |
(orig->y1 - orig->y2)*(orig->y3 - orig->y4); |
|
375 |
if (dot < 0 && dot*dot < 0.8*l) |
|
376 |
// the points are close and reverse dirction. Approximate the whole |
|
377 |
// thing by a semi circle |
|
378 |
return Circle; |
|
379 |
} |
|
380 |
||
381 |
QPointF points_shifted[4]; |
|
382 |
||
383 |
QLineF prev = QLineF(QPointF(), points[1] - points[0]); |
|
384 |
QPointF prev_normal = prev.normalVector().unitVector().p2(); |
|
385 |
||
386 |
points_shifted[0] = points[0] + offset * prev_normal; |
|
387 |
||
388 |
for (int i = 1; i < np - 1; ++i) { |
|
389 |
QLineF next = QLineF(QPointF(), points[i + 1] - points[i]); |
|
390 |
QPointF next_normal = next.normalVector().unitVector().p2(); |
|
391 |
||
392 |
QPointF normal_sum = prev_normal + next_normal; |
|
393 |
||
394 |
qreal r = 1.0 + prev_normal.x() * next_normal.x() |
|
395 |
+ prev_normal.y() * next_normal.y(); |
|
396 |
||
397 |
if (qFuzzyIsNull(r)) { |
|
398 |
points_shifted[i] = points[i] + offset * prev_normal; |
|
399 |
} else { |
|
400 |
qreal k = offset / r; |
|
401 |
points_shifted[i] = points[i] + k * normal_sum; |
|
402 |
} |
|
403 |
||
404 |
prev_normal = next_normal; |
|
405 |
} |
|
406 |
||
407 |
points_shifted[np - 1] = points[np - 1] + offset * prev_normal; |
|
408 |
||
409 |
*shifted = QBezier::fromPoints(points_shifted[map[0]], points_shifted[map[1]], |
|
410 |
points_shifted[map[2]], points_shifted[map[3]]); |
|
411 |
||
412 |
return good_offset(orig, shifted, offset, threshold); |
|
413 |
} |
|
414 |
||
415 |
// This value is used to determine the length of control point vectors |
|
416 |
// when approximating arc segments as curves. The factor is multiplied |
|
417 |
// with the radius of the circle. |
|
418 |
#define KAPPA 0.5522847498 |
|
419 |
||
420 |
||
421 |
static bool addCircle(const QBezier *b, qreal offset, QBezier *o) |
|
422 |
{ |
|
423 |
QPointF normals[3]; |
|
424 |
||
425 |
normals[0] = QPointF(b->y2 - b->y1, b->x1 - b->x2); |
|
426 |
qreal dist = qSqrt(normals[0].x()*normals[0].x() + normals[0].y()*normals[0].y()); |
|
427 |
if (qFuzzyIsNull(dist)) |
|
428 |
return false; |
|
429 |
normals[0] /= dist; |
|
430 |
normals[2] = QPointF(b->y4 - b->y3, b->x3 - b->x4); |
|
431 |
dist = qSqrt(normals[2].x()*normals[2].x() + normals[2].y()*normals[2].y()); |
|
432 |
if (qFuzzyIsNull(dist)) |
|
433 |
return false; |
|
434 |
normals[2] /= dist; |
|
435 |
||
436 |
normals[1] = QPointF(b->x1 - b->x2 - b->x3 + b->x4, b->y1 - b->y2 - b->y3 + b->y4); |
|
437 |
normals[1] /= -1*qSqrt(normals[1].x()*normals[1].x() + normals[1].y()*normals[1].y()); |
|
438 |
||
439 |
qreal angles[2]; |
|
440 |
qreal sign = 1.; |
|
441 |
for (int i = 0; i < 2; ++i) { |
|
442 |
qreal cos_a = normals[i].x()*normals[i+1].x() + normals[i].y()*normals[i+1].y(); |
|
443 |
if (cos_a > 1.) |
|
444 |
cos_a = 1.; |
|
445 |
if (cos_a < -1.) |
|
446 |
cos_a = -1; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
447 |
angles[i] = qAcos(cos_a)/Q_PI; |
0 | 448 |
} |
449 |
||
450 |
if (angles[0] + angles[1] > 1.) { |
|
451 |
// more than 180 degrees |
|
452 |
normals[1] = -normals[1]; |
|
453 |
angles[0] = 1. - angles[0]; |
|
454 |
angles[1] = 1. - angles[1]; |
|
455 |
sign = -1.; |
|
456 |
||
457 |
} |
|
458 |
||
459 |
QPointF circle[3]; |
|
460 |
circle[0] = QPointF(b->x1, b->y1) + normals[0]*offset; |
|
461 |
circle[1] = QPointF(0.5*(b->x1 + b->x4), 0.5*(b->y1 + b->y4)) + normals[1]*offset; |
|
462 |
circle[2] = QPointF(b->x4, b->y4) + normals[2]*offset; |
|
463 |
||
464 |
for (int i = 0; i < 2; ++i) { |
|
465 |
qreal kappa = 2.*KAPPA * sign * offset * angles[i]; |
|
466 |
||
467 |
o->x1 = circle[i].x(); |
|
468 |
o->y1 = circle[i].y(); |
|
469 |
o->x2 = circle[i].x() - normals[i].y()*kappa; |
|
470 |
o->y2 = circle[i].y() + normals[i].x()*kappa; |
|
471 |
o->x3 = circle[i+1].x() + normals[i+1].y()*kappa; |
|
472 |
o->y3 = circle[i+1].y() - normals[i+1].x()*kappa; |
|
473 |
o->x4 = circle[i+1].x(); |
|
474 |
o->y4 = circle[i+1].y(); |
|
475 |
||
476 |
++o; |
|
477 |
} |
|
478 |
return true; |
|
479 |
} |
|
480 |
||
481 |
int QBezier::shifted(QBezier *curveSegments, int maxSegments, qreal offset, float threshold) const |
|
482 |
{ |
|
483 |
Q_ASSERT(curveSegments); |
|
484 |
Q_ASSERT(maxSegments > 0); |
|
485 |
||
486 |
if (x1 == x2 && x1 == x3 && x1 == x4 && |
|
487 |
y1 == y2 && y1 == y3 && y1 == y4) |
|
488 |
return 0; |
|
489 |
||
490 |
--maxSegments; |
|
491 |
QBezier beziers[10]; |
|
492 |
redo: |
|
493 |
beziers[0] = *this; |
|
494 |
QBezier *b = beziers; |
|
495 |
QBezier *o = curveSegments; |
|
496 |
||
497 |
while (b >= beziers) { |
|
498 |
int stack_segments = b - beziers + 1; |
|
499 |
if ((stack_segments == 10) || (o - curveSegments == maxSegments - stack_segments)) { |
|
500 |
threshold *= 1.5; |
|
501 |
if (threshold > 2.) |
|
502 |
goto give_up; |
|
503 |
goto redo; |
|
504 |
} |
|
505 |
ShiftResult res = shift(b, o, offset, threshold); |
|
506 |
if (res == Discard) { |
|
507 |
--b; |
|
508 |
} else if (res == Ok) { |
|
509 |
++o; |
|
510 |
--b; |
|
511 |
continue; |
|
512 |
} else if (res == Circle && maxSegments - (o - curveSegments) >= 2) { |
|
513 |
// add semi circle |
|
514 |
if (addCircle(b, offset, o)) |
|
515 |
o += 2; |
|
516 |
--b; |
|
517 |
} else { |
|
518 |
b->split(b+1, b); |
|
519 |
++b; |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
give_up: |
|
524 |
while (b >= beziers) { |
|
525 |
ShiftResult res = shift(b, o, offset, threshold); |
|
526 |
||
527 |
// if res isn't Ok or Split then *o is undefined |
|
528 |
if (res == Ok || res == Split) |
|
529 |
++o; |
|
530 |
||
531 |
--b; |
|
532 |
} |
|
533 |
||
534 |
Q_ASSERT(o - curveSegments <= maxSegments); |
|
535 |
return o - curveSegments; |
|
536 |
} |
|
537 |
||
538 |
#ifdef QDEBUG_BEZIER |
|
539 |
static QDebug operator<<(QDebug dbg, const QBezier &bz) |
|
540 |
{ |
|
541 |
dbg << '[' << bz.x1<< ", " << bz.y1 << "], " |
|
542 |
<< '[' << bz.x2 <<", " << bz.y2 << "], " |
|
543 |
<< '[' << bz.x3 <<", " << bz.y3 << "], " |
|
544 |
<< '[' << bz.x4 <<", " << bz.y4 << ']'; |
|
545 |
return dbg; |
|
546 |
} |
|
547 |
#endif |
|
548 |
||
549 |
static inline void splitBezierAt(const QBezier &bez, qreal t, |
|
550 |
QBezier *left, QBezier *right) |
|
551 |
{ |
|
552 |
left->x1 = bez.x1; |
|
553 |
left->y1 = bez.y1; |
|
554 |
||
555 |
left->x2 = bez.x1 + t * ( bez.x2 - bez.x1 ); |
|
556 |
left->y2 = bez.y1 + t * ( bez.y2 - bez.y1 ); |
|
557 |
||
558 |
left->x3 = bez.x2 + t * ( bez.x3 - bez.x2 ); // temporary holding spot |
|
559 |
left->y3 = bez.y2 + t * ( bez.y3 - bez.y2 ); // temporary holding spot |
|
560 |
||
561 |
right->x3 = bez.x3 + t * ( bez.x4 - bez.x3 ); |
|
562 |
right->y3 = bez.y3 + t * ( bez.y4 - bez.y3 ); |
|
563 |
||
564 |
right->x2 = left->x3 + t * ( right->x3 - left->x3); |
|
565 |
right->y2 = left->y3 + t * ( right->y3 - left->y3); |
|
566 |
||
567 |
left->x3 = left->x2 + t * ( left->x3 - left->x2 ); |
|
568 |
left->y3 = left->y2 + t * ( left->y3 - left->y2 ); |
|
569 |
||
570 |
left->x4 = right->x1 = left->x3 + t * (right->x2 - left->x3); |
|
571 |
left->y4 = right->y1 = left->y3 + t * (right->y2 - left->y3); |
|
572 |
||
573 |
right->x4 = bez.x4; |
|
574 |
right->y4 = bez.y4; |
|
575 |
} |
|
576 |
||
577 |
qreal QBezier::length(qreal error) const |
|
578 |
{ |
|
579 |
qreal length = 0.0; |
|
580 |
||
581 |
addIfClose(&length, error); |
|
582 |
||
583 |
return length; |
|
584 |
} |
|
585 |
||
586 |
void QBezier::addIfClose(qreal *length, qreal error) const |
|
587 |
{ |
|
588 |
QBezier left, right; /* bez poly splits */ |
|
589 |
||
590 |
qreal len = 0.0; /* arc length */ |
|
591 |
qreal chord; /* chord length */ |
|
592 |
||
593 |
len = len + QLineF(QPointF(x1, y1),QPointF(x2, y2)).length(); |
|
594 |
len = len + QLineF(QPointF(x2, y2),QPointF(x3, y3)).length(); |
|
595 |
len = len + QLineF(QPointF(x3, y3),QPointF(x4, y4)).length(); |
|
596 |
||
597 |
chord = QLineF(QPointF(x1, y1),QPointF(x4, y4)).length(); |
|
598 |
||
599 |
if((len-chord) > error) { |
|
600 |
split(&left, &right); /* split in two */ |
|
601 |
left.addIfClose(length, error); /* try left side */ |
|
602 |
right.addIfClose(length, error); /* try right side */ |
|
603 |
return; |
|
604 |
} |
|
605 |
||
606 |
*length = *length + len; |
|
607 |
||
608 |
return; |
|
609 |
} |
|
610 |
||
611 |
qreal QBezier::tForY(qreal t0, qreal t1, qreal y) const |
|
612 |
{ |
|
613 |
qreal py0 = pointAt(t0).y(); |
|
614 |
qreal py1 = pointAt(t1).y(); |
|
615 |
||
616 |
if (py0 > py1) { |
|
617 |
qSwap(py0, py1); |
|
618 |
qSwap(t0, t1); |
|
619 |
} |
|
620 |
||
621 |
Q_ASSERT(py0 <= py1); |
|
622 |
||
623 |
if (py0 >= y) |
|
624 |
return t0; |
|
625 |
else if (py1 <= y) |
|
626 |
return t1; |
|
627 |
||
628 |
Q_ASSERT(py0 < y && y < py1); |
|
629 |
||
630 |
qreal lt = t0; |
|
631 |
qreal dt; |
|
632 |
do { |
|
633 |
qreal t = 0.5 * (t0 + t1); |
|
634 |
||
635 |
qreal a, b, c, d; |
|
636 |
QBezier::coefficients(t, a, b, c, d); |
|
637 |
qreal yt = a * y1 + b * y2 + c * y3 + d * y4; |
|
638 |
||
639 |
if (yt < y) { |
|
640 |
t0 = t; |
|
641 |
py0 = yt; |
|
642 |
} else { |
|
643 |
t1 = t; |
|
644 |
py1 = yt; |
|
645 |
} |
|
646 |
dt = lt - t; |
|
647 |
lt = t; |
|
648 |
} while (qAbs(dt) > 1e-7); |
|
649 |
||
650 |
return t0; |
|
651 |
} |
|
652 |
||
653 |
int QBezier::stationaryYPoints(qreal &t0, qreal &t1) const |
|
654 |
{ |
|
655 |
// y(t) = (1 - t)^3 * y1 + 3 * (1 - t)^2 * t * y2 + 3 * (1 - t) * t^2 * y3 + t^3 * y4 |
|
656 |
// y'(t) = 3 * (-(1-2t+t^2) * y1 + (1 - 4 * t + 3 * t^2) * y2 + (2 * t - 3 * t^2) * y3 + t^2 * y4) |
|
657 |
// y'(t) = 3 * ((-y1 + 3 * y2 - 3 * y3 + y4)t^2 + (2 * y1 - 4 * y2 + 2 * y3)t + (-y1 + y2)) |
|
658 |
||
659 |
const qreal a = -y1 + 3 * y2 - 3 * y3 + y4; |
|
660 |
const qreal b = 2 * y1 - 4 * y2 + 2 * y3; |
|
661 |
const qreal c = -y1 + y2; |
|
662 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
663 |
if (qFuzzyIsNull(a)) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
664 |
if (qFuzzyIsNull(b)) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
665 |
return 0; |
0 | 666 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
667 |
t0 = -c / b; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
668 |
return t0 > 0 && t0 < 1; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
669 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
670 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
671 |
qreal reciprocal = b * b - 4 * a * c; |
0 | 672 |
|
673 |
if (qFuzzyIsNull(reciprocal)) { |
|
674 |
t0 = -b / (2 * a); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
675 |
return t0 > 0 && t0 < 1; |
0 | 676 |
} else if (reciprocal > 0) { |
677 |
qreal temp = qSqrt(reciprocal); |
|
678 |
||
679 |
t0 = (-b - temp)/(2*a); |
|
680 |
t1 = (-b + temp)/(2*a); |
|
681 |
||
682 |
if (t1 < t0) |
|
683 |
qSwap(t0, t1); |
|
684 |
||
685 |
int count = 0; |
|
686 |
qreal t[2] = { 0, 1 }; |
|
687 |
||
688 |
if (t0 > 0 && t0 < 1) |
|
689 |
t[count++] = t0; |
|
690 |
if (t1 > 0 && t1 < 1) |
|
691 |
t[count++] = t1; |
|
692 |
||
693 |
t0 = t[0]; |
|
694 |
t1 = t[1]; |
|
695 |
||
696 |
return count; |
|
697 |
} |
|
698 |
||
699 |
return 0; |
|
700 |
} |
|
701 |
||
702 |
qreal QBezier::tAtLength(qreal l) const |
|
703 |
{ |
|
704 |
qreal len = length(); |
|
705 |
qreal t = 1.0; |
|
706 |
const qreal error = (qreal)0.01; |
|
707 |
if (l > len || qFuzzyCompare(l, len)) |
|
708 |
return t; |
|
709 |
||
710 |
t *= 0.5; |
|
711 |
//int iters = 0; |
|
712 |
//qDebug()<<"LEN is "<<l<<len; |
|
713 |
qreal lastBigger = 1.; |
|
714 |
while (1) { |
|
715 |
//qDebug()<<"\tt is "<<t; |
|
716 |
QBezier right = *this; |
|
717 |
QBezier left; |
|
718 |
right.parameterSplitLeft(t, &left); |
|
719 |
qreal lLen = left.length(); |
|
720 |
if (qAbs(lLen - l) < error) |
|
721 |
break; |
|
722 |
||
723 |
if (lLen < l) { |
|
724 |
t += (lastBigger - t)*.5; |
|
725 |
} else { |
|
726 |
lastBigger = t; |
|
727 |
t -= t*.5; |
|
728 |
} |
|
729 |
//++iters; |
|
730 |
} |
|
731 |
//qDebug()<<"number of iters is "<<iters; |
|
732 |
return t; |
|
733 |
} |
|
734 |
||
735 |
QBezier QBezier::bezierOnInterval(qreal t0, qreal t1) const |
|
736 |
{ |
|
737 |
if (t0 == 0 && t1 == 1) |
|
738 |
return *this; |
|
739 |
||
740 |
QBezier bezier = *this; |
|
741 |
||
742 |
QBezier result; |
|
743 |
bezier.parameterSplitLeft(t0, &result); |
|
744 |
qreal trueT = (t1-t0)/(1-t0); |
|
745 |
bezier.parameterSplitLeft(trueT, &result); |
|
746 |
||
747 |
return result; |
|
748 |
} |
|
749 |
||
750 |
QT_END_NAMESPACE |