1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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
48 QT_BEGIN_HEADER
49
50 QT_BEGIN_NAMESPACE
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
QVector3D()146 inline QVector3D::QVector3D() : xp(0.0f), yp(0.0f), zp(0.0f) {}
147
QVector3D(qreal xpos,qreal ypos,qreal zpos)148 inline QVector3D::QVector3D(qreal xpos, qreal ypos, qreal zpos) : xp(xpos), yp(ypos), zp(zpos) {}
149
QVector3D(float xpos,float ypos,float zpos,int)150 inline QVector3D::QVector3D(float xpos, float ypos, float zpos, int) : xp(xpos), yp(ypos), zp(zpos) {}
151
QVector3D(const QPoint & point)152 inline QVector3D::QVector3D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
153
QVector3D(const QPointF & point)154 inline QVector3D::QVector3D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
155
isNull()156 inline bool QVector3D::isNull() const
157 {
158 return qIsNull(xp) && qIsNull(yp) && qIsNull(zp);
159 }
160
x()161 inline qreal QVector3D::x() const { return qreal(xp); }
y()162 inline qreal QVector3D::y() const { return qreal(yp); }
z()163 inline qreal QVector3D::z() const { return qreal(zp); }
164
setX(qreal aX)165 inline void QVector3D::setX(qreal aX) { xp = aX; }
setY(qreal aY)166 inline void QVector3D::setY(qreal aY) { yp = aY; }
setZ(qreal aZ)167 inline void QVector3D::setZ(qreal aZ) { zp = aZ; }
168
169 inline QVector3D &QVector3D::operator+=(const QVector3D &vector)
170 {
171 xp += vector.xp;
172 yp += vector.yp;
173 zp += vector.zp;
174 return *this;
175 }
176
177 inline QVector3D &QVector3D::operator-=(const QVector3D &vector)
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
193 inline QVector3D &QVector3D::operator*=(const QVector3D& vector)
194 {
195 xp *= vector.xp;
196 yp *= vector.yp;
197 zp *= vector.zp;
198 return *this;
199 }
200
201 inline QVector3D &QVector3D::operator/=(qreal divisor)
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
qFuzzyCompare(const QVector3D & v1,const QVector3D & v2)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
toPoint()261 inline QPoint QVector3D::toPoint() const
262 {
263 return QPoint(qRound(xp), qRound(yp));
264 }
265
toPointF()266 inline QPointF QVector3D::toPointF() const
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
282 QT_END_NAMESPACE
283
284 QT_END_HEADER
285
286 #endif
287