Merge "Merge branch 'newdocs'" into refs/staging/master
[profile/ivi/qtbase.git] / src / gui / math3d / qvector3d.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
48 QT_BEGIN_HEADER
49
50 QT_BEGIN_NAMESPACE
51
52
53 class QMatrix4x4;
54 class QVector2D;
55 class QVector4D;
56
57 #ifndef QT_NO_VECTOR3D
58
59 class Q_GUI_EXPORT QVector3D
60 {
61 public:
62     QVector3D();
63     QVector3D(float xpos, float ypos, float zpos);
64     explicit QVector3D(const QPoint& point);
65     explicit QVector3D(const QPointF& point);
66 #ifndef QT_NO_VECTOR2D
67     QVector3D(const QVector2D& vector);
68     QVector3D(const QVector2D& vector, float zpos);
69 #endif
70 #ifndef QT_NO_VECTOR4D
71     explicit QVector3D(const QVector4D& vector);
72 #endif
73
74     bool isNull() const;
75
76     float x() const;
77     float y() const;
78     float z() const;
79
80     void setX(float x);
81     void setY(float y);
82     void setZ(float z);
83
84     float length() const;
85     float lengthSquared() const;
86
87     QVector3D normalized() const;
88     void normalize();
89
90     QVector3D &operator+=(const QVector3D &vector);
91     QVector3D &operator-=(const QVector3D &vector);
92     QVector3D &operator*=(float factor);
93     QVector3D &operator*=(const QVector3D& vector);
94     QVector3D &operator/=(float divisor);
95
96     static float dotProduct(const QVector3D& v1, const QVector3D& v2);
97     static QVector3D crossProduct(const QVector3D& v1, const QVector3D& v2);
98     static QVector3D normal(const QVector3D& v1, const QVector3D& v2);
99     static QVector3D normal
100         (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3);
101
102     float distanceToPlane(const QVector3D& plane, const QVector3D& normal) const;
103     float distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const;
104     float distanceToLine(const QVector3D& point, const QVector3D& direction) const;
105
106     friend inline bool operator==(const QVector3D &v1, const QVector3D &v2);
107     friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2);
108     friend inline const QVector3D 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*(float factor, const QVector3D &vector);
111     friend inline const QVector3D operator*(const QVector3D &vector, float factor);
112     friend const QVector3D operator*(const QVector3D &v1, const QVector3D& v2);
113     friend inline const QVector3D operator-(const QVector3D &vector);
114     friend inline const QVector3D operator/(const QVector3D &vector, float divisor);
115
116     friend inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2);
117
118 #ifndef QT_NO_VECTOR2D
119     QVector2D toVector2D() const;
120 #endif
121 #ifndef QT_NO_VECTOR4D
122     QVector4D toVector4D() const;
123 #endif
124
125     QPoint toPoint() const;
126     QPointF toPointF() const;
127
128     operator QVariant() const;
129
130 private:
131     float xp, yp, zp;
132
133     friend class QVector2D;
134     friend class QVector4D;
135 #ifndef QT_NO_MATRIX4X4
136     friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
137     friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
138 #endif
139 };
140
141 Q_DECLARE_TYPEINFO(QVector3D, Q_MOVABLE_TYPE);
142
143 inline QVector3D::QVector3D() : xp(0.0f), yp(0.0f), zp(0.0f) {}
144
145 inline QVector3D::QVector3D(float xpos, float ypos, float zpos) : xp(xpos), yp(ypos), zp(zpos) {}
146
147 inline QVector3D::QVector3D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
148
149 inline QVector3D::QVector3D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
150
151 inline bool QVector3D::isNull() const
152 {
153     return qIsNull(xp) && qIsNull(yp) && qIsNull(zp);
154 }
155
156 inline float QVector3D::x() const { return xp; }
157 inline float QVector3D::y() const { return yp; }
158 inline float QVector3D::z() const { return zp; }
159
160 inline void QVector3D::setX(float aX) { xp = aX; }
161 inline void QVector3D::setY(float aY) { yp = aY; }
162 inline void QVector3D::setZ(float aZ) { zp = aZ; }
163
164 inline QVector3D &QVector3D::operator+=(const QVector3D &vector)
165 {
166     xp += vector.xp;
167     yp += vector.yp;
168     zp += vector.zp;
169     return *this;
170 }
171
172 inline QVector3D &QVector3D::operator-=(const QVector3D &vector)
173 {
174     xp -= vector.xp;
175     yp -= vector.yp;
176     zp -= vector.zp;
177     return *this;
178 }
179
180 inline QVector3D &QVector3D::operator*=(float factor)
181 {
182     xp *= factor;
183     yp *= factor;
184     zp *= factor;
185     return *this;
186 }
187
188 inline QVector3D &QVector3D::operator*=(const QVector3D& vector)
189 {
190     xp *= vector.xp;
191     yp *= vector.yp;
192     zp *= vector.zp;
193     return *this;
194 }
195
196 inline QVector3D &QVector3D::operator/=(float divisor)
197 {
198     xp /= divisor;
199     yp /= divisor;
200     zp /= divisor;
201     return *this;
202 }
203
204 inline bool operator==(const QVector3D &v1, const QVector3D &v2)
205 {
206     return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp;
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 const QVector3D operator+(const QVector3D &v1, const QVector3D &v2)
215 {
216     return QVector3D(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);
222 }
223
224 inline const QVector3D operator*(float factor, const QVector3D &vector)
225 {
226     return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor);
227 }
228
229 inline const QVector3D operator*(const QVector3D &vector, float factor)
230 {
231     return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor);
232 }
233
234 inline const QVector3D operator*(const QVector3D &v1, const QVector3D& v2)
235 {
236     return QVector3D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp);
237 }
238
239 inline const QVector3D operator-(const QVector3D &vector)
240 {
241     return QVector3D(-vector.xp, -vector.yp, -vector.zp);
242 }
243
244 inline const QVector3D operator/(const QVector3D &vector, float divisor)
245 {
246     return QVector3D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor);
247 }
248
249 inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2)
250 {
251     return qFuzzyCompare(v1.xp, v2.xp) &&
252            qFuzzyCompare(v1.yp, v2.yp) &&
253            qFuzzyCompare(v1.zp, v2.zp);
254 }
255
256 inline QPoint QVector3D::toPoint() const
257 {
258     return QPoint(qRound(xp), qRound(yp));
259 }
260
261 inline QPointF QVector3D::toPointF() const
262 {
263     return QPointF(qreal(xp), qreal(yp));
264 }
265
266 #ifndef QT_NO_DEBUG_STREAM
267 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector3D &vector);
268 #endif
269
270 #ifndef QT_NO_DATASTREAM
271 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector3D &);
272 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &);
273 #endif
274
275 #endif
276
277 QT_END_NAMESPACE
278
279 QT_END_HEADER
280
281 #endif