1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
45 #include <QtCore/qpoint.h>
46 #include <QtCore/qmetatype.h>
57 #ifndef QT_NO_VECTOR4D
59 class Q_GUI_EXPORT QVector4D
63 QVector4D(float xpos, float ypos, float zpos, float wpos);
64 explicit QVector4D(const QPoint& point);
65 explicit QVector4D(const QPointF& point);
66 #ifndef QT_NO_VECTOR2D
67 QVector4D(const QVector2D& vector);
68 QVector4D(const QVector2D& vector, float zpos, float wpos);
70 #ifndef QT_NO_VECTOR3D
71 QVector4D(const QVector3D& vector);
72 QVector4D(const QVector3D& vector, float wpos);
88 float lengthSquared() const;
90 QVector4D normalized() const;
93 QVector4D &operator+=(const QVector4D &vector);
94 QVector4D &operator-=(const QVector4D &vector);
95 QVector4D &operator*=(float factor);
96 QVector4D &operator*=(const QVector4D &vector);
97 QVector4D &operator/=(float divisor);
99 static float dotProduct(const QVector4D& v1, const QVector4D& v2);
101 friend inline bool operator==(const QVector4D &v1, const QVector4D &v2);
102 friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2);
103 friend inline const QVector4D 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*(float factor, const QVector4D &vector);
106 friend inline const QVector4D operator*(const QVector4D &vector, float factor);
107 friend inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2);
108 friend inline const QVector4D operator-(const QVector4D &vector);
109 friend inline const QVector4D operator/(const QVector4D &vector, float divisor);
111 friend inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2);
113 #ifndef QT_NO_VECTOR2D
114 QVector2D toVector2D() const;
115 QVector2D toVector2DAffine() const;
117 #ifndef QT_NO_VECTOR3D
118 QVector3D toVector3D() const;
119 QVector3D toVector3DAffine() const;
122 QPoint toPoint() const;
123 QPointF toPointF() const;
125 operator QVariant() const;
128 float xp, yp, zp, wp;
130 friend class QVector2D;
131 friend class QVector3D;
132 #ifndef QT_NO_MATRIX4X4
133 friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
134 friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
138 Q_DECLARE_TYPEINFO(QVector4D, Q_MOVABLE_TYPE);
140 inline QVector4D::QVector4D() : xp(0.0f), yp(0.0f), zp(0.0f), wp(0.0f) {}
142 inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
144 inline QVector4D::QVector4D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
146 inline QVector4D::QVector4D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
148 inline bool QVector4D::isNull() const
150 return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
153 inline float QVector4D::x() const { return xp; }
154 inline float QVector4D::y() const { return yp; }
155 inline float QVector4D::z() const { return zp; }
156 inline float QVector4D::w() const { return wp; }
158 inline void QVector4D::setX(float aX) { xp = aX; }
159 inline void QVector4D::setY(float aY) { yp = aY; }
160 inline void QVector4D::setZ(float aZ) { zp = aZ; }
161 inline void QVector4D::setW(float aW) { wp = aW; }
163 inline QVector4D &QVector4D::operator+=(const QVector4D &vector)
172 inline QVector4D &QVector4D::operator-=(const QVector4D &vector)
181 inline QVector4D &QVector4D::operator*=(float factor)
190 inline QVector4D &QVector4D::operator*=(const QVector4D &vector)
199 inline QVector4D &QVector4D::operator/=(float divisor)
208 inline bool operator==(const QVector4D &v1, const QVector4D &v2)
210 return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp && v1.wp == v2.wp;
213 inline bool operator!=(const QVector4D &v1, const QVector4D &v2)
215 return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp || v1.wp != v2.wp;
218 inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
220 return QVector4D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, v1.wp + v2.wp);
223 inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
225 return QVector4D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, v1.wp - v2.wp);
228 inline const QVector4D operator*(float factor, const QVector4D &vector)
230 return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor);
233 inline const QVector4D operator*(const QVector4D &vector, float factor)
235 return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor);
238 inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)
240 return QVector4D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, v1.wp * v2.wp);
243 inline const QVector4D operator-(const QVector4D &vector)
245 return QVector4D(-vector.xp, -vector.yp, -vector.zp, -vector.wp);
248 inline const QVector4D operator/(const QVector4D &vector, float divisor)
250 return QVector4D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, vector.wp / divisor);
253 inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)
255 return qFuzzyCompare(v1.xp, v2.xp) &&
256 qFuzzyCompare(v1.yp, v2.yp) &&
257 qFuzzyCompare(v1.zp, v2.zp) &&
258 qFuzzyCompare(v1.wp, v2.wp);
261 inline QPoint QVector4D::toPoint() const
263 return QPoint(qRound(xp), qRound(yp));
266 inline QPointF QVector4D::toPointF() const
268 return QPointF(qreal(xp), qreal(yp));
271 #ifndef QT_NO_DEBUG_STREAM
272 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector);
275 #ifndef QT_NO_DATASTREAM
276 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &);
277 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);