Qt 4.8
qvector3d.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 QVECTOR3D_H
43 #define QVECTOR3D_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 QVector4D;
57 
58 #ifndef QT_NO_VECTOR3D
59 
61 {
62 public:
63  QVector3D();
64  QVector3D(qreal xpos, qreal ypos, qreal zpos);
65  explicit QVector3D(const QPoint& point);
66  explicit QVector3D(const QPointF& point);
67 #ifndef QT_NO_VECTOR2D
68  QVector3D(const QVector2D& vector);
69  QVector3D(const QVector2D& vector, qreal zpos);
70 #endif
71 #ifndef QT_NO_VECTOR4D
72  explicit QVector3D(const QVector4D& vector);
73 #endif
74 
75  bool isNull() const;
76 
77  qreal x() const;
78  qreal y() const;
79  qreal z() const;
80 
81  void setX(qreal x);
82  void setY(qreal y);
83  void setZ(qreal z);
84 
85  qreal length() const;
86  qreal lengthSquared() const;
87 
88  QVector3D normalized() const;
89  void normalize();
90 
91  QVector3D &operator+=(const QVector3D &vector);
92  QVector3D &operator-=(const QVector3D &vector);
93  QVector3D &operator*=(qreal factor);
94  QVector3D &operator*=(const QVector3D& vector);
95  QVector3D &operator/=(qreal divisor);
96 
97  static qreal dotProduct(const QVector3D& v1, const QVector3D& v2);
98  static QVector3D crossProduct(const QVector3D& v1, const QVector3D& v2);
99  static QVector3D normal(const QVector3D& v1, const QVector3D& v2);
100  static QVector3D normal
101  (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3);
102 
103  qreal distanceToPlane(const QVector3D& plane, const QVector3D& normal) const;
104  qreal distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const;
105  qreal distanceToLine(const QVector3D& point, const QVector3D& direction) const;
106 
107  friend inline bool operator==(const QVector3D &v1, const QVector3D &v2);
108  friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2);
109  friend inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2);
110  friend inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2);
111  friend inline const QVector3D operator*(qreal factor, const QVector3D &vector);
112  friend inline const QVector3D operator*(const QVector3D &vector, qreal factor);
113  friend const QVector3D operator*(const QVector3D &v1, const QVector3D& v2);
114  friend inline const QVector3D operator-(const QVector3D &vector);
115  friend inline const QVector3D operator/(const QVector3D &vector, qreal divisor);
116 
117  friend inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2);
118 
119 #ifndef QT_NO_VECTOR2D
120  QVector2D toVector2D() const;
121 #endif
122 #ifndef QT_NO_VECTOR4D
123  QVector4D toVector4D() const;
124 #endif
125 
126  QPoint toPoint() const;
127  QPointF toPointF() const;
128 
129  operator QVariant() const;
130 
131 private:
132  float xp, yp, zp;
133 
134  QVector3D(float xpos, float ypos, float zpos, int dummy);
135 
136  friend class QVector2D;
137  friend class QVector4D;
138 #ifndef QT_NO_MATRIX4X4
139  friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
140  friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
141 #endif
142 };
143 
145 
146 inline QVector3D::QVector3D() : xp(0.0f), yp(0.0f), zp(0.0f) {}
147 
148 inline QVector3D::QVector3D(qreal xpos, qreal ypos, qreal zpos) : xp(xpos), yp(ypos), zp(zpos) {}
149 
150 inline QVector3D::QVector3D(float xpos, float ypos, float zpos, int) : xp(xpos), yp(ypos), zp(zpos) {}
151 
152 inline QVector3D::QVector3D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
153 
154 inline QVector3D::QVector3D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
155 
156 inline bool QVector3D::isNull() const
157 {
158  return qIsNull(xp) && qIsNull(yp) && qIsNull(zp);
159 }
160 
161 inline qreal QVector3D::x() const { return qreal(xp); }
162 inline qreal QVector3D::y() const { return qreal(yp); }
163 inline qreal QVector3D::z() const { return qreal(zp); }
164 
165 inline void QVector3D::setX(qreal aX) { xp = aX; }
166 inline void QVector3D::setY(qreal aY) { yp = aY; }
167 inline void QVector3D::setZ(qreal aZ) { zp = aZ; }
168 
170 {
171  xp += vector.xp;
172  yp += vector.yp;
173  zp += vector.zp;
174  return *this;
175 }
176 
178 {
179  xp -= vector.xp;
180  yp -= vector.yp;
181  zp -= vector.zp;
182  return *this;
183 }
184 
186 {
187  xp *= factor;
188  yp *= factor;
189  zp *= factor;
190  return *this;
191 }
192 
194 {
195  xp *= vector.xp;
196  yp *= vector.yp;
197  zp *= vector.zp;
198  return *this;
199 }
200 
202 {
203  xp /= divisor;
204  yp /= divisor;
205  zp /= divisor;
206  return *this;
207 }
208 
209 inline bool operator==(const QVector3D &v1, const QVector3D &v2)
210 {
211  return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp;
212 }
213 
214 inline bool operator!=(const QVector3D &v1, const QVector3D &v2)
215 {
216  return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp;
217 }
218 
219 inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2)
220 {
221  return QVector3D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, 1);
222 }
223 
224 inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2)
225 {
226  return QVector3D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, 1);
227 }
228 
229 inline const QVector3D operator*(qreal factor, const QVector3D &vector)
230 {
231  return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
232 }
233 
234 inline const QVector3D operator*(const QVector3D &vector, qreal factor)
235 {
236  return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
237 }
238 
239 inline const QVector3D operator*(const QVector3D &v1, const QVector3D& v2)
240 {
241  return QVector3D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, 1);
242 }
243 
244 inline const QVector3D operator-(const QVector3D &vector)
245 {
246  return QVector3D(-vector.xp, -vector.yp, -vector.zp, 1);
247 }
248 
249 inline const QVector3D operator/(const QVector3D &vector, qreal divisor)
250 {
251  return QVector3D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, 1);
252 }
253 
254 inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2)
255 {
256  return qFuzzyCompare(v1.xp, v2.xp) &&
257  qFuzzyCompare(v1.yp, v2.yp) &&
258  qFuzzyCompare(v1.zp, v2.zp);
259 }
260 
262 {
263  return QPoint(qRound(xp), qRound(yp));
264 }
265 
267 {
268  return QPointF(qreal(xp), qreal(yp));
269 }
270 
271 #ifndef QT_NO_DEBUG_STREAM
272 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector3D &vector);
273 #endif
274 
275 #ifndef QT_NO_DATASTREAM
278 #endif
279 
280 #endif
281 
283 
285 
286 #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
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
QVector3D & operator-=(const QVector3D &vector)
Subtracts the given vector from this vector and returns a reference to this vector.
Definition: qvector3d.h:177
double qreal
Definition: qglobal.h:1193
QVector3D & operator+=(const QVector3D &vector)
Adds the given vector to this vector and returns a reference to this vector.
Definition: qvector3d.h:169
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
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
bool qFuzzyCompare(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:254
const QVector3D operator/(const QVector3D &vector, qreal divisor)
Definition: qvector3d.h:249
friend bool operator==(const QVector3D &v1, const QVector3D &v2)
Returns true if v1 is equal to v2; otherwise returns false.
Definition: qvector3d.h:209
float yp
Definition: qvector4d.h:129
bool operator!=(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:214
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
static bool qIsNull(double d)
Definition: qglobal.h:2061
Q_DECLARE_TYPEINFO(QVector3D, Q_MOVABLE_TYPE)
float yp
Definition: qvector3d.h:132
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool operator==(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:209
QPoint toPoint() const
Returns the QPoint form of this 3D vector.
Definition: qvector3d.h:261
QDataStream & operator>>(QDataStream &stream, QVector3D &vector)
Reads a 3D vector from the given stream into the given vector and returns a reference to the stream...
Definition: qvector3d.cpp:694
void setX(qreal x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qvector3d.h:165
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:63
QVector3D & operator/=(qreal divisor)
Divides this vector&#39;s coordinates by the given divisor, and returns a reference to this vector...
Definition: qvector3d.h:201
The QVector2D class represents a vector or vertex in 2D space.
Definition: qvector2d.h:60
QVector3D & operator*=(qreal factor)
Multiplies this vector&#39;s coordinates by the given factor, and returns a reference to this vector...
Definition: qvector3d.h:185
QPointF toPointF() const
Returns the QPointF form of this 3D vector.
Definition: qvector3d.h:266
friend const QVector3D operator+(const QVector3D &v1, const QVector3D &v2)
Returns a QVector3D object that is the sum of the given vectors, v1 and v2; each component is added s...
Definition: qvector3d.h:219
QDataStream & operator<<(QDataStream &stream, const QVector3D &vector)
Writes the given vector to the given stream and returns a reference to the stream.
Definition: qvector3d.cpp:674
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
const QVector3D operator-(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:224
bool isNull() const
Returns true if the x, y, and z coordinates are set to 0.
Definition: qvector3d.h:156
float zp
Definition: qvector4d.h:129
const QVector3D operator+(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:219
const QVector3D operator*(qreal factor, const QVector3D &vector)
Definition: qvector3d.h:229
void setZ(qreal z)
Sets the z coordinate of this point to the given z coordinate.
Definition: qvector3d.h:167
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162
friend const QVector3D operator/(const QVector3D &vector, qreal divisor)
Returns the QVector3D object formed by dividing all three components of the given vector by the given...
Definition: qvector3d.h:249
float xp
Definition: qvector3d.h:132
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
friend const QVector3D operator-(const QVector3D &v1, const QVector3D &v2)
Returns a QVector3D object that is formed by subtracting v2 from v1; each component is subtracted sep...
Definition: qvector3d.h:224
timeval & operator+=(timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:120
#define QT_END_HEADER
Definition: qglobal.h:137
float xp
Definition: qvector4d.h:129
static void normalize(double &x, double &y)
friend bool qFuzzyCompare(const QVector3D &v1, const QVector3D &v2)
Returns true if v1 and v2 are equal, allowing for a small fuzziness factor for floating-point compari...
Definition: qvector3d.h:254
friend bool operator!=(const QVector3D &v1, const QVector3D &v2)
Returns true if v1 is not equal to v2; otherwise returns false.
Definition: qvector3d.h:214
void setY(qreal y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qvector3d.h:166
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
friend const QVector3D operator*(qreal factor, const QVector3D &vector)
Definition: qvector3d.h:229
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
Qt::LayoutDirection direction