diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-10-26 00:36:45 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-10-26 01:03:23 (GMT) |
commit | 1a299a9631a2d1b4e07cedfece7af5318a766fe6 (patch) | |
tree | bde51d4c5acdc7cd975322932bdbea02bcda59bb /src/gui/math3d/qmatrix4x4.cpp | |
parent | 0d6104d763f4cb32ac6117b3f951afcb73fc50e6 (diff) | |
download | Qt-1a299a9631a2d1b4e07cedfece7af5318a766fe6.zip Qt-1a299a9631a2d1b4e07cedfece7af5318a766fe6.tar.gz Qt-1a299a9631a2d1b4e07cedfece7af5318a766fe6.tar.bz2 |
Optimize QGraphicsRotation's use of QMatrix4x4
Previous code was creating a full 3D rotation matrix and then
projecting back to 2D. This change combines the two steps into
one to avoid calculating matrix components that will be dropped.
Reviewed-by: Sarah Smith
Diffstat (limited to 'src/gui/math3d/qmatrix4x4.cpp')
-rw-r--r-- | src/gui/math3d/qmatrix4x4.cpp | 108 |
1 files changed, 106 insertions, 2 deletions
diff --git a/src/gui/math3d/qmatrix4x4.cpp b/src/gui/math3d/qmatrix4x4.cpp index 00e8f15..fa3826f 100644 --- a/src/gui/math3d/qmatrix4x4.cpp +++ b/src/gui/math3d/qmatrix4x4.cpp @@ -58,6 +58,8 @@ QT_BEGIN_NAMESPACE \sa QVector3D, QGenericMatrix */ +static const qreal inv_dist_to_plane = 1. / 1024.; + /*! \fn QMatrix4x4::QMatrix4x4() @@ -1103,6 +1105,110 @@ QMatrix4x4& QMatrix4x4::rotate(qreal angle, qreal x, qreal y, qreal z) return *this; } +/*! + \internal +*/ +QMatrix4x4& QMatrix4x4::projectedRotate(qreal angle, qreal x, qreal y, qreal z) +{ + // Used by QGraphicsRotation::applyTo() to perform a rotation + // and projection back to 2D in a single step. + if (angle == 0.0f) + return *this; + QMatrix4x4 m(1); // The "1" says to not load the identity. + qreal c, s, ic; + if (angle == 90.0f || angle == -270.0f) { + s = 1.0f; + c = 0.0f; + } else if (angle == -90.0f || angle == 270.0f) { + s = -1.0f; + c = 0.0f; + } else if (angle == 180.0f || angle == -180.0f) { + s = 0.0f; + c = -1.0f; + } else { + qreal a = angle * M_PI / 180.0f; + c = qCos(a); + s = qSin(a); + } + bool quick = false; + if (x == 0.0f) { + if (y == 0.0f) { + if (z != 0.0f) { + // Rotate around the Z axis. + m.setIdentity(); + m.m[0][0] = c; + m.m[1][1] = c; + if (z < 0.0f) { + m.m[1][0] = s; + m.m[0][1] = -s; + } else { + m.m[1][0] = -s; + m.m[0][1] = s; + } + m.flagBits = General; + quick = true; + } + } else if (z == 0.0f) { + // Rotate around the Y axis. + m.setIdentity(); + m.m[0][0] = c; + m.m[2][2] = 1.0f; + if (y < 0.0f) { + m.m[0][3] = -s * inv_dist_to_plane; + } else { + m.m[0][3] = s * inv_dist_to_plane; + } + m.flagBits = General; + quick = true; + } + } else if (y == 0.0f && z == 0.0f) { + // Rotate around the X axis. + m.setIdentity(); + m.m[1][1] = c; + m.m[2][2] = 1.0f; + if (x < 0.0f) { + m.m[1][3] = s * inv_dist_to_plane; + } else { + m.m[1][3] = -s * inv_dist_to_plane; + } + m.flagBits = General; + quick = true; + } + if (!quick) { + qreal len = x * x + y * y + z * z; + if (!qFuzzyIsNull(len - 1.0f) && !qFuzzyIsNull(len)) { + len = qSqrt(len); + x /= len; + y /= len; + z /= len; + } + ic = 1.0f - c; + m.m[0][0] = x * x * ic + c; + m.m[1][0] = x * y * ic - z * s; + m.m[2][0] = 0.0f; + m.m[3][0] = 0.0f; + m.m[0][1] = y * x * ic + z * s; + m.m[1][1] = y * y * ic + c; + m.m[2][1] = 0.0f; + m.m[3][1] = 0.0f; + m.m[0][2] = 0.0f; + m.m[1][2] = 0.0f; + m.m[2][2] = 1.0f; + m.m[3][2] = 0.0f; + m.m[0][3] = (x * z * ic - y * s) * -inv_dist_to_plane; + m.m[1][3] = (y * z * ic + x * s) * -inv_dist_to_plane; + m.m[2][3] = 0.0f; + m.m[3][3] = 1.0f; + } + int flags = flagBits; + *this *= m; + if (flags != Identity) + flagBits = flags | Rotation; + else + flagBits = Rotation; + return *this; +} + #ifndef QT_NO_VECTOR4D /*! @@ -1448,8 +1554,6 @@ QTransform QMatrix4x4::toTransform() const m[3][0], m[3][1], m[3][3]); } -static const qreal inv_dist_to_plane = 1. / 1024.; - /*! Returns the conventional Qt 2D transformation matrix that corresponds to this matrix. |