Qt 4.8
qvector4d.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
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 
49 
51 
52 QT_MODULE(Gui)
53 
54 class QMatrix4x4;
55 class QVector2D;
56 class QVector3D;
57 
58 #ifndef QT_NO_VECTOR4D
59 
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 
142 
143 inline QVector4D::QVector4D() : xp(0.0f), yp(0.0f), zp(0.0f), wp(0.0f) {}
144 
145 inline QVector4D::QVector4D(qreal xpos, qreal ypos, qreal zpos, qreal wpos) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
146 
147 inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos, int) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
148 
149 inline QVector4D::QVector4D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
150 
151 inline QVector4D::QVector4D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
152 
153 inline bool QVector4D::isNull() const
154 {
155  return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
156 }
157 
158 inline qreal QVector4D::x() const { return qreal(xp); }
159 inline qreal QVector4D::y() const { return qreal(yp); }
160 inline qreal QVector4D::z() const { return qreal(zp); }
161 inline qreal QVector4D::w() const { return qreal(wp); }
162 
163 inline void QVector4D::setX(qreal aX) { xp = aX; }
164 inline void QVector4D::setY(qreal aY) { yp = aY; }
165 inline void QVector4D::setZ(qreal aZ) { zp = aZ; }
166 inline void QVector4D::setW(qreal aW) { wp = aW; }
167 
169 {
170  xp += vector.xp;
171  yp += vector.yp;
172  zp += vector.zp;
173  wp += vector.wp;
174  return *this;
175 }
176 
178 {
179  xp -= vector.xp;
180  yp -= vector.yp;
181  zp -= vector.zp;
182  wp -= vector.wp;
183  return *this;
184 }
185 
187 {
188  xp *= factor;
189  yp *= factor;
190  zp *= factor;
191  wp *= factor;
192  return *this;
193 }
194 
196 {
197  xp *= vector.xp;
198  yp *= vector.yp;
199  zp *= vector.zp;
200  wp *= vector.wp;
201  return *this;
202 }
203 
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 
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 
267 {
268  return QPoint(qRound(xp), qRound(yp));
269 }
270 
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
283 #endif
284 
285 #endif
286 
288 
290 
291 #endif
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
friend const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
Returns a QVector4D object that is formed by subtracting v2 from v1; each component is subtracted sep...
Definition: qvector4d.h:228
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
Q_DECLARE_TYPEINFO(QVector4D, Q_MOVABLE_TYPE)
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
bool isNull() const
Returns true if the x, y, z, and w coordinates are set to 0.
Definition: qvector4d.h:153
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
qreal w() const
Returns the w coordinate of this point.
Definition: qvector4d.h:161
The QVector4D class represents a vector or vertex in 4D space.
Definition: qvector4d.h:60
float zp
Definition: qvector3d.h:132
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:223
float yp
Definition: qvector4d.h:129
static bool qIsNull(double d)
Definition: qglobal.h:2061
float yp
Definition: qvector3d.h:132
bool qFuzzyCompare(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:258
const QVector4D operator/(const QVector4D &vector, qreal divisor)
Definition: qvector4d.h:253
QPointF toPointF() const
Returns the QPointF form of this 4D vector.
Definition: qvector4d.h:271
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QPoint toPoint() const
Returns the QPoint form of this 4D vector.
Definition: qvector4d.h:266
qreal x() const
Returns the x coordinate of this point.
Definition: qvector4d.h:158
void setZ(qreal z)
Sets the z coordinate of this point to the given z coordinate.
Definition: qvector4d.h:165
QDataStream & operator<<(QDataStream &stream, const QVector4D &vector)
Writes the given vector to the given stream and returns a reference to the stream.
Definition: qvector4d.cpp:627
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:63
QVector4D & operator+=(const QVector4D &vector)
Adds the given vector to this vector and returns a reference to this vector.
Definition: qvector4d.h:168
float wp
Definition: qvector4d.h:129
The QVector2D class represents a vector or vertex in 2D space.
Definition: qvector2d.h:60
bool operator!=(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:218
friend bool operator==(const QVector4D &v1, const QVector4D &v2)
Returns true if v1 is equal to v2; otherwise returns false.
Definition: qvector4d.h:213
QVector4D()
Constructs a null vector, i.
Definition: qvector4d.h:143
friend const QVector4D operator*(qreal factor, const QVector4D &vector)
Definition: qvector4d.h:233
friend const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
Returns a QVector4D object that is the sum of the given vectors, v1 and v2; each component is added s...
Definition: qvector4d.h:223
friend bool operator!=(const QVector4D &v1, const QVector4D &v2)
Returns true if v1 is not equal to v2; otherwise returns false.
Definition: qvector4d.h:218
const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:228
void setW(qreal w)
Sets the w coordinate of this point to the given w coordinate.
Definition: qvector4d.h:166
bool operator==(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:213
friend bool qFuzzyCompare(const QVector4D &v1, const QVector4D &v2)
Returns true if v1 and v2 are equal, allowing for a small fuzziness factor for floating-point compari...
Definition: qvector4d.h:258
float zp
Definition: qvector4d.h:129
void setX(qreal x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qvector4d.h:163
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QVector4D & operator*=(qreal factor)
Multiplies this vector&#39;s coordinates by the given factor, and returns a reference to this vector...
Definition: qvector4d.h:186
QVector4D & operator-=(const QVector4D &vector)
Subtracts the given vector from this vector and returns a reference to this vector.
Definition: qvector4d.h:177
float xp
Definition: qvector3d.h:132
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QVector4D & operator/=(qreal divisor)
Divides this vector&#39;s coordinates by the given divisor, and returns a reference to this vector...
Definition: qvector4d.h:204
const QVector4D operator*(qreal factor, const QVector4D &vector)
Definition: qvector4d.h:233
void setY(qreal y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qvector4d.h:164
timeval & operator+=(timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:120
QDataStream & operator>>(QDataStream &stream, QVector4D &vector)
Reads a 4D vector from the given stream into the given vector and returns a reference to the stream...
Definition: qvector4d.cpp:647
qreal z() const
Returns the z coordinate of this point.
Definition: qvector4d.h:160
#define QT_END_HEADER
Definition: qglobal.h:137
float xp
Definition: qvector4d.h:129
static void normalize(double &x, double &y)
qreal y() const
Returns the y coordinate of this point.
Definition: qvector4d.h:159
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
friend const QVector4D operator/(const QVector4D &vector, qreal divisor)
Returns the QVector4D object formed by dividing all four components of the given vector by the given ...
Definition: qvector4d.h:253