Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquadpath.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 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
4#include "qquadpath_p.h"
5
6#include <private/qsgcurveprocessor_p.h>
7
8#include <QtGui/private/qbezier_p.h>
9#include <QtMath>
10#include <QtCore/QLoggingCategory>
11#include <QtCore/QVarLengthArray>
12
14
16{
17 static bool init = false;
18 const int numSteps = 21;
19 Q_STATIC_ASSERT(numSteps % 2 == 1); // numTries must be odd
20 static qreal t2s[numSteps];
21 static qreal tmts[numSteps];
22 if (!init) {
23 // Precompute bezier factors
24 qreal t = 0.20;
25 const qreal step = (1 - (2 * t)) / (numSteps - 1);
26 for (int i = 0; i < numSteps; i++) {
27 t2s[i] = t * t;
28 tmts[i] = 2 * t * (1 - t);
29 t += step;
30 }
31 init = true;
32 }
33
34 const QPointF midPoint = b.midPoint();
35 auto distForIndex = [&](int i) -> qreal {
36 QPointF qp = (t2s[numSteps - 1 - i] * b.pt1()) + (tmts[i] * qcp) + (t2s[i] * b.pt4());
37 QPointF d = midPoint - qp;
38 return QPointF::dotProduct(d, d);
39 };
40
41 const int halfSteps = (numSteps - 1) / 2;
42 bool foundIt = false;
43 const qreal centerDist = distForIndex(halfSteps);
44 qreal minDist = centerDist;
45 // Search for the minimum in right half
46 for (int i = 0; i < halfSteps; i++) {
47 qreal tDist = distForIndex(halfSteps + 1 + i);
48 if (tDist < minDist) {
49 minDist = tDist;
50 } else {
51 foundIt = (i > 0);
52 break;
53 }
54 }
55 if (!foundIt) {
56 // Search in left half
57 minDist = centerDist;
58 for (int i = 0; i < halfSteps; i++) {
59 qreal tDist = distForIndex(halfSteps - 1 - i);
60 if (tDist < minDist) {
61 minDist = tDist;
62 } else {
63 foundIt = (i > 0);
64 break;
65 }
66 }
67 }
68 return foundIt ? minDist : centerDist;
69}
70
72{
73 const QLineF st = b.startTangent();
74 const QLineF et = b.endTangent();
75 const QPointF midPoint = b.midPoint();
76 bool valid = true;
77 QPointF quadControlPoint;
78 if (st.intersects(et, &quadControlPoint) == QLineF::NoIntersection) {
79 valid = false;
80 } else {
81 // Check if intersection is on wrong side
82 const QPointF bl = b.pt4() - b.pt1();
83 const QPointF ml = midPoint - b.pt1();
84 const QPointF ql = quadControlPoint - b.pt1();
85 qreal cx1 = (ml.x() * bl.y()) - (ml.y() * bl.x());
86 qreal cx2 = (ql.x() * bl.y()) - (ql.y() * bl.x());
87 valid = (std::signbit(cx1) == std::signbit(cx2));
88 }
89 return valid ? quadControlPoint : midPoint;
90}
91
92static int qt_getInflectionPoints(const QBezier &orig, qreal *tpoints)
93{
94 auto isValidRoot = [](qreal r) {
95 return qIsFinite(r) && (r > 0) && (!qFuzzyIsNull(float(r))) && (r < 1)
96 && (!qFuzzyIsNull(float(r - 1)));
97 };
98
99 // normalize so pt1.x,pt1.y,pt4.y == 0
100 QTransform xf;
101 const QLineF l(orig.pt1(), orig.pt4());
102 xf.rotate(l.angle());
103 xf.translate(-orig.pt1().x(), -orig.pt1().y());
104 const QBezier n = orig.mapBy(xf);
105
106 const qreal x2 = n.pt2().x();
107 const qreal x3 = n.pt3().x();
108 const qreal x4 = n.pt4().x();
109 const qreal y2 = n.pt2().y();
110 const qreal y3 = n.pt3().y();
111
112 const qreal p = x3 * y2;
113 const qreal q = x4 * y2;
114 const qreal r = x2 * y3;
115 const qreal s = x4 * y3;
116
117 const qreal a = 18 * ((-3 * p) + (2 * q) + (3 * r) - s);
118 if (qFuzzyIsNull(float(a))) {
119 if (std::signbit(y2) != std::signbit(y3) && qFuzzyCompare(float(x4 - x3), float(x2))) {
120 tpoints[0] = 0.5; // approx
121 return 1;
122 } else if (!a) {
123 return 0;
124 }
125 }
126 const qreal b = 18 * (((3 * p) - q) - (3 * r));
127 const qreal c = 18 * (r - p);
128 const qreal rad = (b * b) - (4 * a * c);
129 if (rad < 0)
130 return 0;
131 const qreal sqr = qSqrt(rad);
132 const qreal root1 = (-b + sqr) / (2 * a);
133 const qreal root2 = (-b - sqr) / (2 * a);
134
135 int res = 0;
136 if (isValidRoot(root1))
137 tpoints[res++] = root1;
138 if (root2 != root1 && isValidRoot(root2))
139 tpoints[res++] = root2;
140
141 if (res == 2 && tpoints[0] > tpoints[1])
142 qSwap(tpoints[0], tpoints[1]);
143
144 return res;
145}
146
147static void qt_addToQuadratics(const QBezier &b, QPolygonF *p, int maxSplits, qreal maxDiff)
148{
150 if (maxSplits <= 0 || qt_scoreQuadratic(b, qcp) < maxDiff) {
151 p->append(qcp);
152 p->append(b.pt4());
153 } else {
154 QBezier rhs = b;
155 QBezier lhs;
156 rhs.parameterSplitLeft(0.5, &lhs);
157 qt_addToQuadratics(lhs, p, maxSplits - 1, maxDiff);
158 qt_addToQuadratics(rhs, p, maxSplits - 1, maxDiff);
159 }
160}
161
162static void qt_toQuadratics(const QBezier &b, QPolygonF *out, qreal errorLimit = 0.01)
163{
164 out->resize(0);
165 out->append(b.pt1());
166
167 {
168 // Shortcut if the cubic is really a quadratic
169 const qreal f = 3.0 / 2.0;
170 const QPointF c1 = b.pt1() + f * (b.pt2() - b.pt1());
171 const QPointF c2 = b.pt4() + f * (b.pt3() - b.pt4());
172 if (c1 == c2) {
173 out->append(c1);
174 out->append(b.pt4());
175 return;
176 }
177 }
178
179 const QRectF cpr = b.bounds();
180 const QPointF dim = cpr.bottomRight() - cpr.topLeft();
181 qreal maxDiff = QPointF::dotProduct(dim, dim) * errorLimit * errorLimit; // maxdistance^2
182
183 qreal infPoints[2];
184 int numInfPoints = qt_getInflectionPoints(b, infPoints);
185 const int maxSubSplits = numInfPoints > 0 ? 2 : 3;
186 qreal t0 = 0;
187 // number of main segments == #inflectionpoints + 1
188 for (int i = 0; i < numInfPoints + 1; i++) {
189 qreal t1 = (i < numInfPoints) ? infPoints[i] : 1;
191 qt_addToQuadratics(segment, out, maxSubSplits, maxDiff);
192 t0 = t1;
193 }
194}
195
197{
198 if (isLine()) {
199 return sp + t * (ep - sp);
200 } else {
201 const float r = 1 - t;
202 return (r * r * sp) + (2 * t * r * cp) + (t * t * ep);
203 }
204}
205
207{
208 if (t0 <= 0 && t1 >= 1)
209 return *this;
210
211 Element part;
212 part.sp = pointAtFraction(t0);
213 part.ep = pointAtFraction(t1);
214
215 if (isLine()) {
216 part.cp = 0.5f * (part.sp + part.ep);
217 part.m_isLine = true;
218 } else {
219 // Split curve right at t0, yields { t0, rcp, endPoint } quad segment
220 const QVector2D rcp = (1 - t0) * controlPoint() + t0 * endPoint();
221 // Split that left at t1, yields { t0, lcp, t1 } quad segment
222 float segmentT = (t1 - t0) / (1 - t0);
223 part.cp = (1 - segmentT) * part.sp + segmentT * rcp;
224 }
225 return part;
226}
227
229 Element swappedElement;
230 swappedElement.ep = sp;
231 swappedElement.cp = cp;
232 swappedElement.sp = ep;
233 swappedElement.m_isLine = m_isLine;
234 return swappedElement;
235}
236
238{
239 // TBD: cache this value if we start using it a lot
240 QVector2D min(qMin(sp.x(), ep.x()), qMin(sp.y(), ep.y()));
241 QVector2D max(qMax(sp.x(), ep.x()), qMax(sp.y(), ep.y()));
242 if (!isLine()) {
243 min = QVector2D(qMin(min.x(), cp.x()), qMin(min.y(), cp.y()));
244 max = QVector2D(qMax(max.x(), cp.x()), qMax(max.y(), cp.y()));
245 }
246 return (max - min).length();
247}
248
249// Returns the number of intersections between element and a horizontal line at y.
250// The t values of max 2 intersection(s) are stored in the fractions array
251int QQuadPath::Element::intersectionsAtY(float y, float *fractions, bool swapXY) const
252{
253 Q_ASSERT(!isLine());
254
255 auto getY = [=](QVector2D p) -> float { return swapXY ? -p.x() : p.y(); };
256
257 const float y0 = getY(startPoint()) - y;
258 const float y1 = getY(controlPoint()) - y;
259 const float y2 = getY(endPoint()) - y;
260
261 int numRoots = 0;
262 const float a = y0 - (2 * y1) + y2;
263 if (a) {
264 const float b = (y1 * y1) - (y0 * y2);
265 if (b >= 0) {
266 const float sqr = qSqrt(b);
267 const float root1 = -(-y0 + y1 + sqr) / a;
268 if (qIsFinite(root1) && root1 >= 0 && root1 <= 1)
269 fractions[numRoots++] = root1;
270 const float root2 = (y0 - y1 + sqr) / a;
271 if (qIsFinite(root2) && root2 != root1 && root2 >= 0 && root2 <= 1)
272 fractions[numRoots++] = root2;
273 }
274 } else if (y1 != y2) {
275 const float root1 = (y2 - (2 * y1)) / (2 * (y2 - y1));
276 if (qIsFinite(root1) && root1 >= 0 && root1 <= 1)
277 fractions[numRoots++] = root1;
278 }
279
280 return numRoots;
281}
282
283static float crossProduct(const QVector2D &sp, const QVector2D &p, const QVector2D &ep)
284{
285 QVector2D v1 = ep - sp;
286 QVector2D v2 = p - sp;
287 return (v2.x() * v1.y()) - (v2.y() * v1.x());
288}
289
291{
292 // Use cross product to compare directions of base vector and vector from start to p
293 return crossProduct(sp, p, ep) >= 0.0f;
294}
295
297{
298 return qFuzzyIsNull(crossProduct(sp, p, ep));
299}
300
301// Assumes sp != ep
303{
304 // epsilon is max length of p-to-baseline relative to length of baseline. So 0.01 means that
305 // the distance from p to the baseline must be less than 1% of the length of the baseline.
306 constexpr float epsilon = 0.01f;
307 QVector2D bv = ep - sp;
308 float bl2 = QVector2D::dotProduct(bv, bv);
309 float t = QVector2D::dotProduct(p - sp, bv) / bl2;
310 QVector2D pv = p - (sp + t * bv);
311 return (QVector2D::dotProduct(pv, pv) / bl2) < (epsilon * epsilon);
312}
313
315{
316 QVector2D line = ep - sp;
318 return sp + qBound(0.0f, t, 1.0f) * line;
319}
320
321// NOTE: it is assumed that subpaths are closed
322bool QQuadPath::contains(const QVector2D &point) const
323{
324 return contains(point, 0, elementCount() - 1);
325}
326
327bool QQuadPath::contains(const QVector2D &point, int fromIndex, int toIndex) const
328{
329 // if (!controlPointRect().contains(pt) : good opt when we add cpr caching
330 // return false;
331
332 int winding_number = 0;
333 for (int ei = fromIndex; ei <= toIndex; ei++) {
334 const Element &e = m_elements.at(ei);
335 int dir = 1;
336 float y1 = e.startPoint().y();
337 float y2 = e.endPoint().y();
338 if (y2 < y1) {
339 qSwap(y1, y2);
340 dir = -1;
341 }
342 if (e.m_isLine) {
343 if (point.y() < y1 || point.y() >= y2 || y1 == y2)
344 continue;
345 const float t = (point.y() - e.startPoint().y()) / (e.endPoint().y() - e.startPoint().y());
346 const float x = e.startPoint().x() + t * (e.endPoint().x() - e.startPoint().x());
347 if (x <= point.x())
348 winding_number += dir;
349 } else {
350 y1 = qMin(y1, e.controlPoint().y());
351 y2 = qMax(y2, e.controlPoint().y());
352 if (point.y() < y1 || point.y() >= y2)
353 continue;
354 float ts[2];
355 const int numRoots = e.intersectionsAtY(point.y(), ts);
356 // Count if there is exactly one intersection to the left
357 bool oneHit = false;
358 float tForHit = -1;
359 for (int i = 0; i < numRoots; i++) {
360 if (e.pointAtFraction(ts[i]).x() <= point.x()) {
361 oneHit = !oneHit;
362 tForHit = ts[i];
363 }
364 }
365 if (oneHit) {
366 dir = e.tangentAtFraction(tForHit).y() < 0 ? -1 : 1;
367 winding_number += dir;
368 }
369 }
370 };
371
372 return (fillRule() == Qt::WindingFill ? (winding_number != 0) : ((winding_number % 2) != 0));
373}
374
375// similar as contains. But we treat the element with the index elementIdx in a special way
376// that should be numerically more stable. The result is a contains for a point on the left
377// and for the right side of the element.
378QQuadPath::Element::FillSide QQuadPath::fillSideOf(int elementIdx, float elementT) const
379{
380 constexpr float toleranceT = 1e-3f;
381 const QVector2D point = m_elements.at(elementIdx).pointAtFraction(elementT);
382 const QVector2D tangent = m_elements.at(elementIdx).tangentAtFraction(elementT);
383
384 const bool swapXY = qAbs(tangent.x()) > qAbs(tangent.y());
385 auto getX = [=](QVector2D p) -> float { return swapXY ? p.y() : p.x(); };
386 auto getY = [=](QVector2D p) -> float { return swapXY ? -p.x() : p.y(); };
387
388 int winding_number = 0;
389 for (int i = 0; i < elementCount(); i++) {
390 const Element &e = m_elements.at(i);
391 int dir = 1;
392 float y1 = getY(e.startPoint());
393 float y2 = getY(e.endPoint());
394 if (y2 < y1) {
395 qSwap(y1, y2);
396 dir = -1;
397 }
398 if (e.m_isLine) {
399 if (getY(point) < y1 || getY(point) >= y2 || y1 == y2)
400 continue;
401 const float t = (getY(point) - getY(e.startPoint())) / (getY(e.endPoint()) - getY(e.startPoint()));
402 const float x = getX(e.startPoint()) + t * (getX(e.endPoint()) - getX(e.startPoint()));
403 if (x <= getX(point) && (i != elementIdx || qAbs(t - elementT) > toleranceT))
404 winding_number += dir;
405 } else {
406 y1 = qMin(y1, getY(e.controlPoint()));
407 y2 = qMax(y2, getY(e.controlPoint()));
408 if (getY(point) < y1 || getY(point) >= y2)
409 continue;
410 float ts[2];
411 const int numRoots = e.intersectionsAtY(getY(point), ts, swapXY);
412 // Count if there is exactly one intersection to the left
413 bool oneHit = false;
414 float tForHit = -1;
415 for (int j = 0; j < numRoots; j++) {
416 const float x = getX(e.pointAtFraction(ts[j]));
417 if (x <= getX(point) && (i != elementIdx || qAbs(ts[j] - elementT) > toleranceT)) {
418 oneHit = !oneHit;
419 tForHit = ts[j];
420 }
421 }
422 if (oneHit) {
423 dir = getY(e.tangentAtFraction(tForHit)) < 0 ? -1 : 1;
424 winding_number += dir;
425 }
426 }
427 };
428
429 int left_winding_number = winding_number;
430 int right_winding_number = winding_number;
431
432 int dir = getY(tangent) < 0 ? -1 : 1;
433
434 if (dir > 0)
435 left_winding_number += dir;
436 else
437 right_winding_number += dir;
438
439 bool leftInside = (fillRule() == Qt::WindingFill ? (left_winding_number != 0) : ((left_winding_number % 2) != 0));
440 bool rightInside = (fillRule() == Qt::WindingFill ? (right_winding_number != 0) : ((right_winding_number % 2) != 0));
441
442 if (leftInside && rightInside)
443 return QQuadPath::Element::FillSideBoth;
444 else if (leftInside)
445 return QQuadPath::Element::FillSideLeft;
446 else if (rightInside)
447 return QQuadPath::Element::FillSideRight;
448 else
449 return QQuadPath::Element::FillSideUndetermined; //should not happen except for numerical error.
450}
451
452void QQuadPath::addElement(const QVector2D &control, const QVector2D &endPoint, bool isLine)
453{
454 if (qFuzzyCompare(m_currentPoint, endPoint))
455 return; // 0 length element, skip
456
457 isLine = isLine || isPointNearLine(control, m_currentPoint, endPoint); // Turn flat quad into line
458
459 m_elements.resize(m_elements.size() + 1);
460 Element &elem = m_elements.last();
461 elem.sp = m_currentPoint;
462 elem.cp = isLine ? (0.5f * (m_currentPoint + endPoint)) : control;
463 elem.ep = endPoint;
464 elem.m_isLine = isLine;
465 elem.m_isSubpathStart = m_subPathToStart;
466 m_subPathToStart = false;
467 m_currentPoint = endPoint;
468}
469
470void QQuadPath::addElement(const Element &e)
471{
472 m_subPathToStart = false;
473 m_currentPoint = e.endPoint();
474 m_elements.append(e);
475}
476
477#if !defined(QQUADPATH_CONVEX_CHECK_ERROR_MARGIN)
478# define QQUICKSHAPECURVERENDERER_CONVEX_CHECK_ERROR_MARGIN (1.0f / 32.0f)
479#endif
480
481QQuadPath::Element::CurvatureFlags QQuadPath::coordinateOrderOfElement(const QQuadPath::Element &element) const
482{
483 QVector2D baseLine = element.endPoint() - element.startPoint();
484 QVector2D midPoint = element.midPoint();
485 // At the midpoint, the tangent of a quad is parallel to the baseline
486 QVector2D normal = QVector2D(-baseLine.y(), baseLine.x()).normalized();
487 float delta = qMin(element.extent() / 100, QQUICKSHAPECURVERENDERER_CONVEX_CHECK_ERROR_MARGIN);
488 QVector2D justRightOfMid = midPoint + (normal * delta);
489 bool pathContainsPoint = contains(justRightOfMid);
490 return pathContainsPoint ? Element::FillOnRight : Element::CurvatureFlags(0);
491}
492
494{
496 res.reserve(path.elementCount());
497 res.setFillRule(path.fillRule());
498
499 const bool isQuadratic = hints & PathQuadratic;
500
501 QPolygonF quads;
502 QPointF sp;
503 for (int i = 0; i < path.elementCount(); ++i) {
504 QPainterPath::Element element = path.elementAt(i);
505
506 QPointF ep(element);
507 switch (element.type) {
509 res.moveTo(QVector2D(ep));
510 break;
512 res.lineTo(QVector2D(ep));
513 break;
515 QPointF cp1 = ep;
516 QPointF cp2(path.elementAt(++i));
517 ep = path.elementAt(++i);
518 if (isQuadratic) {
519 const qreal f = 3.0 / 2.0;
520 const QPointF cp = sp + f * (cp1 - sp);
521 res.quadTo(QVector2D(cp), QVector2D(ep));
522 } else {
523 QBezier b = QBezier::fromPoints(sp, cp1, cp2, ep);
524 qt_toQuadratics(b, &quads);
525 for (int i = 1; i < quads.size(); i += 2) {
526 QVector2D cp(quads[i]);
527 QVector2D ep(quads[i + 1]);
528 res.quadTo(cp, ep);
529 }
530 }
531 break;
532 }
533 default:
534 Q_UNREACHABLE();
535 break;
536 }
537 sp = ep;
538 }
539
540 res.setPathHints(hints | PathQuadratic);
541 return res;
542}
543
545{
546 // We use the convention that the inside of a curve is on the *right* side of the
547 // direction of the baseline.Thus, as long as this is true: if the control point is
548 // on the left side of the baseline, the curve is convex and otherwise it is
549 // concave. The paths we get can be arbitrary order, but each subpath will have a
550 // consistent order. Therefore, for the first curve element in a subpath, we can
551 // determine if the direction already follows the convention or not, and then we
552 // can easily detect curvature of all subsequent elements in the subpath.
553
554 static bool checkAnomaly = qEnvironmentVariableIntValue("QT_QUICKSHAPES_CHECK_ALL_CURVATURE") != 0;
555 const bool pathHasFillOnRight = testHint(PathFillOnRight);
556
557 Element::CurvatureFlags flags = Element::CurvatureUndetermined;
558 for (QQuadPath::Element &element : m_elements) {
559 Q_ASSERT(element.childCount() == 0);
560 if (element.isSubpathStart()) {
561 if (pathHasFillOnRight && !checkAnomaly)
562 flags = Element::FillOnRight;
563 else
564 flags = coordinateOrderOfElement(element);
565 } else if (checkAnomaly) {
566 Element::CurvatureFlags newFlags = coordinateOrderOfElement(element);
567 if (flags != newFlags) {
568 qDebug() << "Curvature anomaly detected:" << element
569 << "Subpath fill on right:" << (flags & Element::FillOnRight)
570 << "Element fill on right:" << (newFlags & Element::FillOnRight);
571 flags = newFlags;
572 }
573 }
574
575 if (element.isLine()) {
576 element.m_curvatureFlags = flags;
577 } else {
578 bool controlPointOnLeft = element.isControlPointOnLeft();
579 bool isFillOnRight = flags & Element::FillOnRight;
580 bool isConvex = controlPointOnLeft == isFillOnRight;
581
582 if (isConvex)
583 element.m_curvatureFlags = Element::CurvatureFlags(flags | Element::Convex);
584 else
585 element.m_curvatureFlags = flags;
586 }
587 }
588}
589
591{
592 QRectF res;
593 if (elementCount()) {
594 QVector2D min, max;
595 min = max = m_elements.constFirst().sp;
596 // No need to recurse, as split curve's controlpoints are within the parent curve's
597 for (const QQuadPath::Element &e : std::as_const(m_elements)) {
598 min.setX(std::min({ min.x(), e.sp.x(), e.cp.x(), e.ep.x() }));
599 min.setY(std::min({ min.y(), e.sp.y(), e.cp.y(), e.ep.y() }));
600 max.setX(std::max({ max.x(), e.sp.x(), e.cp.x(), e.ep.x() }));
601 max.setY(std::max({ max.y(), e.sp.y(), e.cp.y(), e.ep.y() }));
602 }
603 res = QRectF(min.toPointF(), max.toPointF());
604 }
605 return res;
606}
607
608// Count leaf elements
610{
611 int count = 0;
612 iterateElements([&](const QQuadPath::Element &, int) { count++; });
613 return count;
614}
615
617{
618 // Currently only converts the main, unsplit path; no need for the split path identified
621 res.setFillRule(fillRule());
622 for (const Element &element : m_elements) {
623 if (element.m_isSubpathStart)
624 res.moveTo(element.startPoint().toPointF());
625 if (element.m_isLine)
626 res.lineTo(element.endPoint().toPointF());
627 else
628 res.quadTo(element.controlPoint().toPointF(), element.endPoint().toPointF());
629 };
630 return res;
631}
632
634{
635 QString res;
637 for (const Element &element : m_elements) {
638 if (element.isSubpathStart())
639 str << "M " << element.startPoint().x() << " " << element.startPoint().y() << " ";
640 if (element.isLine())
641 str << "L " << element.endPoint().x() << " " << element.endPoint().y() << " ";
642 else
643 str << "Q " << element.controlPoint().x() << " " << element.controlPoint().y() << " "
644 << element.endPoint().x() << " " << element.endPoint().y() << " ";
645 }
646 return res;
647}
648
649// Returns a new path since doing it inline would probably be less efficient
650// (technically changing it from O(n) to O(n^2))
651// Note that this function should be called before splitting any elements,
652// so we can assume that the structure is a list and not a tree
654{
655 Q_ASSERT(m_childElements.isEmpty());
656 bool closed = false;
657 QQuadPath res = *this;
658 res.m_subPathToStart = false;
659 res.m_elements = {};
660 res.m_elements.reserve(elementCount());
661 int subStart = -1;
662 int prevElement = -1;
663 for (int i = 0; i < elementCount(); i++) {
664 const auto &element = m_elements.at(i);
665 if (element.m_isSubpathStart) {
666 if (subStart >= 0 && m_elements[i - 1].ep != m_elements[subStart].sp) {
667 res.m_currentPoint = m_elements[i - 1].ep;
668 res.lineTo(m_elements[subStart].sp);
669 closed = true;
670 auto &endElement = res.m_elements.last();
671 endElement.m_isSubpathEnd = true;
672 // lineTo() can bail out if the points are too close.
673 // In that case, just change the end point to be equal to the start
674 // (No need to test because the assignment is a no-op otherwise).
675 endElement.ep = m_elements[subStart].sp;
676 } else if (prevElement >= 0) {
677 res.m_elements[prevElement].m_isSubpathEnd = true;
678 }
679 subStart = i;
680 }
681 res.m_elements.append(element);
682 prevElement = res.m_elements.size() - 1;
683 }
684
685 if (subStart >= 0 && m_elements.last().ep != m_elements[subStart].sp) {
686 res.m_currentPoint = m_elements.last().ep;
687 res.lineTo(m_elements[subStart].sp);
688 closed = true;
689 }
690 if (!res.m_elements.isEmpty()) {
691 auto &endElement = res.m_elements.last();
692 endElement.m_isSubpathEnd = true;
693 endElement.ep = m_elements[subStart].sp;
694 }
695
696 if (didClose)
697 *didClose = closed;
698 return res;
699}
700
702{
705 iterateElements([&](const QQuadPath::Element &elem, int) { res.m_elements.append(elem); });
706 res.setPathHints(pathHints());
707 res.setFillRule(fillRule());
708 return res;
709}
710
712{
713public:
715 : m_element(element)
716 {
717 m_currentPoint = m_element.startPoint();
718 if (m_element.isLine())
719 m_lineLength = (m_element.endPoint() - m_element.startPoint()).length();
720 else
721 fillLUT();
722 }
723
724 bool consume(float length)
725 {
726 m_lastT = m_currentT;
727 m_lastPoint = m_currentPoint;
728 float nextCut = m_consumed + length;
729 float cutT = m_element.isLine() ? nextCut / m_lineLength : tForLength(nextCut);
730 if (cutT < 1) {
731 m_currentT = cutT;
732 m_currentPoint = m_element.pointAtFraction(m_currentT);
733 m_consumed = nextCut;
734 return true;
735 } else {
736 m_currentT = 1;
737 m_currentPoint = m_element.endPoint();
738 return false;
739 }
740 }
741
743 {
744 return m_currentPoint;
745 }
746
748 {
749 Q_ASSERT(!m_element.isLine());
750 // Split curve right at lastT, yields { lastPoint, rcp, endPoint } quad segment
751 QVector2D rcp = (1 - m_lastT) * m_element.controlPoint() + m_lastT * m_element.endPoint();
752 // Split that left at currentT, yields { lastPoint, lcp, currentPoint } quad segment
753 float segmentT = (m_currentT - m_lastT) / (1 - m_lastT);
754 QVector2D lcp = (1 - segmentT) * m_lastPoint + segmentT * rcp;
755 return lcp;
756 }
757
759 {
760 float elemLength = m_element.isLine() ? m_lineLength : m_lut.last();
761 return elemLength - m_consumed;
762 }
763
764private:
765 void fillLUT()
766 {
767 Q_ASSERT(!m_element.isLine());
768 QVector2D ap = m_element.startPoint() - 2 * m_element.controlPoint() + m_element.endPoint();
769 QVector2D bp = 2 * m_element.controlPoint() - 2 * m_element.startPoint();
770 float A = 4 * QVector2D::dotProduct(ap, ap);
771 float B = 4 * QVector2D::dotProduct(ap, bp);
772 float C = QVector2D::dotProduct(bp, bp);
773 float b = B / (2 * A);
774 float c = C / A;
775 float k = c - (b * b);
776 float l2 = b * std::sqrt(b * b + k);
777 float lnom = b + std::sqrt(b * b + k);
778 float l0 = 0.5f * std::sqrt(A);
779
780 m_lut.resize(LUTSize, 0);
781 for (int i = 1; i < LUTSize; i++) {
782 float t = float(i) / (LUTSize - 1);
783 float u = t + b;
784 float w = std::sqrt(u * u + k);
785 float l1 = u * w;
786 float lden = u + w;
787 float l3 = k * std::log(std::fabs(lden / lnom));
788 float res = l0 * (l1 - l2 + l3);
789 m_lut[i] = res;
790 }
791 }
792
793 float tForLength(float length)
794 {
795 Q_ASSERT(!m_element.isLine());
796 Q_ASSERT(!m_lut.isEmpty());
797
798 float res = 2; // I.e. invalid, outside [0, 1] range
799 auto it = std::upper_bound(m_lut.cbegin(), m_lut.cend(), length);
800 if (it != m_lut.cend()) {
801 float nextLength = *it--;
802 float prevLength = *it;
803 int prevIndex = std::distance(m_lut.cbegin(), it);
804 float fraction = (length - prevLength) / (nextLength - prevLength);
805 res = (prevIndex + fraction) / (LUTSize - 1);
806 }
807 return res;
808 }
809
810 const QQuadPath::Element &m_element;
811 float m_lastT = 0;
812 float m_currentT = 0;
813 QVector2D m_lastPoint;
814 QVector2D m_currentPoint;
815 float m_consumed = 0;
816 // For line elements:
817 float m_lineLength;
818 // For quadratic curve elements:
819 static constexpr int LUTSize = 21;
820 QVarLengthArray<float, LUTSize> m_lut;
821};
822
823QQuadPath QQuadPath::dashed(qreal lineWidth, const QList<qreal> &dashPattern, qreal dashOffset) const
824{
825 QVarLengthArray<float, 16> pattern;
826 float patternLength = 0;
827 for (int i = 0; i < 2 * (dashPattern.length() / 2); i++) {
828 float dashLength = qMax(lineWidth * dashPattern[i], qreal(0));
829 pattern.append(dashLength);
830 patternLength += dashLength;
831 }
832 if (patternLength == 0)
833 return {};
834
835 int startIndex = 0;
836 float startOffset = std::fmod(lineWidth * dashOffset, patternLength);
837 if (startOffset < 0)
838 startOffset += patternLength;
839 for (float dashLength : pattern) {
840 if (dashLength > startOffset)
841 break;
842 startIndex = (startIndex + 1) % pattern.size(); // The % guards against accuracy issues
843 startOffset -= dashLength;
844 }
845
846 int dashIndex = startIndex;
847 float offset = startOffset;
849 for (int i = 0; i < elementCount(); i++) {
850 const Element &element = elementAt(i);
851 if (element.isSubpathStart()) {
852 res.moveTo(element.startPoint());
853 dashIndex = startIndex;
854 offset = startOffset;
855 }
856 ElementCutter cutter(element);
857 while (true) {
858 bool gotAll = cutter.consume(pattern.at(dashIndex) - offset);
859 QVector2D nextPoint = cutter.currentCutPoint();
860 if (dashIndex & 1)
861 res.moveTo(nextPoint); // gap
862 else if (element.isLine())
863 res.lineTo(nextPoint); // dash in line
864 else
865 res.quadTo(cutter.currentControlPoint(), nextPoint); // dash in curve
866 if (gotAll) {
867 offset = 0;
868 dashIndex = (dashIndex + 1) % pattern.size();
869 } else {
870 offset += cutter.lastLength();
871 break;
872 }
873 }
874 }
875 res.setFillRule(fillRule());
876 res.setPathHints(pathHints());
877 return res;
878}
879
881{
882 const int newChildIndex = m_childElements.size();
883 m_childElements.resize(newChildIndex + 2);
884 Element &parent = elementAt(index);
885 parent.m_numChildren = 2;
886 parent.m_firstChildIndex = newChildIndex;
887
888 Element &quad1 = m_childElements[newChildIndex];
889 const QVector2D mp = parent.midPoint();
890 quad1.sp = parent.sp;
891 quad1.cp = 0.5f * (parent.sp + parent.cp);
892 quad1.ep = mp;
893 quad1.m_isSubpathStart = parent.m_isSubpathStart;
894 quad1.m_isSubpathEnd = false;
895 quad1.m_curvatureFlags = parent.m_curvatureFlags;
896 quad1.m_isLine = parent.m_isLine; //### || isPointNearLine(quad1.cp, quad1.sp, quad1.ep);
897
898 Element &quad2 = m_childElements[newChildIndex + 1];
899 quad2.sp = mp;
900 quad2.cp = 0.5f * (parent.ep + parent.cp);
901 quad2.ep = parent.ep;
902 quad2.m_isSubpathStart = false;
903 quad2.m_isSubpathEnd = parent.m_isSubpathEnd;
904 quad2.m_curvatureFlags = parent.m_curvatureFlags;
905 quad2.m_isLine = parent.m_isLine; //### || isPointNearLine(quad2.cp, quad2.sp, quad2.ep);
906
907#ifndef QT_NO_DEBUG
908 if (qFuzzyCompare(quad1.sp, quad1.ep) || qFuzzyCompare(quad2.sp, quad2.ep))
909 qCDebug(lcSGCurveProcessor) << "Splitting has resulted in ~null quad";
910#endif
911}
912
913static void printElement(QDebug stream, const QQuadPath::Element &element)
914{
915 auto printPoint = [&](QVector2D p) { stream << "(" << p.x() << ", " << p.y() << ") "; };
916 stream << "{ ";
917 printPoint(element.startPoint());
918 printPoint(element.controlPoint());
919 printPoint(element.endPoint());
920 stream << "} " << (element.isLine() ? "L " : "C ") << (element.isConvex() ? "X " : "O ")
921 << (element.isSubpathStart() ? "S" : element.isSubpathEnd() ? "E" : "");
922}
923
925{
927 stream.nospace();
928 stream << "QuadPath::Element( ";
929 printElement(stream, element);
930 stream << " )";
931 return stream;
932}
933
935{
937 stream.nospace();
938 stream << "QuadPath(" << path.elementCount() << " main elements, "
939 << path.elementCountRecursive() << " leaf elements, "
940 << (path.fillRule() == Qt::OddEvenFill ? "OddEven" : "Winding") << Qt::endl;
941 int count = 0;
942 path.iterateElements([&](const QQuadPath::Element &e, int) {
943 stream << " " << count++ << (e.isSubpathStart() ? " >" : " ");
945 stream << Qt::endl;
946 });
947 stream << ")";
948 return stream;
949}
950
bool consume(float length)
float lastLength()
QVector2D currentControlPoint()
QVector2D currentCutPoint()
ElementCutter(const QQuadPath::Element &element)
QPointF pt1() const
Definition qbezier_p.h:58
static QBezier fromPoints(const QPointF &p1, const QPointF &p2, const QPointF &p3, const QPointF &p4)
Definition qbezier_p.h:33
void parameterSplitLeft(qreal t, QBezier *left)
Definition qbezier_p.h:205
QBezier bezierOnInterval(qreal t0, qreal t1) const
Definition qbezier.cpp:578
QBezier mapBy(const QTransform &transform) const
Definition qbezier.cpp:40
QPointF pt4() const
Definition qbezier_p.h:61
QPointF pt2() const
Definition qbezier_p.h:59
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
qreal angle() const
Definition qline.cpp:564
@ NoIntersection
Definition qline.h:195
qsizetype size() const noexcept
Definition qlist.h:398
bool isEmpty() const noexcept
Definition qlist.h:402
T & last()
Definition qlist.h:649
const_reference at(qsizetype i) const noexcept
Definition qlist.h:447
const T & constFirst() const noexcept
Definition qlist.h:648
void resize(qsizetype size)
Definition qlist.h:404
void append(parameter_type t)
Definition qlist.h:459
\inmodule QtGui
\inmodule QtGui
void reserve(int size)
Reserves a given amount of elements in QPainterPath's internal memory.
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
static constexpr qreal dotProduct(const QPointF &p1, const QPointF &p2)
Definition qpoint.h:242
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
bool isSubpathStart() const
Definition qquadpath_p.h:55
Element segmentFromTo(float t0, float t1) const
bool isLine() const
Definition qquadpath_p.h:65
Element reversed() const
QVector2D tangentAtFraction(float t) const
bool isSubpathEnd() const
Definition qquadpath_p.h:60
QVector2D startPoint() const
Definition qquadpath_p.h:75
QVector2D midPoint() const
Definition qquadpath_p.h:90
bool isConvex() const
Definition qquadpath_p.h:70
QVector2D endPoint() const
Definition qquadpath_p.h:85
float extent() const
QVector2D pointAtFraction(float t) const
QVector2D controlPoint() const
Definition qquadpath_p.h:80
QQuadPath flattened() const
QPainterPath toPainterPath() const
static QVector2D closestPointOnLine(const QVector2D &p, const QVector2D &sp, const QVector2D &ep)
Element & elementAt(int i)
bool contains(const QVector2D &point) const
QString asSvgString() const
void iterateElements(Func &&lambda)
static bool isPointOnLine(const QVector2D &p, const QVector2D &sp, const QVector2D &ep)
void splitElementAt(int index)
QQuadPath subPathsClosed(bool *didClose=nullptr) const
void reserve(int size)
static bool isPointOnLeft(const QVector2D &p, const QVector2D &sp, const QVector2D &ep)
static bool isPointNearLine(const QVector2D &p, const QVector2D &sp, const QVector2D &ep)
int elementCountRecursive() const
@ PathFillOnRight
Definition qquadpath_p.h:35
QQuadPath dashed(qreal lineWidth, const QList< qreal > &dashPattern, qreal dashOffset=0) const
PathHints pathHints() const
QRectF controlPointRect() const
Element::FillSide fillSideOf(int elementIdx, float elementT) const
Qt::FillRule fillRule() const
bool testHint(PathHint hint) const
static QQuadPath fromPainterPath(const QPainterPath &path, PathHints hints={})
void addCurvatureData()
friend Q_QUICK_EXPORT QDebug operator<<(QDebug, const QQuadPath &)
int elementCount() const
\inmodule QtCore\reentrant
Definition qrect.h:484
const_iterator cend() const noexcept
Definition qset.h:143
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:31
constexpr float y() const noexcept
Returns the y coordinate of this point.
Definition qvectornd.h:502
QVector2D normalized() const noexcept
Returns the normalized unit vector form of this vector.
Definition qvectornd.h:529
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:501
static constexpr float dotProduct(QVector2D v1, QVector2D v2) noexcept
Returns the dot product of v1 and v2.
Definition qvectornd.h:604
constexpr void setY(float y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qvectornd.h:505
constexpr QPointF toPointF() const noexcept
Returns the QPointF form of this 2D vector.
Definition qvectornd.h:628
constexpr void setX(float x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qvectornd.h:504
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
int y
the y coordinate of the widget relative to its parent and including any window frame
Definition qwidget.h:110
int x
the x coordinate of the widget relative to its parent including any window frame
Definition qwidget.h:109
QString str
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
@ WindingFill
@ OddEvenFill
QTextStream & endl(QTextStream &stream)
Writes '\n' to the stream and flushes the stream.
#define Q_STATIC_ASSERT(Condition)
Definition qassert.h:108
EGLStreamKHR stream
bool qIsFinite(qfloat16 f) noexcept
Definition qfloat16.h:285
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
qfloat16 qSqrt(qfloat16 f)
Definition qfloat16.h:289
#define qDebug
[1]
Definition qlogging.h:165
#define qCDebug(category,...)
QT_BEGIN_NAMESPACE constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:19
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:23
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:21
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
n varying highp vec2 A
GLint GLfloat GLfloat GLfloat v2
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLuint index
[2]
GLboolean r
[2]
GLenum GLuint GLenum GLsizei length
GLenum GLenum GLsizei count
GLfloat GLfloat f
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLbitfield flags
GLint GLfloat GLfloat v1
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
GLenum GLuint GLintptr offset
GLfloat n
GLuint GLfloat GLfloat y0
GLint y
GLdouble s
[6]
Definition qopenglext.h:235
GLuint res
const GLubyte * c
GLfixed GLfixed GLfixed y2
GLuint segment
GLfixed GLfixed x2
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLsizei const GLchar *const * path
GLfloat GLfloat p
[1]
GLubyte * pattern
static bool isLine(const QBezier &bezier)
static void qt_addToQuadratics(const QBezier &b, QPolygonF *p, int maxSplits, qreal maxDiff)
static QPointF qt_quadraticForCubic(const QBezier &b)
Definition qquadpath.cpp:71
static float crossProduct(const QVector2D &sp, const QVector2D &p, const QVector2D &ep)
static void qt_toQuadratics(const QBezier &b, QPolygonF *out, qreal errorLimit=0.01)
static QT_BEGIN_NAMESPACE qreal qt_scoreQuadratic(const QBezier &b, QPointF qcp)
Definition qquadpath.cpp:15
static void printElement(QDebug stream, const QQuadPath::Element &element)
#define QQUICKSHAPECURVERENDERER_CONVEX_CHECK_ERROR_MARGIN
static int qt_getInflectionPoints(const QBezier &orig, qreal *tpoints)
Definition qquadpath.cpp:92
static const qreal epsilon
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
#define sp
#define t0
#define t1
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
double qreal
Definition qtypes.h:187
static uint toIndex(ExecutionEngine *e, const Value &v)
QTextStream out(stdout)
[7]
MyCustomStruct c2
QString dir
[11]