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_p.h
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
4#ifndef QBEZIER_P_H
5#define QBEZIER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtGui/private/qtguiglobal_p.h>
19#include "QtCore/qline.h"
20#include "QtCore/qlist.h"
21#include "QtCore/qpoint.h"
22#include "QtCore/qrect.h"
23#include "QtGui/qtransform.h"
24#include <private/qdatabuffer_p.h>
25
26QT_BEGIN_NAMESPACE
27
28class QPolygonF;
29
30class Q_GUI_EXPORT QBezier
31{
32public:
33 static QBezier fromPoints(const QPointF &p1, const QPointF &p2,
34 const QPointF &p3, const QPointF &p4)
35 { return {p1.x(), p1.y(), p2.x(), p2.y(), p3.x(), p3.y(), p4.x(), p4.y()}; }
36
37 static void coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d);
38
39 inline QPointF pointAt(qreal t) const;
40 inline QPointF normalVector(qreal t) const;
41
42 inline QPointF derivedAt(qreal t) const;
43 inline QPointF secondDerivedAt(qreal t) const;
44
45 QPolygonF toPolygon(qreal bezier_flattening_threshold = 0.5) const;
46 void addToPolygon(QPolygonF *p, qreal bezier_flattening_threshold = 0.5) const;
47 void addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold) const;
48
49 QRectF bounds() const;
50 qreal length(qreal error = 0.01) const;
51 void addIfClose(qreal *length, qreal error) const;
52
53 qreal tAtLength(qreal len) const;
54
55 int stationaryYPoints(qreal &t0, qreal &t1) const;
56 qreal tForY(qreal t0, qreal t1, qreal y) const;
57
58 QPointF pt1() const { return QPointF(x1, y1); }
59 QPointF pt2() const { return QPointF(x2, y2); }
60 QPointF pt3() const { return QPointF(x3, y3); }
61 QPointF pt4() const { return QPointF(x4, y4); }
62
63 QBezier mapBy(const QTransform &transform) const;
64
65 inline QPointF midPoint() const;
66 inline QLineF midTangent() const;
67
68 inline QLineF startTangent() const;
69 inline QLineF endTangent() const;
70
71 inline void parameterSplitLeft(qreal t, QBezier *left);
72 inline std::pair<QBezier, QBezier> split() const;
73
74 int shifted(QBezier *curveSegments, int maxSegmets,
75 qreal offset, float threshold) const;
76
77 QBezier bezierOnInterval(qreal t0, qreal t1) const;
78 QBezier getSubRange(qreal t0, qreal t1) const;
79
80 qreal x1, y1, x2, y2, x3, y3, x4, y4;
81};
82
83inline QPointF QBezier::midPoint() const
84{
85 return QPointF((x1 + x4 + 3*(x2 + x3))/8., (y1 + y4 + 3*(y2 + y3))/8.);
86}
87
88inline QLineF QBezier::midTangent() const
89{
90 QPointF mid = midPoint();
91 QLineF dir(QLineF(x1, y1, x2, y2).pointAt(0.5), QLineF(x3, y3, x4, y4).pointAt(0.5));
92 return QLineF(mid.x() - dir.dx(), mid.y() - dir.dy(),
93 mid.x() + dir.dx(), mid.y() + dir.dy());
94}
95
96inline QLineF QBezier::startTangent() const
97{
98 QLineF tangent(pt1(), pt2());
99 if (tangent.isNull())
100 tangent = QLineF(pt1(), pt3());
101 if (tangent.isNull())
102 tangent = QLineF(pt1(), pt4());
103 return tangent;
104}
105
106inline QLineF QBezier::endTangent() const
107{
108 QLineF tangent(pt4(), pt3());
109 if (tangent.isNull())
110 tangent = QLineF(pt4(), pt2());
111 if (tangent.isNull())
112 tangent = QLineF(pt4(), pt1());
113 return tangent;
114}
115
116inline void QBezier::coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d)
117{
118 qreal m_t = 1. - t;
119 b = m_t * m_t;
120 c = t * t;
121 d = c * t;
122 a = b * m_t;
123 b *= 3. * t;
124 c *= 3. * m_t;
125}
126
127inline QPointF QBezier::pointAt(qreal t) const
128{
129 // numerically more stable:
130 qreal x, y;
131
132 qreal m_t = 1. - t;
133 {
134 qreal a = x1*m_t + x2*t;
135 qreal b = x2*m_t + x3*t;
136 qreal c = x3*m_t + x4*t;
137 a = a*m_t + b*t;
138 b = b*m_t + c*t;
139 x = a*m_t + b*t;
140 }
141 {
142 qreal a = y1*m_t + y2*t;
143 qreal b = y2*m_t + y3*t;
144 qreal c = y3*m_t + y4*t;
145 a = a*m_t + b*t;
146 b = b*m_t + c*t;
147 y = a*m_t + b*t;
148 }
149 return QPointF(x, y);
150}
151
152inline QPointF QBezier::normalVector(qreal t) const
153{
154 qreal m_t = 1. - t;
155 qreal a = m_t * m_t;
156 qreal b = t * m_t;
157 qreal c = t * t;
158
159 return QPointF((y2-y1) * a + (y3-y2) * b + (y4-y3) * c, -(x2-x1) * a - (x3-x2) * b - (x4-x3) * c);
160}
161
162inline QPointF QBezier::derivedAt(qreal t) const
163{
164 // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * t^2) * p2 + t^2 * p3)
165
166 qreal m_t = 1. - t;
167
168 qreal d = t * t;
169 qreal a = -m_t * m_t;
170 qreal b = 1 - 4 * t + 3 * d;
171 qreal c = 2 * t - 3 * d;
172
173 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
174 a * y1 + b * y2 + c * y3 + d * y4);
175}
176
177inline QPointF QBezier::secondDerivedAt(qreal t) const
178{
179 qreal a = 2. - 2. * t;
180 qreal b = -4 + 6 * t;
181 qreal c = 2 - 6 * t;
182 qreal d = 2 * t;
183
184 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
185 a * y1 + b * y2 + c * y3 + d * y4);
186}
187
188std::pair<QBezier, QBezier> QBezier::split() const
189{
190 const auto mid = [](QPointF lhs, QPointF rhs) { return (lhs + rhs) * .5; };
191
192 const QPointF mid_12 = mid(pt1(), pt2());
193 const QPointF mid_23 = mid(pt2(), pt3());
194 const QPointF mid_34 = mid(pt3(), pt4());
195 const QPointF mid_12_23 = mid(mid_12, mid_23);
196 const QPointF mid_23_34 = mid(mid_23, mid_34);
197 const QPointF mid_12_23__23_34 = mid(mid_12_23, mid_23_34);
198
199 return {
200 fromPoints(pt1(), mid_12, mid_12_23, mid_12_23__23_34),
201 fromPoints(mid_12_23__23_34, mid_23_34, mid_34, pt4()),
202 };
203}
204
205inline void QBezier::parameterSplitLeft(qreal t, QBezier *left)
206{
207 left->x1 = x1;
208 left->y1 = y1;
209
210 left->x2 = x1 + t * ( x2 - x1 );
211 left->y2 = y1 + t * ( y2 - y1 );
212
213 left->x3 = x2 + t * ( x3 - x2 ); // temporary holding spot
214 left->y3 = y2 + t * ( y3 - y2 ); // temporary holding spot
215
216 x3 = x3 + t * ( x4 - x3 );
217 y3 = y3 + t * ( y4 - y3 );
218
219 x2 = left->x3 + t * ( x3 - left->x3);
220 y2 = left->y3 + t * ( y3 - left->y3);
221
222 left->x3 = left->x2 + t * ( left->x3 - left->x2 );
223 left->y3 = left->y2 + t * ( left->y3 - left->y2 );
224
225 left->x4 = x1 = left->x3 + t * (x2 - left->x3);
226 left->y4 = y1 = left->y3 + t * (y2 - left->y3);
227}
228
229QT_END_NAMESPACE
230
231#endif // QBEZIER_P_H
Combined button and popup list for selecting options.
ShiftResult
Definition qbezier.cpp:172
@ Ok
Definition qbezier.cpp:173
@ Circle
Definition qbezier.cpp:176
@ Split
Definition qbezier.cpp:175
@ Discard
Definition qbezier.cpp:174
static ShiftResult good_offset(const QBezier *b1, const QBezier *b2, qreal offset, qreal threshold)
Definition qbezier.cpp:179
static bool addCircle(const QBezier *b, qreal offset, QBezier *o)
Definition qbezier.cpp:295
#define KAPPA
Definition qbezier.cpp:292
#define M_1_PI
Definition qmath.h:221