1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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 | inline QVector4D::QVector4D() : xp(0.0f), yp(0.0f), zp(0.0f), wp(0.0f) {}
|
---|
142 |
|
---|
143 | inline QVector4D::QVector4D(qreal xpos, qreal ypos, qreal zpos, qreal wpos) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
|
---|
144 |
|
---|
145 | inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos, int) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
|
---|
146 |
|
---|
147 | inline QVector4D::QVector4D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
|
---|
148 |
|
---|
149 | inline QVector4D::QVector4D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
|
---|
150 |
|
---|
151 | inline bool QVector4D::isNull() const
|
---|
152 | {
|
---|
153 | return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
|
---|
154 | }
|
---|
155 |
|
---|
156 | inline qreal QVector4D::x() const { return qreal(xp); }
|
---|
157 | inline qreal QVector4D::y() const { return qreal(yp); }
|
---|
158 | inline qreal QVector4D::z() const { return qreal(zp); }
|
---|
159 | inline qreal QVector4D::w() const { return qreal(wp); }
|
---|
160 |
|
---|
161 | inline void QVector4D::setX(qreal aX) { xp = aX; }
|
---|
162 | inline void QVector4D::setY(qreal aY) { yp = aY; }
|
---|
163 | inline void QVector4D::setZ(qreal aZ) { zp = aZ; }
|
---|
164 | inline void QVector4D::setW(qreal aW) { wp = aW; }
|
---|
165 |
|
---|
166 | inline QVector4D &QVector4D::operator+=(const QVector4D &vector)
|
---|
167 | {
|
---|
168 | xp += vector.xp;
|
---|
169 | yp += vector.yp;
|
---|
170 | zp += vector.zp;
|
---|
171 | wp += vector.wp;
|
---|
172 | return *this;
|
---|
173 | }
|
---|
174 |
|
---|
175 | inline QVector4D &QVector4D::operator-=(const QVector4D &vector)
|
---|
176 | {
|
---|
177 | xp -= vector.xp;
|
---|
178 | yp -= vector.yp;
|
---|
179 | zp -= vector.zp;
|
---|
180 | wp -= vector.wp;
|
---|
181 | return *this;
|
---|
182 | }
|
---|
183 |
|
---|
184 | inline QVector4D &QVector4D::operator*=(qreal factor)
|
---|
185 | {
|
---|
186 | xp *= factor;
|
---|
187 | yp *= factor;
|
---|
188 | zp *= factor;
|
---|
189 | wp *= factor;
|
---|
190 | return *this;
|
---|
191 | }
|
---|
192 |
|
---|
193 | inline QVector4D &QVector4D::operator*=(const QVector4D &vector)
|
---|
194 | {
|
---|
195 | xp *= vector.xp;
|
---|
196 | yp *= vector.yp;
|
---|
197 | zp *= vector.zp;
|
---|
198 | wp *= vector.wp;
|
---|
199 | return *this;
|
---|
200 | }
|
---|
201 |
|
---|
202 | inline QVector4D &QVector4D::operator/=(qreal divisor)
|
---|
203 | {
|
---|
204 | xp /= divisor;
|
---|
205 | yp /= divisor;
|
---|
206 | zp /= divisor;
|
---|
207 | wp /= divisor;
|
---|
208 | return *this;
|
---|
209 | }
|
---|
210 |
|
---|
211 | inline bool operator==(const QVector4D &v1, const QVector4D &v2)
|
---|
212 | {
|
---|
213 | return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp && v1.wp == v2.wp;
|
---|
214 | }
|
---|
215 |
|
---|
216 | inline bool operator!=(const QVector4D &v1, const QVector4D &v2)
|
---|
217 | {
|
---|
218 | return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp || v1.wp != v2.wp;
|
---|
219 | }
|
---|
220 |
|
---|
221 | inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
|
---|
222 | {
|
---|
223 | return QVector4D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, v1.wp + v2.wp, 1);
|
---|
224 | }
|
---|
225 |
|
---|
226 | inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
|
---|
227 | {
|
---|
228 | return QVector4D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, v1.wp - v2.wp, 1);
|
---|
229 | }
|
---|
230 |
|
---|
231 | inline const QVector4D operator*(qreal factor, const QVector4D &vector)
|
---|
232 | {
|
---|
233 | return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor, 1);
|
---|
234 | }
|
---|
235 |
|
---|
236 | inline const QVector4D operator*(const QVector4D &vector, qreal factor)
|
---|
237 | {
|
---|
238 | return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor, 1);
|
---|
239 | }
|
---|
240 |
|
---|
241 | inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)
|
---|
242 | {
|
---|
243 | return QVector4D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, v1.wp * v2.wp, 1);
|
---|
244 | }
|
---|
245 |
|
---|
246 | inline const QVector4D operator-(const QVector4D &vector)
|
---|
247 | {
|
---|
248 | return QVector4D(-vector.xp, -vector.yp, -vector.zp, -vector.wp, 1);
|
---|
249 | }
|
---|
250 |
|
---|
251 | inline const QVector4D operator/(const QVector4D &vector, qreal divisor)
|
---|
252 | {
|
---|
253 | return QVector4D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, vector.wp / divisor, 1);
|
---|
254 | }
|
---|
255 |
|
---|
256 | inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)
|
---|
257 | {
|
---|
258 | return qFuzzyCompare(v1.xp, v2.xp) &&
|
---|
259 | qFuzzyCompare(v1.yp, v2.yp) &&
|
---|
260 | qFuzzyCompare(v1.zp, v2.zp) &&
|
---|
261 | qFuzzyCompare(v1.wp, v2.wp);
|
---|
262 | }
|
---|
263 |
|
---|
264 | inline QPoint QVector4D::toPoint() const
|
---|
265 | {
|
---|
266 | return QPoint(qRound(xp), qRound(yp));
|
---|
267 | }
|
---|
268 |
|
---|
269 | inline QPointF QVector4D::toPointF() const
|
---|
270 | {
|
---|
271 | return QPointF(qreal(xp), qreal(yp));
|
---|
272 | }
|
---|
273 |
|
---|
274 | #ifndef QT_NO_DEBUG_STREAM
|
---|
275 | Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector);
|
---|
276 | #endif
|
---|
277 |
|
---|
278 | #ifndef QT_NO_DATASTREAM
|
---|
279 | Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &);
|
---|
280 | Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
|
---|
281 | #endif
|
---|
282 |
|
---|
283 | #endif
|
---|
284 |
|
---|
285 | QT_END_NAMESPACE
|
---|
286 |
|
---|
287 | QT_END_HEADER
|
---|
288 |
|
---|
289 | #endif
|
---|