QtGui/qvector3d.h Source File

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 
60 class Q_GUI_EXPORT QVector3D
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 
144 Q_DECLARE_TYPEINFO(QVector3D, Q_MOVABLE_TYPE);
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 
185 inline QVector3D &QVector3D::operator*=(qreal factor)
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
276 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector3D &);
277 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &);
278 #endif
279 
280 #endif
281 
283 
285 
286 #endif
friend class QVector3D
Definition: qvector4d.h:134
GLuint GLenum matrix
Definition: GLee.h:6572
GLenum GLint GLint y
Definition: GLee.h:876
#define QT_END_NAMESPACE
Definition: qglobal.h:128
GLdouble GLdouble z
Definition: GLee.h:1393
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
QPoint toPoint() const
Definition: qvector3d.h:261
bool qFuzzyCompare(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:254
const QVector3D operator/(const QVector3D &vector, qreal divisor)
Definition: qvector3d.h:249
QVector3D & operator/=(qreal divisor)
Definition: qvector3d.h:201
bool operator!=(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:214
QVector3D & operator-=(const QVector3D &vector)
Definition: qvector3d.h:177
Q_DECLARE_TYPEINFO(QVector3D, Q_MOVABLE_TYPE)
Definition: qdebug.h:62
QPointF toPointF() const
Definition: qvector3d.h:266
QVector3D & operator*=(qreal factor)
Definition: qvector3d.h:185
Definition: qpoint.h:53
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
GLfloat GLfloat v1
Definition: GLee.h:1735
QVector3D & operator+=(const QVector3D &vector)
Definition: qvector3d.h:169
bool operator==(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:209
qreal x() const
Definition: qvector3d.h:161
void setX(qreal x)
Definition: qvector3d.h:165
GLenum GLint x
Definition: GLee.h:876
const QVector3D operator-(const QVector3D &v1, const QVector3D &v2)
Definition: qvector3d.h:224
GLuint GLsizei GLsizei * length
Definition: GLee.h:1713
qreal y() const
Definition: qvector3d.h:162
qreal z() const
Definition: qvector3d.h:163
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
GLint GLenum GLboolean normalized
Definition: GLee.h:1790
bool isNull() const
Definition: qvector3d.h:156
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector3D &vector)
void setY(qreal y)
Definition: qvector3d.h:166
GLXDrawable int64_t int64_t divisor
Definition: GLee.h:10690
#define QT_END_HEADER
Definition: qglobal.h:142
GLfloat GLfloat GLfloat GLfloat v3
Definition: GLee.h:1737
GLclampf f
Definition: GLee.h:9303
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QVector3D &)
void setZ(qreal z)
Definition: qvector3d.h:167