summaryrefslogtreecommitdiffstats
path: root/src/gui/math3d
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/math3d')
-rw-r--r--src/gui/math3d/qgenericmatrix.cpp49
-rw-r--r--src/gui/math3d/qgenericmatrix.h60
-rw-r--r--src/gui/math3d/qmatrix4x4.cpp132
-rw-r--r--src/gui/math3d/qmatrix4x4.h278
-rw-r--r--src/gui/math3d/qquaternion.cpp253
-rw-r--r--src/gui/math3d/qquaternion.h239
-rw-r--r--src/gui/math3d/qvector2d.h42
-rw-r--r--src/gui/math3d/qvector3d.h42
-rw-r--r--src/gui/math3d/qvector4d.h42
-rw-r--r--src/gui/math3d/qvectornd.cpp261
-rw-r--r--src/gui/math3d/qvectornd.h254
11 files changed, 707 insertions, 945 deletions
diff --git a/src/gui/math3d/qgenericmatrix.cpp b/src/gui/math3d/qgenericmatrix.cpp
index cf79ed9786..7ffb53a88b 100644
--- a/src/gui/math3d/qgenericmatrix.cpp
+++ b/src/gui/math3d/qgenericmatrix.cpp
@@ -1,46 +1,19 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qgenericmatrix.h"
QT_BEGIN_NAMESPACE
+QT_IMPL_METATYPE_EXTERN(QMatrix2x2)
+QT_IMPL_METATYPE_EXTERN(QMatrix2x3)
+QT_IMPL_METATYPE_EXTERN(QMatrix2x4)
+QT_IMPL_METATYPE_EXTERN(QMatrix3x2)
+QT_IMPL_METATYPE_EXTERN(QMatrix3x3)
+QT_IMPL_METATYPE_EXTERN(QMatrix3x4)
+QT_IMPL_METATYPE_EXTERN(QMatrix4x2)
+QT_IMPL_METATYPE_EXTERN(QMatrix4x3)
+
/*!
\class QGenericMatrix
\brief The QGenericMatrix class is a template class that represents a NxM transformation matrix with N columns and M rows.
diff --git a/src/gui/math3d/qgenericmatrix.h b/src/gui/math3d/qgenericmatrix.h
index 9d8869ff3a..1e20df5a6b 100644
--- a/src/gui/math3d/qgenericmatrix.h
+++ b/src/gui/math3d/qgenericmatrix.h
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QGENERICMATRIX_H
#define QGENERICMATRIX_H
@@ -79,7 +43,6 @@ public:
const T *data() const { return *m; }
const T *constData() const { return *m; }
-#if !defined(Q_NO_TEMPLATE_FRIENDS)
template<int NN, int MM, typename TT>
friend QGenericMatrix<NN, MM, TT> operator+(const QGenericMatrix<NN, MM, TT>& m1, const QGenericMatrix<NN, MM, TT>& m2);
template<int NN, int MM, typename TT>
@@ -96,13 +59,10 @@ public:
friend QGenericMatrix<NN, MM, TT> operator/(const QGenericMatrix<NN, MM, TT>& matrix, TT divisor);
private:
-#endif
T m[N][M]; // Column-major order to match OpenGL.
-#if !defined(Q_NO_TEMPLATE_FRIENDS)
template <int NN, int MM, typename TT>
friend class QGenericMatrix;
-#endif
};
template <int N, int M, typename T>
class QTypeInfo<QGenericMatrix<N, M, T> >
@@ -385,13 +345,13 @@ QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
QT_END_NAMESPACE
-Q_DECLARE_METATYPE(QMatrix2x2)
-Q_DECLARE_METATYPE(QMatrix2x3)
-Q_DECLARE_METATYPE(QMatrix2x4)
-Q_DECLARE_METATYPE(QMatrix3x2)
-Q_DECLARE_METATYPE(QMatrix3x3)
-Q_DECLARE_METATYPE(QMatrix3x4)
-Q_DECLARE_METATYPE(QMatrix4x2)
-Q_DECLARE_METATYPE(QMatrix4x3)
+QT_DECL_METATYPE_EXTERN(QMatrix2x2, Q_GUI_EXPORT)
+QT_DECL_METATYPE_EXTERN(QMatrix2x3, Q_GUI_EXPORT)
+QT_DECL_METATYPE_EXTERN(QMatrix2x4, Q_GUI_EXPORT)
+QT_DECL_METATYPE_EXTERN(QMatrix3x2, Q_GUI_EXPORT)
+QT_DECL_METATYPE_EXTERN(QMatrix3x3, Q_GUI_EXPORT)
+QT_DECL_METATYPE_EXTERN(QMatrix3x4, Q_GUI_EXPORT)
+QT_DECL_METATYPE_EXTERN(QMatrix4x2, Q_GUI_EXPORT)
+QT_DECL_METATYPE_EXTERN(QMatrix4x3, Q_GUI_EXPORT)
#endif
diff --git a/src/gui/math3d/qmatrix4x4.cpp b/src/gui/math3d/qmatrix4x4.cpp
index 34f8a7d892..d04a502519 100644
--- a/src/gui/math3d/qmatrix4x4.cpp
+++ b/src/gui/math3d/qmatrix4x4.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qmatrix4x4.h"
#include <QtCore/qmath.h>
@@ -136,7 +100,7 @@ QMatrix4x4::QMatrix4x4(const float *values)
*/
/*!
- \fn QGenericMatrix<N, M, float> QMatrix4x4::toGenericMatrix() const
+ \fn template <int N, int M> QGenericMatrix<N, M, float> QMatrix4x4::toGenericMatrix() const
Constructs a NxM generic matrix from the left-most N columns and
top-most M rows of this 4x4 matrix. If N or M is greater than 4,
@@ -627,24 +591,33 @@ QMatrix4x4& QMatrix4x4::operator/=(float divisor)
#ifndef QT_NO_VECTOR3D
+#if QT_DEPRECATED_SINCE(6, 1)
+
/*!
\fn QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix)
\relates QMatrix4x4
+ \deprecated [6.1] Convert the QVector3D to a QVector4D with 1.0 as the w coordinate, then multiply.
+
Returns the result of transforming \a vector according to \a matrix,
- with the matrix applied post-vector.
+ with the matrix applied post-vector. The vector is transformed as a point.
*/
/*!
\fn QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector)
\relates QMatrix4x4
+ \deprecated [6.1] Use QMatrix4x4::map() instead.
+
Returns the result of transforming \a vector according to \a matrix,
- with the matrix applied pre-vector.
+ with the matrix applied pre-vector. The vector is transformed as a
+ projective point.
*/
#endif
+#endif
+
#ifndef QT_NO_VECTOR4D
/*!
@@ -681,10 +654,14 @@ QMatrix4x4& QMatrix4x4::operator/=(float divisor)
with the matrix applied post-point.
*/
+#if QT_DEPRECATED_SINCE(6, 1)
+
/*!
\fn QPoint operator*(const QMatrix4x4& matrix, const QPoint& point)
\relates QMatrix4x4
+ \deprecated [6.1] Use QMatrix4x4::map() instead.
+
Returns the result of transforming \a point according to \a matrix,
with the matrix applied pre-point.
*/
@@ -693,10 +670,14 @@ QMatrix4x4& QMatrix4x4::operator/=(float divisor)
\fn QPointF operator*(const QMatrix4x4& matrix, const QPointF& point)
\relates QMatrix4x4
+ \deprecated [6.1] Use QMatrix4x4::map() instead.
+
Returns the result of transforming \a point according to \a matrix,
with the matrix applied pre-point.
*/
+#endif
+
/*!
\fn QMatrix4x4 operator-(const QMatrix4x4& matrix)
\overload
@@ -1183,12 +1164,15 @@ void QMatrix4x4::rotate(float angle, float x, float y, float z)
/*!
\internal
*/
-void QMatrix4x4::projectedRotate(float angle, float x, float y, float z)
+void QMatrix4x4::projectedRotate(float angle, float x, float y, float z, float distanceToPlane)
{
// Used by QGraphicsRotation::applyTo() to perform a rotation
// and projection back to 2D in a single step.
+ if (qIsNull(distanceToPlane))
+ return rotate(angle, x, y, z);
if (angle == 0.0f)
return;
+
float c, s;
if (angle == 90.0f || angle == -270.0f) {
s = 1.0f;
@@ -1204,6 +1188,8 @@ void QMatrix4x4::projectedRotate(float angle, float x, float y, float z)
c = std::cos(a);
s = std::sin(a);
}
+
+ const qreal d = 1.0 / distanceToPlane;
if (x == 0.0f) {
if (y == 0.0f) {
if (z != 0.0f) {
@@ -1227,10 +1213,11 @@ void QMatrix4x4::projectedRotate(float angle, float x, float y, float z)
// Rotate around the Y axis.
if (y < 0)
s = -s;
- m[0][0] = m[0][0] * c + m[3][0] * s * inv_dist_to_plane;
- m[0][1] = m[0][1] * c + m[3][1] * s * inv_dist_to_plane;
- m[0][2] = m[0][2] * c + m[3][2] * s * inv_dist_to_plane;
- m[0][3] = m[0][3] * c + m[3][3] * s * inv_dist_to_plane;
+ s *= d;
+ m[0][0] = m[0][0] * c + m[3][0] * s;
+ m[0][1] = m[0][1] * c + m[3][1] * s;
+ m[0][2] = m[0][2] * c + m[3][2] * s;
+ m[0][3] = m[0][3] * c + m[3][3] * s;
flagBits = General;
return;
}
@@ -1238,10 +1225,11 @@ void QMatrix4x4::projectedRotate(float angle, float x, float y, float z)
// Rotate around the X axis.
if (x < 0)
s = -s;
- m[1][0] = m[1][0] * c - m[3][0] * s * inv_dist_to_plane;
- m[1][1] = m[1][1] * c - m[3][1] * s * inv_dist_to_plane;
- m[1][2] = m[1][2] * c - m[3][2] * s * inv_dist_to_plane;
- m[1][3] = m[1][3] * c - m[3][3] * s * inv_dist_to_plane;
+ s *= d;
+ m[1][0] = m[1][0] * c - m[3][0] * s;
+ m[1][1] = m[1][1] * c - m[3][1] * s;
+ m[1][2] = m[1][2] * c - m[3][2] * s;
+ m[1][3] = m[1][3] * c - m[3][3] * s;
flagBits = General;
return;
}
@@ -1268,14 +1256,24 @@ void QMatrix4x4::projectedRotate(float angle, float x, float y, float z)
rot.m[1][2] = 0.0f;
rot.m[2][2] = 1.0f;
rot.m[3][2] = 0.0f;
- rot.m[0][3] = (x * z * ic - y * s) * -inv_dist_to_plane;
- rot.m[1][3] = (y * z * ic + x * s) * -inv_dist_to_plane;
+ rot.m[0][3] = (x * z * ic - y * s) * -d;
+ rot.m[1][3] = (y * z * ic + x * s) * -d;
rot.m[2][3] = 0.0f;
rot.m[3][3] = 1.0f;
rot.flagBits = General;
*this *= rot;
}
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
+/*!
+ \internal
+*/
+void QMatrix4x4::projectedRotate(float angle, float x, float y, float z)
+{
+ projectedRotate(angle, x, y, z, 1024.0);
+}
+#endif
+
/*!
\fn int QMatrix4x4::flags() const
\internal
@@ -1600,7 +1598,7 @@ void QMatrix4x4::viewport(float left, float bottom, float width, float height, f
\deprecated
Flips between right-handed and left-handed coordinate systems
- by multiplying the y and z co-ordinates by -1. This is normally
+ by multiplying the y and z coordinates by -1. This is normally
used to create a left-handed orthographic view without scaling
the viewport as ortho() does.
@@ -1645,7 +1643,7 @@ void QMatrix4x4::copyDataTo(float *values) const
The returned QTransform is formed by simply dropping the
third row and third column of the QMatrix4x4. This is suitable
- for implementing orthographic projections where the z co-ordinate
+ for implementing orthographic projections where the z coordinate
should be dropped rather than projected.
*/
QTransform QMatrix4x4::toTransform() const
@@ -1660,14 +1658,14 @@ QTransform QMatrix4x4::toTransform() const
corresponds to this matrix.
If \a distanceToPlane is non-zero, it indicates a projection
- factor to use to adjust for the z co-ordinate. The value of
+ factor to use to adjust for the z coordinate. The value of
1024 corresponds to the projection factor used
by QTransform::rotate() for the x and y axes.
If \a distanceToPlane is zero, then the returned QTransform
is formed by simply dropping the third row and third column
of the QMatrix4x4. This is suitable for implementing
- orthographic projections where the z co-ordinate should
+ orthographic projections where the z coordinate should
be dropped rather than projected.
*/
QTransform QMatrix4x4::toTransform(float distanceToPlane) const
@@ -1701,6 +1699,7 @@ QTransform QMatrix4x4::toTransform(float distanceToPlane) const
\fn QPoint QMatrix4x4::map(const QPoint& point) const
Maps \a point by multiplying this matrix by \a point.
+ The matrix is applied pre-point.
\sa mapRect()
*/
@@ -1708,7 +1707,8 @@ QTransform QMatrix4x4::toTransform(float distanceToPlane) const
/*!
\fn QPointF QMatrix4x4::map(const QPointF& point) const
- Maps \a point by multiplying this matrix by \a point.
+ Maps \a point by post-multiplying this matrix by \a point.
+ The matrix is applied pre-point.
\sa mapRect()
*/
@@ -1718,7 +1718,12 @@ QTransform QMatrix4x4::toTransform(float distanceToPlane) const
/*!
\fn QVector3D QMatrix4x4::map(const QVector3D& point) const
- Maps \a point by multiplying this matrix by \a point.
+ Maps \a point by multiplying this matrix by \a point extended to a 4D
+ vector by assuming 1.0 for the w coordinate. The matrix is applied
+ pre-point.
+
+ \note This function is not the same as mapVector(). For points, always use
+ map(). mapVector() is suitable for vectors (directions) only.
\sa mapRect(), mapVector()
*/
@@ -1728,7 +1733,7 @@ QTransform QMatrix4x4::toTransform(float distanceToPlane) const
Maps \a vector by multiplying the top 3x3 portion of this matrix
by \a vector. The translation and projection components of
- this matrix are ignored.
+ this matrix are ignored. The matrix is applied pre-vector.
\sa map()
*/
@@ -1741,6 +1746,7 @@ QTransform QMatrix4x4::toTransform(float distanceToPlane) const
\fn QVector4D QMatrix4x4::map(const QVector4D& point) const;
Maps \a point by multiplying this matrix by \a point.
+ The matrix is applied pre-point.
\sa mapRect()
*/
@@ -1902,15 +1908,15 @@ QMatrix4x4 QMatrix4x4::orthonormalInverse() const
Normally the QMatrix4x4 class keeps track of this special type internally
as operations are performed. However, if the matrix is modified
- directly with \l {QMatrix4x4::}{operator()()} or data(), then
- QMatrix4x4 will lose track of the special type and will revert to the
- safest but least efficient operations thereafter.
+ directly with operator()(int, int) or data(), then QMatrix4x4 will
+ lose track of the special type and will revert to the safest but least
+ efficient operations thereafter.
By calling optimize() after directly modifying the matrix,
the programmer can force QMatrix4x4 to recover the special type if
the elements appear to conform to one of the known optimized types.
- \sa {QMatrix4x4::}{operator()()}, data(), translate()
+ \sa operator()(int, int), data(), translate()
*/
void QMatrix4x4::optimize()
{
diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h
index 9ac02693df..f8e5bc4314 100644
--- a/src/gui/math3d/qmatrix4x4.h
+++ b/src/gui/math3d/qmatrix4x4.h
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QMATRIX4X4_H
#define QMATRIX4X4_H
@@ -47,6 +11,8 @@
#include <QtGui/qgenericmatrix.h>
#include <QtCore/qrect.h>
+class tst_QMatrixNxN;
+
QT_BEGIN_NAMESPACE
@@ -107,9 +73,11 @@ public:
friend QMatrix4x4 operator-(const QMatrix4x4& m1, const QMatrix4x4& m2);
friend QMatrix4x4 operator*(const QMatrix4x4& m1, const QMatrix4x4& m2);
#ifndef QT_NO_VECTOR3D
+#if QT_DEPRECATED_SINCE(6, 1)
friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
#endif
+#endif
#ifndef QT_NO_VECTOR4D
friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
@@ -117,8 +85,10 @@ public:
friend QPoint operator*(const QPoint& point, const QMatrix4x4& matrix);
friend QPointF operator*(const QPointF& point, const QMatrix4x4& matrix);
friend QMatrix4x4 operator-(const QMatrix4x4& matrix);
+#if QT_DEPRECATED_SINCE(6, 1)
friend QPoint operator*(const QMatrix4x4& matrix, const QPoint& point);
friend QPointF operator*(const QMatrix4x4& matrix, const QPointF& point);
+#endif
friend QMatrix4x4 operator*(float factor, const QMatrix4x4& matrix);
friend QMatrix4x4 operator*(const QMatrix4x4& matrix, float factor);
friend Q_GUI_EXPORT QMatrix4x4 operator/(const QMatrix4x4& matrix, float divisor);
@@ -157,14 +127,14 @@ public:
QTransform toTransform() const;
QTransform toTransform(float distanceToPlane) const;
- QPoint map(const QPoint& point) const;
- QPointF map(const QPointF& point) const;
+ inline QPoint map(const QPoint& point) const;
+ inline QPointF map(const QPointF& point) const;
#ifndef QT_NO_VECTOR3D
- QVector3D map(const QVector3D& point) const;
- QVector3D mapVector(const QVector3D& vector) const;
+ inline QVector3D map(const QVector3D& point) const;
+ inline QVector3D mapVector(const QVector3D& vector) const;
#endif
#ifndef QT_NO_VECTOR4D
- QVector4D map(const QVector4D& point) const;
+ inline QVector4D map(const QVector4D& point) const;
#endif
QRect mapRect(const QRect& rect) const;
QRectF mapRect(const QRectF& rect) const;
@@ -184,7 +154,13 @@ public:
friend Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QMatrix4x4 &m);
#endif
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
+ void projectedRotate(float angle, float x, float y, float z, float distanceToPlane);
+ // ### Qt7: Remove
void projectedRotate(float angle, float x, float y, float z);
+#else
+ void projectedRotate(float angle, float x, float y, float z, float distanceToPlane = 1024.0);
+#endif
// When matrices are multiplied, the flag bits are or-ed together.
// Note that the ordering of the bit values matters. (ident < t < s < r2d < r < p)
@@ -206,6 +182,8 @@ private:
Flags flagBits;
QMatrix4x4 orthonormalInverse() const;
+
+ friend class ::tst_QMatrixNxN; // for access to flagBits
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QMatrix4x4::Flags)
@@ -426,7 +404,7 @@ inline QMatrix4x4& QMatrix4x4::operator*=(const QMatrix4x4& o)
const QMatrix4x4 other = o; // prevent aliasing when &o == this ### Qt 6: take o by value
flagBits |= other.flagBits;
- if (flagBits < Rotation2D) {
+ if (flagBits.toInt() < Rotation2D) {
m[3][0] += m[0][0] * other.m[3][0];
m[3][1] += m[1][1] * other.m[3][1];
m[3][2] += m[2][2] * other.m[3][2];
@@ -629,7 +607,7 @@ inline QMatrix4x4 operator-(const QMatrix4x4& m1, const QMatrix4x4& m2)
inline QMatrix4x4 operator*(const QMatrix4x4& m1, const QMatrix4x4& m2)
{
QMatrix4x4::Flags flagBits = m1.flagBits | m2.flagBits;
- if (flagBits < QMatrix4x4::Rotation2D) {
+ if (flagBits.toInt() < QMatrix4x4::Rotation2D) {
QMatrix4x4 m = m1;
m.m[3][0] += m.m[0][0] * m2.m[3][0];
m.m[3][1] += m.m[1][1] * m2.m[3][1];
@@ -716,6 +694,9 @@ inline QMatrix4x4 operator*(const QMatrix4x4& m1, const QMatrix4x4& m2)
#ifndef QT_NO_VECTOR3D
+#if QT_DEPRECATED_SINCE(6, 1)
+
+QT_DEPRECATED_VERSION_X_6_1("Extend the QVector3D to a QVector4D with 1.0 as the w coordinate before multiplying")
inline QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix)
{
float x, y, z, w;
@@ -741,47 +722,16 @@ inline QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix)
return QVector3D(x / w, y / w, z / w);
}
+QT_DEPRECATED_VERSION_X_6_1("Use matrix.map(vector) instead")
inline QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector)
{
- float x, y, z, w;
- if (matrix.flagBits == QMatrix4x4::Identity) {
- return vector;
- } else if (matrix.flagBits < QMatrix4x4::Rotation2D) {
- // Translation | Scale
- return QVector3D(vector.x() * matrix.m[0][0] + matrix.m[3][0],
- vector.y() * matrix.m[1][1] + matrix.m[3][1],
- vector.z() * matrix.m[2][2] + matrix.m[3][2]);
- } else if (matrix.flagBits < QMatrix4x4::Rotation) {
- // Translation | Scale | Rotation2D
- return QVector3D(vector.x() * matrix.m[0][0] + vector.y() * matrix.m[1][0] + matrix.m[3][0],
- vector.x() * matrix.m[0][1] + vector.y() * matrix.m[1][1] + matrix.m[3][1],
- vector.z() * matrix.m[2][2] + matrix.m[3][2]);
- } else {
- x = vector.x() * matrix.m[0][0] +
- vector.y() * matrix.m[1][0] +
- vector.z() * matrix.m[2][0] +
- matrix.m[3][0];
- y = vector.x() * matrix.m[0][1] +
- vector.y() * matrix.m[1][1] +
- vector.z() * matrix.m[2][1] +
- matrix.m[3][1];
- z = vector.x() * matrix.m[0][2] +
- vector.y() * matrix.m[1][2] +
- vector.z() * matrix.m[2][2] +
- matrix.m[3][2];
- w = vector.x() * matrix.m[0][3] +
- vector.y() * matrix.m[1][3] +
- vector.z() * matrix.m[2][3] +
- matrix.m[3][3];
- if (w == 1.0f)
- return QVector3D(x, y, z);
- else
- return QVector3D(x / w, y / w, z / w);
- }
+ return matrix.map(vector);
}
#endif
+#endif
+
#ifndef QT_NO_VECTOR4D
inline QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix)
@@ -873,73 +823,22 @@ inline QPointF operator*(const QPointF& point, const QMatrix4x4& matrix)
}
}
+#if QT_DEPRECATED_SINCE(6, 1)
+
+QT_DEPRECATED_VERSION_X_6_1("Use matrix.map(point) instead")
inline QPoint operator*(const QMatrix4x4& matrix, const QPoint& point)
{
- float xin, yin;
- float x, y, w;
- xin = point.x();
- yin = point.y();
- if (matrix.flagBits == QMatrix4x4::Identity) {
- return point;
- } else if (matrix.flagBits < QMatrix4x4::Rotation2D) {
- // Translation | Scale
- return QPoint(qRound(xin * matrix.m[0][0] + matrix.m[3][0]),
- qRound(yin * matrix.m[1][1] + matrix.m[3][1]));
- } else if (matrix.flagBits < QMatrix4x4::Perspective) {
- return QPoint(qRound(xin * matrix.m[0][0] + yin * matrix.m[1][0] + matrix.m[3][0]),
- qRound(xin * matrix.m[0][1] + yin * matrix.m[1][1] + matrix.m[3][1]));
- } else {
- x = xin * matrix.m[0][0] +
- yin * matrix.m[1][0] +
- matrix.m[3][0];
- y = xin * matrix.m[0][1] +
- yin * matrix.m[1][1] +
- matrix.m[3][1];
- w = xin * matrix.m[0][3] +
- yin * matrix.m[1][3] +
- matrix.m[3][3];
- if (w == 1.0f)
- return QPoint(qRound(x), qRound(y));
- else
- return QPoint(qRound(x / w), qRound(y / w));
- }
+ return matrix.map(point);
}
+QT_DEPRECATED_VERSION_X_6_1("Use matrix.map(point) instead")
inline QPointF operator*(const QMatrix4x4& matrix, const QPointF& point)
{
- qreal xin, yin;
- qreal x, y, w;
- xin = point.x();
- yin = point.y();
- if (matrix.flagBits == QMatrix4x4::Identity) {
- return point;
- } else if (matrix.flagBits < QMatrix4x4::Rotation2D) {
- // Translation | Scale
- return QPointF(xin * qreal(matrix.m[0][0]) + qreal(matrix.m[3][0]),
- yin * qreal(matrix.m[1][1]) + qreal(matrix.m[3][1]));
- } else if (matrix.flagBits < QMatrix4x4::Perspective) {
- return QPointF(xin * qreal(matrix.m[0][0]) + yin * qreal(matrix.m[1][0]) +
- qreal(matrix.m[3][0]),
- xin * qreal(matrix.m[0][1]) + yin * qreal(matrix.m[1][1]) +
- qreal(matrix.m[3][1]));
- } else {
- x = xin * qreal(matrix.m[0][0]) +
- yin * qreal(matrix.m[1][0]) +
- qreal(matrix.m[3][0]);
- y = xin * qreal(matrix.m[0][1]) +
- yin * qreal(matrix.m[1][1]) +
- qreal(matrix.m[3][1]);
- w = xin * qreal(matrix.m[0][3]) +
- yin * qreal(matrix.m[1][3]) +
- qreal(matrix.m[3][3]);
- if (w == 1.0) {
- return QPointF(qreal(x), qreal(y));
- } else {
- return QPointF(qreal(x / w), qreal(y / w));
- }
- }
+ return matrix.map(point);
}
+#endif
+
inline QMatrix4x4 operator-(const QMatrix4x4& matrix)
{
QMatrix4x4 m(Qt::Uninitialized);
@@ -1008,27 +907,118 @@ inline QMatrix4x4 operator*(const QMatrix4x4& matrix, float factor)
inline QPoint QMatrix4x4::map(const QPoint& point) const
{
- return *this * point;
+ float xin, yin;
+ float x, y, w;
+ xin = point.x();
+ yin = point.y();
+ if (flagBits == QMatrix4x4::Identity) {
+ return point;
+ } else if (flagBits.toInt() < QMatrix4x4::Rotation2D) {
+ // Translation | Scale
+ return QPoint(qRound(xin * m[0][0] + m[3][0]),
+ qRound(yin * m[1][1] + m[3][1]));
+ } else if (flagBits.toInt() < QMatrix4x4::Perspective) {
+ return QPoint(qRound(xin * m[0][0] + yin * m[1][0] + m[3][0]),
+ qRound(xin * m[0][1] + yin * m[1][1] + m[3][1]));
+ } else {
+ x = xin * m[0][0] +
+ yin * m[1][0] +
+ m[3][0];
+ y = xin * m[0][1] +
+ yin * m[1][1] +
+ m[3][1];
+ w = xin * m[0][3] +
+ yin * m[1][3] +
+ m[3][3];
+ if (w == 1.0f)
+ return QPoint(qRound(x), qRound(y));
+ else
+ return QPoint(qRound(x / w), qRound(y / w));
+ }
}
inline QPointF QMatrix4x4::map(const QPointF& point) const
{
- return *this * point;
+ qreal xin, yin;
+ qreal x, y, w;
+ xin = point.x();
+ yin = point.y();
+ if (flagBits == QMatrix4x4::Identity) {
+ return point;
+ } else if (flagBits.toInt() < QMatrix4x4::Rotation2D) {
+ // Translation | Scale
+ return QPointF(xin * qreal(m[0][0]) + qreal(m[3][0]),
+ yin * qreal(m[1][1]) + qreal(m[3][1]));
+ } else if (flagBits.toInt() < QMatrix4x4::Perspective) {
+ return QPointF(xin * qreal(m[0][0]) + yin * qreal(m[1][0]) +
+ qreal(m[3][0]),
+ xin * qreal(m[0][1]) + yin * qreal(m[1][1]) +
+ qreal(m[3][1]));
+ } else {
+ x = xin * qreal(m[0][0]) +
+ yin * qreal(m[1][0]) +
+ qreal(m[3][0]);
+ y = xin * qreal(m[0][1]) +
+ yin * qreal(m[1][1]) +
+ qreal(m[3][1]);
+ w = xin * qreal(m[0][3]) +
+ yin * qreal(m[1][3]) +
+ qreal(m[3][3]);
+ if (w == 1.0) {
+ return QPointF(qreal(x), qreal(y));
+ } else {
+ return QPointF(qreal(x / w), qreal(y / w));
+ }
+ }
}
#ifndef QT_NO_VECTOR3D
inline QVector3D QMatrix4x4::map(const QVector3D& point) const
{
- return *this * point;
+ float x, y, z, w;
+ if (flagBits == QMatrix4x4::Identity) {
+ return point;
+ } else if (flagBits.toInt() < QMatrix4x4::Rotation2D) {
+ // Translation | Scale
+ return QVector3D(point.x() * m[0][0] + m[3][0],
+ point.y() * m[1][1] + m[3][1],
+ point.z() * m[2][2] + m[3][2]);
+ } else if (flagBits.toInt() < QMatrix4x4::Rotation) {
+ // Translation | Scale | Rotation2D
+ return QVector3D(point.x() * m[0][0] + point.y() * m[1][0] + m[3][0],
+ point.x() * m[0][1] + point.y() * m[1][1] + m[3][1],
+ point.z() * m[2][2] + m[3][2]);
+ } else {
+ x = point.x() * m[0][0] +
+ point.y() * m[1][0] +
+ point.z() * m[2][0] +
+ m[3][0];
+ y = point.x() * m[0][1] +
+ point.y() * m[1][1] +
+ point.z() * m[2][1] +
+ m[3][1];
+ z = point.x() * m[0][2] +
+ point.y() * m[1][2] +
+ point.z() * m[2][2] +
+ m[3][2];
+ w = point.x() * m[0][3] +
+ point.y() * m[1][3] +
+ point.z() * m[2][3] +
+ m[3][3];
+ if (w == 1.0f)
+ return QVector3D(x, y, z);
+ else
+ return QVector3D(x / w, y / w, z / w);
+ }
}
inline QVector3D QMatrix4x4::mapVector(const QVector3D& vector) const
{
- if (flagBits < Scale) {
+ if (flagBits.toInt() < Scale) {
// Translation
return vector;
- } else if (flagBits < Rotation2D) {
+ } else if (flagBits.toInt() < Rotation2D) {
// Translation | Scale
return QVector3D(vector.x() * m[0][0],
vector.y() * m[1][1],
diff --git a/src/gui/math3d/qquaternion.cpp b/src/gui/math3d/qquaternion.cpp
index 440ed3c99b..e7c5945208 100644
--- a/src/gui/math3d/qquaternion.cpp
+++ b/src/gui/math3d/qquaternion.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2020 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qquaternion.h"
#include <QtCore/qdatastream.h>
@@ -62,14 +26,14 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn QQuaternion::QQuaternion()
+ \fn QQuaternion::QQuaternion() noexcept
Constructs an identity quaternion (1, 0, 0, 0), i.e. with the vector (0, 0, 0)
and scalar 1.
*/
/*!
- \fn QQuaternion::QQuaternion(Qt::Initialization)
+ \fn QQuaternion::QQuaternion(Qt::Initialization) noexcept
\since 5.5
\internal
@@ -77,7 +41,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn QQuaternion::QQuaternion(float scalar, float xpos, float ypos, float zpos)
+ \fn QQuaternion::QQuaternion(float scalar, float xpos, float ypos, float zpos) noexcept
Constructs a quaternion with the vector (\a xpos, \a ypos, \a zpos)
and \a scalar.
@@ -86,7 +50,7 @@ QT_BEGIN_NAMESPACE
#ifndef QT_NO_VECTOR3D
/*!
- \fn QQuaternion::QQuaternion(float scalar, const QVector3D& vector)
+ \fn QQuaternion::QQuaternion(float scalar, const QVector3D &vector) noexcept
Constructs a quaternion vector from the specified \a vector and
\a scalar.
@@ -95,7 +59,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn QVector3D QQuaternion::vector() const
+ \fn QVector3D QQuaternion::vector() const noexcept
Returns the vector component of this quaternion.
@@ -103,7 +67,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn void QQuaternion::setVector(const QVector3D& vector)
+ \fn void QQuaternion::setVector(const QVector3D &vector) noexcept
Sets the vector component of this quaternion to \a vector.
@@ -113,7 +77,7 @@ QT_BEGIN_NAMESPACE
#endif
/*!
- \fn void QQuaternion::setVector(float x, float y, float z)
+ \fn void QQuaternion::setVector(float x, float y, float z) noexcept
Sets the vector component of this quaternion to (\a x, \a y, \a z).
@@ -123,13 +87,13 @@ QT_BEGIN_NAMESPACE
#ifndef QT_NO_VECTOR4D
/*!
- \fn QQuaternion::QQuaternion(const QVector4D& vector)
+ \fn QQuaternion::QQuaternion(const QVector4D &vector) noexcept
Constructs a quaternion from the components of \a vector.
*/
/*!
- \fn QVector4D QQuaternion::toVector4D() const
+ \fn QVector4D QQuaternion::toVector4D() const noexcept
Returns this quaternion as a 4D vector.
*/
@@ -137,14 +101,14 @@ QT_BEGIN_NAMESPACE
#endif
/*!
- \fn bool QQuaternion::isNull() const
+ \fn bool QQuaternion::isNull() const noexcept
Returns \c true if the x, y, z, and scalar components of this
quaternion are set to 0.0; otherwise returns \c false.
*/
/*!
- \fn bool QQuaternion::isIdentity() const
+ \fn bool QQuaternion::isIdentity() const noexcept
Returns \c true if the x, y, and z components of this
quaternion are set to 0.0, and the scalar component is set
@@ -152,7 +116,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn float QQuaternion::x() const
+ \fn float QQuaternion::x() const noexcept
Returns the x coordinate of this quaternion's vector.
@@ -160,7 +124,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn float QQuaternion::y() const
+ \fn float QQuaternion::y() const noexcept
Returns the y coordinate of this quaternion's vector.
@@ -168,7 +132,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn float QQuaternion::z() const
+ \fn float QQuaternion::z() const noexcept
Returns the z coordinate of this quaternion's vector.
@@ -176,7 +140,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn float QQuaternion::scalar() const
+ \fn float QQuaternion::scalar() const noexcept
Returns the scalar component of this quaternion.
@@ -184,7 +148,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn void QQuaternion::setX(float x)
+ \fn void QQuaternion::setX(float x) noexcept
Sets the x coordinate of this quaternion's vector to the given
\a x coordinate.
@@ -193,7 +157,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn void QQuaternion::setY(float y)
+ \fn void QQuaternion::setY(float y) noexcept
Sets the y coordinate of this quaternion's vector to the given
\a y coordinate.
@@ -202,7 +166,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn void QQuaternion::setZ(float z)
+ \fn void QQuaternion::setZ(float z) noexcept
Sets the z coordinate of this quaternion's vector to the given
\a z coordinate.
@@ -211,7 +175,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn void QQuaternion::setScalar(float scalar)
+ \fn void QQuaternion::setScalar(float scalar) noexcept
Sets the scalar component of this quaternion to \a scalar.
@@ -219,7 +183,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2)
+ \fn float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2) noexcept
\since 5.5
Returns the dot product of \a q1 and \a q2.
@@ -234,12 +198,15 @@ QT_BEGIN_NAMESPACE
*/
float QQuaternion::length() const
{
- return std::sqrt(xp * xp + yp * yp + zp * zp + wp * wp);
+ return qHypot(xp, yp, zp, wp);
}
/*!
Returns the squared length of the quaternion.
+ \note Though cheap to compute, this is susceptible to overflow and underflow
+ that length() avoids in many cases.
+
\sa length(), dotProduct()
*/
float QQuaternion::lengthSquared() const
@@ -259,17 +226,10 @@ float QQuaternion::lengthSquared() const
*/
QQuaternion QQuaternion::normalized() const
{
- // Need some extra precision if the length is very small.
- double len = double(xp) * double(xp) +
- double(yp) * double(yp) +
- double(zp) * double(zp) +
- double(wp) * double(wp);
- if (qFuzzyIsNull(len - 1.0f))
- return *this;
- else if (!qFuzzyIsNull(len))
- return *this / std::sqrt(len);
- else
+ const float scale = length();
+ if (qFuzzyIsNull(scale))
return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
+ return *this / scale;
}
/*!
@@ -280,16 +240,10 @@ QQuaternion QQuaternion::normalized() const
*/
void QQuaternion::normalize()
{
- // Need some extra precision if the length is very small.
- double len = double(xp) * double(xp) +
- double(yp) * double(yp) +
- double(zp) * double(zp) +
- double(wp) * double(wp);
- if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
+ const float len = length();
+ if (qFuzzyIsNull(len))
return;
- len = std::sqrt(len);
-
xp /= len;
yp /= len;
zp /= len;
@@ -297,7 +251,7 @@ void QQuaternion::normalize()
}
/*!
- \fn QQuaternion QQuaternion::inverted() const
+ \fn QQuaternion QQuaternion::inverted() const noexcept
\since 5.5
Returns the inverse of this quaternion.
@@ -307,7 +261,7 @@ void QQuaternion::normalize()
*/
/*!
- \fn QQuaternion QQuaternion::conjugated() const
+ \fn QQuaternion QQuaternion::conjugated() const noexcept
\since 5.5
Returns the conjugate of this quaternion, which is
@@ -324,13 +278,13 @@ void QQuaternion::normalize()
\snippet code/src_gui_math3d_qquaternion.cpp 1
*/
-QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
+QVector3D QQuaternion::rotatedVector(const QVector3D &vector) const
{
return (*this * QQuaternion(0, vector) * conjugated()).vector();
}
/*!
- \fn QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion)
+ \fn QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion) noexcept
Adds the given \a quaternion to this quaternion and returns a reference to
this quaternion.
@@ -339,7 +293,7 @@ QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
*/
/*!
- \fn QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion)
+ \fn QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion) noexcept
Subtracts the given \a quaternion from this quaternion and returns a
reference to this quaternion.
@@ -348,7 +302,7 @@ QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
*/
/*!
- \fn QQuaternion &QQuaternion::operator*=(float factor)
+ \fn QQuaternion &QQuaternion::operator*=(float factor) noexcept
Multiplies this quaternion's components by the given \a factor, and
returns a reference to this quaternion.
@@ -357,7 +311,7 @@ QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
*/
/*!
- \fn QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion)
+ \fn QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion) noexcept
Multiplies this quaternion by \a quaternion and returns a reference
to this quaternion.
@@ -375,7 +329,7 @@ QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
#ifndef QT_NO_VECTOR3D
/*!
- \fn void QQuaternion::getAxisAndAngle(QVector3D *axis, float *angle) const
+ \fn void QQuaternion::getAxisAndAngle(QVector3D *axis, float *angle) const noexcept
\since 5.5
\overload
@@ -391,7 +345,7 @@ QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
\sa getAxisAndAngle()
*/
-QQuaternion QQuaternion::fromAxisAndAngle(const QVector3D& axis, float angle)
+QQuaternion QQuaternion::fromAxisAndAngle(const QVector3D &axis, float angle)
{
// Algorithm from:
// http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56
@@ -421,24 +375,22 @@ void QQuaternion::getAxisAndAngle(float *x, float *y, float *z, float *angle) co
// The quaternion representing the rotation is
// q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)
- float length = xp * xp + yp * yp + zp * zp;
+ const float length = qHypot(xp, yp, zp);
if (!qFuzzyIsNull(length)) {
- *x = xp;
- *y = yp;
- *z = zp;
- if (!qFuzzyIsNull(length - 1.0f)) {
- length = std::sqrt(length);
- *x /= length;
- *y /= length;
- *z /= length;
+ if (qFuzzyCompare(length, 1.0f)) {
+ *x = xp;
+ *y = yp;
+ *z = zp;
+ } else {
+ *x = xp / length;
+ *y = yp / length;
+ *z = zp / length;
}
- *angle = 2.0f * std::acos(wp);
+ *angle = qRadiansToDegrees(2.0f * std::atan2(length, wp));
} else {
// angle is 0 (mod 2*pi), so any axis will fit
*x = *y = *z = *angle = 0.0f;
}
-
- *angle = qRadiansToDegrees(*angle);
}
/*!
@@ -450,8 +402,8 @@ void QQuaternion::getAxisAndAngle(float *x, float *y, float *z, float *angle) co
QQuaternion QQuaternion::fromAxisAndAngle
(float x, float y, float z, float angle)
{
- float length = std::sqrt(x * x + y * y + z * z);
- if (!qFuzzyIsNull(length - 1.0f) && !qFuzzyIsNull(length)) {
+ float length = qHypot(x, y, z);
+ if (!qFuzzyCompare(length, 1.0f) && !qFuzzyIsNull(length)) {
x /= length;
y /= length;
z /= length;
@@ -501,46 +453,47 @@ void QQuaternion::getEulerAngles(float *pitch, float *yaw, float *roll) const
{
Q_ASSERT(pitch && yaw && roll);
- // Algorithm from:
- // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q37
-
- float xx = xp * xp;
- float xy = xp * yp;
- float xz = xp * zp;
- float xw = xp * wp;
- float yy = yp * yp;
- float yz = yp * zp;
- float yw = yp * wp;
- float zz = zp * zp;
- float zw = zp * wp;
-
- const float lengthSquared = xx + yy + zz + wp * wp;
- if (!qFuzzyIsNull(lengthSquared - 1.0f) && !qFuzzyIsNull(lengthSquared)) {
- xx /= lengthSquared;
- xy /= lengthSquared; // same as (xp / length) * (yp / length)
- xz /= lengthSquared;
- xw /= lengthSquared;
- yy /= lengthSquared;
- yz /= lengthSquared;
- yw /= lengthSquared;
- zz /= lengthSquared;
- zw /= lengthSquared;
- }
-
- *pitch = std::asin(-2.0f * (yz - xw));
- if (*pitch < M_PI_2) {
- if (*pitch > -M_PI_2) {
- *yaw = std::atan2(2.0f * (xz + yw), 1.0f - 2.0f * (xx + yy));
- *roll = std::atan2(2.0f * (xy + zw), 1.0f - 2.0f * (xx + zz));
- } else {
- // not a unique solution
- *roll = 0.0f;
- *yaw = -std::atan2(-2.0f * (xy - zw), 1.0f - 2.0f * (yy + zz));
- }
+ // Algorithm adapted from:
+ // https://ingmec.ual.es/~jlblanco/papers/jlblanco2010geometry3D_techrep.pdf
+ // "A tutorial on SE(3) transformation parameterizations and on-manifold optimization".
+
+ // We can only detect Gimbal lock when we normalize, which we can't do when
+ // length is nearly zero. Do so before multiplying coordinates, to avoid
+ // underflow.
+ const float len = length();
+ const bool rescale = !qFuzzyIsNull(len);
+ const float xps = rescale ? xp / len : xp;
+ const float yps = rescale ? yp / len : yp;
+ const float zps = rescale ? zp / len : zp;
+ const float wps = rescale ? wp / len : wp;
+
+ const float xx = xps * xps;
+ const float xy = xps * yps;
+ const float xz = xps * zps;
+ const float xw = xps * wps;
+ const float yy = yps * yps;
+ const float yz = yps * zps;
+ const float yw = yps * wps;
+ const float zz = zps * zps;
+ const float zw = zps * wps;
+
+ // For the common case, we have a hidden division by cos(pitch) to calculate
+ // yaw and roll: atan2(a / cos(pitch), b / cos(pitch)) = atan2(a, b). This equation
+ // wouldn't work if cos(pitch) is close to zero (i.e. abs(sin(pitch)) =~ 1.0).
+ // This threshold is copied from qFuzzyIsNull() to avoid the hidden division by zero.
+ constexpr float epsilon = 0.00001f;
+
+ const float sinp = -2.0f * (yz - xw);
+ if (std::abs(sinp) < 1.0f - epsilon) {
+ *pitch = std::asin(sinp);
+ *yaw = std::atan2(2.0f * (xz + yw), 1.0f - 2.0f * (xx + yy));
+ *roll = std::atan2(2.0f * (xy + zw), 1.0f - 2.0f * (xx + zz));
} else {
- // not a unique solution
+ // Gimbal lock case, which doesn't have a unique solution. We just use
+ // XY rotation.
+ *pitch = std::copysign(static_cast<float>(M_PI_2), sinp);
+ *yaw = 2.0f * std::atan2(yps, wps);
*roll = 0.0f;
- *yaw = std::atan2(-2.0f * (xy - zw), 1.0f - 2.0f * (yy + zz));
}
*pitch = qRadiansToDegrees(*pitch);
@@ -785,21 +738,21 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
#endif // QT_NO_VECTOR3D
/*!
- \fn bool QQuaternion::operator==(const QQuaternion &q1, const QQuaternion &q2)
+ \fn bool QQuaternion::operator==(const QQuaternion &q1, const QQuaternion &q2) noexcept
Returns \c true if \a q1 is equal to \a q2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
- \fn bool QQuaternion::operator!=(const QQuaternion &q1, const QQuaternion &q2)
+ \fn bool QQuaternion::operator!=(const QQuaternion &q1, const QQuaternion &q2) noexcept
Returns \c true if \a q1 is not equal to \a q2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
- \fn const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
+ \fn const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2) noexcept
\relates QQuaternion
Returns a QQuaternion object that is the sum of the given quaternions,
@@ -809,7 +762,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
*/
/*!
- \fn const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
+ \fn const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2) noexcept
\relates QQuaternion
Returns a QQuaternion object that is formed by subtracting
@@ -819,7 +772,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
*/
/*!
- \fn const QQuaternion operator*(float factor, const QQuaternion &quaternion)
+ \fn const QQuaternion operator*(float factor, const QQuaternion &quaternion) noexcept
\relates QQuaternion
Returns a copy of the given \a quaternion, multiplied by the
@@ -829,7 +782,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
*/
/*!
- \fn const QQuaternion operator*(const QQuaternion &quaternion, float factor)
+ \fn const QQuaternion operator*(const QQuaternion &quaternion, float factor) noexcept
\relates QQuaternion
Returns a copy of the given \a quaternion, multiplied by the
@@ -839,7 +792,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
*/
/*!
- \fn const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
+ \fn const QQuaternion operator*(const QQuaternion &q1, const QQuaternion &q2) noexcept
\relates QQuaternion
Multiplies \a q1 and \a q2 using quaternion multiplication.
@@ -850,7 +803,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
*/
/*!
- \fn const QQuaternion operator-(const QQuaternion &quaternion)
+ \fn const QQuaternion operator-(const QQuaternion &quaternion) noexcept
\relates QQuaternion
\overload
@@ -873,7 +826,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
#ifndef QT_NO_VECTOR3D
/*!
- \fn QVector3D operator*(const QQuaternion &quaternion, const QVector3D &vec)
+ \fn QVector3D operator*(const QQuaternion &quaternion, const QVector3D &vec) noexcept
\since 5.5
\relates QQuaternion
@@ -883,7 +836,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
#endif
/*!
- \fn bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2)
+ \fn bool qFuzzyCompare(const QQuaternion &q1, const QQuaternion &q2) noexcept
\relates QQuaternion
Returns \c true if \a q1 and \a q2 are equal, allowing for a small
@@ -902,7 +855,7 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
\sa nlerp()
*/
QQuaternion QQuaternion::slerp
- (const QQuaternion& q1, const QQuaternion& q2, float t)
+ (const QQuaternion &q1, const QQuaternion &q2, float t)
{
// Handle the easy cases first.
if (t <= 0.0f)
@@ -951,7 +904,7 @@ QQuaternion QQuaternion::slerp
\sa slerp()
*/
QQuaternion QQuaternion::nlerp
- (const QQuaternion& q1, const QQuaternion& q2, float t)
+ (const QQuaternion &q1, const QQuaternion &q2, float t)
{
// Handle the easy cases first.
if (t <= 0.0f)
diff --git a/src/gui/math3d/qquaternion.h b/src/gui/math3d/qquaternion.h
index 76fa237afd..7cfbd9b818 100644
--- a/src/gui/math3d/qquaternion.h
+++ b/src/gui/math3d/qquaternion.h
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QQUATERNION_H
#define QQUATERNION_H
@@ -53,158 +17,165 @@ QT_BEGIN_NAMESPACE
class QMatrix4x4;
class QVariant;
-class Q_GUI_EXPORT QQuaternion
+class QT6_ONLY(Q_GUI_EXPORT) QQuaternion
{
public:
- QQuaternion();
- explicit QQuaternion(Qt::Initialization) {}
- QQuaternion(float scalar, float xpos, float ypos, float zpos);
+ constexpr QQuaternion() noexcept;
+ explicit QQuaternion(Qt::Initialization) noexcept {}
+ constexpr QQuaternion(float scalar, float xpos, float ypos, float zpos) noexcept;
#ifndef QT_NO_VECTOR3D
- QQuaternion(float scalar, const QVector3D& vector);
+ constexpr QQuaternion(float scalar, const QVector3D &vector) noexcept;
#endif
#ifndef QT_NO_VECTOR4D
- explicit QQuaternion(const QVector4D& vector);
+ constexpr explicit QQuaternion(const QVector4D &vector) noexcept;
#endif
- bool isNull() const;
- bool isIdentity() const;
+ constexpr bool isNull() const noexcept;
+ constexpr bool isIdentity() const noexcept;
#ifndef QT_NO_VECTOR3D
- QVector3D vector() const;
- void setVector(const QVector3D& vector);
+ constexpr QVector3D vector() const noexcept;
+ constexpr void setVector(const QVector3D &vector) noexcept;
#endif
- void setVector(float x, float y, float z);
+ constexpr void setVector(float x, float y, float z) noexcept;
- float x() const;
- float y() const;
- float z() const;
- float scalar() const;
+ constexpr float x() const noexcept;
+ constexpr float y() const noexcept;
+ constexpr float z() const noexcept;
+ constexpr float scalar() const noexcept;
- void setX(float x);
- void setY(float y);
- void setZ(float z);
- void setScalar(float scalar);
+ constexpr void setX(float x) noexcept;
+ constexpr void setY(float y) noexcept;
+ constexpr void setZ(float z) noexcept;
+ constexpr void setScalar(float scalar) noexcept;
- constexpr static inline float dotProduct(const QQuaternion &q1, const QQuaternion &q2);
+ constexpr static float dotProduct(const QQuaternion &q1, const QQuaternion &q2) noexcept;
- float length() const;
- float lengthSquared() const;
+ // ### Qt 7: make the next four constexpr
+ // (perhaps using std::hypot, constexpr in C++26, or constexpr qHypot)
+ QT7_ONLY(Q_GUI_EXPORT) float length() const;
+ QT7_ONLY(Q_GUI_EXPORT) float lengthSquared() const;
- [[nodiscard]] QQuaternion normalized() const;
- void normalize();
+ [[nodiscard]] QT7_ONLY(Q_GUI_EXPORT) QQuaternion normalized() const;
+ QT7_ONLY(Q_GUI_EXPORT) void normalize();
- inline QQuaternion inverted() const;
+ constexpr QQuaternion inverted() const noexcept;
- [[nodiscard]] QQuaternion conjugated() const;
+ [[nodiscard]] constexpr QQuaternion conjugated() const noexcept;
- QVector3D rotatedVector(const QVector3D& vector) const;
+ QT7_ONLY(Q_GUI_EXPORT) QVector3D rotatedVector(const QVector3D &vector) const;
- QQuaternion &operator+=(const QQuaternion &quaternion);
- QQuaternion &operator-=(const QQuaternion &quaternion);
- QQuaternion &operator*=(float factor);
- QQuaternion &operator*=(const QQuaternion &quaternion);
- QQuaternion &operator/=(float divisor);
+ constexpr QQuaternion &operator+=(const QQuaternion &quaternion) noexcept;
+ constexpr QQuaternion &operator-=(const QQuaternion &quaternion) noexcept;
+ constexpr QQuaternion &operator*=(float factor) noexcept;
+ constexpr QQuaternion &operator*=(const QQuaternion &quaternion) noexcept;
+ constexpr QQuaternion &operator/=(float divisor);
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
- friend inline bool operator==(const QQuaternion &q1, const QQuaternion &q2) noexcept
+ friend constexpr bool operator==(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return q1.wp == q2.wp && q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp;
}
- friend inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2) noexcept
+ friend constexpr bool operator!=(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return !(q1 == q2);
}
QT_WARNING_POP
- friend inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2);
- friend inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2);
- friend inline const QQuaternion operator*(float factor, const QQuaternion &quaternion);
- friend inline const QQuaternion operator*(const QQuaternion &quaternion, float factor);
- friend inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2);
- friend inline const QQuaternion operator-(const QQuaternion &quaternion);
- friend inline const QQuaternion operator/(const QQuaternion &quaternion, float divisor);
+ friend constexpr QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2) noexcept;
+ friend constexpr QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2) noexcept;
+ friend constexpr QQuaternion operator*(float factor, const QQuaternion &quaternion) noexcept;
+ friend constexpr QQuaternion operator*(const QQuaternion &quaternion, float factor) noexcept;
+ friend constexpr QQuaternion operator*(const QQuaternion &q1, const QQuaternion &q2) noexcept;
+ friend constexpr QQuaternion operator-(const QQuaternion &quaternion) noexcept;
+ friend constexpr QQuaternion operator/(const QQuaternion &quaternion, float divisor);
- friend inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2);
+ friend constexpr bool qFuzzyCompare(const QQuaternion &q1, const QQuaternion &q2) noexcept;
#ifndef QT_NO_VECTOR4D
- QVector4D toVector4D() const;
+ constexpr QVector4D toVector4D() const noexcept;
#endif
- operator QVariant() const;
+ QT7_ONLY(Q_GUI_EXPORT) operator QVariant() const;
#ifndef QT_NO_VECTOR3D
inline void getAxisAndAngle(QVector3D *axis, float *angle) const;
- static QQuaternion fromAxisAndAngle(const QVector3D& axis, float angle);
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromAxisAndAngle(const QVector3D &axis, float angle);
#endif
- void getAxisAndAngle(float *x, float *y, float *z, float *angle) const;
- static QQuaternion fromAxisAndAngle
- (float x, float y, float z, float angle);
+ QT7_ONLY(Q_GUI_EXPORT) void getAxisAndAngle(float *x, float *y, float *z, float *angle) const;
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromAxisAndAngle(float x, float y, float z,
+ float angle);
#ifndef QT_NO_VECTOR3D
inline QVector3D toEulerAngles() const;
- static inline QQuaternion fromEulerAngles(const QVector3D &eulerAngles);
+ QT7_ONLY(Q_GUI_EXPORT) static inline QQuaternion fromEulerAngles(const QVector3D &eulerAngles);
#endif
- void getEulerAngles(float *pitch, float *yaw, float *roll) const;
- static QQuaternion fromEulerAngles(float pitch, float yaw, float roll);
+ QT7_ONLY(Q_GUI_EXPORT) void getEulerAngles(float *pitch, float *yaw, float *roll) const;
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromEulerAngles(float pitch, float yaw, float roll);
- QMatrix3x3 toRotationMatrix() const;
- static QQuaternion fromRotationMatrix(const QMatrix3x3 &rot3x3);
+ QT7_ONLY(Q_GUI_EXPORT) QMatrix3x3 toRotationMatrix() const;
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromRotationMatrix(const QMatrix3x3 &rot3x3);
#ifndef QT_NO_VECTOR3D
- void getAxes(QVector3D *xAxis, QVector3D *yAxis, QVector3D *zAxis) const;
- static QQuaternion fromAxes(const QVector3D &xAxis, const QVector3D &yAxis, const QVector3D &zAxis);
+ QT7_ONLY(Q_GUI_EXPORT) void getAxes(QVector3D *xAxis, QVector3D *yAxis, QVector3D *zAxis) const;
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromAxes(const QVector3D &xAxis,
+ const QVector3D &yAxis,
+ const QVector3D &zAxis);
- static QQuaternion fromDirection(const QVector3D &direction, const QVector3D &up);
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromDirection(const QVector3D &direction,
+ const QVector3D &up);
- static QQuaternion rotationTo(const QVector3D &from, const QVector3D &to);
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion rotationTo(const QVector3D &from,
+ const QVector3D &to);
#endif
- static QQuaternion slerp
- (const QQuaternion& q1, const QQuaternion& q2, float t);
- static QQuaternion nlerp
- (const QQuaternion& q1, const QQuaternion& q2, float t);
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion slerp(const QQuaternion &q1, const QQuaternion &q2,
+ float t);
+ QT7_ONLY(Q_GUI_EXPORT) static QQuaternion nlerp(const QQuaternion &q1, const QQuaternion &q2,
+ float t);
private:
float wp, xp, yp, zp;
};
-Q_DECLARE_TYPEINFO(QQuaternion, Q_RELOCATABLE_TYPE);
+Q_DECLARE_TYPEINFO(QQuaternion, Q_PRIMITIVE_TYPE);
-inline QQuaternion::QQuaternion() : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
+constexpr QQuaternion::QQuaternion() noexcept : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
-inline QQuaternion::QQuaternion(float aScalar, float xpos, float ypos, float zpos) : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
+constexpr QQuaternion::QQuaternion(float aScalar, float xpos, float ypos, float zpos) noexcept
+ : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
-inline bool QQuaternion::isNull() const
+constexpr bool QQuaternion::isNull() const noexcept
{
return wp == 0.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
}
-inline bool QQuaternion::isIdentity() const
+constexpr bool QQuaternion::isIdentity() const noexcept
{
return wp == 1.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
}
QT_WARNING_POP
-inline float QQuaternion::x() const { return xp; }
-inline float QQuaternion::y() const { return yp; }
-inline float QQuaternion::z() const { return zp; }
-inline float QQuaternion::scalar() const { return wp; }
+constexpr float QQuaternion::x() const noexcept { return xp; }
+constexpr float QQuaternion::y() const noexcept { return yp; }
+constexpr float QQuaternion::z() const noexcept { return zp; }
+constexpr float QQuaternion::scalar() const noexcept { return wp; }
-inline void QQuaternion::setX(float aX) { xp = aX; }
-inline void QQuaternion::setY(float aY) { yp = aY; }
-inline void QQuaternion::setZ(float aZ) { zp = aZ; }
-inline void QQuaternion::setScalar(float aScalar) { wp = aScalar; }
+constexpr void QQuaternion::setX(float aX) noexcept { xp = aX; }
+constexpr void QQuaternion::setY(float aY) noexcept { yp = aY; }
+constexpr void QQuaternion::setZ(float aZ) noexcept { zp = aZ; }
+constexpr void QQuaternion::setScalar(float aScalar) noexcept { wp = aScalar; }
-constexpr inline float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2)
+constexpr float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return q1.wp * q2.wp + q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp;
}
-inline QQuaternion QQuaternion::inverted() const
+constexpr QQuaternion QQuaternion::inverted() const noexcept
{
// Need some extra precision if the length is very small.
double len = double(wp) * double(wp) +
@@ -217,12 +188,12 @@ inline QQuaternion QQuaternion::inverted() const
return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
}
-inline QQuaternion QQuaternion::conjugated() const
+constexpr QQuaternion QQuaternion::conjugated() const noexcept
{
return QQuaternion(wp, -xp, -yp, -zp);
}
-inline QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion)
+constexpr QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion) noexcept
{
wp += quaternion.wp;
xp += quaternion.xp;
@@ -231,7 +202,7 @@ inline QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion)
return *this;
}
-inline QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion)
+constexpr QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion) noexcept
{
wp -= quaternion.wp;
xp -= quaternion.xp;
@@ -240,7 +211,7 @@ inline QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion)
return *this;
}
-inline QQuaternion &QQuaternion::operator*=(float factor)
+constexpr QQuaternion &QQuaternion::operator*=(float factor) noexcept
{
wp *= factor;
xp *= factor;
@@ -249,7 +220,7 @@ inline QQuaternion &QQuaternion::operator*=(float factor)
return *this;
}
-inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
+constexpr QQuaternion operator*(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
float yy = (q1.wp - q1.yp) * (q2.wp + q2.zp);
float zz = (q1.wp + q1.yp) * (q2.wp - q2.zp);
@@ -265,13 +236,13 @@ inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
return QQuaternion(w, x, y, z);
}
-inline QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion)
+constexpr QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion) noexcept
{
*this = *this * quaternion;
return *this;
}
-inline QQuaternion &QQuaternion::operator/=(float divisor)
+constexpr QQuaternion &QQuaternion::operator/=(float divisor)
{
wp /= divisor;
xp /= divisor;
@@ -280,37 +251,37 @@ inline QQuaternion &QQuaternion::operator/=(float divisor)
return *this;
}
-inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
+constexpr QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp);
}
-inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
+constexpr QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return QQuaternion(q1.wp - q2.wp, q1.xp - q2.xp, q1.yp - q2.yp, q1.zp - q2.zp);
}
-inline const QQuaternion operator*(float factor, const QQuaternion &quaternion)
+constexpr QQuaternion operator*(float factor, const QQuaternion &quaternion) noexcept
{
return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
}
-inline const QQuaternion operator*(const QQuaternion &quaternion, float factor)
+constexpr QQuaternion operator*(const QQuaternion &quaternion, float factor) noexcept
{
return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
}
-inline const QQuaternion operator-(const QQuaternion &quaternion)
+constexpr QQuaternion operator-(const QQuaternion &quaternion) noexcept
{
return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp);
}
-inline const QQuaternion operator/(const QQuaternion &quaternion, float divisor)
+constexpr QQuaternion operator/(const QQuaternion &quaternion, float divisor)
{
return QQuaternion(quaternion.wp / divisor, quaternion.xp / divisor, quaternion.yp / divisor, quaternion.zp / divisor);
}
-inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2)
+constexpr bool qFuzzyCompare(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return qFuzzyCompare(q1.wp, q2.wp) &&
qFuzzyCompare(q1.xp, q2.xp) &&
@@ -320,17 +291,17 @@ inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2)
#ifndef QT_NO_VECTOR3D
-inline QQuaternion::QQuaternion(float aScalar, const QVector3D& aVector)
+constexpr QQuaternion::QQuaternion(float aScalar, const QVector3D &aVector) noexcept
: wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
-inline void QQuaternion::setVector(const QVector3D& aVector)
+constexpr void QQuaternion::setVector(const QVector3D &aVector) noexcept
{
xp = aVector.x();
yp = aVector.y();
zp = aVector.z();
}
-inline QVector3D QQuaternion::vector() const
+constexpr QVector3D QQuaternion::vector() const noexcept
{
return QVector3D(xp, yp, zp);
}
@@ -361,7 +332,7 @@ inline QQuaternion QQuaternion::fromEulerAngles(const QVector3D &eulerAngles)
#endif
-inline void QQuaternion::setVector(float aX, float aY, float aZ)
+constexpr void QQuaternion::setVector(float aX, float aY, float aZ) noexcept
{
xp = aX;
yp = aY;
@@ -370,10 +341,10 @@ inline void QQuaternion::setVector(float aX, float aY, float aZ)
#ifndef QT_NO_VECTOR4D
-inline QQuaternion::QQuaternion(const QVector4D& aVector)
+constexpr QQuaternion::QQuaternion(const QVector4D &aVector) noexcept
: wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
-inline QVector4D QQuaternion::toVector4D() const
+constexpr QVector4D QQuaternion::toVector4D() const noexcept
{
return QVector4D(xp, yp, zp, wp);
}
diff --git a/src/gui/math3d/qvector2d.h b/src/gui/math3d/qvector2d.h
index fc42579918..a9e55a8ef8 100644
--- a/src/gui/math3d/qvector2d.h
+++ b/src/gui/math3d/qvector2d.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// 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>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QtGui/qvectornd.h>
diff --git a/src/gui/math3d/qvector3d.h b/src/gui/math3d/qvector3d.h
index fc42579918..a9e55a8ef8 100644
--- a/src/gui/math3d/qvector3d.h
+++ b/src/gui/math3d/qvector3d.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// 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>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QtGui/qvectornd.h>
diff --git a/src/gui/math3d/qvector4d.h b/src/gui/math3d/qvector4d.h
index fc42579918..a9e55a8ef8 100644
--- a/src/gui/math3d/qvector4d.h
+++ b/src/gui/math3d/qvector4d.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// 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>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QtGui/qvectornd.h>
diff --git a/src/gui/math3d/qvectornd.cpp b/src/gui/math3d/qvectornd.cpp
index 1c456a9d6d..dcd7bdbcf8 100644
--- a/src/gui/math3d/qvectornd.cpp
+++ b/src/gui/math3d/qvectornd.cpp
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// 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>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qvectornd.h"
#include <QtCore/qdatastream.h>
@@ -56,6 +20,10 @@ QT_BEGIN_NAMESPACE
\ingroup painting-3D
\inmodule QtGui
+ Vectors are one of the main building blocks of 2D representation and
+ drawing. They consist of two finite floating-point coordinates,
+ traditionally called x and y.
+
The QVector2D class can also be used to represent vertices in 2D space.
We therefore do not need to provide a separate vertex class.
@@ -80,6 +48,7 @@ QT_BEGIN_NAMESPACE
\fn QVector2D::QVector2D(float xpos, float ypos)
Constructs a vector with coordinates (\a xpos, \a ypos).
+ Both coordinates must be finite.
*/
/*!
@@ -146,7 +115,7 @@ QT_BEGIN_NAMESPACE
/*!
\fn void QVector2D::setX(float x)
- Sets the x coordinate of this point to the given \a x coordinate.
+ Sets the x coordinate of this point to the given finite \a x coordinate.
\sa x(), setY()
*/
@@ -154,7 +123,7 @@ QT_BEGIN_NAMESPACE
/*!
\fn void QVector2D::setY(float y)
- Sets the y coordinate of this point to the given \a y coordinate.
+ Sets the y coordinate of this point to the given finite \a y coordinate.
\sa y(), setX()
*/
@@ -210,7 +179,7 @@ QT_BEGIN_NAMESPACE
/*!
\fn void QVector2D::normalize()
- Normalizes the currect vector in place. Nothing happens if this
+ Normalizes the current 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()
@@ -260,24 +229,29 @@ QT_BEGIN_NAMESPACE
/*!
\fn QVector2D &QVector2D::operator*=(float factor)
- Multiplies this vector's coordinates by the given \a factor, and
+ Multiplies this vector's coordinates by the given finite \a factor and
returns a reference to this vector.
- \sa operator/=()
+ \sa operator/=(), operator*()
*/
/*!
\fn QVector2D &QVector2D::operator*=(QVector2D vector)
- Multiplies the components of this vector by the corresponding
- components in \a vector.
+ Multiplies each component of this vector by the corresponding component of
+ \a vector and returns a reference to this vector.
+
+ \note This is not a cross product of this vector with \a vector. (Its
+ components add up to the dot product of this vector and \a vector.)
+
+ \sa operator/=(), operator*()
*/
/*!
\fn QVector2D &QVector2D::operator/=(float divisor)
- Divides this vector's coordinates by the given \a divisor, and
- returns a reference to this vector.
+ Divides this vector's coordinates by the given \a divisor and returns a
+ reference to this vector. The \a divisor must not be either zero or NaN.
\sa operator*=()
*/
@@ -286,10 +260,12 @@ QT_BEGIN_NAMESPACE
\fn QVector2D &QVector2D::operator/=(QVector2D vector)
\since 5.5
- Divides the components of this vector by the corresponding
- components in \a vector.
+ Divides each component of this vector by the corresponding component of \a
+ vector and returns a reference to this vector.
- \sa operator*=()
+ The \a vector must have no component that is either zero or NaN.
+
+ \sa operator*=(), operator/()
*/
/*!
@@ -333,7 +309,7 @@ QT_BEGIN_NAMESPACE
/*! //! friend
\fn const QVector2D QVector2D::operator*(float factor, QVector2D vector)
- Returns a copy of the given \a vector, multiplied by the given \a factor.
+ Returns a copy of the given \a vector, multiplied by the given finite \a factor.
\sa QVector2D::operator*=()
*/
@@ -341,7 +317,7 @@ QT_BEGIN_NAMESPACE
/*! //! friend
\fn const QVector2D QVector2D::operator*(QVector2D vector, float factor)
- Returns a copy of the given \a vector, multiplied by the given \a factor.
+ Returns a copy of the given \a vector, multiplied by the given finite \a factor.
\sa QVector2D::operator*=()
*/
@@ -349,16 +325,21 @@ QT_BEGIN_NAMESPACE
/*! //! friend
\fn const QVector2D QVector2D::operator*(QVector2D v1, QVector2D v2)
- Multiplies the components of \a v1 by the corresponding
- components in \a v2.
+ Returns the QVector2D object formed by multiplying each component of \a v1
+ by the corresponding component of \a v2.
+
+ \note This is not a cross product of \a v1 and \a v2 in any sense.
+ (Its components add up to the dot product of \a v1 and \a v2.)
+
+ \sa QVector2D::operator*=()
*/
/*! //! friend
\fn const QVector2D QVector2D::operator-(QVector2D vector)
\overload
- Returns a QVector2D object that is formed by changing the sign of
- the components of the given \a vector.
+ Returns a QVector2D object that is formed by changing the sign of each
+ component of the given \a vector.
Equivalent to \c {QVector2D(0,0) - vector}.
*/
@@ -366,8 +347,10 @@ QT_BEGIN_NAMESPACE
/*! //! friend
\fn const QVector2D QVector2D::operator/(QVector2D vector, float divisor)
- Returns the QVector2D object formed by dividing all three components of
- the given \a vector by the given \a divisor.
+ Returns the QVector2D object formed by dividing each component of the given
+ \a vector by the given \a divisor.
+
+ The \a divisor must not be either zero or NaN.
\sa QVector2D::operator/=()
*/
@@ -376,8 +359,10 @@ QT_BEGIN_NAMESPACE
\fn const QVector2D QVector2D::operator/(QVector2D vector, QVector2D divisor)
\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.
+ Returns the QVector2D object formed by dividing each component of the given
+ \a vector by the corresponding component of the given \a divisor.
+
+ The \a divisor must have no component that is either zero or NaN.
\sa QVector2D::operator/=()
*/
@@ -417,6 +402,7 @@ bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept
\fn QPoint QVector2D::toPoint() const
Returns the QPoint form of this 2D vector.
+ Each coordinate is rounded to the nearest integer.
\sa toPointF(), toVector3D()
*/
@@ -481,6 +467,7 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
float x, y;
stream >> x;
stream >> y;
+ Q_ASSERT(qIsFinite(x) && qIsFinite(y));
vector.setX(x);
vector.setY(y);
return stream;
@@ -502,8 +489,8 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
\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.
+ drawing. They consist of three finite floating-point 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.
@@ -529,6 +516,7 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
\fn QVector3D::QVector3D(float xpos, float ypos, float zpos)
Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos).
+ All parameters must be finite.
*/
/*!
@@ -560,7 +548,7 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
\fn QVector3D::QVector3D(QVector2D vector, float zpos)
Constructs a 3D vector from the specified 2D \a vector. The z
- coordinate is set to \a zpos.
+ coordinate is set to \a zpos, which must be finite.
\sa toVector2D()
*/
@@ -613,7 +601,7 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\fn void QVector3D::setX(float x)
- Sets the x coordinate of this point to the given \a x coordinate.
+ Sets the x coordinate of this point to the given finite \a x coordinate.
\sa x(), setY(), setZ()
*/
@@ -621,7 +609,7 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\fn void QVector3D::setY(float y)
- Sets the y coordinate of this point to the given \a y coordinate.
+ Sets the y coordinate of this point to the given finite \a y coordinate.
\sa y(), setX(), setZ()
*/
@@ -629,7 +617,7 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\fn void QVector3D::setZ(float z)
- Sets the z coordinate of this point to the given \a z coordinate.
+ Sets the z coordinate of this point to the given finite \a z coordinate.
\sa z(), setX(), setY()
*/
@@ -668,7 +656,7 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\fn void QVector3D::normalize()
- Normalizes the currect vector in place. Nothing happens if this
+ Normalizes the current 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()
@@ -695,42 +683,45 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\fn QVector3D &QVector3D::operator*=(float factor)
- Multiplies this vector's coordinates by the given \a factor, and
+ Multiplies this vector's coordinates by the given finite \a factor and
returns a reference to this vector.
- \sa operator/=()
+ \sa operator/=(), operator*()
*/
/*!
\fn QVector3D &QVector3D::operator*=(QVector3D vector)
\overload
- Multiplies the components of this vector by the corresponding
- components in \a vector.
+ Multiplies each component of this vector by the corresponding component in
+ \a vector and returns a reference to this vector.
- Note: this is not the same as the crossProduct() of this
- vector and \a vector.
+ Note: this is not the same as the crossProduct() of this vector and
+ \a vector. (Its components add up to the dot product of this vector and
+ \a vector.)
- \sa crossProduct()
+ \sa crossProduct(), operator/=(), operator*()
*/
/*!
\fn QVector3D &QVector3D::operator/=(float divisor)
- Divides this vector's coordinates by the given \a divisor, and
- returns a reference to this vector.
+ Divides this vector's coordinates by the given \a divisor, and returns a
+ reference to this vector. The \a divisor must not be either zero or NaN.
- \sa operator*=()
+ \sa operator*=(), operator/()
*/
/*!
\fn QVector3D &QVector3D::operator/=(QVector3D vector)
\since 5.5
- Divides the components of this vector by the corresponding
- components in \a vector.
+ Divides each component of this vector by the corresponding component in \a
+ vector and returns a reference to this vector.
- \sa operator*=()
+ The \a vector must have no component that is either zero or NaN.
+
+ \sa operator*=(), operator/()
*/
/*!
@@ -742,8 +733,9 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\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.
+ Returns the cross-product of vectors \a v1 and \a v2, which is normal to the
+ plane spanned by \a v1 and \a v2. It will be zero if the two vectors are
+ parallel.
\sa normal()
*/
@@ -751,8 +743,8 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\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.
+ Returns the unit normal vector of a plane spanned by vectors \a v1 and \a
+ v2, which must not be parallel to one another.
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.
@@ -763,8 +755,8 @@ QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
/*!
\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.
+ Returns the unit normal vector of a plane spanned by vectors \a v2 - \a v1
+ and \a v3 - \a v1, which must not be parallel to one another.
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
@@ -922,7 +914,7 @@ QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &pr
/*! //! friend
\fn const QVector3D QVector3D::operator*(float factor, QVector3D vector)
- Returns a copy of the given \a vector, multiplied by the given \a factor.
+ Returns a copy of the given \a vector, multiplied by the given finite \a factor.
\sa QVector3D::operator*=()
*/
@@ -930,7 +922,7 @@ QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &pr
/*! //! friend
\fn const QVector3D QVector3D::operator*(QVector3D vector, float factor)
- Returns a copy of the given \a vector, multiplied by the given \a factor.
+ Returns a copy of the given \a vector, multiplied by the given finite \a factor.
\sa QVector3D::operator*=()
*/
@@ -938,9 +930,11 @@ QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &pr
/*! //! friend
\fn const QVector3D QVector3D::operator*(QVector3D v1, QVector3D v2)
- Multiplies the components of \a v1 by the corresponding components in \a v2.
+ Returns the QVector3D object formed by multiplying each component of \a v1
+ by the corresponding component of \a v2.
\note This is not the same as the crossProduct() of \a v1 and \a v2.
+ (Its components add up to the dot product of \a v1 and \a v2.)
\sa QVector3D::crossProduct()
*/
@@ -949,8 +943,8 @@ QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &pr
\fn const QVector3D QVector3D::operator-(QVector3D vector)
\overload
- Returns a QVector3D object that is formed by changing the sign of
- all three components of the given \a vector.
+ Returns a QVector3D object that is formed by changing the sign of each
+ component of the given \a vector.
Equivalent to \c {QVector3D(0,0,0) - vector}.
*/
@@ -958,8 +952,10 @@ QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &pr
/*! //! friend
\fn const QVector3D QVector3D::operator/(QVector3D vector, float divisor)
- Returns the QVector3D object formed by dividing all three components of
- the given \a vector by the given \a divisor.
+ Returns the QVector3D object formed by dividing each component of the given
+ \a vector by the given \a divisor.
+
+ The \a divisor must not be either zero or NaN.
\sa QVector3D::operator/=()
*/
@@ -968,8 +964,10 @@ QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &pr
\fn const QVector3D QVector3D::operator/(QVector3D vector, QVector3D divisor)
\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.
+ Returns the QVector3D object formed by dividing each component of the given
+ \a vector by the corresponding component of the given \a divisor.
+
+ The \a divisor must have no component that is either zero or NaN.
\sa QVector3D::operator/=()
*/
@@ -1014,8 +1012,8 @@ bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept
/*!
\fn QPoint QVector3D::toPoint() const
- Returns the QPoint form of this 3D vector. The z coordinate
- is dropped.
+ Returns the QPoint form of this 3D vector. The z coordinate is dropped. The
+ x and y coordinates are rounded to nearest integers.
\sa toPointF(), toVector2D()
*/
@@ -1100,6 +1098,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
stream >> x;
stream >> y;
stream >> z;
+ Q_ASSERT(qIsFinite(x) && qIsFinite(y) && qIsFinite(z));
vector.setX(x);
vector.setY(y);
vector.setZ(z);
@@ -1121,6 +1120,10 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
\ingroup painting-3D
\inmodule QtGui
+ Vectors are one of the main building blocks of 4D affine representations of
+ 3D space. They consist of four finite floating-point coordinates,
+ traditionally called x, y, z and w.
+
The QVector4D class can also be used to represent vertices in 4D space.
We therefore do not need to provide a separate vertex class.
@@ -1145,6 +1148,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
\fn QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos)
Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos, \a wpos).
+ All parameters must be finite.
*/
/*!
@@ -1176,7 +1180,8 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
\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.
+ and w coordinates are set to \a zpos and \a wpos respectively,
+ each of which must be finite.
\sa toVector2D()
*/
@@ -1198,7 +1203,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
\fn QVector4D::QVector4D(QVector3D vector, float wpos)
Constructs a 4D vector from the specified 3D \a vector. The w
- coordinate is set to \a wpos.
+ coordinate is set to \a wpos, which must be finite.
\sa toVector3D()
*/
@@ -1247,7 +1252,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*!
\fn void QVector4D::setX(float x)
- Sets the x coordinate of this point to the given \a x coordinate.
+ Sets the x coordinate of this point to the given finite \a x coordinate.
\sa x(), setY(), setZ(), setW()
*/
@@ -1255,7 +1260,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*!
\fn void QVector4D::setY(float y)
- Sets the y coordinate of this point to the given \a y coordinate.
+ Sets the y coordinate of this point to the given finite \a y coordinate.
\sa y(), setX(), setZ(), setW()
*/
@@ -1263,7 +1268,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*!
\fn void QVector4D::setZ(float z)
- Sets the z coordinate of this point to the given \a z coordinate.
+ Sets the z coordinate of this point to the given finite \a z coordinate.
\sa z(), setX(), setY(), setW()
*/
@@ -1271,7 +1276,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*!
\fn void QVector4D::setW(float w)
- Sets the w coordinate of this point to the given \a w coordinate.
+ Sets the w coordinate of this point to the given finite \a w coordinate.
\sa w(), setX(), setY(), setZ()
*/
@@ -1327,7 +1332,7 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*!
\fn void QVector4D::normalize()
- Normalizes the currect vector in place. Nothing happens if this
+ Normalizes the current 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()
@@ -1355,24 +1360,26 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*!
\fn QVector4D &QVector4D::operator*=(float factor)
- Multiplies this vector's coordinates by the given \a factor, and
+ Multiplies this vector's coordinates by the given finite \a factor, and
returns a reference to this vector.
- \sa operator/=()
+ \sa operator/=(), operator*()
*/
/*!
\fn QVector4D &QVector4D::operator*=(QVector4D vector)
- Multiplies the components of this vector by the corresponding
- components in \a vector.
+ Multiplies each component of this vector by the corresponding component of
+ \a vector and returns a reference to this vector.
+
+ \sa operator/=(), operator*()
*/
/*!
\fn QVector4D &QVector4D::operator/=(float divisor)
- Divides this vector's coordinates by the given \a divisor, and
- returns a reference to this vector.
+ Divides this vector's coordinates by the given \a divisor, and returns a
+ reference to this vector. The \a divisor must not be either zero or NaN.
\sa operator*=()
*/
@@ -1381,10 +1388,12 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
\fn QVector4D &QVector4D::operator/=(QVector4D vector)
\since 5.5
- Divides the components of this vector by the corresponding
- components in \a vector.
+ Divides each component of this vector by the corresponding component of \a
+ vector and returns a reference to this vector.
- \sa operator*=()
+ The \a vector must have no component that is either zero or NaN.
+
+ \sa operator*=(), operator/()
*/
/*!
@@ -1444,8 +1453,11 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*! //! friend
\fn const QVector4D QVector4D::operator*(QVector4D v1, QVector4D v2)
- Returns the vector consisting of the multiplication of the
- components from \a v1 and \a v2.
+ Returns the QVector4D object formed by multiplying each component of \a v1
+ by the corresponding component of \a v2.
+
+ \note This is not a cross product of \a v1 and \a v2 in any sense.
+ (Its components add up to the dot product of \a v1 and \a v2.)
\sa QVector4D::operator*=()
*/
@@ -1463,8 +1475,10 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
/*! //! friend
\fn const QVector4D QVector4D::operator/(QVector4D vector, float divisor)
- Returns the QVector4D object formed by dividing all four components of
- the given \a vector by the given \a divisor.
+ Returns the QVector4D object formed by dividing each component of the given
+ \a vector by the given \a divisor.
+
+ The \a divisor must not be either zero or NaN.
\sa QVector4D::operator/=()
*/
@@ -1473,8 +1487,10 @@ QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
\fn const QVector4D QVector4D::operator/(QVector4D vector, QVector4D divisor)
\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.
+ Returns the QVector4D object formed by dividing each component of the given
+ \a vector by the corresponding component of the given \a divisor.
+
+ The \a divisor must have no component that is either zero or NaN.
\sa QVector4D::operator/=()
*/
@@ -1539,8 +1555,8 @@ bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept
/*!
\fn QPoint QVector4D::toPoint() const
- Returns the QPoint form of this 4D vector. The z and w coordinates
- are dropped.
+ Returns the QPoint form of this 4D vector. The z and w coordinates are
+ dropped. The x and y coordinates are rounded to nearest integers.
\sa toPointF(), toVector2D()
*/
@@ -1611,6 +1627,7 @@ QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
stream >> y;
stream >> z;
stream >> w;
+ Q_ASSERT(qIsFinite(x) && qIsFinite(y) && qIsFinite(z) && qIsFinite(w));
vector.setX(x);
vector.setY(y);
vector.setZ(z);
diff --git a/src/gui/math3d/qvectornd.h b/src/gui/math3d/qvectornd.h
index 48031105cf..a8adf12801 100644
--- a/src/gui/math3d/qvectornd.h
+++ b/src/gui/math3d/qvectornd.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2020 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>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QVECTORND_H
#define QVECTORND_H
@@ -46,8 +10,13 @@
#include <QtCore/qrect.h>
#include <QtCore/qmath.h>
+#include <QtCore/q20type_traits.h>
+#include <QtCore/q23utility.h>
+
QT_BEGIN_NAMESPACE
+// QT_ENABLE_P0846_SEMANTICS_FOR(get) // from qpoint.h
+
class QVector2D;
class QVector3D;
class QVector4D;
@@ -147,11 +116,14 @@ QT_WARNING_POP
constexpr friend inline QVector2D operator/(QVector2D vector, float divisor)
{
+ Q_ASSERT(divisor < 0 || divisor > 0);
return QVector2D(vector.v[0] / divisor, vector.v[1] / divisor);
}
constexpr friend inline QVector2D operator/(QVector2D vector, QVector2D divisor)
{
+ Q_ASSERT(divisor.v[0] < 0 || divisor.v[0] > 0);
+ Q_ASSERT(divisor.v[1] < 0 || divisor.v[1] > 0);
return QVector2D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1]);
}
@@ -174,6 +146,15 @@ private:
friend class QVector3D;
friend class QVector4D;
+
+ template <std::size_t I,
+ typename V,
+ std::enable_if_t<(I < 2), bool> = true,
+ std::enable_if_t<std::is_same_v<q20::remove_cvref_t<V>, QVector2D>, bool> = true>
+ friend constexpr decltype(auto) get(V &&vec) noexcept
+ {
+ return q23::forward_like<V>(vec.v[I]);
+ }
};
Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE);
@@ -288,12 +269,17 @@ QT_WARNING_POP
constexpr friend inline QVector3D operator/(QVector3D vector, float divisor)
{
+ Q_ASSERT(divisor < 0 || divisor > 0);
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]);
+ Q_ASSERT(divisor.v[0] > 0 || divisor.v[0] < 0);
+ Q_ASSERT(divisor.v[1] > 0 || divisor.v[1] < 0);
+ Q_ASSERT(divisor.v[2] > 0 || divisor.v[2] < 0);
+ return QVector3D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1],
+ vector.v[2] / divisor.v[2]);
}
friend Q_GUI_EXPORT bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept;
@@ -319,6 +305,15 @@ private:
friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
#endif
+
+ template <std::size_t I,
+ typename V,
+ std::enable_if_t<(I < 3), bool> = true,
+ std::enable_if_t<std::is_same_v<q20::remove_cvref_t<V>, QVector3D>, bool> = true>
+ friend constexpr decltype(auto) get(V &&vec) noexcept
+ {
+ return q23::forward_like<V>(vec.v[I]);
+ }
};
Q_DECLARE_TYPEINFO(QVector3D, Q_PRIMITIVE_TYPE);
@@ -422,12 +417,18 @@ QT_WARNING_POP
constexpr friend inline QVector4D operator/(QVector4D vector, float divisor)
{
+ Q_ASSERT(divisor < 0 || divisor > 0);
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]);
+ Q_ASSERT(divisor.v[0] > 0 || divisor.v[0] < 0);
+ Q_ASSERT(divisor.v[1] > 0 || divisor.v[1] < 0);
+ Q_ASSERT(divisor.v[2] > 0 || divisor.v[2] < 0);
+ Q_ASSERT(divisor.v[3] > 0 || divisor.v[3] < 0);
+ 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]);
}
friend Q_GUI_EXPORT bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept;
@@ -456,6 +457,15 @@ private:
friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
#endif
+
+ template <std::size_t I,
+ typename V,
+ std::enable_if_t<(I < 4), bool> = true,
+ std::enable_if_t<std::is_same_v<q20::remove_cvref_t<V>, QVector4D>, bool> = true>
+ friend constexpr decltype(auto) get(V &&vec) noexcept
+ {
+ return q23::forward_like<V>(vec.v[I]);
+ }
};
Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE);
@@ -508,10 +518,7 @@ constexpr inline float QVector2D::operator[](int i) const
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));
+ return qHypot(v[0], v[1]);
}
constexpr inline float QVector2D::lengthSquared() const noexcept
@@ -521,31 +528,19 @@ constexpr inline float QVector2D::lengthSquared() const noexcept
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();
- }
+ const float len = length();
+ return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector2D()
+ : QVector2D(v[0] / len, v[1] / len);
}
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))
+ const float len = length();
+ 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[0] /= len;
+ v[1] /= len;
}
inline float QVector2D::distanceToPoint(QVector2D point) const noexcept
@@ -591,6 +586,7 @@ constexpr inline QVector2D &QVector2D::operator*=(QVector2D vector) noexcept
constexpr inline QVector2D &QVector2D::operator/=(float divisor)
{
+ Q_ASSERT(divisor < 0 || divisor > 0);
v[0] /= divisor;
v[1] /= divisor;
return *this;
@@ -598,6 +594,8 @@ constexpr inline QVector2D &QVector2D::operator/=(float divisor)
constexpr inline QVector2D &QVector2D::operator/=(QVector2D vector)
{
+ Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
+ Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
v[0] /= vector.v[0];
v[1] /= vector.v[1];
return *this;
@@ -691,45 +689,25 @@ constexpr inline float QVector3D::operator[](int i) const
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));
+ return qHypot(v[0], v[1], v[2]);
}
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();
- }
+ const float len = length();
+ return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector3D()
+ : QVector3D(v[0] / len, v[1] / len, v[2] / len);
}
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))
+ const float len = length();
+ 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[0] /= len;
+ v[1] /= len;
+ v[2] /= len;
}
constexpr inline float QVector3D::lengthSquared() const noexcept
@@ -771,6 +749,7 @@ constexpr inline QVector3D &QVector3D::operator*=(QVector3D vector) noexcept
constexpr inline QVector3D &QVector3D::operator/=(float divisor)
{
+ Q_ASSERT(divisor < 0 || divisor > 0);
v[0] /= divisor;
v[1] /= divisor;
v[2] /= divisor;
@@ -779,6 +758,9 @@ constexpr inline QVector3D &QVector3D::operator/=(float divisor)
constexpr inline QVector3D &QVector3D::operator/=(QVector3D vector)
{
+ Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
+ Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
+ Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
v[0] /= vector.v[0];
v[1] /= vector.v[1];
v[2] /= vector.v[2];
@@ -917,12 +899,7 @@ constexpr inline float QVector4D::operator[](int i) const
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));
+ return qHypot(v[0], v[1], v[2], v[3]);
}
constexpr inline float QVector4D::lengthSquared() const noexcept
@@ -932,40 +909,21 @@ constexpr inline float QVector4D::lengthSquared() const noexcept
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();
- }
+ const float len = length();
+ return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector4D()
+ : QVector4D(v[0] / len, v[1] / len, v[2] / len, v[3] / len);
}
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))
+ const float len = length();
+ 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);
+ v[0] /= len;
+ v[1] /= len;
+ v[2] /= len;
+ v[3] /= len;
}
constexpr inline QVector4D &QVector4D::operator+=(QVector4D vector) noexcept
@@ -1006,6 +964,7 @@ constexpr inline QVector4D &QVector4D::operator*=(QVector4D vector) noexcept
constexpr inline QVector4D &QVector4D::operator/=(float divisor)
{
+ Q_ASSERT(divisor < 0 || divisor > 0);
v[0] /= divisor;
v[1] /= divisor;
v[2] /= divisor;
@@ -1015,6 +974,10 @@ constexpr inline QVector4D &QVector4D::operator/=(float divisor)
constexpr inline QVector4D &QVector4D::operator/=(QVector4D vector)
{
+ Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
+ Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
+ Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
+ Q_ASSERT(vector.v[3] > 0 || vector.v[3] < 0);
v[0] /= vector.v[0];
v[1] /= vector.v[1];
v[2] /= vector.v[2];
@@ -1083,4 +1046,41 @@ Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
QT_END_NAMESPACE
+/***************************** Tuple protocol *****************************/
+
+namespace std {
+#ifndef QT_NO_VECTOR2D
+ template <>
+ class tuple_size<QT_PREPEND_NAMESPACE(QVector2D)> : public integral_constant<size_t, 2> {};
+ template <>
+ class tuple_element<0, QT_PREPEND_NAMESPACE(QVector2D)> { public: using type = float; };
+ template <>
+ class tuple_element<1, QT_PREPEND_NAMESPACE(QVector2D)> { public: using type = float; };
+#endif // QT_NO_VECTOR2D
+
+#ifndef QT_NO_VECTOR3D
+ template <>
+ class tuple_size<QT_PREPEND_NAMESPACE(QVector3D)> : public integral_constant<size_t, 3> {};
+ template <>
+ class tuple_element<0, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; };
+ template <>
+ class tuple_element<1, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; };
+ template <>
+ class tuple_element<2, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; };
+#endif // QT_NO_VECTOR3D
+
+#ifndef QT_NO_VECTOR4D
+ template <>
+ class tuple_size<QT_PREPEND_NAMESPACE(QVector4D)> : public integral_constant<size_t, 4> {};
+ template <>
+ class tuple_element<0, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
+ template <>
+ class tuple_element<1, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
+ template <>
+ class tuple_element<2, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
+ template <>
+ class tuple_element<3, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; };
+#endif // QT_NO_VECTOR4D
+}
+
#endif // QVECTORND_H