Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbezier.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qbezier_p.h"
6#include <qdebug.h>
7#include <qline.h>
8#include <qmath.h>
9#include <qpolygon.h>
10
11#include <private/qnumeric_p.h>
12
13#include <tuple> // for std::tie()
14
16
17//#define QDEBUG_BEZIER
18
19/*!
20 \internal
21*/
22QPolygonF QBezier::toPolygon(qreal bezier_flattening_threshold) const
23{
24 // flattening is done by splitting the bezier until we can replace the segment by a straight
25 // line. We split further until the control points are close enough to the line connecting the
26 // boundary points.
27 //
28 // the Distance of a point p from a line given by the points (a,b) is given by:
29 //
30 // d = abs( (bx - ax)(ay - py) - (by - ay)(ax - px) ) / line_length
31 //
32 // We can stop splitting if both control points are close enough to the line.
33 // To make the algorithm faster we use the manhattan length of the line.
34
35 QPolygonF polygon;
36 polygon.append(QPointF(x1, y1));
37 addToPolygon(&polygon, bezier_flattening_threshold);
38 return polygon;
39}
40
41QBezier QBezier::mapBy(const QTransform &transform) const
42{
43 return QBezier::fromPoints(transform.map(pt1()), transform.map(pt2()), transform.map(pt3()), transform.map(pt4()));
44}
45
46QBezier QBezier::getSubRange(qreal t0, qreal t1) const
47{
48 QBezier result;
49 QBezier temp;
50
51 // cut at t1
52 if (qFuzzyIsNull(t1 - qreal(1.))) {
53 result = *this;
54 } else {
55 temp = *this;
56 temp.parameterSplitLeft(t1, &result);
57 }
58
59 // cut at t0
60 if (!qFuzzyIsNull(t0))
61 result.parameterSplitLeft(t0 / t1, &temp);
62
63 return result;
64}
65
66void QBezier::addToPolygon(QPolygonF *polygon, qreal bezier_flattening_threshold) const
67{
68 QBezier beziers[10];
69 int levels[10];
70 beziers[0] = *this;
71 levels[0] = 9;
72 int top = 0;
73
74 while (top >= 0) {
75 QBezier *b = &beziers[top];
76 // check if we can pop the top bezier curve from the stack
77 qreal y4y1 = b->y4 - b->y1;
78 qreal x4x1 = b->x4 - b->x1;
79 qreal l = qAbs(x4x1) + qAbs(y4y1);
80 qreal d;
81 if (l > 1.) {
82 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )
83 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );
84 } else {
85 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
86 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
87 l = 1.;
88 }
89 if (d < bezier_flattening_threshold * l || levels[top] == 0) {
90 // good enough, we pop it off and add the endpoint
91 polygon->append(QPointF(b->x4, b->y4));
92 --top;
93 } else {
94 // split, second half of the polygon goes lower into the stack
95 std::tie(b[1], b[0]) = b->split();
96 levels[top + 1] = --levels[top];
97 ++top;
98 }
99 }
100}
101
102void QBezier::addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold) const
103{
104 QBezier beziers[10];
105 int levels[10];
106 beziers[0] = *this;
107 levels[0] = 9;
108 int top = 0;
109
110 while (top >= 0) {
111 QBezier *b = &beziers[top];
112 // check if we can pop the top bezier curve from the stack
113 qreal y4y1 = b->y4 - b->y1;
114 qreal x4x1 = b->x4 - b->x1;
115 qreal l = qAbs(x4x1) + qAbs(y4y1);
116 qreal d;
117 if (l > 1.) {
118 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )
119 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );
120 } else {
121 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
122 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
123 l = 1.;
124 }
125 if (d < bezier_flattening_threshold * l || levels[top] == 0) {
126 // good enough, we pop it off and add the endpoint
127 polygon.add(QPointF(b->x4, b->y4));
128 --top;
129 } else {
130 // split, second half of the polygon goes lower into the stack
131 std::tie(b[1], b[0]) = b->split();
132 levels[top + 1] = --levels[top];
133 ++top;
134 }
135 }
136}
137
138QRectF QBezier::bounds() const
139{
140 qreal xmin = x1;
141 qreal xmax = x1;
142 if (x2 < xmin)
143 xmin = x2;
144 else if (x2 > xmax)
145 xmax = x2;
146 if (x3 < xmin)
147 xmin = x3;
148 else if (x3 > xmax)
149 xmax = x3;
150 if (x4 < xmin)
151 xmin = x4;
152 else if (x4 > xmax)
153 xmax = x4;
154
155 qreal ymin = y1;
156 qreal ymax = y1;
157 if (y2 < ymin)
158 ymin = y2;
159 else if (y2 > ymax)
160 ymax = y2;
161 if (y3 < ymin)
162 ymin = y3;
163 else if (y3 > ymax)
164 ymax = y3;
165 if (y4 < ymin)
166 ymin = y4;
167 else if (y4 > ymax)
168 ymax = y4;
169 return QRectF(xmin, ymin, xmax-xmin, ymax-ymin);
170}
171
172
179
180static ShiftResult good_offset(const QBezier *b1, const QBezier *b2, qreal offset, qreal threshold)
181{
182 const qreal o2 = offset*offset;
183 const qreal max_dist_line = threshold*offset*offset;
184 const qreal max_dist_normal = threshold*offset;
185 const int divisions = 4;
186 const qreal spacing = qreal(1.0) / divisions;
187 qreal t = spacing;
188 for (int i = 1; i < divisions; ++i, t += spacing) {
189 QPointF p1 = b1->pointAt(t);
190 QPointF p2 = b2->pointAt(t);
191 qreal d = (p1.x() - p2.x())*(p1.x() - p2.x()) + (p1.y() - p2.y())*(p1.y() - p2.y());
192 if (qAbs(d - o2) > max_dist_line)
193 return Split;
194
195 QPointF normalPoint = b1->normalVector(t);
196 qreal l = qAbs(normalPoint.x()) + qAbs(normalPoint.y());
197 if (l != qreal(0.0)) {
198 d = qAbs( normalPoint.x()*(p1.y() - p2.y()) - normalPoint.y()*(p1.x() - p2.x()) ) / l;
199 if (d > max_dist_normal)
200 return Split;
201 }
202 }
203 return Ok;
204}
205
206QT_WARNING_DISABLE_FLOAT_COMPARE
207
208static ShiftResult shift(const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)
209{
210 int map[4];
211 bool p1_p2_equal = qFuzzyCompare(orig->x1, orig->x2) && qFuzzyCompare(orig->y1, orig->y2);
212 bool p2_p3_equal = qFuzzyCompare(orig->x2, orig->x3) && qFuzzyCompare(orig->y2, orig->y3);
213 bool p3_p4_equal = qFuzzyCompare(orig->x3, orig->x4) && qFuzzyCompare(orig->y3, orig->y4);
214
215 QPointF points[4];
216 int np = 0;
217 points[np] = QPointF(orig->x1, orig->y1);
218 map[0] = 0;
219 ++np;
220 if (!p1_p2_equal) {
221 points[np] = QPointF(orig->x2, orig->y2);
222 ++np;
223 }
224 map[1] = np - 1;
225 if (!p2_p3_equal) {
226 points[np] = QPointF(orig->x3, orig->y3);
227 ++np;
228 }
229 map[2] = np - 1;
230 if (!p3_p4_equal) {
231 points[np] = QPointF(orig->x4, orig->y4);
232 ++np;
233 }
234 map[3] = np - 1;
235 if (np == 1)
236 return Discard;
237
238 QRectF b = orig->bounds();
239 if (np == 4 && b.width() < .1*offset && b.height() < .1*offset) {
240 qreal l = (orig->x1 - orig->x2)*(orig->x1 - orig->x2) +
241 (orig->y1 - orig->y2)*(orig->y1 - orig->y2) *
242 (orig->x3 - orig->x4)*(orig->x3 - orig->x4) +
243 (orig->y3 - orig->y4)*(orig->y3 - orig->y4);
244 qreal dot = (orig->x1 - orig->x2)*(orig->x3 - orig->x4) +
245 (orig->y1 - orig->y2)*(orig->y3 - orig->y4);
246 if (dot < 0 && dot*dot < 0.8*l)
247 // the points are close and reverse dirction. Approximate the whole
248 // thing by a semi circle
249 return Circle;
250 }
251
252 QPointF points_shifted[4];
253
254 QLineF prev = QLineF(QPointF(), points[1] - points[0]);
255 if (!prev.length())
256 return Discard;
257 QPointF prev_normal = prev.normalVector().unitVector().p2();
258
259 points_shifted[0] = points[0] + offset * prev_normal;
260
261 for (int i = 1; i < np - 1; ++i) {
262 QLineF next = QLineF(QPointF(), points[i + 1] - points[i]);
263 QPointF next_normal = next.normalVector().unitVector().p2();
264
265 QPointF normal_sum = prev_normal + next_normal;
266
267 qreal r = qreal(1.0) + prev_normal.x() * next_normal.x()
268 + prev_normal.y() * next_normal.y();
269
270 if (qFuzzyIsNull(r)) {
271 points_shifted[i] = points[i] + offset * prev_normal;
272 } else {
273 qreal k = offset / r;
274 points_shifted[i] = points[i] + k * normal_sum;
275 }
276
277 prev_normal = next_normal;
278 }
279
280 points_shifted[np - 1] = points[np - 1] + offset * prev_normal;
281
282 *shifted = QBezier::fromPoints(points_shifted[map[0]], points_shifted[map[1]],
283 points_shifted[map[2]], points_shifted[map[3]]);
284
285 if (np > 2)
286 return good_offset(orig, shifted, offset, threshold);
287 return Ok;
288}
289
290// This value is used to determine the length of control point vectors
291// when approximating arc segments as curves. The factor is multiplied
292// with the radius of the circle.
293#define KAPPA qreal(0.5522847498)
294
295
296static bool addCircle(const QBezier *b, qreal offset, QBezier *o)
297{
298 QPointF normals[3];
299
300 normals[0] = QPointF(b->y2 - b->y1, b->x1 - b->x2);
301 qreal dist = qSqrt(normals[0].x()*normals[0].x() + normals[0].y()*normals[0].y());
302 if (qFuzzyIsNull(dist))
303 return false;
304 normals[0] /= dist;
305 normals[2] = QPointF(b->y4 - b->y3, b->x3 - b->x4);
306 dist = qSqrt(normals[2].x()*normals[2].x() + normals[2].y()*normals[2].y());
307 if (qFuzzyIsNull(dist))
308 return false;
309 normals[2] /= dist;
310
311 normals[1] = QPointF(b->x1 - b->x2 - b->x3 + b->x4, b->y1 - b->y2 - b->y3 + b->y4);
312 normals[1] /= -1*qSqrt(normals[1].x()*normals[1].x() + normals[1].y()*normals[1].y());
313
314 qreal angles[2];
315 qreal sign = 1.;
316 for (int i = 0; i < 2; ++i) {
317 qreal cos_a = normals[i].x()*normals[i+1].x() + normals[i].y()*normals[i+1].y();
318 if (cos_a > 1.)
319 cos_a = 1.;
320 if (cos_a < -1.)
321 cos_a = -1;
322 angles[i] = qAcos(cos_a) * qreal(M_1_PI);
323 }
324
325 if (angles[0] + angles[1] > 1.) {
326 // more than 180 degrees
327 normals[1] = -normals[1];
328 angles[0] = 1. - angles[0];
329 angles[1] = 1. - angles[1];
330 sign = -1.;
331
332 }
333
334 QPointF circle[3];
335 circle[0] = QPointF(b->x1, b->y1) + normals[0]*offset;
336 circle[1] = QPointF(qreal(0.5)*(b->x1 + b->x4), qreal(0.5)*(b->y1 + b->y4)) + normals[1]*offset;
337 circle[2] = QPointF(b->x4, b->y4) + normals[2]*offset;
338
339 for (int i = 0; i < 2; ++i) {
340 qreal kappa = qreal(2.0) * KAPPA * sign * offset * angles[i];
341
342 o->x1 = circle[i].x();
343 o->y1 = circle[i].y();
344 o->x2 = circle[i].x() - normals[i].y()*kappa;
345 o->y2 = circle[i].y() + normals[i].x()*kappa;
346 o->x3 = circle[i+1].x() + normals[i+1].y()*kappa;
347 o->y3 = circle[i+1].y() - normals[i+1].x()*kappa;
348 o->x4 = circle[i+1].x();
349 o->y4 = circle[i+1].y();
350
351 ++o;
352 }
353 return true;
354}
355
356int QBezier::shifted(QBezier *curveSegments, int maxSegments, qreal offset, float threshold) const
357{
358 Q_ASSERT(curveSegments);
359 Q_ASSERT(maxSegments > 0);
360
361 if (qFuzzyCompare(x1, x2) && qFuzzyCompare(x1, x3) && qFuzzyCompare(x1, x4) &&
362 qFuzzyCompare(y1, y2) && qFuzzyCompare(y1, y3) && qFuzzyCompare(y1, y4))
363 return 0;
364
365 --maxSegments;
366 QBezier beziers[10];
367redo:
368 beziers[0] = *this;
369 QBezier *b = beziers;
370 QBezier *o = curveSegments;
371
372 while (b >= beziers) {
373 int stack_segments = b - beziers + 1;
374 if ((stack_segments == 10) || (o - curveSegments == maxSegments - stack_segments)) {
375 threshold *= qreal(1.5);
376 if (threshold > qreal(2.0))
377 goto give_up;
378 goto redo;
379 }
380 ShiftResult res = shift(b, o, offset, threshold);
381 if (res == Discard) {
382 --b;
383 } else if (res == Ok) {
384 ++o;
385 --b;
386 } else if (res == Circle && maxSegments - (o - curveSegments) >= 2) {
387 // add semi circle
388 if (addCircle(b, offset, o))
389 o += 2;
390 --b;
391 } else {
392 std::tie(b[1], b[0]) = b->split();
393 ++b;
394 }
395 }
396
397give_up:
398 while (b >= beziers) {
399 ShiftResult res = shift(b, o, offset, threshold);
400
401 // if res isn't Ok or Split then *o is undefined
402 if (res == Ok || res == Split)
403 ++o;
404
405 --b;
406 }
407
408 Q_ASSERT(o - curveSegments <= maxSegments);
409 return o - curveSegments;
410}
411
412#ifdef QDEBUG_BEZIER
413static QDebug operator<<(QDebug dbg, const QBezier &bz)
414{
415 dbg << '[' << bz.x1<< ", " << bz.y1 << "], "
416 << '[' << bz.x2 <<", " << bz.y2 << "], "
417 << '[' << bz.x3 <<", " << bz.y3 << "], "
418 << '[' << bz.x4 <<", " << bz.y4 << ']';
419 return dbg;
420}
421#endif
422
423qreal QBezier::length(qreal error) const
424{
425 qreal length = qreal(0.0);
426
427 addIfClose(&length, error);
428
429 return length;
430}
431
432void QBezier::addIfClose(qreal *length, qreal error) const
433{
434 qreal len = qreal(0.0); /* arc length */
435 qreal chord; /* chord length */
436
437 len = len + QLineF(QPointF(x1, y1),QPointF(x2, y2)).length();
438 len = len + QLineF(QPointF(x2, y2),QPointF(x3, y3)).length();
439 len = len + QLineF(QPointF(x3, y3),QPointF(x4, y4)).length();
440
441 chord = QLineF(QPointF(x1, y1),QPointF(x4, y4)).length();
442
443 if ((len-chord) > error) {
444 const auto halves = split(); /* split in two */
445 halves.first.addIfClose(length, error); /* try left side */
446 halves.second.addIfClose(length, error); /* try right side */
447 return;
448 }
449
450 *length = *length + len;
451
452 return;
453}
454
455qreal QBezier::tForY(qreal t0, qreal t1, qreal y) const
456{
457 qreal py0 = pointAt(t0).y();
458 qreal py1 = pointAt(t1).y();
459
460 if (py0 > py1) {
461 qSwap(py0, py1);
462 qSwap(t0, t1);
463 }
464
465 Q_ASSERT(py0 <= py1);
466
467 if (py0 >= y)
468 return t0;
469 else if (py1 <= y)
470 return t1;
471
472 Q_ASSERT(py0 < y && y < py1);
473
474 qreal lt = t0;
475 qreal dt;
476 do {
477 qreal t = qreal(0.5) * (t0 + t1);
478
479 qreal a, b, c, d;
480 QBezier::coefficients(t, a, b, c, d);
481 qreal yt = a * y1 + b * y2 + c * y3 + d * y4;
482
483 if (yt < y) {
484 t0 = t;
485 py0 = yt;
486 } else {
487 t1 = t;
488 py1 = yt;
489 }
490 dt = lt - t;
491 lt = t;
492 } while (qAbs(dt) > qreal(1e-7));
493
494 return t0;
495}
496
497int QBezier::stationaryYPoints(qreal &t0, qreal &t1) const
498{
499 // y(t) = (1 - t)^3 * y1 + 3 * (1 - t)^2 * t * y2 + 3 * (1 - t) * t^2 * y3 + t^3 * y4
500 // y'(t) = 3 * (-(1-2t+t^2) * y1 + (1 - 4 * t + 3 * t^2) * y2 + (2 * t - 3 * t^2) * y3 + t^2 * y4)
501 // y'(t) = 3 * ((-y1 + 3 * y2 - 3 * y3 + y4)t^2 + (2 * y1 - 4 * y2 + 2 * y3)t + (-y1 + y2))
502
503 const qreal a = -y1 + 3 * y2 - 3 * y3 + y4;
504 const qreal b = 2 * y1 - 4 * y2 + 2 * y3;
505 const qreal c = -y1 + y2;
506
507 if (qFuzzyIsNull(a)) {
508 if (qFuzzyIsNull(b))
509 return 0;
510
511 t0 = -c / b;
512 return t0 > 0 && t0 < 1;
513 }
514
515 qreal reciprocal = b * b - 4 * a * c;
516
517 if (qFuzzyIsNull(reciprocal)) {
518 t0 = -b / (2 * a);
519 return t0 > 0 && t0 < 1;
520 } else if (reciprocal > 0) {
521 qreal temp = qSqrt(reciprocal);
522
523 t0 = (-b - temp)/(2*a);
524 t1 = (-b + temp)/(2*a);
525
526 if (t1 < t0)
527 qSwap(t0, t1);
528
529 int count = 0;
530 qreal t[2] = { 0, 1 };
531
532 if (t0 > 0 && t0 < 1)
533 t[count++] = t0;
534 if (t1 > 0 && t1 < 1)
535 t[count++] = t1;
536
537 t0 = t[0];
538 t1 = t[1];
539
540 return count;
541 }
542
543 return 0;
544}
545
546qreal QBezier::tAtLength(qreal l) const
547{
548 qreal len = length();
549 qreal t = qreal(1.0);
550 const qreal error = qreal(0.01);
551 if (l > len || qFuzzyCompare(l, len))
552 return t;
553
554 t *= qreal(0.5);
555 //int iters = 0;
556 //qDebug()<<"LEN is "<<l<<len;
557 qreal lastBigger = qreal(1.0);
558 while (1) {
559 //qDebug()<<"\tt is "<<t;
560 QBezier right = *this;
561 QBezier left;
562 right.parameterSplitLeft(t, &left);
563 qreal lLen = left.length();
564 if (qAbs(lLen - l) < error)
565 break;
566
567 if (lLen < l) {
568 t += (lastBigger - t) * qreal(0.5);
569 } else {
570 lastBigger = t;
571 t -= t * qreal(0.5);
572 }
573 //++iters;
574 }
575 //qDebug()<<"number of iters is "<<iters;
576 return t;
577}
578
579QBezier QBezier::bezierOnInterval(qreal t0, qreal t1) const
580{
581 if (t0 == 0 && t1 == 1)
582 return *this;
583
584 QBezier bezier = *this;
585
586 QBezier result;
587 bezier.parameterSplitLeft(t0, &result);
588 qreal trueT = (t1-t0)/(1-t0);
589 bezier.parameterSplitLeft(trueT, &result);
590
591 return result;
592}
593
594QT_END_NAMESPACE
Combined button and popup list for selecting options.
ShiftResult
Definition qbezier.cpp:173
@ Ok
Definition qbezier.cpp:174
@ Split
Definition qbezier.cpp:176
@ Circle
Definition qbezier.cpp:177
@ Discard
Definition qbezier.cpp:175
static ShiftResult good_offset(const QBezier *b1, const QBezier *b2, qreal offset, qreal threshold)
Definition qbezier.cpp:180
static bool addCircle(const QBezier *b, qreal offset, QBezier *o)
Definition qbezier.cpp:296
#define KAPPA
Definition qbezier.cpp:293
#define M_1_PI
Definition qmath.h:213