1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QBEZIER_P_H |
41 | #define QBEZIER_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of other Qt classes. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtGui/private/qtguiglobal_p.h> |
55 | #include "QtCore/qpoint.h" |
56 | #include "QtCore/qline.h" |
57 | #include "QtCore/qrect.h" |
58 | #include "QtCore/qvector.h" |
59 | #include "QtCore/qlist.h" |
60 | #include "QtCore/qpair.h" |
61 | #include "QtGui/qtransform.h" |
62 | #include <private/qdatabuffer_p.h> |
63 | |
64 | QT_BEGIN_NAMESPACE |
65 | |
66 | class QPolygonF; |
67 | |
68 | class Q_GUI_EXPORT QBezier |
69 | { |
70 | public: |
71 | static QBezier fromPoints(const QPointF &p1, const QPointF &p2, |
72 | const QPointF &p3, const QPointF &p4) |
73 | { 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()}; } |
74 | |
75 | static void coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d); |
76 | |
77 | inline QPointF pointAt(qreal t) const; |
78 | inline QPointF normalVector(qreal t) const; |
79 | |
80 | inline QPointF derivedAt(qreal t) const; |
81 | inline QPointF secondDerivedAt(qreal t) const; |
82 | |
83 | QPolygonF toPolygon(qreal bezier_flattening_threshold = 0.5) const; |
84 | void addToPolygon(QPolygonF *p, qreal bezier_flattening_threshold = 0.5) const; |
85 | void addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold) const; |
86 | |
87 | QRectF bounds() const; |
88 | qreal length(qreal error = 0.01) const; |
89 | void addIfClose(qreal *length, qreal error) const; |
90 | |
91 | qreal tAtLength(qreal len) const; |
92 | |
93 | int stationaryYPoints(qreal &t0, qreal &t1) const; |
94 | qreal tForY(qreal t0, qreal t1, qreal y) const; |
95 | |
96 | QPointF pt1() const { return QPointF(x1, y1); } |
97 | QPointF pt2() const { return QPointF(x2, y2); } |
98 | QPointF pt3() const { return QPointF(x3, y3); } |
99 | QPointF pt4() const { return QPointF(x4, y4); } |
100 | |
101 | QBezier mapBy(const QTransform &transform) const; |
102 | |
103 | inline QPointF midPoint() const; |
104 | inline QLineF midTangent() const; |
105 | |
106 | inline QLineF startTangent() const; |
107 | inline QLineF endTangent() const; |
108 | |
109 | inline void parameterSplitLeft(qreal t, QBezier *left); |
110 | inline std::pair<QBezier, QBezier> split() const; |
111 | |
112 | int shifted(QBezier *curveSegments, int maxSegmets, |
113 | qreal offset, float threshold) const; |
114 | |
115 | QBezier bezierOnInterval(qreal t0, qreal t1) const; |
116 | QBezier getSubRange(qreal t0, qreal t1) const; |
117 | |
118 | qreal x1, y1, x2, y2, x3, y3, x4, y4; |
119 | }; |
120 | |
121 | inline QPointF QBezier::midPoint() const |
122 | { |
123 | return QPointF((x1 + x4 + 3*(x2 + x3))/8., (y1 + y4 + 3*(y2 + y3))/8.); |
124 | } |
125 | |
126 | inline QLineF QBezier::midTangent() const |
127 | { |
128 | QPointF mid = midPoint(); |
129 | QLineF dir(QLineF(x1, y1, x2, y2).pointAt(t: 0.5), QLineF(x3, y3, x4, y4).pointAt(t: 0.5)); |
130 | return QLineF(mid.x() - dir.dx(), mid.y() - dir.dy(), |
131 | mid.x() + dir.dx(), mid.y() + dir.dy()); |
132 | } |
133 | |
134 | inline QLineF QBezier::startTangent() const |
135 | { |
136 | QLineF tangent(pt1(), pt2()); |
137 | if (tangent.isNull()) |
138 | tangent = QLineF(pt1(), pt3()); |
139 | if (tangent.isNull()) |
140 | tangent = QLineF(pt1(), pt4()); |
141 | return tangent; |
142 | } |
143 | |
144 | inline QLineF QBezier::endTangent() const |
145 | { |
146 | QLineF tangent(pt4(), pt3()); |
147 | if (tangent.isNull()) |
148 | tangent = QLineF(pt4(), pt2()); |
149 | if (tangent.isNull()) |
150 | tangent = QLineF(pt4(), pt1()); |
151 | return tangent; |
152 | } |
153 | |
154 | inline void QBezier::coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d) |
155 | { |
156 | qreal m_t = 1. - t; |
157 | b = m_t * m_t; |
158 | c = t * t; |
159 | d = c * t; |
160 | a = b * m_t; |
161 | b *= 3. * t; |
162 | c *= 3. * m_t; |
163 | } |
164 | |
165 | inline QPointF QBezier::pointAt(qreal t) const |
166 | { |
167 | // numerically more stable: |
168 | qreal x, y; |
169 | |
170 | qreal m_t = 1. - t; |
171 | { |
172 | qreal a = x1*m_t + x2*t; |
173 | qreal b = x2*m_t + x3*t; |
174 | qreal c = x3*m_t + x4*t; |
175 | a = a*m_t + b*t; |
176 | b = b*m_t + c*t; |
177 | x = a*m_t + b*t; |
178 | } |
179 | { |
180 | qreal a = y1*m_t + y2*t; |
181 | qreal b = y2*m_t + y3*t; |
182 | qreal c = y3*m_t + y4*t; |
183 | a = a*m_t + b*t; |
184 | b = b*m_t + c*t; |
185 | y = a*m_t + b*t; |
186 | } |
187 | return QPointF(x, y); |
188 | } |
189 | |
190 | inline QPointF QBezier::normalVector(qreal t) const |
191 | { |
192 | qreal m_t = 1. - t; |
193 | qreal a = m_t * m_t; |
194 | qreal b = t * m_t; |
195 | qreal c = t * t; |
196 | |
197 | return QPointF((y2-y1) * a + (y3-y2) * b + (y4-y3) * c, -(x2-x1) * a - (x3-x2) * b - (x4-x3) * c); |
198 | } |
199 | |
200 | inline QPointF QBezier::derivedAt(qreal t) const |
201 | { |
202 | // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * t^2) * p2 + t^2 * p3) |
203 | |
204 | qreal m_t = 1. - t; |
205 | |
206 | qreal d = t * t; |
207 | qreal a = -m_t * m_t; |
208 | qreal b = 1 - 4 * t + 3 * d; |
209 | qreal c = 2 * t - 3 * d; |
210 | |
211 | return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4, |
212 | a * y1 + b * y2 + c * y3 + d * y4); |
213 | } |
214 | |
215 | inline QPointF QBezier::secondDerivedAt(qreal t) const |
216 | { |
217 | qreal a = 2. - 2. * t; |
218 | qreal b = -4 + 6 * t; |
219 | qreal c = 2 - 6 * t; |
220 | qreal d = 2 * t; |
221 | |
222 | return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4, |
223 | a * y1 + b * y2 + c * y3 + d * y4); |
224 | } |
225 | |
226 | std::pair<QBezier, QBezier> QBezier::split() const |
227 | { |
228 | const auto mid = [](QPointF lhs, QPointF rhs) { return (lhs + rhs) * .5; }; |
229 | |
230 | const QPointF mid_12 = mid(pt1(), pt2()); |
231 | const QPointF mid_23 = mid(pt2(), pt3()); |
232 | const QPointF mid_34 = mid(pt3(), pt4()); |
233 | const QPointF mid_12_23 = mid(mid_12, mid_23); |
234 | const QPointF mid_23_34 = mid(mid_23, mid_34); |
235 | const QPointF mid_12_23__23_34 = mid(mid_12_23, mid_23_34); |
236 | |
237 | return { |
238 | fromPoints(p1: pt1(), p2: mid_12, p3: mid_12_23, p4: mid_12_23__23_34), |
239 | fromPoints(p1: mid_12_23__23_34, p2: mid_23_34, p3: mid_34, p4: pt4()), |
240 | }; |
241 | } |
242 | |
243 | inline void QBezier::parameterSplitLeft(qreal t, QBezier *left) |
244 | { |
245 | left->x1 = x1; |
246 | left->y1 = y1; |
247 | |
248 | left->x2 = x1 + t * ( x2 - x1 ); |
249 | left->y2 = y1 + t * ( y2 - y1 ); |
250 | |
251 | left->x3 = x2 + t * ( x3 - x2 ); // temporary holding spot |
252 | left->y3 = y2 + t * ( y3 - y2 ); // temporary holding spot |
253 | |
254 | x3 = x3 + t * ( x4 - x3 ); |
255 | y3 = y3 + t * ( y4 - y3 ); |
256 | |
257 | x2 = left->x3 + t * ( x3 - left->x3); |
258 | y2 = left->y3 + t * ( y3 - left->y3); |
259 | |
260 | left->x3 = left->x2 + t * ( left->x3 - left->x2 ); |
261 | left->y3 = left->y2 + t * ( left->y3 - left->y2 ); |
262 | |
263 | left->x4 = x1 = left->x3 + t * (x2 - left->x3); |
264 | left->y4 = y1 = left->y3 + t * (y2 - left->y3); |
265 | } |
266 | |
267 | QT_END_NAMESPACE |
268 | |
269 | #endif // QBEZIER_P_H |
270 | |