diff options
-rw-r--r-- | src/gui/.prev_CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/gui/CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/gui/math3d/math3d.pri | 7 | ||||
-rw-r--r-- | src/gui/math3d/qvector2d.cpp | 558 | ||||
-rw-r--r-- | src/gui/math3d/qvector2d.h | 249 | ||||
-rw-r--r-- | src/gui/math3d/qvector3d.cpp | 759 | ||||
-rw-r--r-- | src/gui/math3d/qvector3d.h | 276 | ||||
-rw-r--r-- | src/gui/math3d/qvector4d.cpp | 639 | ||||
-rw-r--r-- | src/gui/math3d/qvector4d.h | 277 | ||||
-rw-r--r-- | src/gui/math3d/qvectornd.cpp | 1635 | ||||
-rw-r--r-- | src/gui/math3d/qvectornd.h | 1105 |
11 files changed, 2763 insertions, 2756 deletions
diff --git a/src/gui/.prev_CMakeLists.txt b/src/gui/.prev_CMakeLists.txt index f7f275fbb0..aa7963cbc9 100644 --- a/src/gui/.prev_CMakeLists.txt +++ b/src/gui/.prev_CMakeLists.txt @@ -92,9 +92,10 @@ qt_internal_add_module(Gui math3d/qgenericmatrix.cpp math3d/qgenericmatrix.h math3d/qmatrix4x4.cpp math3d/qmatrix4x4.h math3d/qquaternion.cpp math3d/qquaternion.h - math3d/qvector2d.cpp math3d/qvector2d.h - math3d/qvector3d.cpp math3d/qvector3d.h - math3d/qvector4d.cpp math3d/qvector4d.h + math3d/qvector2d.h + math3d/qvector3d.h + math3d/qvector4d.h + math3d/qvectornd.cpp math3d/qvectornd.h painting/qbackingstore.cpp painting/qbackingstore.h painting/qbezier.cpp painting/qbezier_p.h painting/qblendfunctions.cpp painting/qblendfunctions_p.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 36fdb298c1..b6b1bb439c 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -143,9 +143,10 @@ qt_internal_add_module(Gui math3d/qgenericmatrix.cpp math3d/qgenericmatrix.h math3d/qmatrix4x4.cpp math3d/qmatrix4x4.h math3d/qquaternion.cpp math3d/qquaternion.h - math3d/qvector2d.cpp math3d/qvector2d.h - math3d/qvector3d.cpp math3d/qvector3d.h - math3d/qvector4d.cpp math3d/qvector4d.h + math3d/qvector2d.h + math3d/qvector3d.h + math3d/qvector4d.h + math3d/qvectornd.cpp math3d/qvectornd.h painting/qbackingstore.cpp painting/qbackingstore.h painting/qbezier.cpp painting/qbezier_p.h painting/qblendfunctions.cpp painting/qblendfunctions_p.h diff --git a/src/gui/math3d/math3d.pri b/src/gui/math3d/math3d.pri index e4dd53a68e..0cbc3db0e1 100644 --- a/src/gui/math3d/math3d.pri +++ b/src/gui/math3d/math3d.pri @@ -4,12 +4,11 @@ HEADERS += \ math3d/qquaternion.h \ math3d/qvector2d.h \ math3d/qvector3d.h \ - math3d/qvector4d.h + math3d/qvector4d.h \ + math3d/qvectornd.h \ SOURCES += \ math3d/qgenericmatrix.cpp \ math3d/qmatrix4x4.cpp \ math3d/qquaternion.cpp \ - math3d/qvector2d.cpp \ - math3d/qvector3d.cpp \ - math3d/qvector4d.cpp + math3d/qvectornd.cpp \ diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp deleted file mode 100644 index 4121f86b92..0000000000 --- a/src/gui/math3d/qvector2d.cpp +++ /dev/null @@ -1,558 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qvector2d.h" -#include "qvector3d.h" -#include "qvector4d.h" -#include <QtCore/qdatastream.h> -#include <QtCore/qdebug.h> -#include <QtCore/qvariant.h> -#include <QtCore/qmath.h> - -QT_BEGIN_NAMESPACE - -#ifndef QT_NO_VECTOR2D - -static_assert(std::is_standard_layout<QVector2D>::value, "QVector2D is supposed to be standard layout"); -static_assert(sizeof(QVector2D) == sizeof(float) * 2, "QVector2D is not supposed to have padding at the end"); - -/*! - \class QVector2D - \brief The QVector2D class represents a vector or vertex in 2D space. - \since 4.6 - \ingroup painting - \ingroup painting-3D - \inmodule QtGui - - The QVector2D class can also be used to represent vertices in 2D space. - We therefore do not need to provide a separate vertex class. - - \sa QVector3D, QVector4D, QQuaternion -*/ - -/*! - \fn QVector2D::QVector2D() - - Constructs a null vector, i.e. with coordinates (0, 0). -*/ - -/*! - \fn QVector2D::QVector2D(Qt::Initialization) - \since 5.5 - \internal - - Constructs a vector without initializing the contents. -*/ - -/*! - \fn QVector2D::QVector2D(float xpos, float ypos) - - Constructs a vector with coordinates (\a xpos, \a ypos). -*/ - -/*! - \fn QVector2D::QVector2D(const QPoint& point) - - Constructs a vector with x and y coordinates from a 2D \a point. -*/ - -/*! - \fn QVector2D::QVector2D(const QPointF& point) - - Constructs a vector with x and y coordinates from a 2D \a point. -*/ - -#ifndef QT_NO_VECTOR3D - -/*! - Constructs a vector with x and y coordinates from a 3D \a vector. - The z coordinate of \a vector is dropped. - - \sa toVector3D() -*/ -QVector2D::QVector2D(const QVector3D& vector) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; -} - -#endif - -#ifndef QT_NO_VECTOR4D - -/*! - Constructs a vector with x and y coordinates from a 3D \a vector. - The z and w coordinates of \a vector are dropped. - - \sa toVector4D() -*/ -QVector2D::QVector2D(const QVector4D& vector) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; -} - -#endif - -/*! - \fn bool QVector2D::isNull() const - - Returns \c true if the x and y coordinates are set to 0.0, - otherwise returns \c false. -*/ - -/*! - \fn float QVector2D::x() const - - Returns the x coordinate of this point. - - \sa setX(), y() -*/ - -/*! - \fn float QVector2D::y() const - - Returns the y coordinate of this point. - - \sa setY(), x() -*/ - -/*! - \fn void QVector2D::setX(float x) - - Sets the x coordinate of this point to the given \a x coordinate. - - \sa x(), setY() -*/ - -/*! - \fn void QVector2D::setY(float y) - - Sets the y coordinate of this point to the given \a y coordinate. - - \sa y(), setX() -*/ - -/*! \fn float &QVector2D::operator[](int i) - \since 5.2 - - Returns the component of the vector at index position \a i - as a modifiable reference. - - \a i must be a valid index position in the vector (i.e., 0 <= \a i - < 2). -*/ - -/*! \fn float QVector2D::operator[](int i) const - \since 5.2 - - Returns the component of the vector at index position \a i. - - \a i must be a valid index position in the vector (i.e., 0 <= \a i - < 2). -*/ - -/*! - Returns the length of the vector from the origin. - - \sa lengthSquared(), normalized() -*/ -float QVector2D::length() const -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]); - return float(std::sqrt(len)); -} - -/*! - Returns the squared length of the vector from the origin. - This is equivalent to the dot product of the vector with itself. - - \sa length(), dotProduct() -*/ -float QVector2D::lengthSquared() const -{ - return v[0] * v[0] + v[1] * v[1]; -} - -/*! - Returns the normalized unit vector form of this vector. - - If this vector is null, then a null vector is returned. If the length - of the vector is very close to 1, then the vector will be returned as-is. - Otherwise the normalized form of the vector of length 1 will be returned. - - \sa length(), normalize() -*/ -QVector2D QVector2D::normalized() const -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]); - if (qFuzzyIsNull(len - 1.0f)) { - return *this; - } else if (!qFuzzyIsNull(len)) { - double sqrtLen = std::sqrt(len); - return QVector2D(float(double(v[0]) / sqrtLen), float(double(v[1]) / sqrtLen)); - } else { - return QVector2D(); - } -} - -/*! - Normalizes the currect vector in place. Nothing happens if this - vector is a null vector or the length of the vector is very close to 1. - - \sa length(), normalized() -*/ -void QVector2D::normalize() -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]); - if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len)) - return; - - len = std::sqrt(len); - - v[0] = float(double(v[0]) / len); - v[1] = float(double(v[1]) / len); -} - -/*! - \since 5.1 - - Returns the distance from this vertex to a point defined by - the vertex \a point. - - \sa distanceToLine() -*/ -float QVector2D::distanceToPoint(const QVector2D& point) const -{ - return (*this - point).length(); -} - -/*! - \since 5.1 - - Returns the distance that this vertex is from a line defined - by \a point and the unit vector \a direction. - - If \a direction is a null vector, then it does not define a line. - In that case, the distance from \a point to this vertex is returned. - - \sa distanceToPoint() -*/ -float QVector2D::distanceToLine - (const QVector2D& point, const QVector2D& direction) const -{ - if (direction.isNull()) - return (*this - point).length(); - QVector2D p = point + dotProduct(*this - point, direction) * direction; - return (*this - p).length(); -} - -/*! - \fn QVector2D &QVector2D::operator+=(const QVector2D &vector) - - Adds the given \a vector to this vector and returns a reference to - this vector. - - \sa operator-=() -*/ - -/*! - \fn QVector2D &QVector2D::operator-=(const QVector2D &vector) - - Subtracts the given \a vector from this vector and returns a reference to - this vector. - - \sa operator+=() -*/ - -/*! - \fn QVector2D &QVector2D::operator*=(float factor) - - Multiplies this vector's coordinates by the given \a factor, and - returns a reference to this vector. - - \sa operator/=() -*/ - -/*! - \fn QVector2D &QVector2D::operator*=(const QVector2D &vector) - - Multiplies the components of this vector by the corresponding - components in \a vector. -*/ - -/*! - \fn QVector2D &QVector2D::operator/=(float divisor) - - Divides this vector's coordinates by the given \a divisor, and - returns a reference to this vector. - - \sa operator*=() -*/ - -/*! - \fn QVector2D &QVector2D::operator/=(const QVector2D &vector) - \since 5.5 - - Divides the components of this vector by the corresponding - components in \a vector. - - \sa operator*=() -*/ - -/*! - Returns the dot product of \a v1 and \a v2. -*/ -float QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) -{ - return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1]; -} - -/*! - \fn bool QVector2D::operator==(const QVector2D &v1, const QVector2D &v2) - - Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false. - This operator uses an exact floating-point comparison. -*/ - -/*! - \fn bool QVector2D::operator!=(const QVector2D &v1, const QVector2D &v2) - - Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false. - This operator uses an exact floating-point comparison. -*/ - -/*! - \fn const QVector2D operator+(const QVector2D &v1, const QVector2D &v2) - \relates QVector2D - - Returns a QVector2D object that is the sum of the given vectors, \a v1 - and \a v2; each component is added separately. - - \sa QVector2D::operator+=() -*/ - -/*! - \fn const QVector2D operator-(const QVector2D &v1, const QVector2D &v2) - \relates QVector2D - - Returns a QVector2D object that is formed by subtracting \a v2 from \a v1; - each component is subtracted separately. - - \sa QVector2D::operator-=() -*/ - -/*! - \fn const QVector2D operator*(float factor, const QVector2D &vector) - \relates QVector2D - - Returns a copy of the given \a vector, multiplied by the given \a factor. - - \sa QVector2D::operator*=() -*/ - -/*! - \fn const QVector2D operator*(const QVector2D &vector, float factor) - \relates QVector2D - - Returns a copy of the given \a vector, multiplied by the given \a factor. - - \sa QVector2D::operator*=() -*/ - -/*! - \fn const QVector2D operator*(const QVector2D &v1, const QVector2D &v2) - \relates QVector2D - - Multiplies the components of \a v1 by the corresponding - components in \a v2. -*/ - -/*! - \fn const QVector2D operator-(const QVector2D &vector) - \relates QVector2D - \overload - - Returns a QVector2D object that is formed by changing the sign of - the components of the given \a vector. - - Equivalent to \c {QVector2D(0,0) - vector}. -*/ - -/*! - \fn const QVector2D operator/(const QVector2D &vector, float divisor) - \relates QVector2D - - Returns the QVector2D object formed by dividing all three components of - the given \a vector by the given \a divisor. - - \sa QVector2D::operator/=() -*/ - -/*! - \fn const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor) - \relates QVector2D - \since 5.5 - - Returns the QVector2D object formed by dividing components of the given - \a vector by a respective components of the given \a divisor. - - \sa QVector2D::operator/=() -*/ - -/*! - \fn bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2) - \relates QVector2D - - Returns \c true if \a v1 and \a v2 are equal, allowing for a small - fuzziness factor for floating-point comparisons; false otherwise. -*/ - -#ifndef QT_NO_VECTOR3D - -/*! - Returns the 3D form of this 2D vector, with the z coordinate set to zero. - - \sa toVector4D(), toPoint() -*/ -QVector3D QVector2D::toVector3D() const -{ - return QVector3D(v[0], v[1], 0.0f); -} - -#endif - -#ifndef QT_NO_VECTOR4D - -/*! - Returns the 4D form of this 2D vector, with the z and w coordinates set to zero. - - \sa toVector3D(), toPoint() -*/ -QVector4D QVector2D::toVector4D() const -{ - return QVector4D(v[0], v[1], 0.0f, 0.0f); -} - -#endif - -/*! - \fn QPoint QVector2D::toPoint() const - - Returns the QPoint form of this 2D vector. - - \sa toPointF(), toVector3D() -*/ - -/*! - \fn QPointF QVector2D::toPointF() const - - Returns the QPointF form of this 2D vector. - - \sa toPoint(), toVector3D() -*/ - -/*! - Returns the 2D vector as a QVariant. -*/ -QVector2D::operator QVariant() const -{ - return QVariant::fromValue(*this); -} - -#ifndef QT_NO_DEBUG_STREAM - -QDebug operator<<(QDebug dbg, const QVector2D &vector) -{ - QDebugStateSaver saver(dbg); - dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')'; - return dbg; -} - -#endif - -#ifndef QT_NO_DATASTREAM - -/*! - \fn QDataStream &operator<<(QDataStream &stream, const QVector2D &vector) - \relates QVector2D - - Writes the given \a vector to the given \a stream and returns a - reference to the stream. - - \sa {Serializing Qt Data Types} -*/ - -QDataStream &operator<<(QDataStream &stream, const QVector2D &vector) -{ - stream << vector.x() << vector.y(); - return stream; -} - -/*! - \fn QDataStream &operator>>(QDataStream &stream, QVector2D &vector) - \relates QVector2D - - Reads a 2D vector from the given \a stream into the given \a vector - and returns a reference to the stream. - - \sa {Serializing Qt Data Types} -*/ - -QDataStream &operator>>(QDataStream &stream, QVector2D &vector) -{ - float x, y; - stream >> x; - stream >> y; - vector.setX(x); - vector.setY(y); - return stream; -} - -#endif // QT_NO_DATASTREAM - -#endif // QT_NO_VECTOR2D - -QT_END_NAMESPACE diff --git a/src/gui/math3d/qvector2d.h b/src/gui/math3d/qvector2d.h index b24f4df949..fc42579918 100644 --- a/src/gui/math3d/qvector2d.h +++ b/src/gui/math3d/qvector2d.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. @@ -37,250 +38,8 @@ ** ****************************************************************************/ -#ifndef QVECTOR2D_H -#define QVECTOR2D_H - -#include <QtGui/qtguiglobal.h> -#include <QtCore/qpoint.h> -#include <QtCore/qmetatype.h> - -QT_BEGIN_NAMESPACE - - -class QVector3D; -class QVector4D; -class QVariant; - -#ifndef QT_NO_VECTOR2D - -class Q_GUI_EXPORT QVector2D -{ -public: - constexpr QVector2D(); - explicit QVector2D(Qt::Initialization) {} - constexpr QVector2D(float xpos, float ypos); - constexpr explicit QVector2D(const QPoint& point); - constexpr explicit QVector2D(const QPointF& point); -#ifndef QT_NO_VECTOR3D - explicit QVector2D(const QVector3D& vector); -#endif -#ifndef QT_NO_VECTOR4D - explicit QVector2D(const QVector4D& vector); -#endif - - bool isNull() const; - - constexpr float x() const; - constexpr float y() const; - - void setX(float x); - void setY(float y); - - float &operator[](int i); - float operator[](int i) const; - - float length() const; - float lengthSquared() const; //In Qt 6 convert to inline and constexpr - - [[nodiscard]] QVector2D normalized() const; - void normalize(); - - float distanceToPoint(const QVector2D &point) const; - float distanceToLine(const QVector2D& point, const QVector2D& direction) const; - - QVector2D &operator+=(const QVector2D &vector); - QVector2D &operator-=(const QVector2D &vector); - QVector2D &operator*=(float factor); - QVector2D &operator*=(const QVector2D &vector); - QVector2D &operator/=(float divisor); - inline QVector2D &operator/=(const QVector2D &vector); - - static float dotProduct(const QVector2D& v1, const QVector2D& v2); //In Qt 6 convert to inline and constexpr - -QT_WARNING_PUSH -QT_WARNING_DISABLE_FLOAT_COMPARE - constexpr friend inline bool operator==(const QVector2D &v1, const QVector2D &v2) noexcept - { - return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1]; - } - - constexpr friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2) noexcept - { - return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1]; - } -QT_WARNING_POP - - constexpr friend inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2); - constexpr friend inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2); - constexpr friend inline const QVector2D operator*(float factor, const QVector2D &vector); - constexpr friend inline const QVector2D operator*(const QVector2D &vector, float factor); - constexpr friend inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2); - constexpr friend inline const QVector2D operator-(const QVector2D &vector); - constexpr friend inline const QVector2D operator/(const QVector2D &vector, float divisor); - constexpr friend inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor); - - constexpr friend inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2); - -#ifndef QT_NO_VECTOR3D - QVector3D toVector3D() const; -#endif -#ifndef QT_NO_VECTOR4D - QVector4D toVector4D() const; -#endif - - constexpr QPoint toPoint() const; - constexpr QPointF toPointF() const; - - operator QVariant() const; - -private: - float v[2]; - - friend class QVector3D; - friend class QVector4D; -}; - -Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE); - -constexpr inline QVector2D::QVector2D() : v{0.0f, 0.0f} {} - -constexpr inline QVector2D::QVector2D(float xpos, float ypos) : v{xpos, ypos} {} - -constexpr inline QVector2D::QVector2D(const QPoint& point) : v{float(point.x()), float(point.y())} {} - -constexpr inline QVector2D::QVector2D(const QPointF& point) : v{float(point.x()), float(point.y())} {} - -inline bool QVector2D::isNull() const -{ - return qIsNull(v[0]) && qIsNull(v[1]); -} - -constexpr inline float QVector2D::x() const { return v[0]; } -constexpr inline float QVector2D::y() const { return v[1]; } - -inline void QVector2D::setX(float aX) { v[0] = aX; } -inline void QVector2D::setY(float aY) { v[1] = aY; } - -inline float &QVector2D::operator[](int i) -{ - Q_ASSERT(uint(i) < 2u); - return v[i]; -} - -inline float QVector2D::operator[](int i) const -{ - Q_ASSERT(uint(i) < 2u); - return v[i]; -} - -inline QVector2D &QVector2D::operator+=(const QVector2D &vector) -{ - v[0] += vector.v[0]; - v[1] += vector.v[1]; - return *this; -} - -inline QVector2D &QVector2D::operator-=(const QVector2D &vector) -{ - v[0] -= vector.v[0]; - v[1] -= vector.v[1]; - return *this; -} - -inline QVector2D &QVector2D::operator*=(float factor) -{ - v[0] *= factor; - v[1] *= factor; - return *this; -} - -inline QVector2D &QVector2D::operator*=(const QVector2D &vector) -{ - v[0] *= vector.v[0]; - v[1] *= vector.v[1]; - return *this; -} - -inline QVector2D &QVector2D::operator/=(float divisor) -{ - v[0] /= divisor; - v[1] /= divisor; - return *this; -} - -inline QVector2D &QVector2D::operator/=(const QVector2D &vector) -{ - v[0] /= vector.v[0]; - v[1] /= vector.v[1]; - return *this; -} - -constexpr inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2) -{ - return QVector2D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1]); -} - -constexpr inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2) -{ - return QVector2D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1]); -} - -constexpr inline const QVector2D operator*(float factor, const QVector2D &vector) -{ - return QVector2D(vector.v[0] * factor, vector.v[1] * factor); -} - -constexpr inline const QVector2D operator*(const QVector2D &vector, float factor) -{ - return QVector2D(vector.v[0] * factor, vector.v[1] * factor); -} - -constexpr inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2) -{ - return QVector2D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1]); -} - -constexpr inline const QVector2D operator-(const QVector2D &vector) -{ - return QVector2D(-vector.v[0], -vector.v[1]); -} - -constexpr inline const QVector2D operator/(const QVector2D &vector, float divisor) -{ - return QVector2D(vector.v[0] / divisor, vector.v[1] / divisor); -} - -constexpr inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor) -{ - return QVector2D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1]); -} - -constexpr inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2) -{ - return qFuzzyCompare(v1.v[0], v2.v[0]) && qFuzzyCompare(v1.v[1], v2.v[1]); -} - -constexpr inline QPoint QVector2D::toPoint() const -{ - return QPoint(qRound(v[0]), qRound(v[1])); -} - -constexpr inline QPointF QVector2D::toPointF() const -{ - return QPointF(qreal(v[0]), qreal(v[1])); -} - -#ifndef QT_NO_DEBUG_STREAM -Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector2D &vector); -#endif - -#ifndef QT_NO_DATASTREAM -Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector2D &); -Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &); -#endif - -#endif - -QT_END_NAMESPACE +#include <QtGui/qvectornd.h> +#if 0 +#pragma qt_sync_stop_processing #endif diff --git a/src/gui/math3d/qvector3d.cpp b/src/gui/math3d/qvector3d.cpp deleted file mode 100644 index 6d1d19c457..0000000000 --- a/src/gui/math3d/qvector3d.cpp +++ /dev/null @@ -1,759 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qvector3d.h" -#include "qvector2d.h" -#include "qvector4d.h" -#include "qmatrix4x4.h" -#include <QtCore/qdatastream.h> -#include <QtCore/qmath.h> -#include <QtCore/qvariant.h> -#include <QtCore/qdebug.h> -#include <QtCore/qrect.h> - -QT_BEGIN_NAMESPACE - -#ifndef QT_NO_VECTOR3D - -static_assert(std::is_standard_layout<QVector3D>::value, "QVector3D is supposed to be standard layout"); -static_assert(sizeof(QVector3D) == sizeof(float) * 3, "QVector3D is not supposed to have padding at the end"); - -/*! - \class QVector3D - \brief The QVector3D class represents a vector or vertex in 3D space. - \since 4.6 - \ingroup painting-3D - \inmodule QtGui - - Vectors are one of the main building blocks of 3D representation and - drawing. They consist of three coordinates, traditionally called - x, y, and z. - - The QVector3D class can also be used to represent vertices in 3D space. - We therefore do not need to provide a separate vertex class. - - \sa QVector2D, QVector4D, QQuaternion -*/ - -/*! - \fn QVector3D::QVector3D() - - Constructs a null vector, i.e. with coordinates (0, 0, 0). -*/ - -/*! - \fn QVector3D::QVector3D(Qt::Initialization) - \since 5.5 - \internal - - Constructs a vector without initializing the contents. -*/ - -/*! - \fn QVector3D::QVector3D(float xpos, float ypos, float zpos) - - Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos). -*/ - -/*! - \fn QVector3D::QVector3D(const QPoint& point) - - Constructs a vector with x and y coordinates from a 2D \a point, and a - z coordinate of 0. -*/ - -/*! - \fn QVector3D::QVector3D(const QPointF& point) - - Constructs a vector with x and y coordinates from a 2D \a point, and a - z coordinate of 0. -*/ - -#ifndef QT_NO_VECTOR2D - -/*! - Constructs a 3D vector from the specified 2D \a vector. The z - coordinate is set to zero. - - \sa toVector2D() -*/ -QVector3D::QVector3D(const QVector2D& vector) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; - v[2] = 0.0f; -} - -/*! - Constructs a 3D vector from the specified 2D \a vector. The z - coordinate is set to \a zpos. - - \sa toVector2D() -*/ -QVector3D::QVector3D(const QVector2D& vector, float zpos) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; - v[2] = zpos; -} - -#endif - -#ifndef QT_NO_VECTOR4D - -/*! - Constructs a 3D vector from the specified 4D \a vector. The w - coordinate is dropped. - - \sa toVector4D() -*/ -QVector3D::QVector3D(const QVector4D& vector) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; - v[2] = vector.v[2]; -} - -#endif - -/*! - \fn bool QVector3D::isNull() const - - Returns \c true if the x, y, and z coordinates are set to 0.0, - otherwise returns \c false. -*/ - -/*! - \fn float QVector3D::x() const - - Returns the x coordinate of this point. - - \sa setX(), y(), z() -*/ - -/*! - \fn float QVector3D::y() const - - Returns the y coordinate of this point. - - \sa setY(), x(), z() -*/ - -/*! - \fn float QVector3D::z() const - - Returns the z coordinate of this point. - - \sa setZ(), x(), y() -*/ - -/*! - \fn void QVector3D::setX(float x) - - Sets the x coordinate of this point to the given \a x coordinate. - - \sa x(), setY(), setZ() -*/ - -/*! - \fn void QVector3D::setY(float y) - - Sets the y coordinate of this point to the given \a y coordinate. - - \sa y(), setX(), setZ() -*/ - -/*! - \fn void QVector3D::setZ(float z) - - Sets the z coordinate of this point to the given \a z coordinate. - - \sa z(), setX(), setY() -*/ - -/*! \fn float &QVector3D::operator[](int i) - \since 5.2 - - Returns the component of the vector at index position \a i - as a modifiable reference. - - \a i must be a valid index position in the vector (i.e., 0 <= \a i - < 3). -*/ - -/*! \fn float QVector3D::operator[](int i) const - \since 5.2 - - Returns the component of the vector at index position \a i. - - \a i must be a valid index position in the vector (i.e., 0 <= \a i - < 3). -*/ - -/*! - Returns the normalized unit vector form of this vector. - - If this vector is null, then a null vector is returned. If the length - of the vector is very close to 1, then the vector will be returned as-is. - Otherwise the normalized form of the vector of length 1 will be returned. - - \sa length(), normalize() -*/ -QVector3D QVector3D::normalized() const -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]) + - double(v[2]) * double(v[2]); - if (qFuzzyIsNull(len - 1.0f)) { - return *this; - } else if (!qFuzzyIsNull(len)) { - double sqrtLen = std::sqrt(len); - return QVector3D(float(double(v[0]) / sqrtLen), - float(double(v[1]) / sqrtLen), - float(double(v[2]) / sqrtLen)); - } else { - return QVector3D(); - } -} - -/*! - Normalizes the currect vector in place. Nothing happens if this - vector is a null vector or the length of the vector is very close to 1. - - \sa length(), normalized() -*/ -void QVector3D::normalize() -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]) + - double(v[2]) * double(v[2]); - if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len)) - return; - - len = std::sqrt(len); - - v[0] = float(double(v[0]) / len); - v[1] = float(double(v[1]) / len); - v[2] = float(double(v[2]) / len); -} - -/*! - \fn QVector3D &QVector3D::operator+=(const QVector3D &vector) - - Adds the given \a vector to this vector and returns a reference to - this vector. - - \sa operator-=() -*/ - -/*! - \fn QVector3D &QVector3D::operator-=(const QVector3D &vector) - - Subtracts the given \a vector from this vector and returns a reference to - this vector. - - \sa operator+=() -*/ - -/*! - \fn QVector3D &QVector3D::operator*=(float factor) - - Multiplies this vector's coordinates by the given \a factor, and - returns a reference to this vector. - - \sa operator/=() -*/ - -/*! - \fn QVector3D &QVector3D::operator*=(const QVector3D& vector) - \overload - - Multiplies the components of this vector by the corresponding - components in \a vector. - - Note: this is not the same as the crossProduct() of this - vector and \a vector. - - \sa crossProduct() -*/ - -/*! - \fn QVector3D &QVector3D::operator/=(float divisor) - - Divides this vector's coordinates by the given \a divisor, and - returns a reference to this vector. - - \sa operator*=() -*/ - -/*! - \fn QVector3D &QVector3D::operator/=(const QVector3D &vector) - \since 5.5 - - Divides the components of this vector by the corresponding - components in \a vector. - - \sa operator*=() -*/ - -/*! - Returns the dot product of \a v1 and \a v2. -*/ -float QVector3D::dotProduct(const QVector3D& v1, const QVector3D& v2) -{ - return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1] + v1.v[2] * v2.v[2]; -} - -/*! - Returns the cross-product of vectors \a v1 and \a v2, which corresponds - to the normal vector of a plane defined by \a v1 and \a v2. - - \sa normal() -*/ -QVector3D QVector3D::crossProduct(const QVector3D& v1, const QVector3D& v2) -{ - return QVector3D(v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1], - v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2], - v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0]); -} - -/*! - Returns the normal vector of a plane defined by vectors \a v1 and \a v2, - normalized to be a unit vector. - - Use crossProduct() to compute the cross-product of \a v1 and \a v2 if you - do not need the result to be normalized to a unit vector. - - \sa crossProduct(), distanceToPlane() -*/ -QVector3D QVector3D::normal(const QVector3D& v1, const QVector3D& v2) -{ - return crossProduct(v1, v2).normalized(); -} - -/*! - \overload - - Returns the normal vector of a plane defined by vectors - \a v2 - \a v1 and \a v3 - \a v1, normalized to be a unit vector. - - Use crossProduct() to compute the cross-product of \a v2 - \a v1 and - \a v3 - \a v1 if you do not need the result to be normalized to a - unit vector. - - \sa crossProduct(), distanceToPlane() -*/ -QVector3D QVector3D::normal - (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3) -{ - return crossProduct((v2 - v1), (v3 - v1)).normalized(); -} - -/*! - \since 5.5 - - Returns the window coordinates of this vector initially in object/model - coordinates using the model view matrix \a modelView, the projection matrix - \a projection and the viewport dimensions \a viewport. - - When transforming from clip to normalized space, a division by the w - component on the vector components takes place. To prevent dividing by 0 if - w equals to 0, it is set to 1. - - \note the returned y coordinates are in OpenGL orientation. OpenGL expects - the bottom to be 0 whereas for Qt top is 0. - - \sa unproject() - */ -QVector3D QVector3D::project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const -{ - QVector4D tmp(*this, 1.0f); - tmp = projection * modelView * tmp; - if (qFuzzyIsNull(tmp.w())) - tmp.setW(1.0f); - tmp /= tmp.w(); - - tmp = tmp * 0.5f + QVector4D(0.5f, 0.5f, 0.5f, 0.5f); - tmp.setX(tmp.x() * viewport.width() + viewport.x()); - tmp.setY(tmp.y() * viewport.height() + viewport.y()); - - return tmp.toVector3D(); -} - -/*! - \since 5.5 - - Returns the object/model coordinates of this vector initially in window - coordinates using the model view matrix \a modelView, the projection matrix - \a projection and the viewport dimensions \a viewport. - - When transforming from clip to normalized space, a division by the w - component of the vector components takes place. To prevent dividing by 0 if - w equals to 0, it is set to 1. - - \note y coordinates in \a viewport should use OpenGL orientation. OpenGL - expects the bottom to be 0 whereas for Qt top is 0. - - \sa project() - */ -QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const -{ - QMatrix4x4 inverse = QMatrix4x4( projection * modelView ).inverted(); - - QVector4D tmp(*this, 1.0f); - tmp.setX((tmp.x() - float(viewport.x())) / float(viewport.width())); - tmp.setY((tmp.y() - float(viewport.y())) / float(viewport.height())); - tmp = tmp * 2.0f - QVector4D(1.0f, 1.0f, 1.0f, 1.0f); - - QVector4D obj = inverse * tmp; - if (qFuzzyIsNull(obj.w())) - obj.setW(1.0f); - obj /= obj.w(); - return obj.toVector3D(); -} - -/*! - \since 5.1 - - Returns the distance from this vertex to a point defined by - the vertex \a point. - - \sa distanceToPlane(), distanceToLine() -*/ -float QVector3D::distanceToPoint(const QVector3D& point) const -{ - return (*this - point).length(); -} - -/*! - Returns the distance from this vertex to a plane defined by - the vertex \a plane and a \a normal unit vector. The \a normal - parameter is assumed to have been normalized to a unit vector. - - The return value will be negative if the vertex is below the plane, - or zero if it is on the plane. - - \sa normal(), distanceToLine() -*/ -float QVector3D::distanceToPlane - (const QVector3D& plane, const QVector3D& normal) const -{ - return dotProduct(*this - plane, normal); -} - -/*! - \overload - - Returns the distance from this vertex to a plane defined by - the vertices \a plane1, \a plane2 and \a plane3. - - The return value will be negative if the vertex is below the plane, - or zero if it is on the plane. - - The two vectors that define the plane are \a plane2 - \a plane1 - and \a plane3 - \a plane1. - - \sa normal(), distanceToLine() -*/ -float QVector3D::distanceToPlane - (const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const -{ - QVector3D n = normal(plane2 - plane1, plane3 - plane1); - return dotProduct(*this - plane1, n); -} - -/*! - Returns the distance that this vertex is from a line defined - by \a point and the unit vector \a direction. - - If \a direction is a null vector, then it does not define a line. - In that case, the distance from \a point to this vertex is returned. - - \sa distanceToPlane() -*/ -float QVector3D::distanceToLine - (const QVector3D& point, const QVector3D& direction) const -{ - if (direction.isNull()) - return (*this - point).length(); - QVector3D p = point + dotProduct(*this - point, direction) * direction; - return (*this - p).length(); -} - -/*! - \fn bool QVector3D::operator==(const QVector3D &v1, const QVector3D &v2) - - Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false. - This operator uses an exact floating-point comparison. -*/ - -/*! - \fn bool QVector3D::operator!=(const QVector3D &v1, const QVector3D &v2) - - Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false. - This operator uses an exact floating-point comparison. -*/ - -/*! - \fn const QVector3D operator+(const QVector3D &v1, const QVector3D &v2) - \relates QVector3D - - Returns a QVector3D object that is the sum of the given vectors, \a v1 - and \a v2; each component is added separately. - - \sa QVector3D::operator+=() -*/ - -/*! - \fn const QVector3D operator-(const QVector3D &v1, const QVector3D &v2) - \relates QVector3D - - Returns a QVector3D object that is formed by subtracting \a v2 from \a v1; - each component is subtracted separately. - - \sa QVector3D::operator-=() -*/ - -/*! - \fn const QVector3D operator*(float factor, const QVector3D &vector) - \relates QVector3D - - Returns a copy of the given \a vector, multiplied by the given \a factor. - - \sa QVector3D::operator*=() -*/ - -/*! - \fn const QVector3D operator*(const QVector3D &vector, float factor) - \relates QVector3D - - Returns a copy of the given \a vector, multiplied by the given \a factor. - - \sa QVector3D::operator*=() -*/ - -/*! - \fn const QVector3D operator*(const QVector3D &v1, const QVector3D& v2) - \relates QVector3D - - Multiplies the components of \a v1 by the corresponding components in \a v2. - - Note: this is not the same as the crossProduct() of \a v1 and \a v2. - - \sa QVector3D::crossProduct() -*/ - -/*! - \fn const QVector3D operator-(const QVector3D &vector) - \relates QVector3D - \overload - - Returns a QVector3D object that is formed by changing the sign of - all three components of the given \a vector. - - Equivalent to \c {QVector3D(0,0,0) - vector}. -*/ - -/*! - \fn const QVector3D operator/(const QVector3D &vector, float divisor) - \relates QVector3D - - Returns the QVector3D object formed by dividing all three components of - the given \a vector by the given \a divisor. - - \sa QVector3D::operator/=() -*/ - -/*! - \fn const QVector3D operator/(const QVector3D &vector, const QVector3D &divisor) - \relates QVector3D - \since 5.5 - - Returns the QVector3D object formed by dividing components of the given - \a vector by a respective components of the given \a divisor. - - \sa QVector3D::operator/=() -*/ - -/*! - \fn bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2) - \relates QVector3D - - Returns \c true if \a v1 and \a v2 are equal, allowing for a small - fuzziness factor for floating-point comparisons; false otherwise. -*/ - -#ifndef QT_NO_VECTOR2D - -/*! - Returns the 2D vector form of this 3D vector, dropping the z coordinate. - - \sa toVector4D(), toPoint() -*/ -QVector2D QVector3D::toVector2D() const -{ - return QVector2D(v[0], v[1]); -} - -#endif - -#ifndef QT_NO_VECTOR4D - -/*! - Returns the 4D form of this 3D vector, with the w coordinate set to zero. - - \sa toVector2D(), toPoint() -*/ -QVector4D QVector3D::toVector4D() const -{ - return QVector4D(v[0], v[1], v[2], 0.0f); -} - -#endif - -/*! - \fn QPoint QVector3D::toPoint() const - - Returns the QPoint form of this 3D vector. The z coordinate - is dropped. - - \sa toPointF(), toVector2D() -*/ - -/*! - \fn QPointF QVector3D::toPointF() const - - Returns the QPointF form of this 3D vector. The z coordinate - is dropped. - - \sa toPoint(), toVector2D() -*/ - -/*! - Returns the 3D vector as a QVariant. -*/ -QVector3D::operator QVariant() const -{ - return QVariant::fromValue(*this); -} - -/*! - Returns the length of the vector from the origin. - - \sa lengthSquared(), normalized() -*/ -float QVector3D::length() const -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]) + - double(v[2]) * double(v[2]); - return float(std::sqrt(len)); -} - -/*! - Returns the squared length of the vector from the origin. - This is equivalent to the dot product of the vector with itself. - - \sa length(), dotProduct() -*/ -float QVector3D::lengthSquared() const -{ - return v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; -} - -#ifndef QT_NO_DEBUG_STREAM - -QDebug operator<<(QDebug dbg, const QVector3D &vector) -{ - QDebugStateSaver saver(dbg); - dbg.nospace() << "QVector3D(" - << vector.x() << ", " << vector.y() << ", " << vector.z() << ')'; - return dbg; -} - -#endif - -#ifndef QT_NO_DATASTREAM - -/*! - \fn QDataStream &operator<<(QDataStream &stream, const QVector3D &vector) - \relates QVector3D - - Writes the given \a vector to the given \a stream and returns a - reference to the stream. - - \sa {Serializing Qt Data Types} -*/ - -QDataStream &operator<<(QDataStream &stream, const QVector3D &vector) -{ - stream << vector.x() << vector.y() << vector.z(); - return stream; -} - -/*! - \fn QDataStream &operator>>(QDataStream &stream, QVector3D &vector) - \relates QVector3D - - Reads a 3D vector from the given \a stream into the given \a vector - and returns a reference to the stream. - - \sa {Serializing Qt Data Types} -*/ - -QDataStream &operator>>(QDataStream &stream, QVector3D &vector) -{ - float x, y, z; - stream >> x; - stream >> y; - stream >> z; - vector.setX(x); - vector.setY(y); - vector.setZ(z); - return stream; -} - -#endif // QT_NO_DATASTREAM - -#endif // QT_NO_VECTOR3D - -QT_END_NAMESPACE diff --git a/src/gui/math3d/qvector3d.h b/src/gui/math3d/qvector3d.h index ac4f7dffef..fc42579918 100644 --- a/src/gui/math3d/qvector3d.h +++ b/src/gui/math3d/qvector3d.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. @@ -37,277 +38,8 @@ ** ****************************************************************************/ -#ifndef QVECTOR3D_H -#define QVECTOR3D_H - -#include <QtGui/qtguiglobal.h> -#include <QtCore/qpoint.h> -#include <QtCore/qmetatype.h> - -QT_BEGIN_NAMESPACE - - -class QMatrix4x4; -class QVector2D; -class QVector4D; -class QRect; - -#ifndef QT_NO_VECTOR3D - -class Q_GUI_EXPORT QVector3D -{ -public: - constexpr QVector3D(); - explicit QVector3D(Qt::Initialization) {} - constexpr QVector3D(float xpos, float ypos, float zpos) : v{xpos, ypos, zpos} {} - - constexpr explicit QVector3D(const QPoint& point); - constexpr explicit QVector3D(const QPointF& point); -#ifndef QT_NO_VECTOR2D - QVector3D(const QVector2D& vector); - QVector3D(const QVector2D& vector, float zpos); -#endif -#ifndef QT_NO_VECTOR4D - explicit QVector3D(const QVector4D& vector); -#endif - - bool isNull() const; - - constexpr float x() const; - constexpr float y() const; - constexpr float z() const; - - void setX(float x); - void setY(float y); - void setZ(float z); - - float &operator[](int i); - float operator[](int i) const; - - float length() const; - float lengthSquared() const; - - QVector3D normalized() const; - void normalize(); - - QVector3D &operator+=(const QVector3D &vector); - QVector3D &operator-=(const QVector3D &vector); - QVector3D &operator*=(float factor); - QVector3D &operator*=(const QVector3D& vector); - QVector3D &operator/=(float divisor); - inline QVector3D &operator/=(const QVector3D &vector); - - static float dotProduct(const QVector3D& v1, const QVector3D& v2); //In Qt 6 convert to inline and constexpr - static QVector3D crossProduct(const QVector3D& v1, const QVector3D& v2); //in Qt 6 convert to inline and constexpr - - static QVector3D normal(const QVector3D& v1, const QVector3D& v2); - static QVector3D normal - (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3); - - QVector3D project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const; - QVector3D unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const; - - float distanceToPoint(const QVector3D& point) const; - float distanceToPlane(const QVector3D& plane, const QVector3D& normal) const; - float distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const; - float distanceToLine(const QVector3D& point, const QVector3D& direction) const; - -QT_WARNING_PUSH -QT_WARNING_DISABLE_FLOAT_COMPARE - constexpr friend inline bool operator==(const QVector3D &v1, const QVector3D &v2) noexcept - { - return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2]; - } - - constexpr friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2) noexcept - { - return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2]; - } -QT_WARNING_POP - - constexpr friend inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2); - constexpr friend inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2); - constexpr friend inline const QVector3D operator*(float factor, const QVector3D &vector); - constexpr friend inline const QVector3D operator*(const QVector3D &vector, float factor); - constexpr friend const QVector3D operator*(const QVector3D &v1, const QVector3D& v2); - constexpr friend inline const QVector3D operator-(const QVector3D &vector); - constexpr friend inline const QVector3D operator/(const QVector3D &vector, float divisor); - constexpr friend inline const QVector3D operator/(const QVector3D &vector, const QVector3D &divisor); - - constexpr friend inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2); - -#ifndef QT_NO_VECTOR2D - QVector2D toVector2D() const; -#endif -#ifndef QT_NO_VECTOR4D - QVector4D toVector4D() const; -#endif - - constexpr QPoint toPoint() const; - constexpr QPointF toPointF() const; - - operator QVariant() const; - -private: - float v[3]; - - friend class QVector2D; - friend class QVector4D; -#ifndef QT_NO_MATRIX4X4 - friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix); - friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector); -#endif -}; - -Q_DECLARE_TYPEINFO(QVector3D, Q_PRIMITIVE_TYPE); - -constexpr inline QVector3D::QVector3D() : v{0.0f, 0.0f, 0.0f} {} - -constexpr inline QVector3D::QVector3D(const QPoint& point) : v{float(point.x()), float(point.y()), float(0.0f)} {} - -constexpr inline QVector3D::QVector3D(const QPointF& point) : v{float(point.x()), float(point.y()), 0.0f} {} - -inline bool QVector3D::isNull() const -{ - return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]); -} - -constexpr inline float QVector3D::x() const { return v[0]; } -constexpr inline float QVector3D::y() const { return v[1]; } -constexpr inline float QVector3D::z() const { return v[2]; } - -inline void QVector3D::setX(float aX) { v[0] = aX; } -inline void QVector3D::setY(float aY) { v[1] = aY; } -inline void QVector3D::setZ(float aZ) { v[2] = aZ; } - -inline float &QVector3D::operator[](int i) -{ - Q_ASSERT(uint(i) < 3u); - return v[i]; -} - -inline float QVector3D::operator[](int i) const -{ - Q_ASSERT(uint(i) < 3u); - return v[i]; -} - -inline QVector3D &QVector3D::operator+=(const QVector3D &vector) -{ - v[0] += vector.v[0]; - v[1] += vector.v[1]; - v[2] += vector.v[2]; - return *this; -} - -inline QVector3D &QVector3D::operator-=(const QVector3D &vector) -{ - v[0] -= vector.v[0]; - v[1] -= vector.v[1]; - v[2] -= vector.v[2]; - return *this; -} - -inline QVector3D &QVector3D::operator*=(float factor) -{ - v[0] *= factor; - v[1] *= factor; - v[2] *= factor; - return *this; -} - -inline QVector3D &QVector3D::operator*=(const QVector3D& vector) -{ - v[0] *= vector.v[0]; - v[1] *= vector.v[1]; - v[2] *= vector.v[2]; - return *this; -} - -inline QVector3D &QVector3D::operator/=(float divisor) -{ - v[0] /= divisor; - v[1] /= divisor; - v[2] /= divisor; - return *this; -} - -inline QVector3D &QVector3D::operator/=(const QVector3D &vector) -{ - v[0] /= vector.v[0]; - v[1] /= vector.v[1]; - v[2] /= vector.v[2]; - return *this; -} - -constexpr inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2) -{ - return QVector3D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2]); -} - -constexpr inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2) -{ - return QVector3D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2]); -} - -constexpr inline const QVector3D operator*(float factor, const QVector3D &vector) -{ - return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor); -} - -constexpr inline const QVector3D operator*(const QVector3D &vector, float factor) -{ - return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor); -} - -constexpr inline const QVector3D operator*(const QVector3D &v1, const QVector3D& v2) -{ - return QVector3D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2]); -} - -constexpr inline const QVector3D operator-(const QVector3D &vector) -{ - return QVector3D(-vector.v[0], -vector.v[1], -vector.v[2]); -} - -constexpr inline const QVector3D operator/(const QVector3D &vector, float divisor) -{ - return QVector3D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor); -} - -constexpr inline const QVector3D operator/(const QVector3D &vector, const QVector3D &divisor) -{ - return QVector3D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1], vector.v[2] / divisor.v[2]); -} - -constexpr inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2) -{ - return qFuzzyCompare(v1.v[0], v2.v[0]) && - qFuzzyCompare(v1.v[1], v2.v[1]) && - qFuzzyCompare(v1.v[2], v2.v[2]); -} - -constexpr inline QPoint QVector3D::toPoint() const -{ - return QPoint(qRound(v[0]), qRound(v[1])); -} - -constexpr inline QPointF QVector3D::toPointF() const -{ - return QPointF(qreal(v[0]), qreal(v[1])); -} - -#ifndef QT_NO_DEBUG_STREAM -Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector3D &vector); -#endif - -#ifndef QT_NO_DATASTREAM -Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector3D &); -Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &); -#endif - -#endif - -QT_END_NAMESPACE +#include <QtGui/qvectornd.h> +#if 0 +#pragma qt_sync_stop_processing #endif diff --git a/src/gui/math3d/qvector4d.cpp b/src/gui/math3d/qvector4d.cpp deleted file mode 100644 index a9d57156f9..0000000000 --- a/src/gui/math3d/qvector4d.cpp +++ /dev/null @@ -1,639 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qvector4d.h" -#include "qvector3d.h" -#include "qvector2d.h" -#include <QtCore/qdatastream.h> -#include <QtCore/qdebug.h> -#include <QtCore/qvariant.h> -#include <QtCore/qmath.h> - -QT_BEGIN_NAMESPACE - -#ifndef QT_NO_VECTOR4D - -static_assert(std::is_standard_layout<QVector4D>::value, "QVector4D is supposed to be standard layout"); -static_assert(sizeof(QVector4D) == sizeof(float) * 4, "QVector4D is not supposed to have padding at the end"); - -/*! - \class QVector4D - \brief The QVector4D class represents a vector or vertex in 4D space. - \since 4.6 - \ingroup painting-3D - \inmodule QtGui - - The QVector4D class can also be used to represent vertices in 4D space. - We therefore do not need to provide a separate vertex class. - - \sa QQuaternion, QVector2D, QVector3D -*/ - -/*! - \fn QVector4D::QVector4D() - - Constructs a null vector, i.e. with coordinates (0, 0, 0, 0). -*/ - -/*! - \fn QVector4D::QVector4D(Qt::Initialization) - \since 5.5 - \internal - - Constructs a vector without initializing the contents. -*/ - -/*! - \fn QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) - - Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos, \a wpos). -*/ - -/*! - \fn QVector4D::QVector4D(const QPoint& point) - - Constructs a vector with x and y coordinates from a 2D \a point, and - z and w coordinates of 0. -*/ - -/*! - \fn QVector4D::QVector4D(const QPointF& point) - - Constructs a vector with x and y coordinates from a 2D \a point, and - z and w coordinates of 0. -*/ - -#ifndef QT_NO_VECTOR2D - -/*! - Constructs a 4D vector from the specified 2D \a vector. The z - and w coordinates are set to zero. - - \sa toVector2D() -*/ -QVector4D::QVector4D(const QVector2D& vector) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; - v[2] = 0.0f; - v[3] = 0.0f; -} - -/*! - Constructs a 4D vector from the specified 2D \a vector. The z - and w coordinates are set to \a zpos and \a wpos respectively. - - \sa toVector2D() -*/ -QVector4D::QVector4D(const QVector2D& vector, float zpos, float wpos) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; - v[2] = zpos; - v[3] = wpos; -} - -#endif - -#ifndef QT_NO_VECTOR3D - -/*! - Constructs a 4D vector from the specified 3D \a vector. The w - coordinate is set to zero. - - \sa toVector3D() -*/ -QVector4D::QVector4D(const QVector3D& vector) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; - v[2] = vector.v[2]; - v[3] = 0.0f; -} - -/*! - Constructs a 4D vector from the specified 3D \a vector. The w - coordinate is set to \a wpos. - - \sa toVector3D() -*/ -QVector4D::QVector4D(const QVector3D& vector, float wpos) -{ - v[0] = vector.v[0]; - v[1] = vector.v[1]; - v[2] = vector.v[2]; - v[3] = wpos; -} - -#endif - -/*! - \fn bool QVector4D::isNull() const - - Returns \c true if the x, y, z, and w coordinates are set to 0.0, - otherwise returns \c false. -*/ - -/*! - \fn float QVector4D::x() const - - Returns the x coordinate of this point. - - \sa setX(), y(), z(), w() -*/ - -/*! - \fn float QVector4D::y() const - - Returns the y coordinate of this point. - - \sa setY(), x(), z(), w() -*/ - -/*! - \fn float QVector4D::z() const - - Returns the z coordinate of this point. - - \sa setZ(), x(), y(), w() -*/ - -/*! - \fn float QVector4D::w() const - - Returns the w coordinate of this point. - - \sa setW(), x(), y(), z() -*/ - -/*! - \fn void QVector4D::setX(float x) - - Sets the x coordinate of this point to the given \a x coordinate. - - \sa x(), setY(), setZ(), setW() -*/ - -/*! - \fn void QVector4D::setY(float y) - - Sets the y coordinate of this point to the given \a y coordinate. - - \sa y(), setX(), setZ(), setW() -*/ - -/*! - \fn void QVector4D::setZ(float z) - - Sets the z coordinate of this point to the given \a z coordinate. - - \sa z(), setX(), setY(), setW() -*/ - -/*! - \fn void QVector4D::setW(float w) - - Sets the w coordinate of this point to the given \a w coordinate. - - \sa w(), setX(), setY(), setZ() -*/ - -/*! \fn float &QVector4D::operator[](int i) - \since 5.2 - - Returns the component of the vector at index position \a i - as a modifiable reference. - - \a i must be a valid index position in the vector (i.e., 0 <= \a i - < 4). -*/ - -/*! \fn float QVector4D::operator[](int i) const - \since 5.2 - - Returns the component of the vector at index position \a i. - - \a i must be a valid index position in the vector (i.e., 0 <= \a i - < 4). -*/ - -/*! - Returns the length of the vector from the origin. - - \sa lengthSquared(), normalized() -*/ -float QVector4D::length() const -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]) + - double(v[2]) * double(v[2]) + - double(v[3]) * double(v[3]); - return float(std::sqrt(len)); -} - -/*! - Returns the squared length of the vector from the origin. - This is equivalent to the dot product of the vector with itself. - - \sa length(), dotProduct() -*/ -float QVector4D::lengthSquared() const -{ - return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]; -} - -/*! - Returns the normalized unit vector form of this vector. - - If this vector is null, then a null vector is returned. If the length - of the vector is very close to 1, then the vector will be returned as-is. - Otherwise the normalized form of the vector of length 1 will be returned. - - \sa length(), normalize() -*/ -QVector4D QVector4D::normalized() const -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]) + - double(v[2]) * double(v[2]) + - double(v[3]) * double(v[3]); - if (qFuzzyIsNull(len - 1.0f)) { - return *this; - } else if (!qFuzzyIsNull(len)) { - double sqrtLen = std::sqrt(len); - return QVector4D(float(double(v[0]) / sqrtLen), - float(double(v[1]) / sqrtLen), - float(double(v[2]) / sqrtLen), - float(double(v[3]) / sqrtLen)); - } else { - return QVector4D(); - } -} - -/*! - Normalizes the currect vector in place. Nothing happens if this - vector is a null vector or the length of the vector is very close to 1. - - \sa length(), normalized() -*/ -void QVector4D::normalize() -{ - // Need some extra precision if the length is very small. - double len = double(v[0]) * double(v[0]) + - double(v[1]) * double(v[1]) + - double(v[2]) * double(v[2]) + - double(v[3]) * double(v[3]); - if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len)) - return; - - len = std::sqrt(len); - - v[0] = float(double(v[0]) / len); - v[1] = float(double(v[1]) / len); - v[2] = float(double(v[2]) / len); - v[3] = float(double(v[3]) / len); -} - -/*! - \fn QVector4D &QVector4D::operator+=(const QVector4D &vector) - - Adds the given \a vector to this vector and returns a reference to - this vector. - - \sa operator-=() -*/ - -/*! - \fn QVector4D &QVector4D::operator-=(const QVector4D &vector) - - Subtracts the given \a vector from this vector and returns a reference to - this vector. - - \sa operator+=() -*/ - -/*! - \fn QVector4D &QVector4D::operator*=(float factor) - - Multiplies this vector's coordinates by the given \a factor, and - returns a reference to this vector. - - \sa operator/=() -*/ - -/*! - \fn QVector4D &QVector4D::operator*=(const QVector4D &vector) - - Multiplies the components of this vector by the corresponding - components in \a vector. -*/ - -/*! - \fn QVector4D &QVector4D::operator/=(float divisor) - - Divides this vector's coordinates by the given \a divisor, and - returns a reference to this vector. - - \sa operator*=() -*/ - -/*! - \fn QVector4D &QVector4D::operator/=(const QVector4D &vector) - \since 5.5 - - Divides the components of this vector by the corresponding - components in \a vector. - - \sa operator*=() -*/ - -/*! - Returns the dot product of \a v1 and \a v2. -*/ -float QVector4D::dotProduct(const QVector4D& v1, const QVector4D& v2) -{ - 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]; -} - -/*! - \fn bool QVector4D::operator==(const QVector4D &v1, const QVector4D &v2) - - Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false. - This operator uses an exact floating-point comparison. -*/ - -/*! - \fn bool QVector4D::operator!=(const QVector4D &v1, const QVector4D &v2) - - Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false. - This operator uses an exact floating-point comparison. -*/ - -/*! - \fn const QVector4D operator+(const QVector4D &v1, const QVector4D &v2) - \relates QVector4D - - Returns a QVector4D object that is the sum of the given vectors, \a v1 - and \a v2; each component is added separately. - - \sa QVector4D::operator+=() -*/ - -/*! - \fn const QVector4D operator-(const QVector4D &v1, const QVector4D &v2) - \relates QVector4D - - Returns a QVector4D object that is formed by subtracting \a v2 from \a v1; - each component is subtracted separately. - - \sa QVector4D::operator-=() -*/ - -/*! - \fn const QVector4D operator*(float factor, const QVector4D &vector) - \relates QVector4D - - Returns a copy of the given \a vector, multiplied by the given \a factor. - - \sa QVector4D::operator*=() -*/ - -/*! - \fn const QVector4D operator*(const QVector4D &vector, float factor) - \relates QVector4D - - Returns a copy of the given \a vector, multiplied by the given \a factor. - - \sa QVector4D::operator*=() -*/ - -/*! - \fn const QVector4D operator*(const QVector4D &v1, const QVector4D& v2) - \relates QVector4D - - Returns the vector consisting of the multiplication of the - components from \a v1 and \a v2. - - \sa QVector4D::operator*=() -*/ - -/*! - \fn const QVector4D operator-(const QVector4D &vector) - \relates QVector4D - \overload - - Returns a QVector4D object that is formed by changing the sign of - all three components of the given \a vector. - - Equivalent to \c {QVector4D(0,0,0,0) - vector}. -*/ - -/*! - \fn const QVector4D operator/(const QVector4D &vector, float divisor) - \relates QVector4D - - Returns the QVector4D object formed by dividing all four components of - the given \a vector by the given \a divisor. - - \sa QVector4D::operator/=() -*/ - -/*! - \fn const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor) - \relates QVector4D - \since 5.5 - - Returns the QVector4D object formed by dividing components of the given - \a vector by a respective components of the given \a divisor. - - \sa QVector4D::operator/=() -*/ - -/*! - \fn bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2) - \relates QVector4D - - Returns \c true if \a v1 and \a v2 are equal, allowing for a small - fuzziness factor for floating-point comparisons; false otherwise. -*/ - -#ifndef QT_NO_VECTOR2D - -/*! - Returns the 2D vector form of this 4D vector, dropping the z and w coordinates. - - \sa toVector2DAffine(), toVector3D(), toPoint() -*/ -QVector2D QVector4D::toVector2D() const -{ - return QVector2D(v[0], v[1]); -} - -/*! - Returns the 2D vector form of this 4D vector, dividing the x and y - coordinates by the w coordinate and dropping the z coordinate. - Returns a null vector if w is zero. - - \sa toVector2D(), toVector3DAffine(), toPoint() -*/ -QVector2D QVector4D::toVector2DAffine() const -{ - if (qIsNull(v[3])) - return QVector2D(); - return QVector2D(v[0] / v[3], v[1] / v[3]); -} - -#endif - -#ifndef QT_NO_VECTOR3D - -/*! - Returns the 3D vector form of this 4D vector, dropping the w coordinate. - - \sa toVector3DAffine(), toVector2D(), toPoint() -*/ -QVector3D QVector4D::toVector3D() const -{ - return QVector3D(v[0], v[1], v[2]); -} - -/*! - Returns the 3D vector form of this 4D vector, dividing the x, y, and - z coordinates by the w coordinate. Returns a null vector if w is zero. - - \sa toVector3D(), toVector2DAffine(), toPoint() -*/ -QVector3D QVector4D::toVector3DAffine() const -{ - if (qIsNull(v[3])) - return QVector3D(); - return QVector3D(v[0] / v[3], v[1] / v[3], v[2] / v[3]); -} - -#endif - -/*! - \fn QPoint QVector4D::toPoint() const - - Returns the QPoint form of this 4D vector. The z and w coordinates - are dropped. - - \sa toPointF(), toVector2D() -*/ - -/*! - \fn QPointF QVector4D::toPointF() const - - Returns the QPointF form of this 4D vector. The z and w coordinates - are dropped. - - \sa toPoint(), toVector2D() -*/ - -/*! - Returns the 4D vector as a QVariant. -*/ -QVector4D::operator QVariant() const -{ - return QVariant::fromValue(*this); -} - -#ifndef QT_NO_DEBUG_STREAM - -QDebug operator<<(QDebug dbg, const QVector4D &vector) -{ - QDebugStateSaver saver(dbg); - dbg.nospace() << "QVector4D(" - << vector.x() << ", " << vector.y() << ", " - << vector.z() << ", " << vector.w() << ')'; - return dbg; -} - -#endif - -#ifndef QT_NO_DATASTREAM - -/*! - \fn QDataStream &operator<<(QDataStream &stream, const QVector4D &vector) - \relates QVector4D - - Writes the given \a vector to the given \a stream and returns a - reference to the stream. - - \sa {Serializing Qt Data Types} -*/ - -QDataStream &operator<<(QDataStream &stream, const QVector4D &vector) -{ - stream << vector.x() << vector.y() - << vector.z() << vector.w(); - return stream; -} - -/*! - \fn QDataStream &operator>>(QDataStream &stream, QVector4D &vector) - \relates QVector4D - - Reads a 4D vector from the given \a stream into the given \a vector - and returns a reference to the stream. - - \sa {Serializing Qt Data Types} -*/ - -QDataStream &operator>>(QDataStream &stream, QVector4D &vector) -{ - float x, y, z, w; - stream >> x; - stream >> y; - stream >> z; - stream >> w; - vector.setX(x); - vector.setY(y); - vector.setZ(z); - vector.setW(w); - return stream; -} - -#endif // QT_NO_DATASTREAM - -#endif // QT_NO_VECTOR4D - -QT_END_NAMESPACE diff --git a/src/gui/math3d/qvector4d.h b/src/gui/math3d/qvector4d.h index 327c6f01d3..fc42579918 100644 --- a/src/gui/math3d/qvector4d.h +++ b/src/gui/math3d/qvector4d.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. @@ -37,278 +38,8 @@ ** ****************************************************************************/ -#ifndef QVECTOR4D_H -#define QVECTOR4D_H - -#include <QtGui/qtguiglobal.h> -#include <QtCore/qpoint.h> -#include <QtCore/qmetatype.h> - -QT_BEGIN_NAMESPACE - - -class QMatrix4x4; -class QVector2D; -class QVector3D; - -#ifndef QT_NO_VECTOR4D - -class Q_GUI_EXPORT QVector4D -{ -public: - constexpr QVector4D(); - explicit QVector4D(Qt::Initialization) {} - constexpr QVector4D(float xpos, float ypos, float zpos, float wpos); - constexpr explicit QVector4D(const QPoint& point); - constexpr explicit QVector4D(const QPointF& point); -#ifndef QT_NO_VECTOR2D - QVector4D(const QVector2D& vector); - QVector4D(const QVector2D& vector, float zpos, float wpos); -#endif -#ifndef QT_NO_VECTOR3D - QVector4D(const QVector3D& vector); - QVector4D(const QVector3D& vector, float wpos); -#endif - - bool isNull() const; - - constexpr float x() const; - constexpr float y() const; - constexpr float z() const; - constexpr float w() const; - - void setX(float x); - void setY(float y); - void setZ(float z); - void setW(float w); - - float &operator[](int i); - float operator[](int i) const; - - float length() const; - float lengthSquared() const; //In Qt 6 convert to inline and constexpr - - [[nodiscard]] QVector4D normalized() const; - void normalize(); - - QVector4D &operator+=(const QVector4D &vector); - QVector4D &operator-=(const QVector4D &vector); - QVector4D &operator*=(float factor); - QVector4D &operator*=(const QVector4D &vector); - QVector4D &operator/=(float divisor); - inline QVector4D &operator/=(const QVector4D &vector); - - static float dotProduct(const QVector4D& v1, const QVector4D& v2); //In Qt 6 convert to inline and constexpr - -QT_WARNING_PUSH -QT_WARNING_DISABLE_FLOAT_COMPARE - constexpr friend inline bool operator==(const QVector4D &v1, const QVector4D &v2) noexcept - { - 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]; - } - - constexpr friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2) noexcept - { - 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]; - } -QT_WARNING_POP - - constexpr friend inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2); - constexpr friend inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2); - constexpr friend inline const QVector4D operator*(float factor, const QVector4D &vector); - constexpr friend inline const QVector4D operator*(const QVector4D &vector, float factor); - constexpr friend inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2); - constexpr friend inline const QVector4D operator-(const QVector4D &vector); - constexpr friend inline const QVector4D operator/(const QVector4D &vector, float divisor); - constexpr friend inline const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor); - - constexpr friend inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2); - -#ifndef QT_NO_VECTOR2D - QVector2D toVector2D() const; - QVector2D toVector2DAffine() const; -#endif -#ifndef QT_NO_VECTOR3D - QVector3D toVector3D() const; - QVector3D toVector3DAffine() const; -#endif - - constexpr QPoint toPoint() const; - constexpr QPointF toPointF() const; - - operator QVariant() const; - -private: - float v[4]; - - friend class QVector2D; - friend class QVector3D; -#ifndef QT_NO_MATRIX4X4 - friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix); - friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector); -#endif -}; - -Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE); - -constexpr inline QVector4D::QVector4D() : v{0.0f, 0.0f, 0.0f, 0.0f} {} - -constexpr inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) : v{xpos, ypos, zpos, wpos} {} - -constexpr inline QVector4D::QVector4D(const QPoint& point) : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {} - -constexpr inline QVector4D::QVector4D(const QPointF& point) : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {} - -inline bool QVector4D::isNull() const -{ - return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]) && qIsNull(v[3]); -} - -constexpr inline float QVector4D::x() const { return v[0]; } -constexpr inline float QVector4D::y() const { return v[1]; } -constexpr inline float QVector4D::z() const { return v[2]; } -constexpr inline float QVector4D::w() const { return v[3]; } - -inline void QVector4D::setX(float aX) { v[0] = aX; } -inline void QVector4D::setY(float aY) { v[1] = aY; } -inline void QVector4D::setZ(float aZ) { v[2] = aZ; } -inline void QVector4D::setW(float aW) { v[3] = aW; } - -inline float &QVector4D::operator[](int i) -{ - Q_ASSERT(uint(i) < 4u); - return v[i]; -} - -inline float QVector4D::operator[](int i) const -{ - Q_ASSERT(uint(i) < 4u); - return v[i]; -} - -inline QVector4D &QVector4D::operator+=(const QVector4D &vector) -{ - v[0] += vector.v[0]; - v[1] += vector.v[1]; - v[2] += vector.v[2]; - v[3] += vector.v[3]; - return *this; -} - -inline QVector4D &QVector4D::operator-=(const QVector4D &vector) -{ - v[0] -= vector.v[0]; - v[1] -= vector.v[1]; - v[2] -= vector.v[2]; - v[3] -= vector.v[3]; - return *this; -} - -inline QVector4D &QVector4D::operator*=(float factor) -{ - v[0] *= factor; - v[1] *= factor; - v[2] *= factor; - v[3] *= factor; - return *this; -} - -inline QVector4D &QVector4D::operator*=(const QVector4D &vector) -{ - v[0] *= vector.v[0]; - v[1] *= vector.v[1]; - v[2] *= vector.v[2]; - v[3] *= vector.v[3]; - return *this; -} - -inline QVector4D &QVector4D::operator/=(float divisor) -{ - v[0] /= divisor; - v[1] /= divisor; - v[2] /= divisor; - v[3] /= divisor; - return *this; -} - -inline QVector4D &QVector4D::operator/=(const QVector4D &vector) -{ - v[0] /= vector.v[0]; - v[1] /= vector.v[1]; - v[2] /= vector.v[2]; - v[3] /= vector.v[3]; - return *this; -} - -constexpr inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2) -{ - 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]); -} - -constexpr inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2) -{ - 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]); -} - -constexpr inline const QVector4D operator*(float factor, const QVector4D &vector) -{ - return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor); -} - -constexpr inline const QVector4D operator*(const QVector4D &vector, float factor) -{ - return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor); -} - -constexpr inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2) -{ - 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]); -} - -constexpr inline const QVector4D operator-(const QVector4D &vector) -{ - return QVector4D(-vector.v[0], -vector.v[1], -vector.v[2], -vector.v[3]); -} - -constexpr inline const QVector4D operator/(const QVector4D &vector, float divisor) -{ - return QVector4D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor, vector.v[3] / divisor); -} - -constexpr inline const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor) -{ - return QVector4D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1], vector.v[2] / divisor.v[2], vector.v[3] / divisor.v[3]); -} - -constexpr inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2) -{ - return qFuzzyCompare(v1.v[0], v2.v[0]) && - qFuzzyCompare(v1.v[1], v2.v[1]) && - qFuzzyCompare(v1.v[2], v2.v[2]) && - qFuzzyCompare(v1.v[3], v2.v[3]); -} - -constexpr inline QPoint QVector4D::toPoint() const -{ - return QPoint(qRound(v[0]), qRound(v[1])); -} - -constexpr inline QPointF QVector4D::toPointF() const -{ - return QPointF(qreal(v[0]), qreal(v[1])); -} - -#ifndef QT_NO_DEBUG_STREAM -Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector); -#endif - -#ifndef QT_NO_DATASTREAM -Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &); -Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &); -#endif - -#endif - -QT_END_NAMESPACE +#include <QtGui/qvectornd.h> +#if 0 +#pragma qt_sync_stop_processing #endif diff --git a/src/gui/math3d/qvectornd.cpp b/src/gui/math3d/qvectornd.cpp new file mode 100644 index 0000000000..5433e9bc6c --- /dev/null +++ b/src/gui/math3d/qvectornd.cpp @@ -0,0 +1,1635 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qvectornd.h" +#include <QtCore/qdatastream.h> +#include <QtCore/qdebug.h> +#include <QtCore/qvariant.h> +#include <QtGui/qmatrix4x4.h> + +QT_BEGIN_NAMESPACE + +#ifndef QT_NO_VECTOR2D + +/*! + \class QVector2D + \brief The QVector2D class represents a vector or vertex in 2D space. + \since 4.6 + \ingroup painting + \ingroup painting-3D + \inmodule QtGui + + The QVector2D class can also be used to represent vertices in 2D space. + We therefore do not need to provide a separate vertex class. + + \sa QVector3D, QVector4D, QQuaternion +*/ + +/*! + \fn QVector2D::QVector2D() + + Constructs a null vector, i.e. with coordinates (0, 0). +*/ + +/*! + \fn QVector2D::QVector2D(Qt::Initialization) + \since 5.5 + \internal + + Constructs a vector without initializing the contents. +*/ + +/*! + \fn QVector2D::QVector2D(float xpos, float ypos) + + Constructs a vector with coordinates (\a xpos, \a ypos). +*/ + +/*! + \fn QVector2D::QVector2D(QPoint point) + + Constructs a vector with x and y coordinates from a 2D \a point. +*/ + +/*! + \fn QVector2D::QVector2D(QPointF point) + + Constructs a vector with x and y coordinates from a 2D \a point. +*/ + +#ifndef QT_NO_VECTOR3D + +/*! + \fn QVector2D::QVector2D(QVector3D vector) + + Constructs a vector with x and y coordinates from a 3D \a vector. + The z coordinate of \a vector is dropped. + + \sa toVector3D() +*/ + +#endif + +#ifndef QT_NO_VECTOR4D + +/*! + \fn QVector2D::QVector2D(QVector4D vector) + + Constructs a vector with x and y coordinates from a 3D \a vector. + The z and w coordinates of \a vector are dropped. + + \sa toVector4D() +*/ + +#endif + +/*! + \fn bool QVector2D::isNull() const + + Returns \c true if the x and y coordinates are set to 0.0, + otherwise returns \c false. +*/ + +/*! + \fn float QVector2D::x() const + + Returns the x coordinate of this point. + + \sa setX(), y() +*/ + +/*! + \fn float QVector2D::y() const + + Returns the y coordinate of this point. + + \sa setY(), x() +*/ + +/*! + \fn void QVector2D::setX(float x) + + Sets the x coordinate of this point to the given \a x coordinate. + + \sa x(), setY() +*/ + +/*! + \fn void QVector2D::setY(float y) + + Sets the y coordinate of this point to the given \a y coordinate. + + \sa y(), setX() +*/ + +/*! \fn float &QVector2D::operator[](int i) + \since 5.2 + + Returns the component of the vector at index position \a i + as a modifiable reference. + + \a i must be a valid index position in the vector (i.e., 0 <= \a i + < 2). +*/ + +/*! \fn float QVector2D::operator[](int i) const + \since 5.2 + + Returns the component of the vector at index position \a i. + + \a i must be a valid index position in the vector (i.e., 0 <= \a i + < 2). +*/ + +/*! + \fn float QVector2D::length() const + + Returns the length of the vector from the origin. + + \sa lengthSquared(), normalized() +*/ + +/*! + \fn float QVector2D::lengthSquared() const + + Returns the squared length of the vector from the origin. + This is equivalent to the dot product of the vector with itself. + + \sa length(), dotProduct() +*/ + +/*! + \fn QVector2D QVector2D::normalized() const + + Returns the normalized unit vector form of this vector. + + If this vector is null, then a null vector is returned. If the length + of the vector is very close to 1, then the vector will be returned as-is. + Otherwise the normalized form of the vector of length 1 will be returned. + + \sa length(), normalize() +*/ + +/*! + \fn void QVector2D::normalize() + + Normalizes the currect vector in place. Nothing happens if this + vector is a null vector or the length of the vector is very close to 1. + + \sa length(), normalized() +*/ + +/*! + \fn float QVector2D::distanceToPoint(QVector2D point) const + \since 5.1 + + Returns the distance from this vertex to a point defined by + the vertex \a point. + + \sa distanceToLine() +*/ + +/*! + \fn float QVector2D::distanceToLine(QVector2D point, QVector2D direction) const + \since 5.1 + + Returns the distance that this vertex is from a line defined + by \a point and the unit vector \a direction. + + If \a direction is a null vector, then it does not define a line. + In that case, the distance from \a point to this vertex is returned. + + \sa distanceToPoint() +*/ + +/*! + \fn QVector2D &QVector2D::operator+=(QVector2D vector) + + Adds the given \a vector to this vector and returns a reference to + this vector. + + \sa operator-=() +*/ + +/*! + \fn QVector2D &QVector2D::operator-=(QVector2D vector) + + Subtracts the given \a vector from this vector and returns a reference to + this vector. + + \sa operator+=() +*/ + +/*! + \fn QVector2D &QVector2D::operator*=(float factor) + + Multiplies this vector's coordinates by the given \a factor, and + returns a reference to this vector. + + \sa operator/=() +*/ + +/*! + \fn QVector2D &QVector2D::operator*=(QVector2D vector) + + Multiplies the components of this vector by the corresponding + components in \a vector. +*/ + +/*! + \fn QVector2D &QVector2D::operator/=(float divisor) + + Divides this vector's coordinates by the given \a divisor, and + returns a reference to this vector. + + \sa operator*=() +*/ + +/*! + \fn QVector2D &QVector2D::operator/=(QVector2D vector) + \since 5.5 + + Divides the components of this vector by the corresponding + components in \a vector. + + \sa operator*=() +*/ + +/*! + \fn float QVector2D::dotProduct(QVector2D v1, QVector2D v2) + + Returns the dot product of \a v1 and \a v2. +*/ + +/*! + \fn bool QVector2D::operator==(QVector2D v1, QVector2D v2) + + Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false. + This operator uses an exact floating-point comparison. +*/ + +/*! + \fn bool QVector2D::operator!=(QVector2D v1, QVector2D v2) + + Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false. + This operator uses an exact floating-point comparison. +*/ + +/*! + \fn const QVector2D operator+(QVector2D v1, QVector2D v2) + \relates QVector2D + + Returns a QVector2D object that is the sum of the given vectors, \a v1 + and \a v2; each component is added separately. + + \sa QVector2D::operator+=() +*/ + +/*! + \fn const QVector2D operator-(QVector2D v1, QVector2D v2) + \relates QVector2D + + Returns a QVector2D object that is formed by subtracting \a v2 from \a v1; + each component is subtracted separately. + + \sa QVector2D::operator-=() +*/ + +/*! + \fn const QVector2D operator*(float factor, QVector2D vector) + \relates QVector2D + + Returns a copy of the given \a vector, multiplied by the given \a factor. + + \sa QVector2D::operator*=() +*/ + +/*! + \fn const QVector2D operator*(QVector2D vector, float factor) + \relates QVector2D + + Returns a copy of the given \a vector, multiplied by the given \a factor. + + \sa QVector2D::operator*=() +*/ + +/*! + \fn const QVector2D operator*(QVector2D v1, QVector2D v2) + \relates QVector2D + + Multiplies the components of \a v1 by the corresponding + components in \a v2. +*/ + +/*! + \fn const QVector2D operator-(QVector2D vector) + \relates QVector2D + \overload + + Returns a QVector2D object that is formed by changing the sign of + the components of the given \a vector. + + Equivalent to \c {QVector2D(0,0) - vector}. +*/ + +/*! + \fn const QVector2D operator/(QVector2D vector, float divisor) + \relates QVector2D + + Returns the QVector2D object formed by dividing all three components of + the given \a vector by the given \a divisor. + + \sa QVector2D::operator/=() +*/ + +/*! + \fn const QVector2D operator/(QVector2D vector, QVector2D divisor) + \relates QVector2D + \since 5.5 + + Returns the QVector2D object formed by dividing components of the given + \a vector by a respective components of the given \a divisor. + + \sa QVector2D::operator/=() +*/ + +/*! + \fn bool qFuzzyCompare(QVector2D v1, QVector2D v2) + \relates QVector2D + + Returns \c true if \a v1 and \a v2 are equal, allowing for a small + fuzziness factor for floating-point comparisons; false otherwise. +*/ + +#ifndef QT_NO_VECTOR3D +/*! + \fn QVector3D QVector2D::toVector3D() const + + Returns the 3D form of this 2D vector, with the z coordinate set to zero. + + \sa toVector4D(), toPoint() +*/ +#endif + +#ifndef QT_NO_VECTOR4D +/*! + \fn QVector4D QVector2D::toVector4D() const + + Returns the 4D form of this 2D vector, with the z and w coordinates set to zero. + + \sa toVector3D(), toPoint() +*/ +#endif + +/*! + \fn QPoint QVector2D::toPoint() const + + Returns the QPoint form of this 2D vector. + + \sa toPointF(), toVector3D() +*/ + +/*! + \fn QPointF QVector2D::toPointF() const + + Returns the QPointF form of this 2D vector. + + \sa toPoint(), toVector3D() +*/ + +/*! + Returns the 2D vector as a QVariant. +*/ +QVector2D::operator QVariant() const +{ + return QVariant::fromValue(*this); +} + +#ifndef QT_NO_DEBUG_STREAM + +QDebug operator<<(QDebug dbg, QVector2D vector) +{ + QDebugStateSaver saver(dbg); + dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')'; + return dbg; +} + +#endif + +#ifndef QT_NO_DATASTREAM + +/*! + \fn QDataStream &operator<<(QDataStream &stream, QVector2D vector) + \relates QVector2D + + Writes the given \a vector to the given \a stream and returns a + reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator<<(QDataStream &stream, QVector2D vector) +{ + stream << vector.x() << vector.y(); + return stream; +} + +/*! + \fn QDataStream &operator>>(QDataStream &stream, QVector2D &vector) + \relates QVector2D + + Reads a 2D vector from the given \a stream into the given \a vector + and returns a reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator>>(QDataStream &stream, QVector2D &vector) +{ + float x, y; + stream >> x; + stream >> y; + vector.setX(x); + vector.setY(y); + return stream; +} + +#endif // QT_NO_DATASTREAM + +#endif // QT_NO_VECTOR2D + + + +#ifndef QT_NO_VECTOR3D + +/*! + \class QVector3D + \brief The QVector3D class represents a vector or vertex in 3D space. + \since 4.6 + \ingroup painting-3D + \inmodule QtGui + + Vectors are one of the main building blocks of 3D representation and + drawing. They consist of three coordinates, traditionally called + x, y, and z. + + The QVector3D class can also be used to represent vertices in 3D space. + We therefore do not need to provide a separate vertex class. + + \sa QVector2D, QVector4D, QQuaternion +*/ + +/*! + \fn QVector3D::QVector3D() + + Constructs a null vector, i.e. with coordinates (0, 0, 0). +*/ + +/*! + \fn QVector3D::QVector3D(Qt::Initialization) + \since 5.5 + \internal + + Constructs a vector without initializing the contents. +*/ + +/*! + \fn QVector3D::QVector3D(float xpos, float ypos, float zpos) + + Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos). +*/ + +/*! + \fn QVector3D::QVector3D(QPoint point) + + Constructs a vector with x and y coordinates from a 2D \a point, and a + z coordinate of 0. +*/ + +/*! + \fn QVector3D::QVector3D(QPointF point) + + Constructs a vector with x and y coordinates from a 2D \a point, and a + z coordinate of 0. +*/ + +#ifndef QT_NO_VECTOR2D + +/*! + \fn QVector3D::QVector3D(QVector2D vector) + + Constructs a 3D vector from the specified 2D \a vector. The z + coordinate is set to zero. + + \sa toVector2D() +*/ + +/*! + QVector3D::QVector3D(QVector2D vector, float zpos) + + Constructs a 3D vector from the specified 2D \a vector. The z + coordinate is set to \a zpos. + + \sa toVector2D() +*/ +#endif + +#ifndef QT_NO_VECTOR4D + +/*! + \fn QVector3D::QVector3D(QVector4D vector) + + Constructs a 3D vector from the specified 4D \a vector. The w + coordinate is dropped. + + \sa toVector4D() +*/ + +#endif + +/*! + \fn bool QVector3D::isNull() const + + Returns \c true if the x, y, and z coordinates are set to 0.0, + otherwise returns \c false. +*/ + +/*! + \fn float QVector3D::x() const + + Returns the x coordinate of this point. + + \sa setX(), y(), z() +*/ + +/*! + \fn float QVector3D::y() const + + Returns the y coordinate of this point. + + \sa setY(), x(), z() +*/ + +/*! + \fn float QVector3D::z() const + + Returns the z coordinate of this point. + + \sa setZ(), x(), y() +*/ + +/*! + \fn void QVector3D::setX(float x) + + Sets the x coordinate of this point to the given \a x coordinate. + + \sa x(), setY(), setZ() +*/ + +/*! + \fn void QVector3D::setY(float y) + + Sets the y coordinate of this point to the given \a y coordinate. + + \sa y(), setX(), setZ() +*/ + +/*! + \fn void QVector3D::setZ(float z) + + Sets the z coordinate of this point to the given \a z coordinate. + + \sa z(), setX(), setY() +*/ + +/*! \fn float &QVector3D::operator[](int i) + \since 5.2 + + Returns the component of the vector at index position \a i + as a modifiable reference. + + \a i must be a valid index position in the vector (i.e., 0 <= \a i + < 3). +*/ + +/*! \fn float QVector3D::operator[](int i) const + \since 5.2 + + Returns the component of the vector at index position \a i. + + \a i must be a valid index position in the vector (i.e., 0 <= \a i + < 3). +*/ + +/*! + \fn QVector3D QVector3D::normalized() const + + Returns the normalized unit vector form of this vector. + + If this vector is null, then a null vector is returned. If the length + of the vector is very close to 1, then the vector will be returned as-is. + Otherwise the normalized form of the vector of length 1 will be returned. + + \sa length(), normalize() +*/ + +/*! + \fn void QVector3D::normalize() + + Normalizes the currect vector in place. Nothing happens if this + vector is a null vector or the length of the vector is very close to 1. + + \sa length(), normalized() +*/ + +/*! + \fn QVector3D &QVector3D::operator+=(QVector3D vector) + + Adds the given \a vector to this vector and returns a reference to + this vector. + + \sa operator-=() +*/ + +/*! + \fn QVector3D &QVector3D::operator-=(QVector3D vector) + + Subtracts the given \a vector from this vector and returns a reference to + this vector. + + \sa operator+=() +*/ + +/*! + \fn QVector3D &QVector3D::operator*=(float factor) + + Multiplies this vector's coordinates by the given \a factor, and + returns a reference to this vector. + + \sa operator/=() +*/ + +/*! + \fn QVector3D &QVector3D::operator*=(QVector3D vector) + \overload + + Multiplies the components of this vector by the corresponding + components in \a vector. + + Note: this is not the same as the crossProduct() of this + vector and \a vector. + + \sa crossProduct() +*/ + +/*! + \fn QVector3D &QVector3D::operator/=(float divisor) + + Divides this vector's coordinates by the given \a divisor, and + returns a reference to this vector. + + \sa operator*=() +*/ + +/*! + \fn QVector3D &QVector3D::operator/=(QVector3D vector) + \since 5.5 + + Divides the components of this vector by the corresponding + components in \a vector. + + \sa operator*=() +*/ + +/*! + \fn float QVector3D::dotProduct(QVector3D v1, QVector3D v2) + + Returns the dot product of \a v1 and \a v2. +*/ + +/*! + \fn QVector3D QVector3D::crossProduct(QVector3D v1, QVector3D v2) + + Returns the cross-product of vectors \a v1 and \a v2, which corresponds + to the normal vector of a plane defined by \a v1 and \a v2. + + \sa normal() +*/ + +/*! + \fn QVector3D QVector3D::normal(QVector3D v1, QVector3D v2) + + Returns the normal vector of a plane defined by vectors \a v1 and \a v2, + normalized to be a unit vector. + + Use crossProduct() to compute the cross-product of \a v1 and \a v2 if you + do not need the result to be normalized to a unit vector. + + \sa crossProduct(), distanceToPlane() +*/ + +/*! + \fn QVector3D QVector3D::normal(QVector3D v1, QVector3D v2, QVector3D v3) + + Returns the normal vector of a plane defined by vectors + \a v2 - \a v1 and \a v3 - \a v1, normalized to be a unit vector. + + Use crossProduct() to compute the cross-product of \a v2 - \a v1 and + \a v3 - \a v1 if you do not need the result to be normalized to a + unit vector. + + \sa crossProduct(), distanceToPlane() +*/ + +/*! + \since 5.5 + + Returns the window coordinates of this vector initially in object/model + coordinates using the model view matrix \a modelView, the projection matrix + \a projection and the viewport dimensions \a viewport. + + When transforming from clip to normalized space, a division by the w + component on the vector components takes place. To prevent dividing by 0 if + w equals to 0, it is set to 1. + + \note the returned y coordinates are in OpenGL orientation. OpenGL expects + the bottom to be 0 whereas for Qt top is 0. + + \sa unproject() + */ +QVector3D QVector3D::project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const +{ + QVector4D tmp(*this, 1.0f); + tmp = projection * modelView * tmp; + if (qFuzzyIsNull(tmp.w())) + tmp.setW(1.0f); + tmp /= tmp.w(); + + tmp = tmp * 0.5f + QVector4D(0.5f, 0.5f, 0.5f, 0.5f); + tmp.setX(tmp.x() * viewport.width() + viewport.x()); + tmp.setY(tmp.y() * viewport.height() + viewport.y()); + + return tmp.toVector3D(); +} + +/*! + \since 5.5 + + Returns the object/model coordinates of this vector initially in window + coordinates using the model view matrix \a modelView, the projection matrix + \a projection and the viewport dimensions \a viewport. + + When transforming from clip to normalized space, a division by the w + component of the vector components takes place. To prevent dividing by 0 if + w equals to 0, it is set to 1. + + \note y coordinates in \a viewport should use OpenGL orientation. OpenGL + expects the bottom to be 0 whereas for Qt top is 0. + + \sa project() + */ +QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const +{ + QMatrix4x4 inverse = QMatrix4x4( projection * modelView ).inverted(); + + QVector4D tmp(*this, 1.0f); + tmp.setX((tmp.x() - float(viewport.x())) / float(viewport.width())); + tmp.setY((tmp.y() - float(viewport.y())) / float(viewport.height())); + tmp = tmp * 2.0f - QVector4D(1.0f, 1.0f, 1.0f, 1.0f); + + QVector4D obj = inverse * tmp; + if (qFuzzyIsNull(obj.w())) + obj.setW(1.0f); + obj /= obj.w(); + return obj.toVector3D(); +} + +/*! + \fn float QVector3D::distanceToPoint(QVector3D point) const + + \since 5.1 + + Returns the distance from this vertex to a point defined by + the vertex \a point. + + \sa distanceToPlane(), distanceToLine() +*/ + +/*! + \fn float QVector3D::distanceToPlane(QVector3D plane, QVector3D normal) const + + Returns the distance from this vertex to a plane defined by + the vertex \a plane and a \a normal unit vector. The \a normal + parameter is assumed to have been normalized to a unit vector. + + The return value will be negative if the vertex is below the plane, + or zero if it is on the plane. + + \sa normal(), distanceToLine() +*/ + +/*! + \fn float QVector3D::distanceToPlane(QVector3D plane1, QVector3D plane2, QVector3D plane3) const + + Returns the distance from this vertex to a plane defined by + the vertices \a plane1, \a plane2 and \a plane3. + + The return value will be negative if the vertex is below the plane, + or zero if it is on the plane. + + The two vectors that define the plane are \a plane2 - \a plane1 + and \a plane3 - \a plane1. + + \sa normal(), distanceToLine() +*/ + +/*! + \fn float QVector3D::distanceToLine(QVector3D point, QVector3D direction) const + + Returns the distance that this vertex is from a line defined + by \a point and the unit vector \a direction. + + If \a direction is a null vector, then it does not define a line. + In that case, the distance from \a point to this vertex is returned. + + \sa distanceToPlane() +*/ + +/*! + \fn bool QVector3D::operator==(QVector3D v1, QVector3D v2) + + Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false. + This operator uses an exact floating-point comparison. +*/ + +/*! + \fn bool QVector3D::operator!=(QVector3D v1, QVector3D v2) + + Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false. + This operator uses an exact floating-point comparison. +*/ + +/*! + \fn const QVector3D operator+(QVector3D v1, QVector3D v2) + \relates QVector3D + + Returns a QVector3D object that is the sum of the given vectors, \a v1 + and \a v2; each component is added separately. + + \sa QVector3D::operator+=() +*/ + +/*! + \fn const QVector3D operator-(QVector3D v1, QVector3D v2) + \relates QVector3D + + Returns a QVector3D object that is formed by subtracting \a v2 from \a v1; + each component is subtracted separately. + + \sa QVector3D::operator-=() +*/ + +/*! + \fn const QVector3D operator*(float factor, QVector3D vector) + \relates QVector3D + + Returns a copy of the given \a vector, multiplied by the given \a factor. + + \sa QVector3D::operator*=() +*/ + +/*! + \fn const QVector3D operator*(QVector3D vector, float factor) + \relates QVector3D + + Returns a copy of the given \a vector, multiplied by the given \a factor. + + \sa QVector3D::operator*=() +*/ + +/*! + \fn const QVector3D operator*(QVector3D v1, QVector3D v2) + \relates QVector3D + + Multiplies the components of \a v1 by the corresponding components in \a v2. + + Note: this is not the same as the crossProduct() of \a v1 and \a v2. + + \sa QVector3D::crossProduct() +*/ + +/*! + \fn const QVector3D operator-(QVector3D vector) + \relates QVector3D + \overload + + Returns a QVector3D object that is formed by changing the sign of + all three components of the given \a vector. + + Equivalent to \c {QVector3D(0,0,0) - vector}. +*/ + +/*! + \fn const QVector3D operator/(QVector3D vector, float divisor) + \relates QVector3D + + Returns the QVector3D object formed by dividing all three components of + the given \a vector by the given \a divisor. + + \sa QVector3D::operator/=() +*/ + +/*! + \fn const QVector3D operator/(QVector3D vector, QVector3D divisor) + \relates QVector3D + \since 5.5 + + Returns the QVector3D object formed by dividing components of the given + \a vector by a respective components of the given \a divisor. + + \sa QVector3D::operator/=() +*/ + +/*! + \fn bool qFuzzyCompare(QVector3D v1, QVector3D v2) + \relates QVector3D + + Returns \c true if \a v1 and \a v2 are equal, allowing for a small + fuzziness factor for floating-point comparisons; false otherwise. +*/ + +#ifndef QT_NO_VECTOR2D + +/*! + \fn QVector2D QVector3D::toVector2D() const + + Returns the 2D vector form of this 3D vector, dropping the z coordinate. + + \sa toVector4D(), toPoint() +*/ + +#endif + +#ifndef QT_NO_VECTOR4D + +/*! + \fn QVector4D QVector3D::toVector4D() const + + Returns the 4D form of this 3D vector, with the w coordinate set to zero. + + \sa toVector2D(), toPoint() +*/ + +#endif + +/*! + \fn QPoint QVector3D::toPoint() const + + Returns the QPoint form of this 3D vector. The z coordinate + is dropped. + + \sa toPointF(), toVector2D() +*/ + +/*! + \fn QPointF QVector3D::toPointF() const + + Returns the QPointF form of this 3D vector. The z coordinate + is dropped. + + \sa toPoint(), toVector2D() +*/ + +/*! + Returns the 3D vector as a QVariant. +*/ +QVector3D::operator QVariant() const +{ + return QVariant::fromValue(*this); +} + +/*! + \fn float QVector3D::length() const + + Returns the length of the vector from the origin. + + \sa lengthSquared(), normalized() +*/ + +/*! + \fn float QVector3D::lengthSquared() const + + Returns the squared length of the vector from the origin. + This is equivalent to the dot product of the vector with itself. + + \sa length(), dotProduct() +*/ + +#ifndef QT_NO_DEBUG_STREAM + +QDebug operator<<(QDebug dbg, QVector3D vector) +{ + QDebugStateSaver saver(dbg); + dbg.nospace() << "QVector3D(" + << vector.x() << ", " << vector.y() << ", " << vector.z() << ')'; + return dbg; +} + +#endif + +#ifndef QT_NO_DATASTREAM + +/*! + \fn QDataStream &operator<<(QDataStream &stream, QVector3D vector) + \relates QVector3D + + Writes the given \a vector to the given \a stream and returns a + reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator<<(QDataStream &stream, QVector3D vector) +{ + stream << vector.x() << vector.y() << vector.z(); + return stream; +} + +/*! + \fn QDataStream &operator>>(QDataStream &stream, QVector3D &vector) + \relates QVector3D + + Reads a 3D vector from the given \a stream into the given \a vector + and returns a reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator>>(QDataStream &stream, QVector3D &vector) +{ + float x, y, z; + stream >> x; + stream >> y; + stream >> z; + vector.setX(x); + vector.setY(y); + vector.setZ(z); + return stream; +} + +#endif // QT_NO_DATASTREAM + +#endif // QT_NO_VECTOR3D + + + +#ifndef QT_NO_VECTOR4D + +/*! + \class QVector4D + \brief The QVector4D class represents a vector or vertex in 4D space. + \since 4.6 + \ingroup painting-3D + \inmodule QtGui + + The QVector4D class can also be used to represent vertices in 4D space. + We therefore do not need to provide a separate vertex class. + + \sa QQuaternion, QVector2D, QVector3D +*/ + +/*! + \fn QVector4D::QVector4D() + + Constructs a null vector, i.e. with coordinates (0, 0, 0, 0). +*/ + +/*! + \fn QVector4D::QVector4D(Qt::Initialization) + \since 5.5 + \internal + + Constructs a vector without initializing the contents. +*/ + +/*! + \fn QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) + + Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos, \a wpos). +*/ + +/*! + \fn QVector4D::QVector4D(QPoint point) + + Constructs a vector with x and y coordinates from a 2D \a point, and + z and w coordinates of 0. +*/ + +/*! + \fn QVector4D::QVector4D(QPointF point) + + Constructs a vector with x and y coordinates from a 2D \a point, and + z and w coordinates of 0. +*/ + +#ifndef QT_NO_VECTOR2D + +/*! + \fn QVector4D::QVector4D(QVector2D vector) + + Constructs a 4D vector from the specified 2D \a vector. The z + and w coordinates are set to zero. + + \sa toVector2D() +*/ + +/*! + \fn QVector4D::QVector4D(QVector2D vector, float zpos, float wpos) + + Constructs a 4D vector from the specified 2D \a vector. The z + and w coordinates are set to \a zpos and \a wpos respectively. + + \sa toVector2D() +*/ + +#endif + +#ifndef QT_NO_VECTOR3D + +/*! + \fn QVector4D::QVector4D(QVector3D vector) + + Constructs a 4D vector from the specified 3D \a vector. The w + coordinate is set to zero. + + \sa toVector3D() +*/ + +/*! + \fn QVector4D::QVector4D(QVector3D vector) + + Constructs a 4D vector from the specified 3D \a vector. The w + coordinate is set to \a wpos. + + \sa toVector3D() +*/ + +#endif + +/*! + \fn bool QVector4D::isNull() const + + Returns \c true if the x, y, z, and w coordinates are set to 0.0, + otherwise returns \c false. +*/ + +/*! + \fn float QVector4D::x() const + + Returns the x coordinate of this point. + + \sa setX(), y(), z(), w() +*/ + +/*! + \fn float QVector4D::y() const + + Returns the y coordinate of this point. + + \sa setY(), x(), z(), w() +*/ + +/*! + \fn float QVector4D::z() const + + Returns the z coordinate of this point. + + \sa setZ(), x(), y(), w() +*/ + +/*! + \fn float QVector4D::w() const + + Returns the w coordinate of this point. + + \sa setW(), x(), y(), z() +*/ + +/*! + \fn void QVector4D::setX(float x) + + Sets the x coordinate of this point to the given \a x coordinate. + + \sa x(), setY(), setZ(), setW() +*/ + +/*! + \fn void QVector4D::setY(float y) + + Sets the y coordinate of this point to the given \a y coordinate. + + \sa y(), setX(), setZ(), setW() +*/ + +/*! + \fn void QVector4D::setZ(float z) + + Sets the z coordinate of this point to the given \a z coordinate. + + \sa z(), setX(), setY(), setW() +*/ + +/*! + \fn void QVector4D::setW(float w) + + Sets the w coordinate of this point to the given \a w coordinate. + + \sa w(), setX(), setY(), setZ() +*/ + +/*! \fn float &QVector4D::operator[](int i) + \since 5.2 + + Returns the component of the vector at index position \a i + as a modifiable reference. + + \a i must be a valid index position in the vector (i.e., 0 <= \a i + < 4). +*/ + +/*! \fn float QVector4D::operator[](int i) const + \since 5.2 + + Returns the component of the vector at index position \a i. + + \a i must be a valid index position in the vector (i.e., 0 <= \a i + < 4). +*/ + +/*! + \fn float QVector4D::length() const + + Returns the length of the vector from the origin. + + \sa lengthSquared(), normalized() +*/ + +/*! + \fn float QVector4D::lengthSquared() const + + Returns the squared length of the vector from the origin. + This is equivalent to the dot product of the vector with itself. + + \sa length(), dotProduct() +*/ + +/*! + \fn QVector4D QVector4D::normalized() const + + Returns the normalized unit vector form of this vector. + + If this vector is null, then a null vector is returned. If the length + of the vector is very close to 1, then the vector will be returned as-is. + Otherwise the normalized form of the vector of length 1 will be returned. + + \sa length(), normalize() +*/ + +/*! + \fn void QVector4D::normalize() + + Normalizes the currect vector in place. Nothing happens if this + vector is a null vector or the length of the vector is very close to 1. + + \sa length(), normalized() +*/ + + +/*! + \fn QVector4D &QVector4D::operator+=(QVector4D vector) + + Adds the given \a vector to this vector and returns a reference to + this vector. + + \sa operator-=() +*/ + +/*! + \fn QVector4D &QVector4D::operator-=(QVector4D vector) + + Subtracts the given \a vector from this vector and returns a reference to + this vector. + + \sa operator+=() +*/ + +/*! + \fn QVector4D &QVector4D::operator*=(float factor) + + Multiplies this vector's coordinates by the given \a factor, and + returns a reference to this vector. + + \sa operator/=() +*/ + +/*! + \fn QVector4D &QVector4D::operator*=(QVector4D vector) + + Multiplies the components of this vector by the corresponding + components in \a vector. +*/ + +/*! + \fn QVector4D &QVector4D::operator/=(float divisor) + + Divides this vector's coordinates by the given \a divisor, and + returns a reference to this vector. + + \sa operator*=() +*/ + +/*! + \fn QVector4D &QVector4D::operator/=(QVector4D vector) + \since 5.5 + + Divides the components of this vector by the corresponding + components in \a vector. + + \sa operator*=() +*/ + +/*! + \fn float QVector4D::dotProduct(QVector4D v1, QVector4D v2) + + Returns the dot product of \a v1 and \a v2. +*/ + +/*! + \fn bool QVector4D::operator==(QVector4D v1, QVector4D v2) + + Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false. + This operator uses an exact floating-point comparison. +*/ + +/*! + \fn bool QVector4D::operator!=(QVector4D v1, QVector4D v2) + + Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false. + This operator uses an exact floating-point comparison. +*/ + +/*! + \fn const QVector4D operator+(QVector4D v1, QVector4D v2) + \relates QVector4D + + Returns a QVector4D object that is the sum of the given vectors, \a v1 + and \a v2; each component is added separately. + + \sa QVector4D::operator+=() +*/ + +/*! + \fn const QVector4D operator-(QVector4D v1, QVector4D v2) + \relates QVector4D + + Returns a QVector4D object that is formed by subtracting \a v2 from \a v1; + each component is subtracted separately. + + \sa QVector4D::operator-=() +*/ + +/*! + \fn const QVector4D operator*(float factor, QVector4D vector) + \relates QVector4D + + Returns a copy of the given \a vector, multiplied by the given \a factor. + + \sa QVector4D::operator*=() +*/ + +/*! + \fn const QVector4D operator*(QVector4D vector, float factor) + \relates QVector4D + + Returns a copy of the given \a vector, multiplied by the given \a factor. + + \sa QVector4D::operator*=() +*/ + +/*! + \fn const QVector4D operator*(QVector4D v1, QVector4D v2) + \relates QVector4D + + Returns the vector consisting of the multiplication of the + components from \a v1 and \a v2. + + \sa QVector4D::operator*=() +*/ + +/*! + \fn const QVector4D operator-(QVector4D vector) + \relates QVector4D + \overload + + Returns a QVector4D object that is formed by changing the sign of + all three components of the given \a vector. + + Equivalent to \c {QVector4D(0,0,0,0) - vector}. +*/ + +/*! + \fn const QVector4D operator/(QVector4D vector, float divisor) + \relates QVector4D + + Returns the QVector4D object formed by dividing all four components of + the given \a vector by the given \a divisor. + + \sa QVector4D::operator/=() +*/ + +/*! + \fn const QVector4D operator/(QVector4D vector, QVector4D divisor) + \relates QVector4D + \since 5.5 + + Returns the QVector4D object formed by dividing components of the given + \a vector by a respective components of the given \a divisor. + + \sa QVector4D::operator/=() +*/ + +/*! + \fn bool qFuzzyCompare(QVector4D v1, QVector4D v2) + \relates QVector4D + + Returns \c true if \a v1 and \a v2 are equal, allowing for a small + fuzziness factor for floating-point comparisons; false otherwise. +*/ + +#ifndef QT_NO_VECTOR2D + +/*! + \fn QVector2D QVector4D::toVector2D() const + + Returns the 2D vector form of this 4D vector, dropping the z and w coordinates. + + \sa toVector2DAffine(), toVector3D(), toPoint() +*/ + +/*! + \fn QVector2D QVector4D::toVector2DAffine() const + + Returns the 2D vector form of this 4D vector, dividing the x and y + coordinates by the w coordinate and dropping the z coordinate. + Returns a null vector if w is zero. + + \sa toVector2D(), toVector3DAffine(), toPoint() +*/ + +#endif + +#ifndef QT_NO_VECTOR3D + +/*! + \fn QVector3D QVector4D::toVector3D() const + + Returns the 3D vector form of this 4D vector, dropping the w coordinate. + + \sa toVector3DAffine(), toVector2D(), toPoint() +*/ + +/*! + \fn QVector3D QVector4D::toVector3DAffine() const + + Returns the 3D vector form of this 4D vector, dividing the x, y, and + z coordinates by the w coordinate. Returns a null vector if w is zero. + + \sa toVector3D(), toVector2DAffine(), toPoint() +*/ + +#endif + +/*! + \fn QPoint QVector4D::toPoint() const + + Returns the QPoint form of this 4D vector. The z and w coordinates + are dropped. + + \sa toPointF(), toVector2D() +*/ + +/*! + \fn QPointF QVector4D::toPointF() const + + Returns the QPointF form of this 4D vector. The z and w coordinates + are dropped. + + \sa toPoint(), toVector2D() +*/ + +/*! + Returns the 4D vector as a QVariant. +*/ +QVector4D::operator QVariant() const +{ + return QVariant::fromValue(*this); +} + +#ifndef QT_NO_DEBUG_STREAM + +QDebug operator<<(QDebug dbg, QVector4D vector) +{ + QDebugStateSaver saver(dbg); + dbg.nospace() << "QVector4D(" + << vector.x() << ", " << vector.y() << ", " + << vector.z() << ", " << vector.w() << ')'; + return dbg; +} + +#endif + +#ifndef QT_NO_DATASTREAM + +/*! + \fn QDataStream &operator<<(QDataStream &stream, QVector4D vector) + \relates QVector4D + + Writes the given \a vector to the given \a stream and returns a + reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator<<(QDataStream &stream, QVector4D vector) +{ + stream << vector.x() << vector.y() + << vector.z() << vector.w(); + return stream; +} + +/*! + \fn QDataStream &operator>>(QDataStream &stream, QVector4D &vector) + \relates QVector4D + + Reads a 4D vector from the given \a stream into the given \a vector + and returns a reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator>>(QDataStream &stream, QVector4D &vector) +{ + float x, y, z, w; + stream >> x; + stream >> y; + stream >> z; + stream >> w; + vector.setX(x); + vector.setY(y); + vector.setZ(z); + vector.setW(w); + return stream; +} + +#endif // QT_NO_DATASTREAM + +#endif // QT_NO_VECTOR4D + +QT_END_NAMESPACE diff --git a/src/gui/math3d/qvectornd.h b/src/gui/math3d/qvectornd.h new file mode 100644 index 0000000000..af81a5d1c0 --- /dev/null +++ b/src/gui/math3d/qvectornd.h @@ -0,0 +1,1105 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QVECTORND_H +#define QVECTORND_H + +#include <QtGui/qtguiglobal.h> +#include <QtCore/qpoint.h> +#include <QtCore/qrect.h> +#include <QtCore/qmath.h> + +QT_BEGIN_NAMESPACE + +class QVector2D; +class QVector3D; +class QVector4D; +class QMatrix4x4; +class QVariant; + +/***************************** QVector2D *****************************/ + +#ifndef QT_NO_VECTOR2D + +class QVector2D +{ +public: + constexpr QVector2D() noexcept; + explicit QVector2D(Qt::Initialization) noexcept {} + constexpr QVector2D(float xpos, float ypos) noexcept; + constexpr explicit QVector2D(QPoint point) noexcept; + constexpr explicit QVector2D(QPointF point) noexcept; +#ifndef QT_NO_VECTOR3D + constexpr explicit QVector2D(QVector3D vector) noexcept; +#endif +#ifndef QT_NO_VECTOR4D + constexpr explicit QVector2D(QVector4D vector) noexcept; +#endif + + constexpr bool isNull() const noexcept; + + constexpr float x() const noexcept; + constexpr float y() const noexcept; + + constexpr void setX(float x) noexcept; + constexpr void setY(float y) noexcept; + + constexpr float &operator[](int i); + constexpr float operator[](int i) const; + + [[nodiscard]] float length() const noexcept; + [[nodiscard]] constexpr float lengthSquared() const noexcept; + + [[nodiscard]] QVector2D normalized() const noexcept; + void normalize() noexcept; + + [[nodiscard]] float distanceToPoint(QVector2D point) const noexcept; + [[nodiscard]] float distanceToLine(QVector2D point, QVector2D direction) const noexcept; + + constexpr QVector2D &operator+=(QVector2D vector) noexcept; + constexpr QVector2D &operator-=(QVector2D vector) noexcept; + constexpr QVector2D &operator*=(float factor) noexcept; + constexpr QVector2D &operator*=(QVector2D vector) noexcept; + constexpr QVector2D &operator/=(float divisor); + constexpr QVector2D &operator/=(QVector2D vector); + + [[nodiscard]] static constexpr float dotProduct(QVector2D v1, QVector2D v2) noexcept; + +QT_WARNING_PUSH +QT_WARNING_DISABLE_FLOAT_COMPARE + constexpr friend inline bool operator==(QVector2D v1, QVector2D v2) noexcept + { + return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1]; + } + + constexpr friend inline bool operator!=(QVector2D v1, QVector2D v2) noexcept + { + return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1]; + } +QT_WARNING_POP + + constexpr friend inline QVector2D operator+(QVector2D v1, QVector2D v2) noexcept + { + return QVector2D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1]); + } + + constexpr friend inline QVector2D operator-(QVector2D v1, QVector2D v2) noexcept + { + return QVector2D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1]); + } + + constexpr friend inline QVector2D operator*(float factor, QVector2D vector) noexcept + { + return QVector2D(vector.v[0] * factor, vector.v[1] * factor); + } + + constexpr friend inline QVector2D operator*(QVector2D vector, float factor) noexcept + { + return QVector2D(vector.v[0] * factor, vector.v[1] * factor); + } + + constexpr friend inline QVector2D operator*(QVector2D v1, QVector2D v2) noexcept + { + return QVector2D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1]); + } + + constexpr friend inline QVector2D operator-(QVector2D vector) noexcept + { + return QVector2D(-vector.v[0], -vector.v[1]); + } + + constexpr friend inline QVector2D operator/(QVector2D vector, float divisor) + { + return QVector2D(vector.v[0] / divisor, vector.v[1] / divisor); + } + + constexpr friend inline QVector2D operator/(QVector2D vector, QVector2D divisor) + { + return QVector2D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1]); + } + + constexpr friend inline bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept; + +#ifndef QT_NO_VECTOR3D + constexpr QVector3D toVector3D() const noexcept; +#endif +#ifndef QT_NO_VECTOR4D + constexpr QVector4D toVector4D() const noexcept; +#endif + + constexpr QPoint toPoint() const noexcept; + constexpr QPointF toPointF() const noexcept; + + Q_GUI_EXPORT operator QVariant() const; + +private: + float v[2]; + + friend class QVector3D; + friend class QVector4D; +}; + +Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE); + +#endif // QT_NO_VECTOR2D + + + +/***************************** QVector3D *****************************/ + +#ifndef QT_NO_VECTOR3D + +class QVector3D +{ +public: + constexpr QVector3D() noexcept; + explicit QVector3D(Qt::Initialization) noexcept {} + constexpr QVector3D(float xpos, float ypos, float zpos) noexcept : v{xpos, ypos, zpos} {} + + constexpr explicit QVector3D(QPoint point) noexcept; + constexpr explicit QVector3D(QPointF point) noexcept; +#ifndef QT_NO_VECTOR2D + constexpr QVector3D(QVector2D vector) noexcept; + constexpr QVector3D(QVector2D vector, float zpos) noexcept; +#endif +#ifndef QT_NO_VECTOR4D + constexpr explicit QVector3D(QVector4D vector) noexcept; +#endif + + constexpr bool isNull() const noexcept; + + constexpr float x() const noexcept; + constexpr float y() const noexcept; + constexpr float z() const noexcept; + + constexpr void setX(float x) noexcept; + constexpr void setY(float y) noexcept; + constexpr void setZ(float z) noexcept; + + constexpr float &operator[](int i); + constexpr float operator[](int i) const; + + [[nodiscard]] float length() const noexcept; + [[nodiscard]] constexpr float lengthSquared() const noexcept; + + [[nodiscard]] QVector3D normalized() const noexcept; + void normalize() noexcept; + + constexpr QVector3D &operator+=(QVector3D vector) noexcept; + constexpr QVector3D &operator-=(QVector3D vector) noexcept; + constexpr QVector3D &operator*=(float factor) noexcept; + constexpr QVector3D &operator*=(QVector3D vector) noexcept; + constexpr QVector3D &operator/=(float divisor); + constexpr QVector3D &operator/=(QVector3D vector); + + [[nodiscard]] static constexpr float dotProduct(QVector3D v1, QVector3D v2) noexcept; + [[nodiscard]] static constexpr QVector3D crossProduct(QVector3D v1, QVector3D v2) noexcept; + + [[nodiscard]] static QVector3D normal(QVector3D v1, QVector3D v2) noexcept; + [[nodiscard]] static QVector3D normal(QVector3D v1, QVector3D v2, QVector3D v3) noexcept; + + Q_GUI_EXPORT QVector3D project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const; + Q_GUI_EXPORT QVector3D unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const; + +QT_WARNING_PUSH +QT_WARNING_DISABLE_FLOAT_COMPARE + constexpr friend inline bool operator==(QVector3D v1, QVector3D v2) noexcept + { + return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2]; + } + + constexpr friend inline bool operator!=(QVector3D v1, QVector3D v2) noexcept + { + return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2]; + } +QT_WARNING_POP + float distanceToPoint(QVector3D point) const noexcept; + constexpr float distanceToPlane(QVector3D plane, QVector3D normal) const noexcept; + float distanceToPlane(QVector3D plane1, QVector3D plane2, QVector3D plane3) const noexcept; + float distanceToLine(QVector3D point, QVector3D direction) const noexcept; + + + constexpr friend inline QVector3D operator+(QVector3D v1, QVector3D v2) noexcept + { + return QVector3D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2]); + } + + constexpr friend inline QVector3D operator-(QVector3D v1, QVector3D v2) noexcept + { + return QVector3D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2]); + } + + constexpr friend inline QVector3D operator*(float factor, QVector3D vector) noexcept + { + return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor); + } + + constexpr friend inline QVector3D operator*(QVector3D vector, float factor) noexcept + { + return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor); + } + + constexpr friend inline QVector3D operator*(QVector3D v1, QVector3D v2) noexcept + { + return QVector3D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2]); + } + + constexpr friend inline QVector3D operator-(QVector3D vector) noexcept + { + return QVector3D(-vector.v[0], -vector.v[1], -vector.v[2]); + } + + constexpr friend inline QVector3D operator/(QVector3D vector, float divisor) + { + return QVector3D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor); + } + + constexpr friend inline QVector3D operator/(QVector3D vector, QVector3D divisor) + { + return QVector3D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1], vector.v[2] / divisor.v[2]); + } + + constexpr friend inline bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept; + +#ifndef QT_NO_VECTOR2D + constexpr QVector2D toVector2D() const noexcept; +#endif +#ifndef QT_NO_VECTOR4D + constexpr QVector4D toVector4D() const noexcept; +#endif + + constexpr QPoint toPoint() const noexcept; + constexpr QPointF toPointF() const noexcept; + + Q_GUI_EXPORT operator QVariant() const; + +private: + float v[3]; + + friend class QVector2D; + friend class QVector4D; +#ifndef QT_NO_MATRIX4X4 + friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix); + friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector); +#endif +}; + +Q_DECLARE_TYPEINFO(QVector3D, Q_PRIMITIVE_TYPE); + +#endif // QT_NO_VECTOR3D + + + +/***************************** QVector4D *****************************/ + +#ifndef QT_NO_VECTOR4D + +class QVector4D +{ +public: + constexpr QVector4D() noexcept; + explicit QVector4D(Qt::Initialization) noexcept {} + constexpr QVector4D(float xpos, float ypos, float zpos, float wpos) noexcept; + constexpr explicit QVector4D(QPoint point) noexcept; + constexpr explicit QVector4D(QPointF point) noexcept; +#ifndef QT_NO_VECTOR2D + constexpr QVector4D(QVector2D vector) noexcept; + constexpr QVector4D(QVector2D vector, float zpos, float wpos) noexcept; +#endif +#ifndef QT_NO_VECTOR3D + constexpr QVector4D(QVector3D vector) noexcept; + constexpr QVector4D(QVector3D vector, float wpos) noexcept; +#endif + + constexpr bool isNull() const noexcept; + + constexpr float x() const noexcept; + constexpr float y() const noexcept; + constexpr float z() const noexcept; + constexpr float w() const noexcept; + + constexpr void setX(float x) noexcept; + constexpr void setY(float y) noexcept; + constexpr void setZ(float z) noexcept; + constexpr void setW(float w) noexcept; + + constexpr float &operator[](int i); + constexpr float operator[](int i) const; + + [[nodiscard]] float length() const noexcept; + [[nodiscard]] constexpr float lengthSquared() const noexcept; + + [[nodiscard]] QVector4D normalized() const noexcept; + void normalize() noexcept; + + constexpr QVector4D &operator+=(QVector4D vector) noexcept; + constexpr QVector4D &operator-=(QVector4D vector) noexcept; + constexpr QVector4D &operator*=(float factor) noexcept; + constexpr QVector4D &operator*=(QVector4D vector) noexcept; + constexpr QVector4D &operator/=(float divisor); + constexpr inline QVector4D &operator/=(QVector4D vector); + + [[nodiscard]] static constexpr float dotProduct(QVector4D v1, QVector4D v2) noexcept; + +QT_WARNING_PUSH +QT_WARNING_DISABLE_FLOAT_COMPARE + constexpr friend inline bool operator==(QVector4D v1, QVector4D v2) noexcept + { + 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]; + } + + constexpr friend inline bool operator!=(QVector4D v1, QVector4D v2) noexcept + { + 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]; + } +QT_WARNING_POP + constexpr friend inline QVector4D operator+(QVector4D v1, QVector4D v2) noexcept + { + 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]); + } + + constexpr friend inline QVector4D operator-(QVector4D v1, QVector4D v2) noexcept + { + 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]); + } + + constexpr friend inline QVector4D operator*(float factor, QVector4D vector) noexcept + { + return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor); + } + + constexpr friend inline QVector4D operator*(QVector4D vector, float factor) noexcept + { + return QVector4D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor, vector.v[3] * factor); + } + + constexpr friend inline QVector4D operator*(QVector4D v1, QVector4D v2) noexcept + { + 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]); + } + + constexpr friend inline QVector4D operator-(QVector4D vector) noexcept + { + return QVector4D(-vector.v[0], -vector.v[1], -vector.v[2], -vector.v[3]); + } + + constexpr friend inline QVector4D operator/(QVector4D vector, float divisor) + { + return QVector4D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor, vector.v[3] / divisor); + } + + constexpr friend inline QVector4D operator/(QVector4D vector, QVector4D divisor) + { + return QVector4D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1], vector.v[2] / divisor.v[2], vector.v[3] / divisor.v[3]); + } + + constexpr friend inline bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept; + +#ifndef QT_NO_VECTOR2D + constexpr QVector2D toVector2D() const noexcept; + constexpr QVector2D toVector2DAffine() const noexcept; +#endif +#ifndef QT_NO_VECTOR3D + constexpr QVector3D toVector3D() const noexcept; + constexpr QVector3D toVector3DAffine() const noexcept; +#endif + + constexpr QPoint toPoint() const noexcept; + constexpr QPointF toPointF() const noexcept; + + Q_GUI_EXPORT operator QVariant() const; + +private: + float v[4]; + + friend class QVector2D; + friend class QVector3D; +#ifndef QT_NO_MATRIX4X4 + friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix); + friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector); +#endif +}; + +Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE); + +#endif // QT_NO_VECTOR4D + + + +/***************************** QVector2D *****************************/ + +#ifndef QT_NO_VECTOR2D + +constexpr inline QVector2D::QVector2D() noexcept : v{0.0f, 0.0f} {} + +constexpr inline QVector2D::QVector2D(float xpos, float ypos) noexcept : v{xpos, ypos} {} + +constexpr inline QVector2D::QVector2D(QPoint point) noexcept : v{float(point.x()), float(point.y())} {} + +constexpr inline QVector2D::QVector2D(QPointF point) noexcept : v{float(point.x()), float(point.y())} {} + +#ifndef QT_NO_VECTOR3D +constexpr inline QVector2D::QVector2D(QVector3D vector) noexcept : v{vector[0], vector[1]} {} +#endif +#ifndef QT_NO_VECTOR4D +constexpr inline QVector2D::QVector2D(QVector4D vector) noexcept : v{vector[0], vector[1]} {} +#endif + +constexpr inline bool QVector2D::isNull() const noexcept +{ + return qIsNull(v[0]) && qIsNull(v[1]); +} + +constexpr inline float QVector2D::x() const noexcept { return v[0]; } +constexpr inline float QVector2D::y() const noexcept { return v[1]; } + +constexpr inline void QVector2D::setX(float aX) noexcept { v[0] = aX; } +constexpr inline void QVector2D::setY(float aY) noexcept { v[1] = aY; } + +constexpr inline float &QVector2D::operator[](int i) +{ + Q_ASSERT(uint(i) < 2u); + return v[i]; +} + +constexpr inline float QVector2D::operator[](int i) const +{ + Q_ASSERT(uint(i) < 2u); + return v[i]; +} + +inline float QVector2D::length() const noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]); + return float(std::sqrt(len)); +} + +constexpr inline float QVector2D::lengthSquared() const noexcept +{ + return v[0] * v[0] + v[1] * v[1]; +} + +inline QVector2D QVector2D::normalized() const noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]); + if (qFuzzyIsNull(len - 1.0)) { + return *this; + } else if (!qFuzzyIsNull(len)) { + double sqrtLen = std::sqrt(len); + return QVector2D(float(double(v[0]) / sqrtLen), float(double(v[1]) / sqrtLen)); + } else { + return QVector2D(); + } +} + +inline void QVector2D::normalize() noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]); + if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len)) + return; + + len = std::sqrt(len); + + v[0] = float(double(v[0]) / len); + v[1] = float(double(v[1]) / len); +} + +inline float QVector2D::distanceToPoint(QVector2D point) const noexcept +{ + return (*this - point).length(); +} + +inline float QVector2D::distanceToLine(QVector2D point, QVector2D direction) const noexcept +{ + if (direction.isNull()) + return (*this - point).length(); + QVector2D p = point + dotProduct(*this - point, direction) * direction; + return (*this - p).length(); +} + +constexpr inline QVector2D &QVector2D::operator+=(QVector2D vector) noexcept +{ + v[0] += vector.v[0]; + v[1] += vector.v[1]; + return *this; +} + +constexpr inline QVector2D &QVector2D::operator-=(QVector2D vector) noexcept +{ + v[0] -= vector.v[0]; + v[1] -= vector.v[1]; + return *this; +} + +constexpr inline QVector2D &QVector2D::operator*=(float factor) noexcept +{ + v[0] *= factor; + v[1] *= factor; + return *this; +} + +constexpr inline QVector2D &QVector2D::operator*=(QVector2D vector) noexcept +{ + v[0] *= vector.v[0]; + v[1] *= vector.v[1]; + return *this; +} + +constexpr inline QVector2D &QVector2D::operator/=(float divisor) +{ + v[0] /= divisor; + v[1] /= divisor; + return *this; +} + +constexpr inline QVector2D &QVector2D::operator/=(QVector2D vector) +{ + v[0] /= vector.v[0]; + v[1] /= vector.v[1]; + return *this; +} + +constexpr inline float QVector2D::dotProduct(QVector2D v1, QVector2D v2) noexcept +{ + return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1]; +} + +constexpr inline bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept +{ + return qFuzzyCompare(v1.v[0], v2.v[0]) && qFuzzyCompare(v1.v[1], v2.v[1]); +} + +#ifndef QT_NO_VECTOR3D +constexpr inline QVector3D QVector2D::toVector3D() const noexcept +{ + return QVector3D(v[0], v[1], 0.0f); +} +#endif +#ifndef QT_NO_VECTOR4D +constexpr inline QVector4D QVector2D::toVector4D() const noexcept +{ + return QVector4D(v[0], v[1], 0.0f, 0.0f); +} +#endif + + +constexpr inline QPoint QVector2D::toPoint() const noexcept +{ + return QPoint(qRound(v[0]), qRound(v[1])); +} + +constexpr inline QPointF QVector2D::toPointF() const noexcept +{ + return QPointF(qreal(v[0]), qreal(v[1])); +} + +#ifndef QT_NO_DEBUG_STREAM +Q_GUI_EXPORT QDebug operator<<(QDebug dbg, QVector2D vector); +#endif + +#ifndef QT_NO_DATASTREAM +Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, QVector2D ); +Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &); +#endif + +#endif // QT_NO_VECTOR2D + + + +/***************************** QVector3D *****************************/ + +#ifndef QT_NO_VECTOR3D + +constexpr inline QVector3D::QVector3D() noexcept : v{0.0f, 0.0f, 0.0f} {} + +constexpr inline QVector3D::QVector3D(QPoint point) noexcept : v{float(point.x()), float(point.y()), 0.0f} {} + +constexpr inline QVector3D::QVector3D(QPointF point) noexcept : v{float(point.x()), float(point.y()), 0.0f} {} + +#ifndef QT_NO_VECTOR2D +constexpr inline QVector3D::QVector3D(QVector2D vector) noexcept : v{vector[0], vector[1], 0.0f} {} +constexpr inline QVector3D::QVector3D(QVector2D vector, float zpos) noexcept : v{vector[0], vector[1], zpos} {} +#endif + +#ifndef QT_NO_VECTOR4D +constexpr inline QVector3D::QVector3D(QVector4D vector) noexcept : v{vector[0], vector[1], vector[2]} {} +#endif + +constexpr inline bool QVector3D::isNull() const noexcept +{ + return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]); +} + +constexpr inline float QVector3D::x() const noexcept { return v[0]; } +constexpr inline float QVector3D::y() const noexcept { return v[1]; } +constexpr inline float QVector3D::z() const noexcept { return v[2]; } + +constexpr inline void QVector3D::setX(float aX) noexcept { v[0] = aX; } +constexpr inline void QVector3D::setY(float aY) noexcept { v[1] = aY; } +constexpr inline void QVector3D::setZ(float aZ) noexcept { v[2] = aZ; } + +constexpr inline float &QVector3D::operator[](int i) +{ + Q_ASSERT(uint(i) < 3u); + return v[i]; +} + +constexpr inline float QVector3D::operator[](int i) const +{ + Q_ASSERT(uint(i) < 3u); + return v[i]; +} + +inline float QVector3D::length() const noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]) + + double(v[2]) * double(v[2]); + return float(std::sqrt(len)); +} + +inline QVector3D QVector3D::normalized() const noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]) + + double(v[2]) * double(v[2]); + if (qFuzzyIsNull(len - 1.0)) { + return *this; + } else if (!qFuzzyIsNull(len)) { + double sqrtLen = std::sqrt(len); + return QVector3D(float(double(v[0]) / sqrtLen), + float(double(v[1]) / sqrtLen), + float(double(v[2]) / sqrtLen)); + } else { + return QVector3D(); + } +} + +inline void QVector3D::normalize() noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]) + + double(v[2]) * double(v[2]); + if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len)) + return; + + len = std::sqrt(len); + + v[0] = float(double(v[0]) / len); + v[1] = float(double(v[1]) / len); + v[2] = float(double(v[2]) / len); +} + +constexpr inline float QVector3D::lengthSquared() const noexcept +{ + return v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; +} + +constexpr inline QVector3D &QVector3D::operator+=(QVector3D vector) noexcept +{ + v[0] += vector.v[0]; + v[1] += vector.v[1]; + v[2] += vector.v[2]; + return *this; +} + +constexpr inline QVector3D &QVector3D::operator-=(QVector3D vector) noexcept +{ + v[0] -= vector.v[0]; + v[1] -= vector.v[1]; + v[2] -= vector.v[2]; + return *this; +} + +constexpr inline QVector3D &QVector3D::operator*=(float factor) noexcept +{ + v[0] *= factor; + v[1] *= factor; + v[2] *= factor; + return *this; +} + +constexpr inline QVector3D &QVector3D::operator*=(QVector3D vector) noexcept +{ + v[0] *= vector.v[0]; + v[1] *= vector.v[1]; + v[2] *= vector.v[2]; + return *this; +} + +constexpr inline QVector3D &QVector3D::operator/=(float divisor) +{ + v[0] /= divisor; + v[1] /= divisor; + v[2] /= divisor; + return *this; +} + +constexpr inline QVector3D &QVector3D::operator/=(QVector3D vector) +{ + v[0] /= vector.v[0]; + v[1] /= vector.v[1]; + v[2] /= vector.v[2]; + return *this; +} + +constexpr inline float QVector3D::dotProduct(QVector3D v1, QVector3D v2) noexcept +{ + return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1] + v1.v[2] * v2.v[2]; +} + +constexpr inline QVector3D QVector3D::crossProduct(QVector3D v1, QVector3D v2) noexcept +{ + return QVector3D(v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1], + v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2], + v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0]); +} + +inline QVector3D QVector3D::normal(QVector3D v1, QVector3D v2) noexcept +{ + return crossProduct(v1, v2).normalized(); +} + +inline QVector3D QVector3D::normal(QVector3D v1, QVector3D v2, QVector3D v3) noexcept +{ + return crossProduct((v2 - v1), (v3 - v1)).normalized(); +} + +inline float QVector3D::distanceToPoint(QVector3D point) const noexcept +{ + return (*this - point).length(); +} + +constexpr inline float QVector3D::distanceToPlane(QVector3D plane, QVector3D normal) const noexcept +{ + return dotProduct(*this - plane, normal); +} + +inline float QVector3D::distanceToPlane(QVector3D plane1, QVector3D plane2, QVector3D plane3) const noexcept +{ + QVector3D n = normal(plane2 - plane1, plane3 - plane1); + return dotProduct(*this - plane1, n); +} + +inline float QVector3D::distanceToLine(QVector3D point, QVector3D direction) const noexcept +{ + if (direction.isNull()) + return (*this - point).length(); + QVector3D p = point + dotProduct(*this - point, direction) * direction; + return (*this - p).length(); +} + +constexpr inline bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept +{ + return qFuzzyCompare(v1.v[0], v2.v[0]) && + qFuzzyCompare(v1.v[1], v2.v[1]) && + qFuzzyCompare(v1.v[2], v2.v[2]); +} + +#ifndef QT_NO_VECTOR2D +constexpr inline QVector2D QVector3D::toVector2D() const noexcept +{ + return QVector2D(v[0], v[1]); +} +#endif +#ifndef QT_NO_VECTOR4D +constexpr inline QVector4D QVector3D::toVector4D() const noexcept +{ + return QVector4D(v[0], v[1], v[2], 0.0f); +} +#endif + +constexpr inline QPoint QVector3D::toPoint() const noexcept +{ + return QPoint(qRound(v[0]), qRound(v[1])); +} + +constexpr inline QPointF QVector3D::toPointF() const noexcept +{ + return QPointF(qreal(v[0]), qreal(v[1])); +} + +#ifndef QT_NO_DEBUG_STREAM +Q_GUI_EXPORT QDebug operator<<(QDebug dbg, QVector3D vector); +#endif + +#ifndef QT_NO_DATASTREAM +Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, QVector3D ); +Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &); +#endif + +#endif // QT_NO_VECTOR3D + + + +/***************************** QVector4D *****************************/ + +#ifndef QT_NO_VECTOR4D + +constexpr inline QVector4D::QVector4D() noexcept : v{0.0f, 0.0f, 0.0f, 0.0f} {} + +constexpr inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) noexcept : v{xpos, ypos, zpos, wpos} {} + +constexpr inline QVector4D::QVector4D(QPoint point) noexcept : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {} + +constexpr inline QVector4D::QVector4D(QPointF point) noexcept : v{float(point.x()), float(point.y()), 0.0f, 0.0f} {} + +#ifndef QT_NO_VECTOR2D +constexpr QVector4D::QVector4D(QVector2D vector) noexcept : v{vector[0], vector[1], 0.0f, 0.0f} {} +constexpr QVector4D::QVector4D(QVector2D vector, float zpos, float wpos) noexcept : v{vector[0], vector[1], zpos, wpos} {} +#endif +#ifndef QT_NO_VECTOR3D +constexpr QVector4D::QVector4D(QVector3D vector) noexcept : v{vector[0], vector[1], vector[2], 0.0f} {} +constexpr QVector4D::QVector4D(QVector3D vector, float wpos) noexcept : v{vector[0], vector[1], vector[2], wpos} {} +#endif + +constexpr inline bool QVector4D::isNull() const noexcept +{ + return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]) && qIsNull(v[3]); +} + +constexpr inline float QVector4D::x() const noexcept { return v[0]; } +constexpr inline float QVector4D::y() const noexcept { return v[1]; } +constexpr inline float QVector4D::z() const noexcept { return v[2]; } +constexpr inline float QVector4D::w() const noexcept { return v[3]; } + +constexpr inline void QVector4D::setX(float aX) noexcept { v[0] = aX; } +constexpr inline void QVector4D::setY(float aY) noexcept { v[1] = aY; } +constexpr inline void QVector4D::setZ(float aZ) noexcept { v[2] = aZ; } +constexpr inline void QVector4D::setW(float aW) noexcept { v[3] = aW; } + +constexpr inline float &QVector4D::operator[](int i) +{ + Q_ASSERT(uint(i) < 4u); + return v[i]; +} + +constexpr inline float QVector4D::operator[](int i) const +{ + Q_ASSERT(uint(i) < 4u); + return v[i]; +} + +inline float QVector4D::length() const noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]) + + double(v[2]) * double(v[2]) + + double(v[3]) * double(v[3]); + return float(std::sqrt(len)); +} + +constexpr inline float QVector4D::lengthSquared() const noexcept +{ + return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]; +} + +inline QVector4D QVector4D::normalized() const noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]) + + double(v[2]) * double(v[2]) + + double(v[3]) * double(v[3]); + if (qFuzzyIsNull(len - 1.0)) { + return *this; + } else if (!qFuzzyIsNull(len)) { + double sqrtLen = std::sqrt(len); + return QVector4D(float(double(v[0]) / sqrtLen), + float(double(v[1]) / sqrtLen), + float(double(v[2]) / sqrtLen), + float(double(v[3]) / sqrtLen)); + } else { + return QVector4D(); + } +} + +inline void QVector4D::normalize() noexcept +{ + // Need some extra precision if the length is very small. + double len = double(v[0]) * double(v[0]) + + double(v[1]) * double(v[1]) + + double(v[2]) * double(v[2]) + + double(v[3]) * double(v[3]); + if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len)) + return; + + len = std::sqrt(len); + + v[0] = float(double(v[0]) / len); + v[1] = float(double(v[1]) / len); + v[2] = float(double(v[2]) / len); + v[3] = float(double(v[3]) / len); +} + +constexpr inline QVector4D &QVector4D::operator+=(QVector4D vector) noexcept +{ + v[0] += vector.v[0]; + v[1] += vector.v[1]; + v[2] += vector.v[2]; + v[3] += vector.v[3]; + return *this; +} + +constexpr inline QVector4D &QVector4D::operator-=(QVector4D vector) noexcept +{ + v[0] -= vector.v[0]; + v[1] -= vector.v[1]; + v[2] -= vector.v[2]; + v[3] -= vector.v[3]; + return *this; +} + +constexpr inline QVector4D &QVector4D::operator*=(float factor) noexcept +{ + v[0] *= factor; + v[1] *= factor; + v[2] *= factor; + v[3] *= factor; + return *this; +} + +constexpr inline QVector4D &QVector4D::operator*=(QVector4D vector) noexcept +{ + v[0] *= vector.v[0]; + v[1] *= vector.v[1]; + v[2] *= vector.v[2]; + v[3] *= vector.v[3]; + return *this; +} + +constexpr inline QVector4D &QVector4D::operator/=(float divisor) +{ + v[0] /= divisor; + v[1] /= divisor; + v[2] /= divisor; + v[3] /= divisor; + return *this; +} + +constexpr inline QVector4D &QVector4D::operator/=(QVector4D vector) +{ + v[0] /= vector.v[0]; + v[1] /= vector.v[1]; + v[2] /= vector.v[2]; + v[3] /= vector.v[3]; + return *this; +} + +constexpr float QVector4D::dotProduct(QVector4D v1, QVector4D v2) noexcept +{ + 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]; +} + +constexpr inline bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept +{ + return qFuzzyCompare(v1.v[0], v2.v[0]) && + qFuzzyCompare(v1.v[1], v2.v[1]) && + qFuzzyCompare(v1.v[2], v2.v[2]) && + qFuzzyCompare(v1.v[3], v2.v[3]); +} + +#ifndef QT_NO_VECTOR2D + +constexpr inline QVector2D QVector4D::toVector2D() const noexcept +{ + return QVector2D(v[0], v[1]); +} + +constexpr inline QVector2D QVector4D::toVector2DAffine() const noexcept +{ + if (qIsNull(v[3])) + return QVector2D(); + return QVector2D(v[0] / v[3], v[1] / v[3]); +} + +#endif // QT_NO_VECTOR2D + +#ifndef QT_NO_VECTOR3D + +constexpr inline QVector3D QVector4D::toVector3D() const noexcept +{ + return QVector3D(v[0], v[1], v[2]); +} + +constexpr QVector3D QVector4D::toVector3DAffine() const noexcept +{ + if (qIsNull(v[3])) + return QVector3D(); + return QVector3D(v[0] / v[3], v[1] / v[3], v[2] / v[3]); +} + +#endif // QT_NO_VECTOR3D + +constexpr inline QPoint QVector4D::toPoint() const noexcept +{ + return QPoint(qRound(v[0]), qRound(v[1])); +} + +constexpr inline QPointF QVector4D::toPointF() const noexcept +{ + return QPointF(qreal(v[0]), qreal(v[1])); +} + +#ifndef QT_NO_DEBUG_STREAM +Q_GUI_EXPORT QDebug operator<<(QDebug dbg, QVector4D vector); +#endif + +#ifndef QT_NO_DATASTREAM +Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, QVector4D ); +Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &); +#endif + +#endif // QT_NO_VECTOR4D + + +QT_END_NAMESPACE + +#endif // QVECTORND_H |