1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QVECTOR4D_H
43 #define QVECTOR4D_H
44 
45 #include <QtCore/qpoint.h>
46 #include <QtCore/qmetatype.h>
47 
48 QT_BEGIN_HEADER
49 
50 QT_BEGIN_NAMESPACE
51 
52 QT_MODULE(Gui)
53 
54 class QMatrix4x4;
55 class QVector2D;
56 class QVector3D;
57 
58 #ifndef QT_NO_VECTOR4D
59 
60 class Q_GUI_EXPORT QVector4D
61 {
62 public:
63     QVector4D();
64     QVector4D(qreal xpos, qreal ypos, qreal zpos, qreal wpos);
65     explicit QVector4D(const QPoint& point);
66     explicit QVector4D(const QPointF& point);
67 #ifndef QT_NO_VECTOR2D
68     QVector4D(const QVector2D& vector);
69     QVector4D(const QVector2D& vector, qreal zpos, qreal wpos);
70 #endif
71 #ifndef QT_NO_VECTOR3D
72     QVector4D(const QVector3D& vector);
73     QVector4D(const QVector3D& vector, qreal wpos);
74 #endif
75 
76     bool isNull() const;
77 
78     qreal x() const;
79     qreal y() const;
80     qreal z() const;
81     qreal w() const;
82 
83     void setX(qreal x);
84     void setY(qreal y);
85     void setZ(qreal z);
86     void setW(qreal w);
87 
88     qreal length() const;
89     qreal lengthSquared() const;
90 
91     QVector4D normalized() const;
92     void normalize();
93 
94     QVector4D &operator+=(const QVector4D &vector);
95     QVector4D &operator-=(const QVector4D &vector);
96     QVector4D &operator*=(qreal factor);
97     QVector4D &operator*=(const QVector4D &vector);
98     QVector4D &operator/=(qreal divisor);
99 
100     static qreal dotProduct(const QVector4D& v1, const QVector4D& v2);
101 
102     friend inline bool operator==(const QVector4D &v1, const QVector4D &v2);
103     friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2);
104     friend inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2);
105     friend inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2);
106     friend inline const QVector4D operator*(qreal factor, const QVector4D &vector);
107     friend inline const QVector4D operator*(const QVector4D &vector, qreal factor);
108     friend inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2);
109     friend inline const QVector4D operator-(const QVector4D &vector);
110     friend inline const QVector4D operator/(const QVector4D &vector, qreal divisor);
111 
112     friend inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2);
113 
114 #ifndef QT_NO_VECTOR2D
115     QVector2D toVector2D() const;
116     QVector2D toVector2DAffine() const;
117 #endif
118 #ifndef QT_NO_VECTOR3D
119     QVector3D toVector3D() const;
120     QVector3D toVector3DAffine() const;
121 #endif
122 
123     QPoint toPoint() const;
124     QPointF toPointF() const;
125 
126     operator QVariant() const;
127 
128 private:
129     float xp, yp, zp, wp;
130 
131     QVector4D(float xpos, float ypos, float zpos, float wpos, int dummy);
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 
141 Q_DECLARE_TYPEINFO(QVector4D, Q_MOVABLE_TYPE);
142 
QVector4D()143 inline QVector4D::QVector4D() : xp(0.0f), yp(0.0f), zp(0.0f), wp(0.0f) {}
144 
QVector4D(qreal xpos,qreal ypos,qreal zpos,qreal wpos)145 inline QVector4D::QVector4D(qreal xpos, qreal ypos, qreal zpos, qreal wpos) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
146 
QVector4D(float xpos,float ypos,float zpos,float wpos,int)147 inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos, int) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
148 
QVector4D(const QPoint & point)149 inline QVector4D::QVector4D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
150 
QVector4D(const QPointF & point)151 inline QVector4D::QVector4D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
152 
isNull()153 inline bool QVector4D::isNull() const
154 {
155     return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
156 }
157 
x()158 inline qreal QVector4D::x() const { return qreal(xp); }
y()159 inline qreal QVector4D::y() const { return qreal(yp); }
z()160 inline qreal QVector4D::z() const { return qreal(zp); }
w()161 inline qreal QVector4D::w() const { return qreal(wp); }
162 
setX(qreal aX)163 inline void QVector4D::setX(qreal aX) { xp = aX; }
setY(qreal aY)164 inline void QVector4D::setY(qreal aY) { yp = aY; }
setZ(qreal aZ)165 inline void QVector4D::setZ(qreal aZ) { zp = aZ; }
setW(qreal aW)166 inline void QVector4D::setW(qreal aW) { wp = aW; }
167 
168 inline QVector4D &QVector4D::operator+=(const QVector4D &vector)
169 {
170     xp += vector.xp;
171     yp += vector.yp;
172     zp += vector.zp;
173     wp += vector.wp;
174     return *this;
175 }
176 
177 inline QVector4D &QVector4D::operator-=(const QVector4D &vector)
178 {
179     xp -= vector.xp;
180     yp -= vector.yp;
181     zp -= vector.zp;
182     wp -= vector.wp;
183     return *this;
184 }
185 
186 inline QVector4D &QVector4D::operator*=(qreal factor)
187 {
188     xp *= factor;
189     yp *= factor;
190     zp *= factor;
191     wp *= factor;
192     return *this;
193 }
194 
195 inline QVector4D &QVector4D::operator*=(const QVector4D &vector)
196 {
197     xp *= vector.xp;
198     yp *= vector.yp;
199     zp *= vector.zp;
200     wp *= vector.wp;
201     return *this;
202 }
203 
204 inline QVector4D &QVector4D::operator/=(qreal divisor)
205 {
206     xp /= divisor;
207     yp /= divisor;
208     zp /= divisor;
209     wp /= divisor;
210     return *this;
211 }
212 
213 inline bool operator==(const QVector4D &v1, const QVector4D &v2)
214 {
215     return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp && v1.wp == v2.wp;
216 }
217 
218 inline bool operator!=(const QVector4D &v1, const QVector4D &v2)
219 {
220     return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp || v1.wp != v2.wp;
221 }
222 
223 inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
224 {
225     return QVector4D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, v1.wp + v2.wp, 1);
226 }
227 
228 inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
229 {
230     return QVector4D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, v1.wp - v2.wp, 1);
231 }
232 
233 inline const QVector4D operator*(qreal factor, const QVector4D &vector)
234 {
235     return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor, 1);
236 }
237 
238 inline const QVector4D operator*(const QVector4D &vector, qreal factor)
239 {
240     return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor, 1);
241 }
242 
243 inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)
244 {
245     return QVector4D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, v1.wp * v2.wp, 1);
246 }
247 
248 inline const QVector4D operator-(const QVector4D &vector)
249 {
250     return QVector4D(-vector.xp, -vector.yp, -vector.zp, -vector.wp, 1);
251 }
252 
253 inline const QVector4D operator/(const QVector4D &vector, qreal divisor)
254 {
255     return QVector4D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, vector.wp / divisor, 1);
256 }
257 
qFuzzyCompare(const QVector4D & v1,const QVector4D & v2)258 inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)
259 {
260     return qFuzzyCompare(v1.xp, v2.xp) &&
261            qFuzzyCompare(v1.yp, v2.yp) &&
262            qFuzzyCompare(v1.zp, v2.zp) &&
263            qFuzzyCompare(v1.wp, v2.wp);
264 }
265 
toPoint()266 inline QPoint QVector4D::toPoint() const
267 {
268     return QPoint(qRound(xp), qRound(yp));
269 }
270 
toPointF()271 inline QPointF QVector4D::toPointF() const
272 {
273     return QPointF(qreal(xp), qreal(yp));
274 }
275 
276 #ifndef QT_NO_DEBUG_STREAM
277 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector);
278 #endif
279 
280 #ifndef QT_NO_DATASTREAM
281 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &);
282 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
283 #endif
284 
285 #endif
286 
287 QT_END_NAMESPACE
288 
289 QT_END_HEADER
290 
291 #endif
292