QtGui/qvector4d.h Source File

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 
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 
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 
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 
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
281 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &);
282 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
283 #endif
284 
285 #endif
286 
288 
290 
291 #endif
GLuint GLenum matrix
Definition: GLee.h:6572
void setY(qreal y)
Definition: qvector4d.h:164
GLenum GLint GLint y
Definition: GLee.h:876
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector)
QVector4D & operator-=(const QVector4D &vector)
Definition: qvector4d.h:177
#define QT_END_NAMESPACE
Definition: qglobal.h:128
GLdouble GLdouble z
Definition: GLee.h:1393
Q_DECLARE_TYPEINFO(QVector4D, Q_MOVABLE_TYPE)
QByteArray & operator+=(QByteArray &a, const QStringBuilder< A, B > &b)
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
GLfloat GLfloat GLfloat v2
Definition: GLee.h:1736
const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:223
qreal x() const
Definition: qvector4d.h:158
Definition: qdebug.h:62
bool qFuzzyCompare(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:258
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QVector4D &)
Definition: qpoint.h:53
const QVector4D operator/(const QVector4D &vector, qreal divisor)
Definition: qvector4d.h:253
qreal w() const
Definition: qvector4d.h:161
qreal y() const
Definition: qvector4d.h:159
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
qreal z() const
Definition: qvector4d.h:160
GLfloat GLfloat v1
Definition: GLee.h:1735
GLenum GLint x
Definition: GLee.h:876
bool operator!=(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:218
QPointF toPointF() const
Definition: qvector4d.h:271
bool isNull() const
Definition: qvector4d.h:153
GLuint GLsizei GLsizei * length
Definition: GLee.h:1713
const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:228
bool operator==(const QVector4D &v1, const QVector4D &v2)
Definition: qvector4d.h:213
friend class QVector4D
Definition: qvector3d.h:137
QVector4D & operator*=(qreal factor)
Definition: qvector4d.h:186
QVector4D & operator+=(const QVector4D &vector)
Definition: qvector4d.h:168
GLint GLenum GLboolean normalized
Definition: GLee.h:1790
void setZ(qreal z)
Definition: qvector4d.h:165
void setX(qreal x)
Definition: qvector4d.h:163
void setW(qreal w)
Definition: qvector4d.h:166
QVector4D & operator/=(qreal divisor)
Definition: qvector4d.h:204
GLubyte GLubyte GLubyte GLubyte w
Definition: GLee.h:1775
GLXDrawable int64_t int64_t divisor
Definition: GLee.h:10690
const QVector4D operator*(qreal factor, const QVector4D &vector)
Definition: qvector4d.h:233
#define QT_END_HEADER
Definition: qglobal.h:142
GLclampf f
Definition: GLee.h:9303
QPoint toPoint() const
Definition: qvector4d.h:266