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 QVECTOR4D_H
41#define QVECTOR4D_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtCore/qpoint.h>
45#include <QtCore/qmetatype.h>
46
47QT_BEGIN_NAMESPACE
48
49
50class QMatrix4x4;
51class QVector2D;
52class QVector3D;
53
54#ifndef QT_NO_VECTOR4D
55
56class Q_GUI_EXPORT QVector4D
57{
58public:
59 Q_DECL_CONSTEXPR QVector4D();
60 explicit QVector4D(Qt::Initialization) {}
61 Q_DECL_CONSTEXPR QVector4D(float xpos, float ypos, float zpos, float wpos);
62 Q_DECL_CONSTEXPR explicit QVector4D(const QPoint& point);
63 Q_DECL_CONSTEXPR explicit QVector4D(const QPointF& point);
64#ifndef QT_NO_VECTOR2D
65 QVector4D(const QVector2D& vector);
66 QVector4D(const QVector2D& vector, float zpos, float wpos);
67#endif
68#ifndef QT_NO_VECTOR3D
69 QVector4D(const QVector3D& vector);
70 QVector4D(const QVector3D& vector, float wpos);
71#endif
72
73 bool isNull() const;
74
75 Q_DECL_CONSTEXPR float x() const;
76 Q_DECL_CONSTEXPR float y() const;
77 Q_DECL_CONSTEXPR float z() const;
78 Q_DECL_CONSTEXPR float w() const;
79
80 void setX(float x);
81 void setY(float y);
82 void setZ(float z);
83 void setW(float w);
84
85 float &operator[](int i);
86 float operator[](int i) const;
87
88 float length() const;
89 float lengthSquared() const; //In Qt 6 convert to inline and constexpr
90
91 Q_REQUIRED_RESULT QVector4D normalized() const;
92 void normalize();
93
94 QVector4D &operator+=(const QVector4D &vector);
95 QVector4D &operator-=(const QVector4D &vector);
96 QVector4D &operator*=(float factor);
97 QVector4D &operator*=(const QVector4D &vector);
98 QVector4D &operator/=(float divisor);
99 inline QVector4D &operator/=(const QVector4D &vector);
100
101 static float dotProduct(const QVector4D& v1, const QVector4D& v2); //In Qt 6 convert to inline and constexpr
102
103 Q_DECL_CONSTEXPR friend inline bool operator==(const QVector4D &v1, const QVector4D &v2);
104 Q_DECL_CONSTEXPR friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2);
105 Q_DECL_CONSTEXPR friend inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2);
106 Q_DECL_CONSTEXPR friend inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2);
107 Q_DECL_CONSTEXPR friend inline const QVector4D operator*(float factor, const QVector4D &vector);
108 Q_DECL_CONSTEXPR friend inline const QVector4D operator*(const QVector4D &vector, float factor);
109 Q_DECL_CONSTEXPR friend inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2);
110 Q_DECL_CONSTEXPR friend inline const QVector4D operator-(const QVector4D &vector);
111 Q_DECL_CONSTEXPR friend inline const QVector4D operator/(const QVector4D &vector, float divisor);
112 Q_DECL_CONSTEXPR friend inline const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor);
113
114 Q_DECL_CONSTEXPR friend inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2);
115
116#ifndef QT_NO_VECTOR2D
117 QVector2D toVector2D() const;
118 QVector2D toVector2DAffine() const;
119#endif
120#ifndef QT_NO_VECTOR3D
121 QVector3D toVector3D() const;
122 QVector3D toVector3DAffine() const;
123#endif
124
125 Q_DECL_CONSTEXPR QPoint toPoint() const;
126 Q_DECL_CONSTEXPR QPointF toPointF() const;
127
128 operator QVariant() const;
129
130private:
131 float v[4];
132
133 friend class QVector2D;
134 friend class QVector3D;
135#ifndef QT_NO_MATRIX4X4
136 friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
137 friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
138#endif
139};
140
141Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE);
142
143Q_DECL_CONSTEXPR inline QVector4D::QVector4D() : v{0.0f, 0.0f, 0.0f, 0.0f} {}
144
145Q_DECL_CONSTEXPR inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) : v{xpos, ypos, zpos, wpos} {}
146
147Q_DECL_CONSTEXPR inline QVector4D::QVector4D(const QPoint& point) : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {}
148
149Q_DECL_CONSTEXPR inline QVector4D::QVector4D(const QPointF& point) : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {}
150
151inline bool QVector4D::isNull() const
152{
153 return qIsNull(f: v[0]) && qIsNull(f: v[1]) && qIsNull(f: v[2]) && qIsNull(f: v[3]);
154}
155
156Q_DECL_CONSTEXPR inline float QVector4D::x() const { return v[0]; }
157Q_DECL_CONSTEXPR inline float QVector4D::y() const { return v[1]; }
158Q_DECL_CONSTEXPR inline float QVector4D::z() const { return v[2]; }
159Q_DECL_CONSTEXPR inline float QVector4D::w() const { return v[3]; }
160
161inline void QVector4D::setX(float aX) { v[0] = aX; }
162inline void QVector4D::setY(float aY) { v[1] = aY; }
163inline void QVector4D::setZ(float aZ) { v[2] = aZ; }
164inline void QVector4D::setW(float aW) { v[3] = aW; }
165
166inline float &QVector4D::operator[](int i)
167{
168 Q_ASSERT(uint(i) < 4u);
169 return v[i];
170}
171
172inline float QVector4D::operator[](int i) const
173{
174 Q_ASSERT(uint(i) < 4u);
175 return v[i];
176}
177
178inline QVector4D &QVector4D::operator+=(const QVector4D &vector)
179{
180 v[0] += vector.v[0];
181 v[1] += vector.v[1];
182 v[2] += vector.v[2];
183 v[3] += vector.v[3];
184 return *this;
185}
186
187inline QVector4D &QVector4D::operator-=(const QVector4D &vector)
188{
189 v[0] -= vector.v[0];
190 v[1] -= vector.v[1];
191 v[2] -= vector.v[2];
192 v[3] -= vector.v[3];
193 return *this;
194}
195
196inline QVector4D &QVector4D::operator*=(float factor)
197{
198 v[0] *= factor;
199 v[1] *= factor;
200 v[2] *= factor;
201 v[3] *= factor;
202 return *this;
203}
204
205inline QVector4D &QVector4D::operator*=(const QVector4D &vector)
206{
207 v[0] *= vector.v[0];
208 v[1] *= vector.v[1];
209 v[2] *= vector.v[2];
210 v[3] *= vector.v[3];
211 return *this;
212}
213
214inline QVector4D &QVector4D::operator/=(float divisor)
215{
216 v[0] /= divisor;
217 v[1] /= divisor;
218 v[2] /= divisor;
219 v[3] /= divisor;
220 return *this;
221}
222
223inline QVector4D &QVector4D::operator/=(const QVector4D &vector)
224{
225 v[0] /= vector.v[0];
226 v[1] /= vector.v[1];
227 v[2] /= vector.v[2];
228 v[3] /= vector.v[3];
229 return *this;
230}
231
232QT_WARNING_PUSH
233QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
234QT_WARNING_DISABLE_GCC("-Wfloat-equal")
235QT_WARNING_DISABLE_INTEL(1572)
236Q_DECL_CONSTEXPR inline bool operator==(const QVector4D &v1, const QVector4D &v2)
237{
238 return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2] && v1.v[3] == v2.v[3];
239}
240
241Q_DECL_CONSTEXPR inline bool operator!=(const QVector4D &v1, const QVector4D &v2)
242{
243 return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2] || v1.v[3] != v2.v[3];
244}
245QT_WARNING_POP
246
247Q_DECL_CONSTEXPR inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
248{
249 return QVector4D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2], v1.v[3] + v2.v[3]);
250}
251
252Q_DECL_CONSTEXPR inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
253{
254 return QVector4D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2], v1.v[3] - v2.v[3]);
255}
256
257Q_DECL_CONSTEXPR inline const QVector4D operator*(float factor, const QVector4D &vector)
258{
259 return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor);
260}
261
262Q_DECL_CONSTEXPR inline const QVector4D operator*(const QVector4D &vector, float factor)
263{
264 return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor);
265}
266
267Q_DECL_CONSTEXPR inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)
268{
269 return QVector4D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2], v1.v[3] * v2.v[3]);
270}
271
272Q_DECL_CONSTEXPR inline const QVector4D operator-(const QVector4D &vector)
273{
274 return QVector4D(-vector.v[0], -vector.v[1], -vector.v[2], -vector.v[3]);
275}
276
277Q_DECL_CONSTEXPR inline const QVector4D operator/(const QVector4D &vector, float divisor)
278{
279 return QVector4D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor, vector.v[3] / divisor);
280}
281
282Q_DECL_CONSTEXPR inline const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor)
283{
284 return QVector4D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1], vector.v[2] / divisor.v[2], vector.v[3] / divisor.v[3]);
285}
286
287Q_DECL_CONSTEXPR inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)
288{
289 return qFuzzyCompare(p1: v1.v[0], p2: v2.v[0]) &&
290 qFuzzyCompare(p1: v1.v[1], p2: v2.v[1]) &&
291 qFuzzyCompare(p1: v1.v[2], p2: v2.v[2]) &&
292 qFuzzyCompare(p1: v1.v[3], p2: v2.v[3]);
293}
294
295Q_DECL_CONSTEXPR inline QPoint QVector4D::toPoint() const
296{
297 return QPoint(qRound(d: v[0]), qRound(d: v[1]));
298}
299
300Q_DECL_CONSTEXPR inline QPointF QVector4D::toPointF() const
301{
302 return QPointF(qreal(v[0]), qreal(v[1]));
303}
304
305#ifndef QT_NO_DEBUG_STREAM
306Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector);
307#endif
308
309#ifndef QT_NO_DATASTREAM
310Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &);
311Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
312#endif
313
314#endif
315
316QT_END_NAMESPACE
317
318#endif
319

source code of qtbase/src/gui/math3d/qvector4d.h