summaryrefslogtreecommitdiff
path: root/src/gui/math3d
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-11-04 10:56:40 +1000
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-11-04 11:40:18 +1000
commit79c6049ccaae4434add40d35b3e47a83ff4a102a (patch)
treeb7dc1277998c24780bedb095af94a3ef465d5315 /src/gui/math3d
parentedc9acd6a0c082b7d6c7810700e77a9d990014c6 (diff)
downloadqt4-tools-79c6049ccaae4434add40d35b3e47a83ff4a102a.tar.gz
Incorporate API review feedback for math3d classes.
Reviewed-by: Sarah Smith
Diffstat (limited to 'src/gui/math3d')
-rw-r--r--src/gui/math3d/qgenericmatrix.cpp106
-rw-r--r--src/gui/math3d/qgenericmatrix.h37
-rw-r--r--src/gui/math3d/qmatrix4x4.cpp134
-rw-r--r--src/gui/math3d/qmatrix4x4.h15
-rw-r--r--src/gui/math3d/qquaternion.cpp4
-rw-r--r--src/gui/math3d/qquaternion.h2
6 files changed, 165 insertions, 133 deletions
diff --git a/src/gui/math3d/qgenericmatrix.cpp b/src/gui/math3d/qgenericmatrix.cpp
index d211229eed..aa985362d3 100644
--- a/src/gui/math3d/qgenericmatrix.cpp
+++ b/src/gui/math3d/qgenericmatrix.cpp
@@ -80,7 +80,7 @@ QT_BEGIN_NAMESPACE
The contents of the array \a values is assumed to be in
row-major order.
- \sa toValueArray()
+ \sa copyDataTo()
*/
/*!
@@ -102,11 +102,11 @@ QT_BEGIN_NAMESPACE
Returns true if this matrix is the identity; false otherwise.
- \sa setIdentity()
+ \sa setToIdentity()
*/
/*!
- \fn void QGenericMatrix::setIdentity()
+ \fn void QGenericMatrix::setToIdentity()
Sets this matrix to the identity.
@@ -213,9 +213,9 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \fn void QGenericMatrix::toValueArray(T *values)
+ \fn void QGenericMatrix::copyDataTo(T *values) const
- Retrieves the N * M items in this matrix and writes them to \a values
+ Retrieves the N * M items in this matrix and copies them to \a values
in row-major order.
*/
@@ -243,4 +243,100 @@ QT_BEGIN_NAMESPACE
\sa data()
*/
+#ifndef QT_NO_DATASTREAM
+
+/*!
+ \fn QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix)
+ \relates QGenericMatrix
+
+ Writes the given \a matrix to the given \a stream and returns a
+ reference to the stream.
+
+ \sa {Format of the QDataStream Operators}
+*/
+
+/*!
+ \fn QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
+ \relates QGenericMatrix
+
+ Reads a NxM matrix from the given \a stream into the given \a matrix
+ and returns a reference to the stream.
+
+ \sa {Format of the QDataStream Operators}
+*/
+
+#endif
+
+/*!
+ \typedef QMatrix2x2
+ \relates QGenericMatrix
+
+ The QMatrix2x2 type defines a convenient instantiation of the
+ QGenericMatrix template for 2 columns, 2 rows, and qreal as
+ the element type.
+*/
+
+/*!
+ \typedef QMatrix2x3
+ \relates QGenericMatrix
+
+ The QMatrix2x3 type defines a convenient instantiation of the
+ QGenericMatrix template for 2 columns, 3 rows, and qreal as
+ the element type.
+*/
+
+/*!
+ \typedef QMatrix2x4
+ \relates QGenericMatrix
+
+ The QMatrix2x4 type defines a convenient instantiation of the
+ QGenericMatrix template for 2 columns, 4 rows, and qreal as
+ the element type.
+*/
+
+/*!
+ \typedef QMatrix3x2
+ \relates QGenericMatrix
+
+ The QMatrix3x2 type defines a convenient instantiation of the
+ QGenericMatrix template for 3 columns, 2 rows, and qreal as
+ the element type.
+*/
+
+/*!
+ \typedef QMatrix3x3
+ \relates QGenericMatrix
+
+ The QMatrix3x3 type defines a convenient instantiation of the
+ QGenericMatrix template for 3 columns, 3 rows, and qreal as
+ the element type.
+*/
+
+/*!
+ \typedef QMatrix3x4
+ \relates QGenericMatrix
+
+ The QMatrix3x4 type defines a convenient instantiation of the
+ QGenericMatrix template for 3 columns, 4 rows, and qreal as
+ the element type.
+*/
+
+/*!
+ \typedef QMatrix4x2
+ \relates QGenericMatrix
+
+ The QMatrix4x2 type defines a convenient instantiation of the
+ QGenericMatrix template for 4 columns, 2 rows, and qreal as
+ the element type.
+*/
+
+/*!
+ \typedef QMatrix4x3
+ \relates QGenericMatrix
+
+ The QMatrix4x3 type defines a convenient instantiation of the
+ QGenericMatrix template for 4 columns, 3 rows, and qreal as
+ the element type.
+*/
+
QT_END_NAMESPACE
diff --git a/src/gui/math3d/qgenericmatrix.h b/src/gui/math3d/qgenericmatrix.h
index f178d027bf..3871754324 100644
--- a/src/gui/math3d/qgenericmatrix.h
+++ b/src/gui/math3d/qgenericmatrix.h
@@ -44,6 +44,7 @@
#include <QtCore/qmetatype.h>
#include <QtCore/qdebug.h>
+#include <QtCore/qdatastream.h>
QT_BEGIN_HEADER
@@ -63,7 +64,7 @@ public:
T& operator()(int row, int column);
bool isIdentity() const;
- void setIdentity();
+ void setToIdentity();
void fill(T value);
@@ -76,7 +77,7 @@ public:
bool operator==(const QGenericMatrix<N, M, T>& other) const;
bool operator!=(const QGenericMatrix<N, M, T>& other) const;
- void toValueArray(T *values);
+ void copyDataTo(T *values) const;
T *data() { return m[0]; }
const T *data() const { return m[0]; }
@@ -113,7 +114,7 @@ private:
template <int N, int M, typename T>
Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix()
{
- setIdentity();
+ setToIdentity();
}
template <int N, int M, typename T>
@@ -164,7 +165,7 @@ Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::isIdentity() const
}
template <int N, int M, typename T>
-Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setIdentity()
+Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity()
{
for (int col = 0; col < N; ++col) {
for (int row = 0; row < M; ++row) {
@@ -316,7 +317,7 @@ Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator/(const QGenericMatrix<N, M
}
template <int N, int M, typename T>
-Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::toValueArray(T *values)
+Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::copyDataTo(T *values) const
{
for (int col = 0; col < N; ++col)
for (int row = 0; row < M; ++row)
@@ -352,6 +353,32 @@ QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T> &m)
#endif
+#ifndef QT_NO_DATASTREAM
+
+template <int N, int M, typename T>
+QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix)
+{
+ for (int row = 0; row < M; ++row)
+ for (int col = 0; col < N; ++col)
+ stream << double(matrix(row, col));
+ return stream;
+}
+
+template <int N, int M, typename T>
+QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
+{
+ double x;
+ for (int row = 0; row < M; ++row) {
+ for (int col = 0; col < N; ++col) {
+ stream >> x;
+ matrix(row, col) = T(x);
+ }
+ }
+ return stream;
+}
+
+#endif
+
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QMatrix2x2)
diff --git a/src/gui/math3d/qmatrix4x4.cpp b/src/gui/math3d/qmatrix4x4.cpp
index 5d624d8af4..ef2e4ab47e 100644
--- a/src/gui/math3d/qmatrix4x4.cpp
+++ b/src/gui/math3d/qmatrix4x4.cpp
@@ -73,10 +73,10 @@ static const qreal inv_dist_to_plane = 1. / 1024.;
If the matrix has a special type (identity, translate, scale, etc),
the programmer should follow this constructor with a call to
- inferSpecialType() if they wish QMatrix4x4 to optimize further
+ optimize() if they wish QMatrix4x4 to optimize further
calls to translate(), scale(), etc.
- \sa toValueArray(), inferSpecialType()
+ \sa copyDataTo(), optimize()
*/
QMatrix4x4::QMatrix4x4(const qreal *values)
{
@@ -96,10 +96,10 @@ QMatrix4x4::QMatrix4x4(const qreal *values)
If the matrix has a special type (identity, translate, scale, etc),
the programmer should follow this constructor with a call to
- inferSpecialType() if they wish QMatrix4x4 to optimize further
+ optimize() if they wish QMatrix4x4 to optimize further
calls to translate(), scale(), etc.
- \sa inferSpecialType()
+ \sa optimize()
*/
#if !defined(QT_NO_MEMBER_TEMPLATES) || defined(Q_QDOC)
@@ -176,10 +176,10 @@ QMatrix4x4::QMatrix4x4(const qreal *values, int cols, int rows)
If \a matrix has a special type (identity, translate, scale, etc),
the programmer should follow this constructor with a call to
- inferSpecialType() if they wish QMatrix4x4 to optimize further
+ optimize() if they wish QMatrix4x4 to optimize further
calls to translate(), scale(), etc.
- \sa toAffine(), inferSpecialType()
+ \sa toAffine(), optimize()
*/
QMatrix4x4::QMatrix4x4(const QMatrix& matrix)
{
@@ -208,10 +208,10 @@ QMatrix4x4::QMatrix4x4(const QMatrix& matrix)
If \a transform has a special type (identity, translate, scale, etc),
the programmer should follow this constructor with a call to
- inferSpecialType() if they wish QMatrix4x4 to optimize further
+ optimize() if they wish QMatrix4x4 to optimize further
calls to translate(), scale(), etc.
- \sa toTransform(), inferSpecialType()
+ \sa toTransform(), optimize()
*/
QMatrix4x4::QMatrix4x4(const QTransform& transform)
{
@@ -249,7 +249,7 @@ QMatrix4x4::QMatrix4x4(const QTransform& transform)
Returns a reference to the element at position (\a row, \a column)
in this matrix so that the element can be assigned to.
- \sa inferSpecialType(), setColumn(), setRow()
+ \sa optimize(), setColumn(), setRow()
*/
/*!
@@ -289,11 +289,11 @@ QMatrix4x4::QMatrix4x4(const QTransform& transform)
Returns true if this matrix is the identity; false otherwise.
- \sa setIdentity()
+ \sa setToIdentity()
*/
/*!
- \fn void QMatrix4x4::setIdentity()
+ \fn void QMatrix4x4::setToIdentity()
Sets this matrix to the identity.
@@ -1027,7 +1027,7 @@ QMatrix4x4& QMatrix4x4::rotate(qreal angle, qreal x, qreal y, qreal z)
if (y == 0.0f) {
if (z != 0.0f) {
// Rotate around the Z axis.
- m.setIdentity();
+ m.setToIdentity();
m.m[0][0] = c;
m.m[1][1] = c;
if (z < 0.0f) {
@@ -1042,7 +1042,7 @@ QMatrix4x4& QMatrix4x4::rotate(qreal angle, qreal x, qreal y, qreal z)
}
} else if (z == 0.0f) {
// Rotate around the Y axis.
- m.setIdentity();
+ m.setToIdentity();
m.m[0][0] = c;
m.m[2][2] = c;
if (y < 0.0f) {
@@ -1057,7 +1057,7 @@ QMatrix4x4& QMatrix4x4::rotate(qreal angle, qreal x, qreal y, qreal z)
}
} else if (y == 0.0f && z == 0.0f) {
// Rotate around the X axis.
- m.setIdentity();
+ m.setToIdentity();
m.m[1][1] = c;
m.m[2][2] = c;
if (x < 0.0f) {
@@ -1135,7 +1135,7 @@ QMatrix4x4& QMatrix4x4::projectedRotate(qreal angle, qreal x, qreal y, qreal z)
if (y == 0.0f) {
if (z != 0.0f) {
// Rotate around the Z axis.
- m.setIdentity();
+ m.setToIdentity();
m.m[0][0] = c;
m.m[1][1] = c;
if (z < 0.0f) {
@@ -1150,7 +1150,7 @@ QMatrix4x4& QMatrix4x4::projectedRotate(qreal angle, qreal x, qreal y, qreal z)
}
} else if (z == 0.0f) {
// Rotate around the Y axis.
- m.setIdentity();
+ m.setToIdentity();
m.m[0][0] = c;
m.m[2][2] = 1.0f;
if (y < 0.0f) {
@@ -1163,7 +1163,7 @@ QMatrix4x4& QMatrix4x4::projectedRotate(qreal angle, qreal x, qreal y, qreal z)
}
} else if (y == 0.0f && z == 0.0f) {
// Rotate around the X axis.
- m.setIdentity();
+ m.setToIdentity();
m.m[1][1] = c;
m.m[2][2] = 1.0f;
if (x < 0.0f) {
@@ -1512,10 +1512,10 @@ QMatrix4x4& QMatrix4x4::flipCoordinates()
}
/*!
- Retrieves the 16 items in this matrix and writes them to \a values
+ Retrieves the 16 items in this matrix and copies them to \a values
in row-major order.
*/
-void QMatrix4x4::toValueArray(qreal *values) const
+void QMatrix4x4::copyDataTo(qreal *values) const
{
for (int row = 0; row < 4; ++row)
for (int col = 0; col < 4; ++col)
@@ -1739,7 +1739,7 @@ QRectF QMatrix4x4::mapRect(const QRectF& rect) const
Returns a pointer to the raw data of this matrix.
- \sa constData(), inferSpecialType()
+ \sa constData(), optimize()
*/
/*!
@@ -1788,94 +1788,8 @@ QMatrix4x4 QMatrix4x4::orthonormalInverse() const
return result;
}
-#ifndef QT_NO_VECTOR3D
-/*!
- Decomposes the current rotation matrix into an \a axis of rotation plus
- an \a angle. The result can be used to construct an equivalent rotation
- matrix using glRotate(). It is assumed that the homogenous coordinate
- is 1.0. The returned vector is guaranteed to be normalized.
-
- \code
- qreal angle;
- QVector3D axis;
-
- matrix.extractAxisAngle(angle, axis);
- glRotate(angle, axis[0], axis[1], axis[2]);
- \endcode
-
- \sa rotate()
-*/
-void QMatrix4x4::extractAxisRotation(qreal &angle, QVector3D &axis) const
-{
- // Orientation is dependent on the upper 3x3 matrix; subtract the
- // homogeneous scaling element from the trace of the 4x4 matrix
- qreal tr = m[0][0] + m[1][1] + m[2][2];
- qreal cosa = qreal(0.5f * (tr - 1.0f));
- angle = acos(cosa) * 180.0f / M_PI;
-
- // Any axis will work if r is zero (means no rotation)
- if (qFuzzyIsNull(angle)) {
- axis.setX(1.0f);
- axis.setY(0.0f);
- axis.setZ(0.0f);
- return;
- }
-
- if (angle < 180.0f) {
- axis.setX(m[1][2] - m[2][1]);
- axis.setY(m[2][0] - m[0][2]);
- axis.setZ(m[0][1] - m[1][0]);
- axis.normalize();
- return;
- }
-
- // rads == PI
- qreal tmp;
-
- // r00 is maximum
- if ((m[0][0] >= m[2][2]) && (m[0][0] >= m[1][1])) {
- axis.setX(0.5f * qSqrt(m[0][0] - m[1][1] - m[2][2] + 1.0f));
- tmp = 0.5f / axis.x();
- axis.setY(m[1][0] * tmp);
- axis.setZ(m[2][0] * tmp);
- }
-
- // r11 is maximum
- if ((m[1][1] >= m[2][2]) && (m[1][1] >= m[0][0])) {
- axis.setY(0.5f * qSqrt(m[1][1] - m[0][0] - m[2][2] + 1.0f));
- tmp = 0.5f / axis.y();
- axis.setX(tmp * m[1][0]);
- axis.setZ(tmp * m[2][1]);
- }
-
- // r22 is maximum
- if ((m[2][2] >= m[1][1]) && (m[2][2] >= m[0][0])) {
- axis.setZ(0.5f * qSqrt(m[2][2] - m[0][0] - m[1][1] + 1.0f));
- tmp = 0.5f / axis.z();
- axis.setX(m[2][0]*tmp);
- axis.setY(m[2][1]*tmp);
- }
-}
-
-/*!
- If this is an orthonormal transformation matrix (e.g. only rotations and
- translations have been applied to the matrix, no scaling, or shearing)
- then the world translational component can be obtained by calling this function.
-
- This is most useful for camera matrices, where the negation of this vector
- is effectively the camera world coordinates.
-*/
-QVector3D QMatrix4x4::extractTranslation() const
-{
- return QVector3D
- (m[0][0] * m[3][0] + m[0][1] * m[3][1] + m[0][2] * m[3][2],
- m[1][0] * m[3][0] + m[1][1] * m[3][1] + m[1][2] * m[3][2],
- m[2][0] * m[3][0] + m[2][1] * m[3][1] + m[2][2] * m[3][2]);
-}
-#endif
-
/*!
- Infers the special type of this matrix from its current elements.
+ Optimize the usage of this matrix from its current elements.
Some operations such as translate(), scale(), and rotate() can be
performed more efficiently if the matrix being modified is already
@@ -1888,13 +1802,13 @@ QVector3D QMatrix4x4::extractTranslation() const
the special type and will revert to the safest but least efficient
operations thereafter.
- By calling inferSpecialType() after directly modifying the matrix,
+ 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 operator()(), data(), translate()
*/
-void QMatrix4x4::inferSpecialType()
+void QMatrix4x4::optimize()
{
// If the last element is not 1, then it can never be special.
if (m[3][3] != 1.0f) {
@@ -2011,7 +1925,7 @@ QDataStream &operator>>(QDataStream &stream, QMatrix4x4 &matrix)
matrix(row, col) = qreal(x);
}
}
- matrix.inferSpecialType();
+ matrix.optimize();
return stream;
}
diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h
index ba74b897cb..daebf9dc50 100644
--- a/src/gui/math3d/qmatrix4x4.h
+++ b/src/gui/math3d/qmatrix4x4.h
@@ -63,7 +63,7 @@ class QVariant;
class Q_GUI_EXPORT QMatrix4x4
{
public:
- inline QMatrix4x4() { setIdentity(); }
+ inline QMatrix4x4() { setToIdentity(); }
explicit QMatrix4x4(const qreal *values);
inline QMatrix4x4(qreal m11, qreal m12, qreal m13, qreal m14,
qreal m21, qreal m22, qreal m23, qreal m24,
@@ -87,7 +87,7 @@ public:
inline void setRow(int index, const QVector4D& value);
inline bool isIdentity() const;
- inline void setIdentity();
+ inline void setToIdentity();
inline void fill(qreal value);
@@ -141,11 +141,6 @@ public:
QMatrix4x4& rotate(const QQuaternion& quaternion);
#endif
-#ifndef QT_NO_VECTOR3D
- void extractAxisRotation(qreal &angle, QVector3D &axis) const;
- QVector3D extractTranslation() const;
-#endif
-
QMatrix4x4& ortho(const QRect& rect);
QMatrix4x4& ortho(const QRectF& rect);
QMatrix4x4& ortho(qreal left, qreal right, qreal bottom, qreal top, qreal nearPlane, qreal farPlane);
@@ -156,7 +151,7 @@ public:
#endif
QMatrix4x4& flipCoordinates();
- void toValueArray(qreal *values) const;
+ void copyDataTo(qreal *values) const;
QMatrix toAffine() const;
QTransform toTransform() const;
@@ -183,7 +178,7 @@ public:
inline const qreal *data() const { return m[0]; }
inline const qreal *constData() const { return m[0]; }
- void inferSpecialType();
+ void optimize();
operator QVariant() const;
@@ -330,7 +325,7 @@ inline bool QMatrix4x4::isIdentity() const
return (m[3][3] == 1.0f);
}
-inline void QMatrix4x4::setIdentity()
+inline void QMatrix4x4::setToIdentity()
{
m[0][0] = 1.0f;
m[0][1] = 0.0f;
diff --git a/src/gui/math3d/qquaternion.cpp b/src/gui/math3d/qquaternion.cpp
index d5ec054a6d..626cb3ccf1 100644
--- a/src/gui/math3d/qquaternion.cpp
+++ b/src/gui/math3d/qquaternion.cpp
@@ -288,7 +288,7 @@ void QQuaternion::normalize()
in 3D space. The following code:
\code
- QVector3D result = q.rotateVector(vector);
+ QVector3D result = q.rotatedVector(vector);
\endcode
is equivalent to the following:
@@ -297,7 +297,7 @@ void QQuaternion::normalize()
QVector3D result = (q * QQuaternion(0, vector) * q.conjugate()).vector();
\endcode
*/
-QVector3D QQuaternion::rotateVector(const QVector3D& vector) const
+QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
{
return (*this * QQuaternion(0, vector) * conjugate()).vector();
}
diff --git a/src/gui/math3d/qquaternion.h b/src/gui/math3d/qquaternion.h
index 7480a5cdae..5b2454f3c3 100644
--- a/src/gui/math3d/qquaternion.h
+++ b/src/gui/math3d/qquaternion.h
@@ -95,7 +95,7 @@ public:
QQuaternion conjugate() const;
- QVector3D rotateVector(const QVector3D& vector) const;
+ QVector3D rotatedVector(const QVector3D& vector) const;
QQuaternion &operator+=(const QQuaternion &quaternion);
QQuaternion &operator-=(const QQuaternion &quaternion);