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/qpair.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 | { return {.x1: p1.x(), .y1: p1.y(), .x2: p2.x(), .y2: p2.y(), .x3: p3.x(), .y3: p3.y(), .x4: p4.x(), .y4: 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 | QPointF pt1() const { return QPointF(x1, y1); } |
60 | QPointF pt2() const { return QPointF(x2, y2); } |
61 | QPointF pt3() const { return QPointF(x3, y3); } |
62 | 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 | { |
86 | return QPointF((x1 + x4 + 3*(x2 + x3))/8., (y1 + y4 + 3*(y2 + y3))/8.); |
87 | } |
88 | |
89 | inline QLineF QBezier::midTangent() const |
90 | { |
91 | QPointF mid = midPoint(); |
92 | QLineF dir(QLineF(x1, y1, x2, y2).pointAt(t: 0.5), QLineF(x3, y3, x4, y4).pointAt(t: 0.5)); |
93 | return QLineF(mid.x() - dir.dx(), mid.y() - dir.dy(), |
94 | mid.x() + dir.dx(), mid.y() + dir.dy()); |
95 | } |
96 | |
97 | inline QLineF QBezier::startTangent() const |
98 | { |
99 | QLineF tangent(pt1(), pt2()); |
100 | if (tangent.isNull()) |
101 | tangent = QLineF(pt1(), pt3()); |
102 | if (tangent.isNull()) |
103 | tangent = QLineF(pt1(), pt4()); |
104 | return tangent; |
105 | } |
106 | |
107 | inline QLineF QBezier::endTangent() const |
108 | { |
109 | QLineF tangent(pt4(), pt3()); |
110 | if (tangent.isNull()) |
111 | tangent = QLineF(pt4(), pt2()); |
112 | if (tangent.isNull()) |
113 | tangent = QLineF(pt4(), pt1()); |
114 | return tangent; |
115 | } |
116 | |
117 | inline void QBezier::coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d) |
118 | { |
119 | qreal m_t = 1. - t; |
120 | b = m_t * m_t; |
121 | c = t * t; |
122 | d = c * t; |
123 | a = b * m_t; |
124 | b *= 3. * t; |
125 | c *= 3. * m_t; |
126 | } |
127 | |
128 | inline QPointF QBezier::pointAt(qreal t) const |
129 | { |
130 | // numerically more stable: |
131 | qreal x, y; |
132 | |
133 | qreal m_t = 1. - t; |
134 | { |
135 | qreal a = x1*m_t + x2*t; |
136 | qreal b = x2*m_t + x3*t; |
137 | qreal c = x3*m_t + x4*t; |
138 | a = a*m_t + b*t; |
139 | b = b*m_t + c*t; |
140 | x = a*m_t + b*t; |
141 | } |
142 | { |
143 | qreal a = y1*m_t + y2*t; |
144 | qreal b = y2*m_t + y3*t; |
145 | qreal c = y3*m_t + y4*t; |
146 | a = a*m_t + b*t; |
147 | b = b*m_t + c*t; |
148 | y = a*m_t + b*t; |
149 | } |
150 | return QPointF(x, y); |
151 | } |
152 | |
153 | inline QPointF QBezier::normalVector(qreal t) const |
154 | { |
155 | qreal m_t = 1. - t; |
156 | qreal a = m_t * m_t; |
157 | qreal b = t * m_t; |
158 | qreal c = t * t; |
159 | |
160 | return QPointF((y2-y1) * a + (y3-y2) * b + (y4-y3) * c, -(x2-x1) * a - (x3-x2) * b - (x4-x3) * c); |
161 | } |
162 | |
163 | inline QPointF QBezier::derivedAt(qreal t) const |
164 | { |
165 | // 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 | |
167 | qreal m_t = 1. - t; |
168 | |
169 | qreal d = t * t; |
170 | qreal a = -m_t * m_t; |
171 | qreal b = 1 - 4 * t + 3 * d; |
172 | qreal c = 2 * t - 3 * d; |
173 | |
174 | return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4, |
175 | a * y1 + b * y2 + c * y3 + d * y4); |
176 | } |
177 | |
178 | inline QPointF QBezier::secondDerivedAt(qreal t) const |
179 | { |
180 | qreal a = 2. - 2. * t; |
181 | qreal b = -4 + 6 * t; |
182 | qreal c = 2 - 6 * t; |
183 | qreal d = 2 * t; |
184 | |
185 | return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4, |
186 | a * y1 + b * y2 + c * y3 + d * y4); |
187 | } |
188 | |
189 | std::pair<QBezier, QBezier> QBezier::split() const |
190 | { |
191 | const auto mid = [](QPointF lhs, QPointF rhs) { return (lhs + rhs) * .5; }; |
192 | |
193 | const QPointF mid_12 = mid(pt1(), pt2()); |
194 | const QPointF mid_23 = mid(pt2(), pt3()); |
195 | const QPointF mid_34 = mid(pt3(), pt4()); |
196 | const QPointF mid_12_23 = mid(mid_12, mid_23); |
197 | const QPointF mid_23_34 = mid(mid_23, mid_34); |
198 | const QPointF mid_12_23__23_34 = mid(mid_12_23, mid_23_34); |
199 | |
200 | return { |
201 | fromPoints(p1: pt1(), p2: mid_12, p3: mid_12_23, p4: mid_12_23__23_34), |
202 | fromPoints(p1: mid_12_23__23_34, p2: mid_23_34, p3: mid_34, p4: pt4()), |
203 | }; |
204 | } |
205 | |
206 | inline void QBezier::parameterSplitLeft(qreal t, QBezier *left) |
207 | { |
208 | left->x1 = x1; |
209 | left->y1 = y1; |
210 | |
211 | left->x2 = x1 + t * ( x2 - x1 ); |
212 | left->y2 = y1 + t * ( y2 - y1 ); |
213 | |
214 | left->x3 = x2 + t * ( x3 - x2 ); // temporary holding spot |
215 | left->y3 = y2 + t * ( y3 - y2 ); // temporary holding spot |
216 | |
217 | x3 = x3 + t * ( x4 - x3 ); |
218 | y3 = y3 + t * ( y4 - y3 ); |
219 | |
220 | x2 = left->x3 + t * ( x3 - left->x3); |
221 | y2 = left->y3 + t * ( y3 - left->y3); |
222 | |
223 | left->x3 = left->x2 + t * ( left->x3 - left->x2 ); |
224 | left->y3 = left->y2 + t * ( left->y3 - left->y2 ); |
225 | |
226 | left->x4 = x1 = left->x3 + t * (x2 - left->x3); |
227 | left->y4 = y1 = left->y3 + t * (y2 - left->y3); |
228 | } |
229 | |
230 | QT_END_NAMESPACE |
231 | |
232 | #endif // QBEZIER_P_H |
233 | |