qbezier.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/painting/qbezier.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtGui module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qbezier_p.h"-
41#include <qdebug.h>-
42#include <qline.h>-
43#include <qpolygon.h>-
44#include <qvector.h>-
45#include <qlist.h>-
46#include <qmath.h>-
47-
48#include <private/qnumeric_p.h>-
49-
50QT_BEGIN_NAMESPACE-
51-
52//#define QDEBUG_BEZIER-
53-
54/*!-
55 \internal-
56*/-
57QBezier QBezier::fromPoints(const QPointF &p1, const QPointF &p2,-
58 const QPointF &p3, const QPointF &p4)-
59{-
60 QBezier b;-
61 b.x1 = p1.x();-
62 b.y1 = p1.y();-
63 b.x2 = p2.x();-
64 b.y2 = p2.y();-
65 b.x3 = p3.x();-
66 b.y3 = p3.y();-
67 b.x4 = p4.x();-
68 b.y4 = p4.y();-
69 return b;
never executed: return b;
0
70}-
71-
72/*!-
73 \internal-
74*/-
75QPolygonF QBezier::toPolygon(qreal bezier_flattening_threshold) const-
76{-
77 // flattening is done by splitting the bezier until we can replace the segment by a straight-
78 // line. We split further until the control points are close enough to the line connecting the-
79 // boundary points.-
80 //-
81 // the Distance of a point p from a line given by the points (a,b) is given by:-
82 //-
83 // d = abs( (bx - ax)(ay - py) - (by - ay)(ax - px) ) / line_length-
84 //-
85 // We can stop splitting if both control points are close enough to the line.-
86 // To make the algorithm faster we use the manhattan length of the line.-
87-
88 QPolygonF polygon;-
89 polygon.append(QPointF(x1, y1));-
90 addToPolygon(&polygon, bezier_flattening_threshold);-
91 return polygon;
never executed: return polygon;
0
92}-
93-
94QBezier QBezier::mapBy(const QTransform &transform) const-
95{-
96 return QBezier::fromPoints(transform.map(pt1()), transform.map(pt2()), transform.map(pt3()), transform.map(pt4()));
never executed: return QBezier::fromPoints(transform.map(pt1()), transform.map(pt2()), transform.map(pt3()), transform.map(pt4()));
0
97}-
98-
99QBezier QBezier::getSubRange(qreal t0, qreal t1) const-
100{-
101 QBezier result;-
102 QBezier temp;-
103-
104 // cut at t1-
105 if (qFuzzyIsNull(t1 - qreal(1.))) {
qFuzzyIsNull(t1 - qreal(1.))Description
TRUEnever evaluated
FALSEnever evaluated
0
106 result = *this;-
107 } else {
never executed: end of block
0
108 temp = *this;-
109 temp.parameterSplitLeft(t1, &result);-
110 }
never executed: end of block
0
111-
112 // cut at t0-
113 if (!qFuzzyIsNull(t0))
!qFuzzyIsNull(t0)Description
TRUEnever evaluated
FALSEnever evaluated
0
114 result.parameterSplitLeft(t0 / t1, &temp);
never executed: result.parameterSplitLeft(t0 / t1, &temp);
0
115-
116 return result;
never executed: return result;
0
117}-
118-
119void QBezier::addToPolygon(QPolygonF *polygon, qreal bezier_flattening_threshold) const-
120{-
121 QBezier beziers[10];-
122 int levels[10];-
123 beziers[0] = *this;-
124 levels[0] = 9;-
125 QBezier *b = beziers;-
126 int *lvl = levels;-
127-
128 while (b >= beziers) {
b >= beziersDescription
TRUEnever evaluated
FALSEnever evaluated
0
129 // check if we can pop the top bezier curve from the stack-
130 qreal y4y1 = b->y4 - b->y1;-
131 qreal x4x1 = b->x4 - b->x1;-
132 qreal l = qAbs(x4x1) + qAbs(y4y1);-
133 qreal d;-
134 if (l > 1.) {
l > 1.Description
TRUEnever evaluated
FALSEnever evaluated
0
135 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )-
136 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );-
137 } else {
never executed: end of block
0
138 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +-
139 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);-
140 l = 1.;-
141 }
never executed: end of block
0
142 if (d < bezier_flattening_threshold*l || *lvl == 0) {
d < bezier_fla...ng_threshold*lDescription
TRUEnever evaluated
FALSEnever evaluated
*lvl == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
143 // good enough, we pop it off and add the endpoint-
144 polygon->append(QPointF(b->x4, b->y4));-
145 --b;-
146 --lvl;-
147 } else {
never executed: end of block
0
148 // split, second half of the polygon goes lower into the stack-
149 b->split(b+1, b);-
150 lvl[1] = --lvl[0];-
151 ++b;-
152 ++lvl;-
153 }
never executed: end of block
0
154 }-
155}
never executed: end of block
0
156-
157void QBezier::addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold) const-
158{-
159 QBezier beziers[10];-
160 int levels[10];-
161 beziers[0] = *this;-
162 levels[0] = 9;-
163 QBezier *b = beziers;-
164 int *lvl = levels;-
165-
166 while (b >= beziers) {
b >= beziersDescription
TRUEnever evaluated
FALSEnever evaluated
0
167 // check if we can pop the top bezier curve from the stack-
168 qreal y4y1 = b->y4 - b->y1;-
169 qreal x4x1 = b->x4 - b->x1;-
170 qreal l = qAbs(x4x1) + qAbs(y4y1);-
171 qreal d;-
172 if (l > 1.) {
l > 1.Description
TRUEnever evaluated
FALSEnever evaluated
0
173 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )-
174 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );-
175 } else {
never executed: end of block
0
176 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +-
177 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);-
178 l = 1.;-
179 }
never executed: end of block
0
180 if (d < bezier_flattening_threshold*l || *lvl == 0) {
d < bezier_fla...ng_threshold*lDescription
TRUEnever evaluated
FALSEnever evaluated
*lvl == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
181 // good enough, we pop it off and add the endpoint-
182 polygon.add(QPointF(b->x4, b->y4));-
183 --b;-
184 --lvl;-
185 } else {
never executed: end of block
0
186 // split, second half of the polygon goes lower into the stack-
187 b->split(b+1, b);-
188 lvl[1] = --lvl[0];-
189 ++b;-
190 ++lvl;-
191 }
never executed: end of block
0
192 }-
193}
never executed: end of block
0
194-
195QRectF QBezier::bounds() const-
196{-
197 qreal xmin = x1;-
198 qreal xmax = x1;-
199 if (x2 < xmin)
x2 < xminDescription
TRUEnever evaluated
FALSEnever evaluated
0
200 xmin = x2;
never executed: xmin = x2;
0
201 else if (x2 > xmax)
x2 > xmaxDescription
TRUEnever evaluated
FALSEnever evaluated
0
202 xmax = x2;
never executed: xmax = x2;
0
203 if (x3 < xmin)
x3 < xminDescription
TRUEnever evaluated
FALSEnever evaluated
0
204 xmin = x3;
never executed: xmin = x3;
0
205 else if (x3 > xmax)
x3 > xmaxDescription
TRUEnever evaluated
FALSEnever evaluated
0
206 xmax = x3;
never executed: xmax = x3;
0
207 if (x4 < xmin)
x4 < xminDescription
TRUEnever evaluated
FALSEnever evaluated
0
208 xmin = x4;
never executed: xmin = x4;
0
209 else if (x4 > xmax)
x4 > xmaxDescription
TRUEnever evaluated
FALSEnever evaluated
0
210 xmax = x4;
never executed: xmax = x4;
0
211-
212 qreal ymin = y1;-
213 qreal ymax = y1;-
214 if (y2 < ymin)
y2 < yminDescription
TRUEnever evaluated
FALSEnever evaluated
0
215 ymin = y2;
never executed: ymin = y2;
0
216 else if (y2 > ymax)
y2 > ymaxDescription
TRUEnever evaluated
FALSEnever evaluated
0
217 ymax = y2;
never executed: ymax = y2;
0
218 if (y3 < ymin)
y3 < yminDescription
TRUEnever evaluated
FALSEnever evaluated
0
219 ymin = y3;
never executed: ymin = y3;
0
220 else if (y3 > ymax)
y3 > ymaxDescription
TRUEnever evaluated
FALSEnever evaluated
0
221 ymax = y3;
never executed: ymax = y3;
0
222 if (y4 < ymin)
y4 < yminDescription
TRUEnever evaluated
FALSEnever evaluated
0
223 ymin = y4;
never executed: ymin = y4;
0
224 else if (y4 > ymax)
y4 > ymaxDescription
TRUEnever evaluated
FALSEnever evaluated
0
225 ymax = y4;
never executed: ymax = y4;
0
226 return QRectF(xmin, ymin, xmax-xmin, ymax-ymin);
never executed: return QRectF(xmin, ymin, xmax-xmin, ymax-ymin);
0
227}-
228-
229-
230enum ShiftResult {-
231 Ok,-
232 Discard,-
233 Split,-
234 Circle-
235};-
236-
237static ShiftResult good_offset(const QBezier *b1, const QBezier *b2, qreal offset, qreal threshold)-
238{-
239 const qreal o2 = offset*offset;-
240 const qreal max_dist_line = threshold*offset*offset;-
241 const qreal max_dist_normal = threshold*offset;-
242 const qreal spacing = qreal(0.25);-
243 for (qreal i = spacing; i < qreal(0.99); i += spacing) {
i < qreal(0.99)Description
TRUEnever evaluated
FALSEnever evaluated
0
244 QPointF p1 = b1->pointAt(i);-
245 QPointF p2 = b2->pointAt(i);-
246 qreal d = (p1.x() - p2.x())*(p1.x() - p2.x()) + (p1.y() - p2.y())*(p1.y() - p2.y());-
247 if (qAbs(d - o2) > max_dist_line)
qAbs(d - o2) > max_dist_lineDescription
TRUEnever evaluated
FALSEnever evaluated
0
248 return Split;
never executed: return Split;
0
249-
250 QPointF normalPoint = b1->normalVector(i);-
251 qreal l = qAbs(normalPoint.x()) + qAbs(normalPoint.y());-
252 if (l != qreal(0.0)) {
l != qreal(0.0)Description
TRUEnever evaluated
FALSEnever evaluated
0
253 d = qAbs( normalPoint.x()*(p1.y() - p2.y()) - normalPoint.y()*(p1.x() - p2.x()) ) / l;-
254 if (d > max_dist_normal)
d > max_dist_normalDescription
TRUEnever evaluated
FALSEnever evaluated
0
255 return Split;
never executed: return Split;
0
256 }
never executed: end of block
0
257 }
never executed: end of block
0
258 return Ok;
never executed: return Ok;
0
259}-
260-
261static ShiftResult shift(const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)-
262{-
263 int map[4];-
264 bool p1_p2_equal = (orig->x1 == orig->x2 && orig->y1 == orig->y2);
orig->x1 == orig->x2Description
TRUEnever evaluated
FALSEnever evaluated
orig->y1 == orig->y2Description
TRUEnever evaluated
FALSEnever evaluated
0
265 bool p2_p3_equal = (orig->x2 == orig->x3 && orig->y2 == orig->y3);
orig->x2 == orig->x3Description
TRUEnever evaluated
FALSEnever evaluated
orig->y2 == orig->y3Description
TRUEnever evaluated
FALSEnever evaluated
0
266 bool p3_p4_equal = (orig->x3 == orig->x4 && orig->y3 == orig->y4);
orig->x3 == orig->x4Description
TRUEnever evaluated
FALSEnever evaluated
orig->y3 == orig->y4Description
TRUEnever evaluated
FALSEnever evaluated
0
267-
268 QPointF points[4];-
269 int np = 0;-
270 points[np] = QPointF(orig->x1, orig->y1);-
271 map[0] = 0;-
272 ++np;-
273 if (!p1_p2_equal) {
!p1_p2_equalDescription
TRUEnever evaluated
FALSEnever evaluated
0
274 points[np] = QPointF(orig->x2, orig->y2);-
275 ++np;-
276 }
never executed: end of block
0
277 map[1] = np - 1;-
278 if (!p2_p3_equal) {
!p2_p3_equalDescription
TRUEnever evaluated
FALSEnever evaluated
0
279 points[np] = QPointF(orig->x3, orig->y3);-
280 ++np;-
281 }
never executed: end of block
0
282 map[2] = np - 1;-
283 if (!p3_p4_equal) {
!p3_p4_equalDescription
TRUEnever evaluated
FALSEnever evaluated
0
284 points[np] = QPointF(orig->x4, orig->y4);-
285 ++np;-
286 }
never executed: end of block
0
287 map[3] = np - 1;-
288 if (np == 1)
np == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
289 return Discard;
never executed: return Discard;
0
290-
291 QRectF b = orig->bounds();-
292 if (np == 4 && b.width() < .1*offset && b.height() < .1*offset) {
np == 4Description
TRUEnever evaluated
FALSEnever evaluated
b.width() < .1*offsetDescription
TRUEnever evaluated
FALSEnever evaluated
b.height() < .1*offsetDescription
TRUEnever evaluated
FALSEnever evaluated
0
293 qreal l = (orig->x1 - orig->x2)*(orig->x1 - orig->x2) +-
294 (orig->y1 - orig->y2)*(orig->y1 - orig->y2) *-
295 (orig->x3 - orig->x4)*(orig->x3 - orig->x4) +-
296 (orig->y3 - orig->y4)*(orig->y3 - orig->y4);-
297 qreal dot = (orig->x1 - orig->x2)*(orig->x3 - orig->x4) +-
298 (orig->y1 - orig->y2)*(orig->y3 - orig->y4);-
299 if (dot < 0 && dot*dot < 0.8*l)
dot < 0Description
TRUEnever evaluated
FALSEnever evaluated
dot*dot < 0.8*lDescription
TRUEnever evaluated
FALSEnever evaluated
0
300 // the points are close and reverse dirction. Approximate the whole-
301 // thing by a semi circle-
302 return Circle;
never executed: return Circle;
0
303 }
never executed: end of block
0
304-
305 QPointF points_shifted[4];-
306-
307 QLineF prev = QLineF(QPointF(), points[1] - points[0]);-
308 QPointF prev_normal = prev.normalVector().unitVector().p2();-
309-
310 points_shifted[0] = points[0] + offset * prev_normal;-
311-
312 for (int i = 1; i < np - 1; ++i) {
i < np - 1Description
TRUEnever evaluated
FALSEnever evaluated
0
313 QLineF next = QLineF(QPointF(), points[i + 1] - points[i]);-
314 QPointF next_normal = next.normalVector().unitVector().p2();-
315-
316 QPointF normal_sum = prev_normal + next_normal;-
317-
318 qreal r = qreal(1.0) + prev_normal.x() * next_normal.x()-
319 + prev_normal.y() * next_normal.y();-
320-
321 if (qFuzzyIsNull(r)) {
qFuzzyIsNull(r)Description
TRUEnever evaluated
FALSEnever evaluated
0
322 points_shifted[i] = points[i] + offset * prev_normal;-
323 } else {
never executed: end of block
0
324 qreal k = offset / r;-
325 points_shifted[i] = points[i] + k * normal_sum;-
326 }
never executed: end of block
0
327-
328 prev_normal = next_normal;-
329 }
never executed: end of block
0
330-
331 points_shifted[np - 1] = points[np - 1] + offset * prev_normal;-
332-
333 *shifted = QBezier::fromPoints(points_shifted[map[0]], points_shifted[map[1]],-
334 points_shifted[map[2]], points_shifted[map[3]]);-
335-
336 return good_offset(orig, shifted, offset, threshold);
never executed: return good_offset(orig, shifted, offset, threshold);
0
337}-
338-
339// This value is used to determine the length of control point vectors-
340// when approximating arc segments as curves. The factor is multiplied-
341// with the radius of the circle.-
342#define KAPPA qreal(0.5522847498)-
343-
344-
345static bool addCircle(const QBezier *b, qreal offset, QBezier *o)-
346{-
347 QPointF normals[3];-
348-
349 normals[0] = QPointF(b->y2 - b->y1, b->x1 - b->x2);-
350 qreal dist = qSqrt(normals[0].x()*normals[0].x() + normals[0].y()*normals[0].y());-
351 if (qFuzzyIsNull(dist))
qFuzzyIsNull(dist)Description
TRUEnever evaluated
FALSEnever evaluated
0
352 return false;
never executed: return false;
0
353 normals[0] /= dist;-
354 normals[2] = QPointF(b->y4 - b->y3, b->x3 - b->x4);-
355 dist = qSqrt(normals[2].x()*normals[2].x() + normals[2].y()*normals[2].y());-
356 if (qFuzzyIsNull(dist))
qFuzzyIsNull(dist)Description
TRUEnever evaluated
FALSEnever evaluated
0
357 return false;
never executed: return false;
0
358 normals[2] /= dist;-
359-
360 normals[1] = QPointF(b->x1 - b->x2 - b->x3 + b->x4, b->y1 - b->y2 - b->y3 + b->y4);-
361 normals[1] /= -1*qSqrt(normals[1].x()*normals[1].x() + normals[1].y()*normals[1].y());-
362-
363 qreal angles[2];-
364 qreal sign = 1.;-
365 for (int i = 0; i < 2; ++i) {
i < 2Description
TRUEnever evaluated
FALSEnever evaluated
0
366 qreal cos_a = normals[i].x()*normals[i+1].x() + normals[i].y()*normals[i+1].y();-
367 if (cos_a > 1.)
cos_a > 1.Description
TRUEnever evaluated
FALSEnever evaluated
0
368 cos_a = 1.;
never executed: cos_a = 1.;
0
369 if (cos_a < -1.)
cos_a < -1.Description
TRUEnever evaluated
FALSEnever evaluated
0
370 cos_a = -1;
never executed: cos_a = -1;
0
371 angles[i] = qAcos(cos_a) * qreal(M_1_PI);-
372 }
never executed: end of block
0
373-
374 if (angles[0] + angles[1] > 1.) {
angles[0] + angles[1] > 1.Description
TRUEnever evaluated
FALSEnever evaluated
0
375 // more than 180 degrees-
376 normals[1] = -normals[1];-
377 angles[0] = 1. - angles[0];-
378 angles[1] = 1. - angles[1];-
379 sign = -1.;-
380-
381 }
never executed: end of block
0
382-
383 QPointF circle[3];-
384 circle[0] = QPointF(b->x1, b->y1) + normals[0]*offset;-
385 circle[1] = QPointF(qreal(0.5)*(b->x1 + b->x4), qreal(0.5)*(b->y1 + b->y4)) + normals[1]*offset;-
386 circle[2] = QPointF(b->x4, b->y4) + normals[2]*offset;-
387-
388 for (int i = 0; i < 2; ++i) {
i < 2Description
TRUEnever evaluated
FALSEnever evaluated
0
389 qreal kappa = qreal(2.0) * KAPPA * sign * offset * angles[i];-
390-
391 o->x1 = circle[i].x();-
392 o->y1 = circle[i].y();-
393 o->x2 = circle[i].x() - normals[i].y()*kappa;-
394 o->y2 = circle[i].y() + normals[i].x()*kappa;-
395 o->x3 = circle[i+1].x() + normals[i+1].y()*kappa;-
396 o->y3 = circle[i+1].y() - normals[i+1].x()*kappa;-
397 o->x4 = circle[i+1].x();-
398 o->y4 = circle[i+1].y();-
399-
400 ++o;-
401 }
never executed: end of block
0
402 return true;
never executed: return true;
0
403}-
404-
405int QBezier::shifted(QBezier *curveSegments, int maxSegments, qreal offset, float threshold) const-
406{-
407 Q_ASSERT(curveSegments);-
408 Q_ASSERT(maxSegments > 0);-
409-
410 if (qFuzzyCompare(x1, x2) && qFuzzyCompare(x1, x3) && qFuzzyCompare(x1, x4) &&
qFuzzyCompare(x1, x2)Description
TRUEnever evaluated
FALSEnever evaluated
qFuzzyCompare(x1, x3)Description
TRUEnever evaluated
FALSEnever evaluated
qFuzzyCompare(x1, x4)Description
TRUEnever evaluated
FALSEnever evaluated
0
411 qFuzzyCompare(y1, y2) && qFuzzyCompare(y1, y3) && qFuzzyCompare(y1, y4))
qFuzzyCompare(y1, y2)Description
TRUEnever evaluated
FALSEnever evaluated
qFuzzyCompare(y1, y3)Description
TRUEnever evaluated
FALSEnever evaluated
qFuzzyCompare(y1, y4)Description
TRUEnever evaluated
FALSEnever evaluated
0
412 return 0;
never executed: return 0;
0
413-
414 --maxSegments;-
415 QBezier beziers[10];-
416redo:
code before this statement never executed: redo:
0
417 beziers[0] = *this;-
418 QBezier *b = beziers;-
419 QBezier *o = curveSegments;-
420-
421 while (b >= beziers) {
b >= beziersDescription
TRUEnever evaluated
FALSEnever evaluated
0
422 int stack_segments = b - beziers + 1;-
423 if ((stack_segments == 10) || (o - curveSegments == maxSegments - stack_segments)) {
(stack_segments == 10)Description
TRUEnever evaluated
FALSEnever evaluated
(o - curveSegm...tack_segments)Description
TRUEnever evaluated
FALSEnever evaluated
0
424 threshold *= qreal(1.5);-
425 if (threshold > qreal(2.0))
threshold > qreal(2.0)Description
TRUEnever evaluated
FALSEnever evaluated
0
426 goto give_up;
never executed: goto give_up;
0
427 goto redo;
never executed: goto redo;
0
428 }-
429 ShiftResult res = shift(b, o, offset, threshold);-
430 if (res == Discard) {
res == DiscardDescription
TRUEnever evaluated
FALSEnever evaluated
0
431 --b;-
432 } else if (res == Ok) {
never executed: end of block
res == OkDescription
TRUEnever evaluated
FALSEnever evaluated
0
433 ++o;-
434 --b;-
435 continue;
never executed: continue;
0
436 } else if (res == Circle && maxSegments - (o - curveSegments) >= 2) {
res == CircleDescription
TRUEnever evaluated
FALSEnever evaluated
maxSegments - ...Segments) >= 2Description
TRUEnever evaluated
FALSEnever evaluated
0
437 // add semi circle-
438 if (addCircle(b, offset, o))
addCircle(b, offset, o)Description
TRUEnever evaluated
FALSEnever evaluated
0
439 o += 2;
never executed: o += 2;
0
440 --b;-
441 } else {
never executed: end of block
0
442 b->split(b+1, b);-
443 ++b;-
444 }
never executed: end of block
0
445 }-
446-
447give_up:
code before this statement never executed: give_up:
0
448 while (b >= beziers) {
b >= beziersDescription
TRUEnever evaluated
FALSEnever evaluated
0
449 ShiftResult res = shift(b, o, offset, threshold);-
450-
451 // if res isn't Ok or Split then *o is undefined-
452 if (res == Ok || res == Split)
res == OkDescription
TRUEnever evaluated
FALSEnever evaluated
res == SplitDescription
TRUEnever evaluated
FALSEnever evaluated
0
453 ++o;
never executed: ++o;
0
454-
455 --b;-
456 }
never executed: end of block
0
457-
458 Q_ASSERT(o - curveSegments <= maxSegments);-
459 return o - curveSegments;
never executed: return o - curveSegments;
0
460}-
461-
462#ifdef QDEBUG_BEZIER-
463static QDebug operator<<(QDebug dbg, const QBezier &bz)-
464{-
465 dbg << '[' << bz.x1<< ", " << bz.y1 << "], "-
466 << '[' << bz.x2 <<", " << bz.y2 << "], "-
467 << '[' << bz.x3 <<", " << bz.y3 << "], "-
468 << '[' << bz.x4 <<", " << bz.y4 << ']';-
469 return dbg;-
470}-
471#endif-
472-
473qreal QBezier::length(qreal error) const-
474{-
475 qreal length = qreal(0.0);-
476-
477 addIfClose(&length, error);-
478-
479 return length;
never executed: return length;
0
480}-
481-
482void QBezier::addIfClose(qreal *length, qreal error) const-
483{-
484 QBezier left, right; /* bez poly splits */-
485-
486 qreal len = qreal(0.0); /* arc length */-
487 qreal chord; /* chord length */-
488-
489 len = len + QLineF(QPointF(x1, y1),QPointF(x2, y2)).length();-
490 len = len + QLineF(QPointF(x2, y2),QPointF(x3, y3)).length();-
491 len = len + QLineF(QPointF(x3, y3),QPointF(x4, y4)).length();-
492-
493 chord = QLineF(QPointF(x1, y1),QPointF(x4, y4)).length();-
494-
495 if((len-chord) > error) {
(len-chord) > errorDescription
TRUEnever evaluated
FALSEnever evaluated
0
496 split(&left, &right); /* split in two */-
497 left.addIfClose(length, error); /* try left side */-
498 right.addIfClose(length, error); /* try right side */-
499 return;
never executed: return;
0
500 }-
501-
502 *length = *length + len;-
503-
504 return;
never executed: return;
0
505}-
506-
507qreal QBezier::tForY(qreal t0, qreal t1, qreal y) const-
508{-
509 qreal py0 = pointAt(t0).y();-
510 qreal py1 = pointAt(t1).y();-
511-
512 if (py0 > py1) {
py0 > py1Description
TRUEnever evaluated
FALSEnever evaluated
0
513 qSwap(py0, py1);-
514 qSwap(t0, t1);-
515 }
never executed: end of block
0
516-
517 Q_ASSERT(py0 <= py1);-
518-
519 if (py0 >= y)
py0 >= yDescription
TRUEnever evaluated
FALSEnever evaluated
0
520 return t0;
never executed: return t0;
0
521 else if (py1 <= y)
py1 <= yDescription
TRUEnever evaluated
FALSEnever evaluated
0
522 return t1;
never executed: return t1;
0
523-
524 Q_ASSERT(py0 < y && y < py1);-
525-
526 qreal lt = t0;-
527 qreal dt;-
528 do {-
529 qreal t = qreal(0.5) * (t0 + t1);-
530-
531 qreal a, b, c, d;-
532 QBezier::coefficients(t, a, b, c, d);-
533 qreal yt = a * y1 + b * y2 + c * y3 + d * y4;-
534-
535 if (yt < y) {
yt < yDescription
TRUEnever evaluated
FALSEnever evaluated
0
536 t0 = t;-
537 py0 = yt;-
538 } else {
never executed: end of block
0
539 t1 = t;-
540 py1 = yt;-
541 }
never executed: end of block
0
542 dt = lt - t;-
543 lt = t;-
544 } while (qAbs(dt) > qreal(1e-7));
never executed: end of block
qAbs(dt) > qreal(1e-7)Description
TRUEnever evaluated
FALSEnever evaluated
0
545-
546 return t0;
never executed: return t0;
0
547}-
548-
549int QBezier::stationaryYPoints(qreal &t0, qreal &t1) const-
550{-
551 // y(t) = (1 - t)^3 * y1 + 3 * (1 - t)^2 * t * y2 + 3 * (1 - t) * t^2 * y3 + t^3 * y4-
552 // y'(t) = 3 * (-(1-2t+t^2) * y1 + (1 - 4 * t + 3 * t^2) * y2 + (2 * t - 3 * t^2) * y3 + t^2 * y4)-
553 // y'(t) = 3 * ((-y1 + 3 * y2 - 3 * y3 + y4)t^2 + (2 * y1 - 4 * y2 + 2 * y3)t + (-y1 + y2))-
554-
555 const qreal a = -y1 + 3 * y2 - 3 * y3 + y4;-
556 const qreal b = 2 * y1 - 4 * y2 + 2 * y3;-
557 const qreal c = -y1 + y2;-
558-
559 if (qFuzzyIsNull(a)) {
qFuzzyIsNull(a)Description
TRUEnever evaluated
FALSEnever evaluated
0
560 if (qFuzzyIsNull(b))
qFuzzyIsNull(b)Description
TRUEnever evaluated
FALSEnever evaluated
0
561 return 0;
never executed: return 0;
0
562-
563 t0 = -c / b;-
564 return t0 > 0 && t0 < 1;
never executed: return t0 > 0 && t0 < 1;
0
565 }-
566-
567 qreal reciprocal = b * b - 4 * a * c;-
568-
569 if (qFuzzyIsNull(reciprocal)) {
qFuzzyIsNull(reciprocal)Description
TRUEnever evaluated
FALSEnever evaluated
0
570 t0 = -b / (2 * a);-
571 return t0 > 0 && t0 < 1;
never executed: return t0 > 0 && t0 < 1;
0
572 } else if (reciprocal > 0) {
reciprocal > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
573 qreal temp = qSqrt(reciprocal);-
574-
575 t0 = (-b - temp)/(2*a);-
576 t1 = (-b + temp)/(2*a);-
577-
578 if (t1 < t0)
t1 < t0Description
TRUEnever evaluated
FALSEnever evaluated
0
579 qSwap(t0, t1);
never executed: qSwap(t0, t1);
0
580-
581 int count = 0;-
582 qreal t[2] = { 0, 1 };-
583-
584 if (t0 > 0 && t0 < 1)
t0 > 0Description
TRUEnever evaluated
FALSEnever evaluated
t0 < 1Description
TRUEnever evaluated
FALSEnever evaluated
0
585 t[count++] = t0;
never executed: t[count++] = t0;
0
586 if (t1 > 0 && t1 < 1)
t1 > 0Description
TRUEnever evaluated
FALSEnever evaluated
t1 < 1Description
TRUEnever evaluated
FALSEnever evaluated
0
587 t[count++] = t1;
never executed: t[count++] = t1;
0
588-
589 t0 = t[0];-
590 t1 = t[1];-
591-
592 return count;
never executed: return count;
0
593 }-
594-
595 return 0;
never executed: return 0;
0
596}-
597-
598qreal QBezier::tAtLength(qreal l) const-
599{-
600 qreal len = length();-
601 qreal t = qreal(1.0);-
602 const qreal error = qreal(0.01);-
603 if (l > len || qFuzzyCompare(l, len))
l > lenDescription
TRUEnever evaluated
FALSEnever evaluated
qFuzzyCompare(l, len)Description
TRUEnever evaluated
FALSEnever evaluated
0
604 return t;
never executed: return t;
0
605-
606 t *= qreal(0.5);-
607 //int iters = 0;-
608 //qDebug()<<"LEN is "<<l<<len;-
609 qreal lastBigger = qreal(1.0);-
610 while (1) {-
611 //qDebug()<<"\tt is "<<t;-
612 QBezier right = *this;-
613 QBezier left;-
614 right.parameterSplitLeft(t, &left);-
615 qreal lLen = left.length();-
616 if (qAbs(lLen - l) < error)
qAbs(lLen - l) < errorDescription
TRUEnever evaluated
FALSEnever evaluated
0
617 break;
never executed: break;
0
618-
619 if (lLen < l) {
lLen < lDescription
TRUEnever evaluated
FALSEnever evaluated
0
620 t += (lastBigger - t) * qreal(0.5);-
621 } else {
never executed: end of block
0
622 lastBigger = t;-
623 t -= t * qreal(0.5);-
624 }
never executed: end of block
0
625 //++iters;-
626 }-
627 //qDebug()<<"number of iters is "<<iters;-
628 return t;
never executed: return t;
0
629}-
630-
631QBezier QBezier::bezierOnInterval(qreal t0, qreal t1) const-
632{-
633 if (t0 == 0 && t1 == 1)
t0 == 0Description
TRUEnever evaluated
FALSEnever evaluated
t1 == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
634 return *this;
never executed: return *this;
0
635-
636 QBezier bezier = *this;-
637-
638 QBezier result;-
639 bezier.parameterSplitLeft(t0, &result);-
640 qreal trueT = (t1-t0)/(1-t0);-
641 bezier.parameterSplitLeft(trueT, &result);-
642-
643 return result;
never executed: return result;
0
644}-
645-
646QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9