Warning, file /include/QtGui/qvectornd.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005 #ifndef QVECTORND_H
0006 #define QVECTORND_H
0007
0008 #include <QtGui/qtguiglobal.h>
0009 #include <QtCore/qpoint.h>
0010 #include <QtCore/qrect.h>
0011 #include <QtCore/qmath.h>
0012
0013 #include <QtCore/q20type_traits.h>
0014 #include <QtCore/q23utility.h>
0015
0016 QT_BEGIN_NAMESPACE
0017
0018
0019
0020 class QVector2D;
0021 class QVector3D;
0022 class QVector4D;
0023 class QMatrix4x4;
0024 class QVariant;
0025
0026
0027
0028 #ifndef QT_NO_VECTOR2D
0029
0030 class QVector2D
0031 {
0032 public:
0033 constexpr QVector2D() noexcept;
0034 explicit QVector2D(Qt::Initialization) noexcept {}
0035 constexpr QVector2D(float xpos, float ypos) noexcept;
0036 constexpr explicit QVector2D(QPoint point) noexcept;
0037 constexpr explicit QVector2D(QPointF point) noexcept;
0038 #ifndef QT_NO_VECTOR3D
0039 constexpr explicit QVector2D(QVector3D vector) noexcept;
0040 #endif
0041 #ifndef QT_NO_VECTOR4D
0042 constexpr explicit QVector2D(QVector4D vector) noexcept;
0043 #endif
0044
0045 constexpr bool isNull() const noexcept;
0046
0047 constexpr float x() const noexcept;
0048 constexpr float y() const noexcept;
0049
0050 constexpr void setX(float x) noexcept;
0051 constexpr void setY(float y) noexcept;
0052
0053 constexpr float &operator[](int i);
0054 constexpr float operator[](int i) const;
0055
0056 [[nodiscard]] float length() const noexcept;
0057 [[nodiscard]] constexpr float lengthSquared() const noexcept;
0058
0059 [[nodiscard]] QVector2D normalized() const noexcept;
0060 void normalize() noexcept;
0061
0062 [[nodiscard]] float distanceToPoint(QVector2D point) const noexcept;
0063 [[nodiscard]] float distanceToLine(QVector2D point, QVector2D direction) const noexcept;
0064
0065 constexpr QVector2D &operator+=(QVector2D vector) noexcept;
0066 constexpr QVector2D &operator-=(QVector2D vector) noexcept;
0067 constexpr QVector2D &operator*=(float factor) noexcept;
0068 constexpr QVector2D &operator*=(QVector2D vector) noexcept;
0069 constexpr QVector2D &operator/=(float divisor);
0070 constexpr QVector2D &operator/=(QVector2D vector);
0071
0072 [[nodiscard]] static constexpr float dotProduct(QVector2D v1, QVector2D v2) noexcept;
0073
0074 QT_WARNING_PUSH
0075 QT_WARNING_DISABLE_FLOAT_COMPARE
0076 constexpr friend inline bool operator==(QVector2D v1, QVector2D v2) noexcept
0077 {
0078 return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1];
0079 }
0080
0081 constexpr friend inline bool operator!=(QVector2D v1, QVector2D v2) noexcept
0082 {
0083 return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1];
0084 }
0085 QT_WARNING_POP
0086
0087 constexpr friend inline QVector2D operator+(QVector2D v1, QVector2D v2) noexcept
0088 {
0089 return QVector2D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1]);
0090 }
0091
0092 constexpr friend inline QVector2D operator-(QVector2D v1, QVector2D v2) noexcept
0093 {
0094 return QVector2D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1]);
0095 }
0096
0097 constexpr friend inline QVector2D operator*(float factor, QVector2D vector) noexcept
0098 {
0099 return QVector2D(vector.v[0] * factor, vector.v[1] * factor);
0100 }
0101
0102 constexpr friend inline QVector2D operator*(QVector2D vector, float factor) noexcept
0103 {
0104 return QVector2D(vector.v[0] * factor, vector.v[1] * factor);
0105 }
0106
0107 constexpr friend inline QVector2D operator*(QVector2D v1, QVector2D v2) noexcept
0108 {
0109 return QVector2D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1]);
0110 }
0111
0112 constexpr friend inline QVector2D operator-(QVector2D vector) noexcept
0113 {
0114 return QVector2D(-vector.v[0], -vector.v[1]);
0115 }
0116
0117 constexpr friend inline QVector2D operator/(QVector2D vector, float divisor)
0118 {
0119 Q_ASSERT(divisor < 0 || divisor > 0);
0120 return QVector2D(vector.v[0] / divisor, vector.v[1] / divisor);
0121 }
0122
0123 constexpr friend inline QVector2D operator/(QVector2D vector, QVector2D divisor)
0124 {
0125 Q_ASSERT(divisor.v[0] < 0 || divisor.v[0] > 0);
0126 Q_ASSERT(divisor.v[1] < 0 || divisor.v[1] > 0);
0127 return QVector2D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1]);
0128 }
0129
0130 friend Q_GUI_EXPORT bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept;
0131
0132 #ifndef QT_NO_VECTOR3D
0133 constexpr QVector3D toVector3D() const noexcept;
0134 #endif
0135 #ifndef QT_NO_VECTOR4D
0136 constexpr QVector4D toVector4D() const noexcept;
0137 #endif
0138
0139 constexpr QPoint toPoint() const noexcept;
0140 constexpr QPointF toPointF() const noexcept;
0141
0142 Q_GUI_EXPORT operator QVariant() const;
0143
0144 private:
0145 float v[2];
0146
0147 friend class QVector3D;
0148 friend class QVector4D;
0149
0150 template <std::size_t I,
0151 typename V,
0152 std::enable_if_t<(I < 2), bool> = true,
0153 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<V>, QVector2D>, bool> = true>
0154 friend constexpr decltype(auto) get(V &&vec) noexcept
0155 {
0156 return q23::forward_like<V>(vec.v[I]);
0157 }
0158 };
0159
0160 Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE);
0161
0162 #endif
0163
0164
0165
0166
0167
0168 #ifndef QT_NO_VECTOR3D
0169
0170 class QVector3D
0171 {
0172 public:
0173 constexpr QVector3D() noexcept;
0174 explicit QVector3D(Qt::Initialization) noexcept {}
0175 constexpr QVector3D(float xpos, float ypos, float zpos) noexcept : v{xpos, ypos, zpos} {}
0176
0177 constexpr explicit QVector3D(QPoint point) noexcept;
0178 constexpr explicit QVector3D(QPointF point) noexcept;
0179 #ifndef QT_NO_VECTOR2D
0180 constexpr explicit QVector3D(QVector2D vector) noexcept;
0181 constexpr QVector3D(QVector2D vector, float zpos) noexcept;
0182 #endif
0183 #ifndef QT_NO_VECTOR4D
0184 constexpr explicit QVector3D(QVector4D vector) noexcept;
0185 #endif
0186
0187 constexpr bool isNull() const noexcept;
0188
0189 constexpr float x() const noexcept;
0190 constexpr float y() const noexcept;
0191 constexpr float z() const noexcept;
0192
0193 constexpr void setX(float x) noexcept;
0194 constexpr void setY(float y) noexcept;
0195 constexpr void setZ(float z) noexcept;
0196
0197 constexpr float &operator[](int i);
0198 constexpr float operator[](int i) const;
0199
0200 [[nodiscard]] float length() const noexcept;
0201 [[nodiscard]] constexpr float lengthSquared() const noexcept;
0202
0203 [[nodiscard]] QVector3D normalized() const noexcept;
0204 void normalize() noexcept;
0205
0206 constexpr QVector3D &operator+=(QVector3D vector) noexcept;
0207 constexpr QVector3D &operator-=(QVector3D vector) noexcept;
0208 constexpr QVector3D &operator*=(float factor) noexcept;
0209 constexpr QVector3D &operator*=(QVector3D vector) noexcept;
0210 constexpr QVector3D &operator/=(float divisor);
0211 constexpr QVector3D &operator/=(QVector3D vector);
0212
0213 [[nodiscard]] static constexpr float dotProduct(QVector3D v1, QVector3D v2) noexcept;
0214 [[nodiscard]] static constexpr QVector3D crossProduct(QVector3D v1, QVector3D v2) noexcept;
0215
0216 [[nodiscard]] static QVector3D normal(QVector3D v1, QVector3D v2) noexcept;
0217 [[nodiscard]] static QVector3D normal(QVector3D v1, QVector3D v2, QVector3D v3) noexcept;
0218
0219 Q_GUI_EXPORT QVector3D project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const;
0220 Q_GUI_EXPORT QVector3D unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const;
0221
0222 QT_WARNING_PUSH
0223 QT_WARNING_DISABLE_FLOAT_COMPARE
0224 constexpr friend inline bool operator==(QVector3D v1, QVector3D v2) noexcept
0225 {
0226 return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2];
0227 }
0228
0229 constexpr friend inline bool operator!=(QVector3D v1, QVector3D v2) noexcept
0230 {
0231 return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2];
0232 }
0233 QT_WARNING_POP
0234 float distanceToPoint(QVector3D point) const noexcept;
0235 constexpr float distanceToPlane(QVector3D plane, QVector3D normal) const noexcept;
0236 float distanceToPlane(QVector3D plane1, QVector3D plane2, QVector3D plane3) const noexcept;
0237 float distanceToLine(QVector3D point, QVector3D direction) const noexcept;
0238
0239
0240 constexpr friend inline QVector3D operator+(QVector3D v1, QVector3D v2) noexcept
0241 {
0242 return QVector3D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2]);
0243 }
0244
0245 constexpr friend inline QVector3D operator-(QVector3D v1, QVector3D v2) noexcept
0246 {
0247 return QVector3D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2]);
0248 }
0249
0250 constexpr friend inline QVector3D operator*(float factor, QVector3D vector) noexcept
0251 {
0252 return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor);
0253 }
0254
0255 constexpr friend inline QVector3D operator*(QVector3D vector, float factor) noexcept
0256 {
0257 return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor);
0258 }
0259
0260 constexpr friend inline QVector3D operator*(QVector3D v1, QVector3D v2) noexcept
0261 {
0262 return QVector3D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2]);
0263 }
0264
0265 constexpr friend inline QVector3D operator-(QVector3D vector) noexcept
0266 {
0267 return QVector3D(-vector.v[0], -vector.v[1], -vector.v[2]);
0268 }
0269
0270 constexpr friend inline QVector3D operator/(QVector3D vector, float divisor)
0271 {
0272 Q_ASSERT(divisor < 0 || divisor > 0);
0273 return QVector3D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor);
0274 }
0275
0276 constexpr friend inline QVector3D operator/(QVector3D vector, QVector3D divisor)
0277 {
0278 Q_ASSERT(divisor.v[0] > 0 || divisor.v[0] < 0);
0279 Q_ASSERT(divisor.v[1] > 0 || divisor.v[1] < 0);
0280 Q_ASSERT(divisor.v[2] > 0 || divisor.v[2] < 0);
0281 return QVector3D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1],
0282 vector.v[2] / divisor.v[2]);
0283 }
0284
0285 friend Q_GUI_EXPORT bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept;
0286
0287 #ifndef QT_NO_VECTOR2D
0288 constexpr QVector2D toVector2D() const noexcept;
0289 #endif
0290 #ifndef QT_NO_VECTOR4D
0291 constexpr QVector4D toVector4D() const noexcept;
0292 #endif
0293
0294 constexpr QPoint toPoint() const noexcept;
0295 constexpr QPointF toPointF() const noexcept;
0296
0297 Q_GUI_EXPORT operator QVariant() const;
0298
0299 private:
0300 float v[3];
0301
0302 friend class QVector2D;
0303 friend class QVector4D;
0304 #ifndef QT_NO_MATRIX4X4
0305 friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
0306 friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
0307 #endif
0308
0309 template <std::size_t I,
0310 typename V,
0311 std::enable_if_t<(I < 3), bool> = true,
0312 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<V>, QVector3D>, bool> = true>
0313 friend constexpr decltype(auto) get(V &&vec) noexcept
0314 {
0315 return q23::forward_like<V>(vec.v[I]);
0316 }
0317 };
0318
0319 Q_DECLARE_TYPEINFO(QVector3D, Q_PRIMITIVE_TYPE);
0320
0321 #endif
0322
0323
0324
0325
0326
0327 #ifndef QT_NO_VECTOR4D
0328
0329 class QVector4D
0330 {
0331 public:
0332 constexpr QVector4D() noexcept;
0333 explicit QVector4D(Qt::Initialization) noexcept {}
0334 constexpr QVector4D(float xpos, float ypos, float zpos, float wpos) noexcept;
0335 constexpr explicit QVector4D(QPoint point) noexcept;
0336 constexpr explicit QVector4D(QPointF point) noexcept;
0337 #ifndef QT_NO_VECTOR2D
0338 constexpr explicit QVector4D(QVector2D vector) noexcept;
0339 constexpr QVector4D(QVector2D vector, float zpos, float wpos) noexcept;
0340 #endif
0341 #ifndef QT_NO_VECTOR3D
0342 constexpr explicit QVector4D(QVector3D vector) noexcept;
0343 constexpr QVector4D(QVector3D vector, float wpos) noexcept;
0344 #endif
0345
0346 constexpr bool isNull() const noexcept;
0347
0348 constexpr float x() const noexcept;
0349 constexpr float y() const noexcept;
0350 constexpr float z() const noexcept;
0351 constexpr float w() const noexcept;
0352
0353 constexpr void setX(float x) noexcept;
0354 constexpr void setY(float y) noexcept;
0355 constexpr void setZ(float z) noexcept;
0356 constexpr void setW(float w) noexcept;
0357
0358 constexpr float &operator[](int i);
0359 constexpr float operator[](int i) const;
0360
0361 [[nodiscard]] float length() const noexcept;
0362 [[nodiscard]] constexpr float lengthSquared() const noexcept;
0363
0364 [[nodiscard]] QVector4D normalized() const noexcept;
0365 void normalize() noexcept;
0366
0367 constexpr QVector4D &operator+=(QVector4D vector) noexcept;
0368 constexpr QVector4D &operator-=(QVector4D vector) noexcept;
0369 constexpr QVector4D &operator*=(float factor) noexcept;
0370 constexpr QVector4D &operator*=(QVector4D vector) noexcept;
0371 constexpr QVector4D &operator/=(float divisor);
0372 constexpr inline QVector4D &operator/=(QVector4D vector);
0373
0374 [[nodiscard]] static constexpr float dotProduct(QVector4D v1, QVector4D v2) noexcept;
0375
0376 QT_WARNING_PUSH
0377 QT_WARNING_DISABLE_FLOAT_COMPARE
0378 constexpr friend inline bool operator==(QVector4D v1, QVector4D v2) noexcept
0379 {
0380 return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2] && v1.v[3] == v2.v[3];
0381 }
0382
0383 constexpr friend inline bool operator!=(QVector4D v1, QVector4D v2) noexcept
0384 {
0385 return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2] || v1.v[3] != v2.v[3];
0386 }
0387 QT_WARNING_POP
0388 constexpr friend inline QVector4D operator+(QVector4D v1, QVector4D v2) noexcept
0389 {
0390 return QVector4D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2], v1.v[3] + v2.v[3]);
0391 }
0392
0393 constexpr friend inline QVector4D operator-(QVector4D v1, QVector4D v2) noexcept
0394 {
0395 return QVector4D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2], v1.v[3] - v2.v[3]);
0396 }
0397
0398 constexpr friend inline QVector4D operator*(float factor, QVector4D vector) noexcept
0399 {
0400 return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor);
0401 }
0402
0403 constexpr friend inline QVector4D operator*(QVector4D vector, float factor) noexcept
0404 {
0405 return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor);
0406 }
0407
0408 constexpr friend inline QVector4D operator*(QVector4D v1, QVector4D v2) noexcept
0409 {
0410 return QVector4D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2], v1.v[3] * v2.v[3]);
0411 }
0412
0413 constexpr friend inline QVector4D operator-(QVector4D vector) noexcept
0414 {
0415 return QVector4D(-vector.v[0], -vector.v[1], -vector.v[2], -vector.v[3]);
0416 }
0417
0418 constexpr friend inline QVector4D operator/(QVector4D vector, float divisor)
0419 {
0420 Q_ASSERT(divisor < 0 || divisor > 0);
0421 return QVector4D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor, vector.v[3] / divisor);
0422 }
0423
0424 constexpr friend inline QVector4D operator/(QVector4D vector, QVector4D divisor)
0425 {
0426 Q_ASSERT(divisor.v[0] > 0 || divisor.v[0] < 0);
0427 Q_ASSERT(divisor.v[1] > 0 || divisor.v[1] < 0);
0428 Q_ASSERT(divisor.v[2] > 0 || divisor.v[2] < 0);
0429 Q_ASSERT(divisor.v[3] > 0 || divisor.v[3] < 0);
0430 return QVector4D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1],
0431 vector.v[2] / divisor.v[2], vector.v[3] / divisor.v[3]);
0432 }
0433
0434 friend Q_GUI_EXPORT bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept;
0435
0436 #ifndef QT_NO_VECTOR2D
0437 constexpr QVector2D toVector2D() const noexcept;
0438 constexpr QVector2D toVector2DAffine() const noexcept;
0439 #endif
0440 #ifndef QT_NO_VECTOR3D
0441 constexpr QVector3D toVector3D() const noexcept;
0442 constexpr QVector3D toVector3DAffine() const noexcept;
0443 #endif
0444
0445 constexpr QPoint toPoint() const noexcept;
0446 constexpr QPointF toPointF() const noexcept;
0447
0448 Q_GUI_EXPORT operator QVariant() const;
0449
0450 private:
0451 float v[4];
0452
0453 friend class QVector2D;
0454 friend class QVector3D;
0455 friend class QMatrix4x4;
0456 #ifndef QT_NO_MATRIX4X4
0457 friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
0458 friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
0459 #endif
0460
0461 template <std::size_t I,
0462 typename V,
0463 std::enable_if_t<(I < 4), bool> = true,
0464 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<V>, QVector4D>, bool> = true>
0465 friend constexpr decltype(auto) get(V &&vec) noexcept
0466 {
0467 return q23::forward_like<V>(vec.v[I]);
0468 }
0469 };
0470
0471 Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE);
0472
0473 #endif
0474
0475
0476
0477
0478
0479 #ifndef QT_NO_VECTOR2D
0480
0481 constexpr inline QVector2D::QVector2D() noexcept : v{0.0f, 0.0f} {}
0482
0483 constexpr inline QVector2D::QVector2D(float xpos, float ypos) noexcept : v{xpos, ypos} {}
0484
0485 constexpr inline QVector2D::QVector2D(QPoint point) noexcept : v{float(point.x()), float(point.y())} {}
0486
0487 constexpr inline QVector2D::QVector2D(QPointF point) noexcept : v{float(point.x()), float(point.y())} {}
0488
0489 #ifndef QT_NO_VECTOR3D
0490 constexpr inline QVector2D::QVector2D(QVector3D vector) noexcept : v{vector[0], vector[1]} {}
0491 #endif
0492 #ifndef QT_NO_VECTOR4D
0493 constexpr inline QVector2D::QVector2D(QVector4D vector) noexcept : v{vector[0], vector[1]} {}
0494 #endif
0495
0496 constexpr inline bool QVector2D::isNull() const noexcept
0497 {
0498 return qIsNull(v[0]) && qIsNull(v[1]);
0499 }
0500
0501 constexpr inline float QVector2D::x() const noexcept { return v[0]; }
0502 constexpr inline float QVector2D::y() const noexcept { return v[1]; }
0503
0504 constexpr inline void QVector2D::setX(float aX) noexcept { v[0] = aX; }
0505 constexpr inline void QVector2D::setY(float aY) noexcept { v[1] = aY; }
0506
0507 constexpr inline float &QVector2D::operator[](int i)
0508 {
0509 Q_ASSERT(uint(i) < 2u);
0510 return v[i];
0511 }
0512
0513 constexpr inline float QVector2D::operator[](int i) const
0514 {
0515 Q_ASSERT(uint(i) < 2u);
0516 return v[i];
0517 }
0518
0519 inline float QVector2D::length() const noexcept
0520 {
0521 return qHypot(v[0], v[1]);
0522 }
0523
0524 constexpr inline float QVector2D::lengthSquared() const noexcept
0525 {
0526 return v[0] * v[0] + v[1] * v[1];
0527 }
0528
0529 inline QVector2D QVector2D::normalized() const noexcept
0530 {
0531 const float len = length();
0532 return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector2D()
0533 : QVector2D(v[0] / len, v[1] / len);
0534 }
0535
0536 inline void QVector2D::normalize() noexcept
0537 {
0538 const float len = length();
0539 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
0540 return;
0541
0542 v[0] /= len;
0543 v[1] /= len;
0544 }
0545
0546 inline float QVector2D::distanceToPoint(QVector2D point) const noexcept
0547 {
0548 return (*this - point).length();
0549 }
0550
0551 inline float QVector2D::distanceToLine(QVector2D point, QVector2D direction) const noexcept
0552 {
0553 if (direction.isNull())
0554 return (*this - point).length();
0555 QVector2D p = point + dotProduct(*this - point, direction) * direction;
0556 return (*this - p).length();
0557 }
0558
0559 constexpr inline QVector2D &QVector2D::operator+=(QVector2D vector) noexcept
0560 {
0561 v[0] += vector.v[0];
0562 v[1] += vector.v[1];
0563 return *this;
0564 }
0565
0566 constexpr inline QVector2D &QVector2D::operator-=(QVector2D vector) noexcept
0567 {
0568 v[0] -= vector.v[0];
0569 v[1] -= vector.v[1];
0570 return *this;
0571 }
0572
0573 constexpr inline QVector2D &QVector2D::operator*=(float factor) noexcept
0574 {
0575 v[0] *= factor;
0576 v[1] *= factor;
0577 return *this;
0578 }
0579
0580 constexpr inline QVector2D &QVector2D::operator*=(QVector2D vector) noexcept
0581 {
0582 v[0] *= vector.v[0];
0583 v[1] *= vector.v[1];
0584 return *this;
0585 }
0586
0587 constexpr inline QVector2D &QVector2D::operator/=(float divisor)
0588 {
0589 Q_ASSERT(divisor < 0 || divisor > 0);
0590 v[0] /= divisor;
0591 v[1] /= divisor;
0592 return *this;
0593 }
0594
0595 constexpr inline QVector2D &QVector2D::operator/=(QVector2D vector)
0596 {
0597 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
0598 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
0599 v[0] /= vector.v[0];
0600 v[1] /= vector.v[1];
0601 return *this;
0602 }
0603
0604 constexpr inline float QVector2D::dotProduct(QVector2D v1, QVector2D v2) noexcept
0605 {
0606 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1];
0607 }
0608
0609 #ifndef QT_NO_VECTOR3D
0610 constexpr inline QVector3D QVector2D::toVector3D() const noexcept
0611 {
0612 return QVector3D(v[0], v[1], 0.0f);
0613 }
0614 #endif
0615 #ifndef QT_NO_VECTOR4D
0616 constexpr inline QVector4D QVector2D::toVector4D() const noexcept
0617 {
0618 return QVector4D(v[0], v[1], 0.0f, 0.0f);
0619 }
0620 #endif
0621
0622
0623 constexpr inline QPoint QVector2D::toPoint() const noexcept
0624 {
0625 return QPoint(qRound(v[0]), qRound(v[1]));
0626 }
0627
0628 constexpr inline QPointF QVector2D::toPointF() const noexcept
0629 {
0630 return QPointF(qreal(v[0]), qreal(v[1]));
0631 }
0632
0633 #ifndef QT_NO_DEBUG_STREAM
0634 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, QVector2D vector);
0635 #endif
0636
0637 #ifndef QT_NO_DATASTREAM
0638 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, QVector2D );
0639 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &);
0640 #endif
0641
0642 #endif
0643
0644
0645
0646
0647
0648 #ifndef QT_NO_VECTOR3D
0649
0650 constexpr inline QVector3D::QVector3D() noexcept : v{0.0f, 0.0f, 0.0f} {}
0651
0652 constexpr inline QVector3D::QVector3D(QPoint point) noexcept : v{float(point.x()), float(point.y()), 0.0f} {}
0653
0654 constexpr inline QVector3D::QVector3D(QPointF point) noexcept : v{float(point.x()), float(point.y()), 0.0f} {}
0655
0656 #ifndef QT_NO_VECTOR2D
0657 constexpr inline QVector3D::QVector3D(QVector2D vector) noexcept : v{vector[0], vector[1], 0.0f} {}
0658 constexpr inline QVector3D::QVector3D(QVector2D vector, float zpos) noexcept : v{vector[0], vector[1], zpos} {}
0659 #endif
0660
0661 #ifndef QT_NO_VECTOR4D
0662 constexpr inline QVector3D::QVector3D(QVector4D vector) noexcept : v{vector[0], vector[1], vector[2]} {}
0663 #endif
0664
0665 constexpr inline bool QVector3D::isNull() const noexcept
0666 {
0667 return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]);
0668 }
0669
0670 constexpr inline float QVector3D::x() const noexcept { return v[0]; }
0671 constexpr inline float QVector3D::y() const noexcept { return v[1]; }
0672 constexpr inline float QVector3D::z() const noexcept { return v[2]; }
0673
0674 constexpr inline void QVector3D::setX(float aX) noexcept { v[0] = aX; }
0675 constexpr inline void QVector3D::setY(float aY) noexcept { v[1] = aY; }
0676 constexpr inline void QVector3D::setZ(float aZ) noexcept { v[2] = aZ; }
0677
0678 constexpr inline float &QVector3D::operator[](int i)
0679 {
0680 Q_ASSERT(uint(i) < 3u);
0681 return v[i];
0682 }
0683
0684 constexpr inline float QVector3D::operator[](int i) const
0685 {
0686 Q_ASSERT(uint(i) < 3u);
0687 return v[i];
0688 }
0689
0690 inline float QVector3D::length() const noexcept
0691 {
0692 return qHypot(v[0], v[1], v[2]);
0693 }
0694
0695 inline QVector3D QVector3D::normalized() const noexcept
0696 {
0697 const float len = length();
0698 return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector3D()
0699 : QVector3D(v[0] / len, v[1] / len, v[2] / len);
0700 }
0701
0702 inline void QVector3D::normalize() noexcept
0703 {
0704 const float len = length();
0705 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
0706 return;
0707
0708 v[0] /= len;
0709 v[1] /= len;
0710 v[2] /= len;
0711 }
0712
0713 constexpr inline float QVector3D::lengthSquared() const noexcept
0714 {
0715 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
0716 }
0717
0718 constexpr inline QVector3D &QVector3D::operator+=(QVector3D vector) noexcept
0719 {
0720 v[0] += vector.v[0];
0721 v[1] += vector.v[1];
0722 v[2] += vector.v[2];
0723 return *this;
0724 }
0725
0726 constexpr inline QVector3D &QVector3D::operator-=(QVector3D vector) noexcept
0727 {
0728 v[0] -= vector.v[0];
0729 v[1] -= vector.v[1];
0730 v[2] -= vector.v[2];
0731 return *this;
0732 }
0733
0734 constexpr inline QVector3D &QVector3D::operator*=(float factor) noexcept
0735 {
0736 v[0] *= factor;
0737 v[1] *= factor;
0738 v[2] *= factor;
0739 return *this;
0740 }
0741
0742 constexpr inline QVector3D &QVector3D::operator*=(QVector3D vector) noexcept
0743 {
0744 v[0] *= vector.v[0];
0745 v[1] *= vector.v[1];
0746 v[2] *= vector.v[2];
0747 return *this;
0748 }
0749
0750 constexpr inline QVector3D &QVector3D::operator/=(float divisor)
0751 {
0752 Q_ASSERT(divisor < 0 || divisor > 0);
0753 v[0] /= divisor;
0754 v[1] /= divisor;
0755 v[2] /= divisor;
0756 return *this;
0757 }
0758
0759 constexpr inline QVector3D &QVector3D::operator/=(QVector3D vector)
0760 {
0761 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
0762 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
0763 Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
0764 v[0] /= vector.v[0];
0765 v[1] /= vector.v[1];
0766 v[2] /= vector.v[2];
0767 return *this;
0768 }
0769
0770 constexpr inline float QVector3D::dotProduct(QVector3D v1, QVector3D v2) noexcept
0771 {
0772 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1] + v1.v[2] * v2.v[2];
0773 }
0774
0775 constexpr inline QVector3D QVector3D::crossProduct(QVector3D v1, QVector3D v2) noexcept
0776 {
0777 return QVector3D(v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1],
0778 v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2],
0779 v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0]);
0780 }
0781
0782 inline QVector3D QVector3D::normal(QVector3D v1, QVector3D v2) noexcept
0783 {
0784 return crossProduct(v1, v2).normalized();
0785 }
0786
0787 inline QVector3D QVector3D::normal(QVector3D v1, QVector3D v2, QVector3D v3) noexcept
0788 {
0789 return crossProduct((v2 - v1), (v3 - v1)).normalized();
0790 }
0791
0792 inline float QVector3D::distanceToPoint(QVector3D point) const noexcept
0793 {
0794 return (*this - point).length();
0795 }
0796
0797 constexpr inline float QVector3D::distanceToPlane(QVector3D plane, QVector3D normal) const noexcept
0798 {
0799 return dotProduct(*this - plane, normal);
0800 }
0801
0802 inline float QVector3D::distanceToPlane(QVector3D plane1, QVector3D plane2, QVector3D plane3) const noexcept
0803 {
0804 QVector3D n = normal(plane2 - plane1, plane3 - plane1);
0805 return dotProduct(*this - plane1, n);
0806 }
0807
0808 inline float QVector3D::distanceToLine(QVector3D point, QVector3D direction) const noexcept
0809 {
0810 if (direction.isNull())
0811 return (*this - point).length();
0812 QVector3D p = point + dotProduct(*this - point, direction) * direction;
0813 return (*this - p).length();
0814 }
0815
0816 #ifndef QT_NO_VECTOR2D
0817 constexpr inline QVector2D QVector3D::toVector2D() const noexcept
0818 {
0819 return QVector2D(v[0], v[1]);
0820 }
0821 #endif
0822 #ifndef QT_NO_VECTOR4D
0823 constexpr inline QVector4D QVector3D::toVector4D() const noexcept
0824 {
0825 return QVector4D(v[0], v[1], v[2], 0.0f);
0826 }
0827 #endif
0828
0829 constexpr inline QPoint QVector3D::toPoint() const noexcept
0830 {
0831 return QPoint(qRound(v[0]), qRound(v[1]));
0832 }
0833
0834 constexpr inline QPointF QVector3D::toPointF() const noexcept
0835 {
0836 return QPointF(qreal(v[0]), qreal(v[1]));
0837 }
0838
0839 #ifndef QT_NO_DEBUG_STREAM
0840 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, QVector3D vector);
0841 #endif
0842
0843 #ifndef QT_NO_DATASTREAM
0844 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, QVector3D );
0845 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &);
0846 #endif
0847
0848 #endif
0849
0850
0851
0852
0853
0854 #ifndef QT_NO_VECTOR4D
0855
0856 constexpr inline QVector4D::QVector4D() noexcept : v{0.0f, 0.0f, 0.0f, 0.0f} {}
0857
0858 constexpr inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) noexcept : v{xpos, ypos, zpos, wpos} {}
0859
0860 constexpr inline QVector4D::QVector4D(QPoint point) noexcept : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {}
0861
0862 constexpr inline QVector4D::QVector4D(QPointF point) noexcept : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {}
0863
0864 #ifndef QT_NO_VECTOR2D
0865 constexpr QVector4D::QVector4D(QVector2D vector) noexcept : v{vector[0], vector[1], 0.0f, 0.0f} {}
0866 constexpr QVector4D::QVector4D(QVector2D vector, float zpos, float wpos) noexcept : v{vector[0], vector[1], zpos, wpos} {}
0867 #endif
0868 #ifndef QT_NO_VECTOR3D
0869 constexpr QVector4D::QVector4D(QVector3D vector) noexcept : v{vector[0], vector[1], vector[2], 0.0f} {}
0870 constexpr QVector4D::QVector4D(QVector3D vector, float wpos) noexcept : v{vector[0], vector[1], vector[2], wpos} {}
0871 #endif
0872
0873 constexpr inline bool QVector4D::isNull() const noexcept
0874 {
0875 return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]) && qIsNull(v[3]);
0876 }
0877
0878 constexpr inline float QVector4D::x() const noexcept { return v[0]; }
0879 constexpr inline float QVector4D::y() const noexcept { return v[1]; }
0880 constexpr inline float QVector4D::z() const noexcept { return v[2]; }
0881 constexpr inline float QVector4D::w() const noexcept { return v[3]; }
0882
0883 constexpr inline void QVector4D::setX(float aX) noexcept { v[0] = aX; }
0884 constexpr inline void QVector4D::setY(float aY) noexcept { v[1] = aY; }
0885 constexpr inline void QVector4D::setZ(float aZ) noexcept { v[2] = aZ; }
0886 constexpr inline void QVector4D::setW(float aW) noexcept { v[3] = aW; }
0887
0888 constexpr inline float &QVector4D::operator[](int i)
0889 {
0890 Q_ASSERT(uint(i) < 4u);
0891 return v[i];
0892 }
0893
0894 constexpr inline float QVector4D::operator[](int i) const
0895 {
0896 Q_ASSERT(uint(i) < 4u);
0897 return v[i];
0898 }
0899
0900 inline float QVector4D::length() const noexcept
0901 {
0902 return qHypot(v[0], v[1], v[2], v[3]);
0903 }
0904
0905 constexpr inline float QVector4D::lengthSquared() const noexcept
0906 {
0907 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
0908 }
0909
0910 inline QVector4D QVector4D::normalized() const noexcept
0911 {
0912 const float len = length();
0913 return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector4D()
0914 : QVector4D(v[0] / len, v[1] / len, v[2] / len, v[3] / len);
0915 }
0916
0917 inline void QVector4D::normalize() noexcept
0918 {
0919 const float len = length();
0920 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
0921 return;
0922
0923 v[0] /= len;
0924 v[1] /= len;
0925 v[2] /= len;
0926 v[3] /= len;
0927 }
0928
0929 constexpr inline QVector4D &QVector4D::operator+=(QVector4D vector) noexcept
0930 {
0931 v[0] += vector.v[0];
0932 v[1] += vector.v[1];
0933 v[2] += vector.v[2];
0934 v[3] += vector.v[3];
0935 return *this;
0936 }
0937
0938 constexpr inline QVector4D &QVector4D::operator-=(QVector4D vector) noexcept
0939 {
0940 v[0] -= vector.v[0];
0941 v[1] -= vector.v[1];
0942 v[2] -= vector.v[2];
0943 v[3] -= vector.v[3];
0944 return *this;
0945 }
0946
0947 constexpr inline QVector4D &QVector4D::operator*=(float factor) noexcept
0948 {
0949 v[0] *= factor;
0950 v[1] *= factor;
0951 v[2] *= factor;
0952 v[3] *= factor;
0953 return *this;
0954 }
0955
0956 constexpr inline QVector4D &QVector4D::operator*=(QVector4D vector) noexcept
0957 {
0958 v[0] *= vector.v[0];
0959 v[1] *= vector.v[1];
0960 v[2] *= vector.v[2];
0961 v[3] *= vector.v[3];
0962 return *this;
0963 }
0964
0965 constexpr inline QVector4D &QVector4D::operator/=(float divisor)
0966 {
0967 Q_ASSERT(divisor < 0 || divisor > 0);
0968 v[0] /= divisor;
0969 v[1] /= divisor;
0970 v[2] /= divisor;
0971 v[3] /= divisor;
0972 return *this;
0973 }
0974
0975 constexpr inline QVector4D &QVector4D::operator/=(QVector4D vector)
0976 {
0977 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
0978 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
0979 Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
0980 Q_ASSERT(vector.v[3] > 0 || vector.v[3] < 0);
0981 v[0] /= vector.v[0];
0982 v[1] /= vector.v[1];
0983 v[2] /= vector.v[2];
0984 v[3] /= vector.v[3];
0985 return *this;
0986 }
0987
0988 constexpr float QVector4D::dotProduct(QVector4D v1, QVector4D v2) noexcept
0989 {
0990 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1] + v1.v[2] * v2.v[2] + v1.v[3] * v2.v[3];
0991 }
0992
0993 #ifndef QT_NO_VECTOR2D
0994
0995 constexpr inline QVector2D QVector4D::toVector2D() const noexcept
0996 {
0997 return QVector2D(v[0], v[1]);
0998 }
0999
1000 constexpr inline QVector2D QVector4D::toVector2DAffine() const noexcept
1001 {
1002 if (qIsNull(v[3]))
1003 return QVector2D();
1004 return QVector2D(v[0] / v[3], v[1] / v[3]);
1005 }
1006
1007 #endif
1008
1009 #ifndef QT_NO_VECTOR3D
1010
1011 constexpr inline QVector3D QVector4D::toVector3D() const noexcept
1012 {
1013 return QVector3D(v[0], v[1], v[2]);
1014 }
1015
1016 constexpr QVector3D QVector4D::toVector3DAffine() const noexcept
1017 {
1018 if (qIsNull(v[3]))
1019 return QVector3D();
1020 return QVector3D(v[0] / v[3], v[1] / v[3], v[2] / v[3]);
1021 }
1022
1023 #endif
1024
1025 constexpr inline QPoint QVector4D::toPoint() const noexcept
1026 {
1027 return QPoint(qRound(v[0]), qRound(v[1]));
1028 }
1029
1030 constexpr inline QPointF QVector4D::toPointF() const noexcept
1031 {
1032 return QPointF(qreal(v[0]), qreal(v[1]));
1033 }
1034
1035 #ifndef QT_NO_DEBUG_STREAM
1036 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, QVector4D vector);
1037 #endif
1038
1039 #ifndef QT_NO_DATASTREAM
1040 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, QVector4D );
1041 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
1042 #endif
1043
1044 #endif
1045
1046
1047 QT_END_NAMESPACE
1048
1049
1050
1051 namespace std {
1052 #ifndef QT_NO_VECTOR2D
1053 template <>
1054 class tuple_size<QT_PREPEND_NAMESPACE(QVector2D)> : public integral_constant<size_t, 2> {};
1055 template <>
1056 class tuple_element<0, QT_PREPEND_NAMESPACE(QVector2D)> { public: using type = float; };
1057 template <>
1058 class tuple_element<1, QT_PREPEND_NAMESPACE(QVector2D)> { public: using type = float; };
1059 #endif
1060
1061 #ifndef QT_NO_VECTOR3D
1062 template <>
1063 class tuple_size<QT_PREPEND_NAMESPACE(QVector3D)> : public integral_constant<size_t, 3> {};
1064 template <>
1065 class tuple_element<0, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; };
1066 template <>
1067 class tuple_element<1, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; };
1068 template <>
1069 class tuple_element<2, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; };
1070 #endif
1071
1072 #ifndef QT_NO_VECTOR4D
1073 template <>
1074 class tuple_size<QT_PREPEND_NAMESPACE(QVector4D)> : public integral_constant<size_t, 4> {};
1075 template <>
1076 class tuple_element<0, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
1077 template <>
1078 class tuple_element<1, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
1079 template <>
1080 class tuple_element<2, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
1081 template <>
1082 class tuple_element<3, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
1083 #endif
1084 }
1085
1086 #endif