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