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/qline.h"
56#include "QtCore/qlist.h"
57#include "QtCore/qpair.h"
58#include "QtCore/qpoint.h"
59#include "QtCore/qrect.h"
60#include "QtGui/qtransform.h"
61#include <private/qdatabuffer_p.h>
62
63QT_BEGIN_NAMESPACE
64
65class QPolygonF;
66
67class Q_GUI_EXPORT QBezier
68{
69public:
70 static QBezier fromPoints(const QPointF &p1, const QPointF &p2,
71 const QPointF &p3, const QPointF &p4)
72 { return {p1.x(), p1.y(), p2.x(), p2.y(), p3.x(), p3.y(), p4.x(), p4.y()}; }
73
74 static void coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d);
75
76 inline QPointF pointAt(qreal t) const;
77 inline QPointF normalVector(qreal t) const;
78
79 inline QPointF derivedAt(qreal t) const;
80 inline QPointF secondDerivedAt(qreal t) const;
81
82 QPolygonF toPolygon(qreal bezier_flattening_threshold = 0.5) const;
83 void addToPolygon(QPolygonF *p, qreal bezier_flattening_threshold = 0.5) const;
84 void addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold) const;
85
86 QRectF bounds() const;
87 qreal length(qreal error = 0.01) const;
88 void addIfClose(qreal *length, qreal error) const;
89
90 qreal tAtLength(qreal len) const;
91
92 int stationaryYPoints(qreal &t0, qreal &t1) const;
93 qreal tForY(qreal t0, qreal t1, qreal y) const;
94
95 QPointF pt1() const { return QPointF(x1, y1); }
96 QPointF pt2() const { return QPointF(x2, y2); }
97 QPointF pt3() const { return QPointF(x3, y3); }
98 QPointF pt4() const { return QPointF(x4, y4); }
99
100 QBezier mapBy(const QTransform &transform) const;
101
102 inline QPointF midPoint() const;
103 inline QLineF midTangent() const;
104
105 inline QLineF startTangent() const;
106 inline QLineF endTangent() const;
107
108 inline void parameterSplitLeft(qreal t, QBezier *left);
109 inline std::pair<QBezier, QBezier> split() const;
110
111 int shifted(QBezier *curveSegments, int maxSegmets,
112 qreal offset, float threshold) const;
113
114 QBezier bezierOnInterval(qreal t0, qreal t1) const;
115 QBezier getSubRange(qreal t0, qreal t1) const;
116
117 qreal x1, y1, x2, y2, x3, y3, x4, y4;
118};
119
120inline QPointF QBezier::midPoint() const
121{
122 return QPointF((x1 + x4 + 3*(x2 + x3))/8., (y1 + y4 + 3*(y2 + y3))/8.);
123}
124
125inline QLineF QBezier::midTangent() const
126{
127 QPointF mid = midPoint();
128 QLineF dir(QLineF(x1, y1, x2, y2).pointAt(0.5), QLineF(x3, y3, x4, y4).pointAt(0.5));
129 return QLineF(mid.x() - dir.dx(), mid.y() - dir.dy(),
130 mid.x() + dir.dx(), mid.y() + dir.dy());
131}
132
133inline QLineF QBezier::startTangent() const
134{
135 QLineF tangent(pt1(), pt2());
136 if (tangent.isNull())
137 tangent = QLineF(pt1(), pt3());
138 if (tangent.isNull())
139 tangent = QLineF(pt1(), pt4());
140 return tangent;
141}
142
143inline QLineF QBezier::endTangent() const
144{
145 QLineF tangent(pt4(), pt3());
146 if (tangent.isNull())
147 tangent = QLineF(pt4(), pt2());
148 if (tangent.isNull())
149 tangent = QLineF(pt4(), pt1());
150 return tangent;
151}
152
153inline void QBezier::coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d)
154{
155 qreal m_t = 1. - t;
156 b = m_t * m_t;
157 c = t * t;
158 d = c * t;
159 a = b * m_t;
160 b *= 3. * t;
161 c *= 3. * m_t;
162}
163
164inline QPointF QBezier::pointAt(qreal t) const
165{
166 // numerically more stable:
167 qreal x, y;
168
169 qreal m_t = 1. - t;
170 {
171 qreal a = x1*m_t + x2*t;
172 qreal b = x2*m_t + x3*t;
173 qreal c = x3*m_t + x4*t;
174 a = a*m_t + b*t;
175 b = b*m_t + c*t;
176 x = a*m_t + b*t;
177 }
178 {
179 qreal a = y1*m_t + y2*t;
180 qreal b = y2*m_t + y3*t;
181 qreal c = y3*m_t + y4*t;
182 a = a*m_t + b*t;
183 b = b*m_t + c*t;
184 y = a*m_t + b*t;
185 }
186 return QPointF(x, y);
187}
188
189inline QPointF QBezier::normalVector(qreal t) const
190{
191 qreal m_t = 1. - t;
192 qreal a = m_t * m_t;
193 qreal b = t * m_t;
194 qreal c = t * t;
195
196 return QPointF((y2-y1) * a + (y3-y2) * b + (y4-y3) * c, -(x2-x1) * a - (x3-x2) * b - (x4-x3) * c);
197}
198
199inline QPointF QBezier::derivedAt(qreal t) const
200{
201 // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * t^2) * p2 + t^2 * p3)
202
203 qreal m_t = 1. - t;
204
205 qreal d = t * t;
206 qreal a = -m_t * m_t;
207 qreal b = 1 - 4 * t + 3 * d;
208 qreal c = 2 * t - 3 * d;
209
210 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
211 a * y1 + b * y2 + c * y3 + d * y4);
212}
213
214inline QPointF QBezier::secondDerivedAt(qreal t) const
215{
216 qreal a = 2. - 2. * t;
217 qreal b = -4 + 6 * t;
218 qreal c = 2 - 6 * t;
219 qreal d = 2 * t;
220
221 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
222 a * y1 + b * y2 + c * y3 + d * y4);
223}
224
225std::pair<QBezier, QBezier> QBezier::split() const
226{
227 const auto mid = [](QPointF lhs, QPointF rhs) { return (lhs + rhs) * .5; };
228
229 const QPointF mid_12 = mid(pt1(), pt2());
230 const QPointF mid_23 = mid(pt2(), pt3());
231 const QPointF mid_34 = mid(pt3(), pt4());
232 const QPointF mid_12_23 = mid(mid_12, mid_23);
233 const QPointF mid_23_34 = mid(mid_23, mid_34);
234 const QPointF mid_12_23__23_34 = mid(mid_12_23, mid_23_34);
235
236 return {
237 fromPoints(pt1(), mid_12, mid_12_23, mid_12_23__23_34),
238 fromPoints(mid_12_23__23_34, mid_23_34, mid_34, pt4()),
239 };
240}
241
242inline void QBezier::parameterSplitLeft(qreal t, QBezier *left)
243{
244 left->x1 = x1;
245 left->y1 = y1;
246
247 left->x2 = x1 + t * ( x2 - x1 );
248 left->y2 = y1 + t * ( y2 - y1 );
249
250 left->x3 = x2 + t * ( x3 - x2 ); // temporary holding spot
251 left->y3 = y2 + t * ( y3 - y2 ); // temporary holding spot
252
253 x3 = x3 + t * ( x4 - x3 );
254 y3 = y3 + t * ( y4 - y3 );
255
256 x2 = left->x3 + t * ( x3 - left->x3);
257 y2 = left->y3 + t * ( y3 - left->y3);
258
259 left->x3 = left->x2 + t * ( left->x3 - left->x2 );
260 left->y3 = left->y2 + t * ( left->y3 - left->y2 );
261
262 left->x4 = x1 = left->x3 + t * (x2 - left->x3);
263 left->y4 = y1 = left->y3 + t * (y2 - left->y3);
264}
265
266QT_END_NAMESPACE
267
268#endif // QBEZIER_P_H
269