/src/qtbase/src/gui/math3d/qvector3d.h
Line | Count | Source |
1 | | /**************************************************************************** |
2 | | ** |
3 | | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | | ** Contact: https://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 https://www.qt.io/terms-conditions. For further |
15 | | ** information use the contact form at https://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 3 as published by the Free Software |
20 | | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | | ** packaging of this file. Please review the following information to |
22 | | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | | ** |
25 | | ** GNU General Public License Usage |
26 | | ** Alternatively, this file may be used under the terms of the GNU |
27 | | ** General Public License version 2.0 or (at your option) the GNU General |
28 | | ** Public license version 3 or any later version approved by the KDE Free |
29 | | ** Qt Foundation. The licenses are as published by the Free Software |
30 | | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | | ** included in the packaging of this file. Please review the following |
32 | | ** information to ensure the GNU General Public License requirements will |
33 | | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | | ** |
36 | | ** $QT_END_LICENSE$ |
37 | | ** |
38 | | ****************************************************************************/ |
39 | | |
40 | | #ifndef QVECTOR3D_H |
41 | | #define QVECTOR3D_H |
42 | | |
43 | | #include <QtGui/qtguiglobal.h> |
44 | | #include <QtCore/qpoint.h> |
45 | | #include <QtCore/qmetatype.h> |
46 | | |
47 | | QT_BEGIN_NAMESPACE |
48 | | |
49 | | |
50 | | class QMatrix4x4; |
51 | | class QVector2D; |
52 | | class QVector4D; |
53 | | class QRect; |
54 | | |
55 | | #ifndef QT_NO_VECTOR3D |
56 | | |
57 | | class Q_GUI_EXPORT QVector3D |
58 | | { |
59 | | public: |
60 | | Q_DECL_CONSTEXPR QVector3D(); |
61 | 0 | explicit QVector3D(Qt::Initialization) {} |
62 | 0 | Q_DECL_CONSTEXPR QVector3D(float xpos, float ypos, float zpos) : v{xpos, ypos, zpos} {} |
63 | | |
64 | | Q_DECL_CONSTEXPR explicit QVector3D(const QPoint& point); |
65 | | Q_DECL_CONSTEXPR 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 | | Q_DECL_CONSTEXPR float x() const; |
77 | | Q_DECL_CONSTEXPR float y() const; |
78 | | Q_DECL_CONSTEXPR float z() const; |
79 | | |
80 | | void setX(float x); |
81 | | void setY(float y); |
82 | | void setZ(float z); |
83 | | |
84 | | float &operator[](int i); |
85 | | float operator[](int i) const; |
86 | | |
87 | | float length() const; |
88 | | float lengthSquared() const; |
89 | | |
90 | | QVector3D normalized() const; |
91 | | void normalize(); |
92 | | |
93 | | QVector3D &operator+=(const QVector3D &vector); |
94 | | QVector3D &operator-=(const QVector3D &vector); |
95 | | QVector3D &operator*=(float factor); |
96 | | QVector3D &operator*=(const QVector3D& vector); |
97 | | QVector3D &operator/=(float divisor); |
98 | | inline QVector3D &operator/=(const QVector3D &vector); |
99 | | |
100 | | static float dotProduct(const QVector3D& v1, const QVector3D& v2); //In Qt 6 convert to inline and constexpr |
101 | | static QVector3D crossProduct(const QVector3D& v1, const QVector3D& v2); //in Qt 6 convert to inline and constexpr |
102 | | |
103 | | static QVector3D normal(const QVector3D& v1, const QVector3D& v2); |
104 | | static QVector3D normal |
105 | | (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3); |
106 | | |
107 | | QVector3D project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const; |
108 | | QVector3D unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const; |
109 | | |
110 | | float distanceToPoint(const QVector3D& point) const; |
111 | | float distanceToPlane(const QVector3D& plane, const QVector3D& normal) const; |
112 | | float distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const; |
113 | | float distanceToLine(const QVector3D& point, const QVector3D& direction) const; |
114 | | |
115 | | Q_DECL_CONSTEXPR friend inline bool operator==(const QVector3D &v1, const QVector3D &v2); |
116 | | Q_DECL_CONSTEXPR friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2); |
117 | | Q_DECL_CONSTEXPR friend inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2); |
118 | | Q_DECL_CONSTEXPR friend inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2); |
119 | | Q_DECL_CONSTEXPR friend inline const QVector3D operator*(float factor, const QVector3D &vector); |
120 | | Q_DECL_CONSTEXPR friend inline const QVector3D operator*(const QVector3D &vector, float factor); |
121 | | Q_DECL_CONSTEXPR friend const QVector3D operator*(const QVector3D &v1, const QVector3D& v2); |
122 | | Q_DECL_CONSTEXPR friend inline const QVector3D operator-(const QVector3D &vector); |
123 | | Q_DECL_CONSTEXPR friend inline const QVector3D operator/(const QVector3D &vector, float divisor); |
124 | | Q_DECL_CONSTEXPR friend inline const QVector3D operator/(const QVector3D &vector, const QVector3D &divisor); |
125 | | |
126 | | Q_DECL_CONSTEXPR friend inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2); |
127 | | |
128 | | #ifndef QT_NO_VECTOR2D |
129 | | QVector2D toVector2D() const; |
130 | | #endif |
131 | | #ifndef QT_NO_VECTOR4D |
132 | | QVector4D toVector4D() const; |
133 | | #endif |
134 | | |
135 | | Q_DECL_CONSTEXPR QPoint toPoint() const; |
136 | | Q_DECL_CONSTEXPR QPointF toPointF() const; |
137 | | |
138 | | operator QVariant() const; |
139 | | |
140 | | private: |
141 | | float v[3]; |
142 | | |
143 | | friend class QVector2D; |
144 | | friend class QVector4D; |
145 | | #ifndef QT_NO_MATRIX4X4 |
146 | | friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix); |
147 | | friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector); |
148 | | #endif |
149 | | }; |
150 | | |
151 | | Q_DECLARE_TYPEINFO(QVector3D, Q_PRIMITIVE_TYPE); |
152 | | |
153 | 0 | Q_DECL_CONSTEXPR inline QVector3D::QVector3D() : v{0.0f, 0.0f, 0.0f} {} |
154 | | |
155 | | Q_DECL_CONSTEXPR inline QVector3D::QVector3D(const QPoint& point) : v{float(point.x()), float(point.y()), float(0.0f)} {} |
156 | | |
157 | | Q_DECL_CONSTEXPR inline QVector3D::QVector3D(const QPointF& point) : v{float(point.x()), float(point.y()), 0.0f} {} |
158 | | |
159 | | inline bool QVector3D::isNull() const |
160 | 0 | { |
161 | 0 | return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]); |
162 | 0 | } |
163 | | |
164 | 0 | Q_DECL_CONSTEXPR inline float QVector3D::x() const { return v[0]; } |
165 | 0 | Q_DECL_CONSTEXPR inline float QVector3D::y() const { return v[1]; } |
166 | 0 | Q_DECL_CONSTEXPR inline float QVector3D::z() const { return v[2]; } |
167 | | |
168 | 0 | inline void QVector3D::setX(float aX) { v[0] = aX; } |
169 | 0 | inline void QVector3D::setY(float aY) { v[1] = aY; } |
170 | 0 | inline void QVector3D::setZ(float aZ) { v[2] = aZ; } |
171 | | |
172 | | inline float &QVector3D::operator[](int i) |
173 | 0 | { |
174 | 0 | Q_ASSERT(uint(i) < 3u); |
175 | 0 | return v[i]; |
176 | 0 | } |
177 | | |
178 | | inline float QVector3D::operator[](int i) const |
179 | 0 | { |
180 | 0 | Q_ASSERT(uint(i) < 3u); |
181 | 0 | return v[i]; |
182 | 0 | } |
183 | | |
184 | | inline QVector3D &QVector3D::operator+=(const QVector3D &vector) |
185 | 0 | { |
186 | 0 | v[0] += vector.v[0]; |
187 | 0 | v[1] += vector.v[1]; |
188 | 0 | v[2] += vector.v[2]; |
189 | 0 | return *this; |
190 | 0 | } |
191 | | |
192 | | inline QVector3D &QVector3D::operator-=(const QVector3D &vector) |
193 | 0 | { |
194 | 0 | v[0] -= vector.v[0]; |
195 | 0 | v[1] -= vector.v[1]; |
196 | 0 | v[2] -= vector.v[2]; |
197 | 0 | return *this; |
198 | 0 | } |
199 | | |
200 | | inline QVector3D &QVector3D::operator*=(float factor) |
201 | 0 | { |
202 | 0 | v[0] *= factor; |
203 | 0 | v[1] *= factor; |
204 | 0 | v[2] *= factor; |
205 | 0 | return *this; |
206 | 0 | } |
207 | | |
208 | | inline QVector3D &QVector3D::operator*=(const QVector3D& vector) |
209 | 0 | { |
210 | 0 | v[0] *= vector.v[0]; |
211 | 0 | v[1] *= vector.v[1]; |
212 | 0 | v[2] *= vector.v[2]; |
213 | 0 | return *this; |
214 | 0 | } |
215 | | |
216 | | inline QVector3D &QVector3D::operator/=(float divisor) |
217 | 0 | { |
218 | 0 | v[0] /= divisor; |
219 | 0 | v[1] /= divisor; |
220 | 0 | v[2] /= divisor; |
221 | 0 | return *this; |
222 | 0 | } |
223 | | |
224 | | inline QVector3D &QVector3D::operator/=(const QVector3D &vector) |
225 | 0 | { |
226 | 0 | v[0] /= vector.v[0]; |
227 | 0 | v[1] /= vector.v[1]; |
228 | 0 | v[2] /= vector.v[2]; |
229 | 0 | return *this; |
230 | 0 | } |
231 | | |
232 | | QT_WARNING_PUSH |
233 | | QT_WARNING_DISABLE_CLANG("-Wfloat-equal") |
234 | | QT_WARNING_DISABLE_GCC("-Wfloat-equal") |
235 | | QT_WARNING_DISABLE_INTEL(1572) |
236 | | Q_DECL_CONSTEXPR inline bool operator==(const QVector3D &v1, const QVector3D &v2) |
237 | 0 | { |
238 | 0 | return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2]; |
239 | 0 | } |
240 | | |
241 | | Q_DECL_CONSTEXPR inline bool operator!=(const QVector3D &v1, const QVector3D &v2) |
242 | 0 | { |
243 | 0 | return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2]; |
244 | 0 | } |
245 | | QT_WARNING_POP |
246 | | |
247 | | Q_DECL_CONSTEXPR inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2) |
248 | 0 | { |
249 | 0 | return QVector3D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2]); |
250 | 0 | } |
251 | | |
252 | | Q_DECL_CONSTEXPR inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2) |
253 | 0 | { |
254 | 0 | return QVector3D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2]); |
255 | 0 | } |
256 | | |
257 | | Q_DECL_CONSTEXPR inline const QVector3D operator*(float factor, const QVector3D &vector) |
258 | 0 | { |
259 | 0 | return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor); |
260 | 0 | } |
261 | | |
262 | | Q_DECL_CONSTEXPR inline const QVector3D operator*(const QVector3D &vector, float factor) |
263 | 0 | { |
264 | 0 | return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor); |
265 | 0 | } |
266 | | |
267 | | Q_DECL_CONSTEXPR inline const QVector3D operator*(const QVector3D &v1, const QVector3D& v2) |
268 | 0 | { |
269 | 0 | return QVector3D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2]); |
270 | 0 | } |
271 | | |
272 | | Q_DECL_CONSTEXPR inline const QVector3D operator-(const QVector3D &vector) |
273 | 0 | { |
274 | 0 | return QVector3D(-vector.v[0], -vector.v[1], -vector.v[2]); |
275 | 0 | } |
276 | | |
277 | | Q_DECL_CONSTEXPR inline const QVector3D operator/(const QVector3D &vector, float divisor) |
278 | 0 | { |
279 | 0 | return QVector3D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor); |
280 | 0 | } |
281 | | |
282 | | Q_DECL_CONSTEXPR inline const QVector3D operator/(const QVector3D &vector, const QVector3D &divisor) |
283 | 0 | { |
284 | 0 | return QVector3D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1], vector.v[2] / divisor.v[2]); |
285 | 0 | } |
286 | | |
287 | | Q_DECL_CONSTEXPR inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2) |
288 | 0 | { |
289 | 0 | return qFuzzyCompare(v1.v[0], v2.v[0]) && |
290 | 0 | qFuzzyCompare(v1.v[1], v2.v[1]) && |
291 | 0 | qFuzzyCompare(v1.v[2], v2.v[2]); |
292 | 0 | } |
293 | | |
294 | | Q_DECL_CONSTEXPR inline QPoint QVector3D::toPoint() const |
295 | 0 | { |
296 | 0 | return QPoint(qRound(v[0]), qRound(v[1])); |
297 | 0 | } |
298 | | |
299 | | Q_DECL_CONSTEXPR inline QPointF QVector3D::toPointF() const |
300 | 0 | { |
301 | 0 | return QPointF(qreal(v[0]), qreal(v[1])); |
302 | 0 | } |
303 | | |
304 | | #ifndef QT_NO_DEBUG_STREAM |
305 | | Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector3D &vector); |
306 | | #endif |
307 | | |
308 | | #ifndef QT_NO_DATASTREAM |
309 | | Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector3D &); |
310 | | Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &); |
311 | | #endif |
312 | | |
313 | | #endif |
314 | | |
315 | | QT_END_NAMESPACE |
316 | | |
317 | | #endif |