summaryrefslogtreecommitdiffstats
path: root/src/gui
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-10-26 00:36:45 (GMT)
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-10-26 01:03:23 (GMT)
commit1a299a9631a2d1b4e07cedfece7af5318a766fe6 (patch)
treebde51d4c5acdc7cd975322932bdbea02bcda59bb /src/gui
parent0d6104d763f4cb32ac6117b3f951afcb73fc50e6 (diff)
downloadQt-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')
-rw-r--r--src/gui/graphicsview/qgraphicstransform.cpp4
-rw-r--r--src/gui/math3d/qmatrix4x4.cpp108
-rw-r--r--src/gui/math3d/qmatrix4x4.h4
3 files changed, 111 insertions, 5 deletions
diff --git a/src/gui/graphicsview/qgraphicstransform.cpp b/src/gui/graphicsview/qgraphicstransform.cpp
index a0b5493..93dc196 100644
--- a/src/gui/graphicsview/qgraphicstransform.cpp
+++ b/src/gui/graphicsview/qgraphicstransform.cpp
@@ -547,9 +547,7 @@ void QGraphicsRotation::applyTo(QMatrix4x4 *matrix) const
return;
matrix->translate(d->origin);
- QMatrix4x4 m;
- m.rotate(d->angle, d->axis.x(), d->axis.y(), d->axis.z());
- *matrix *= m.toTransform(1024.0f); // Project back to 2D.
+ matrix->projectedRotate(d->angle, d->axis.x(), d->axis.y(), d->axis.z());
matrix->translate(-d->origin);
}
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.
diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h
index 42d992e..ba74b89 100644
--- a/src/gui/math3d/qmatrix4x4.h
+++ b/src/gui/math3d/qmatrix4x4.h
@@ -207,6 +207,10 @@ private:
QMatrix4x4(int) { flagBits = General; }
QMatrix4x4 orthonormalInverse() const;
+
+ QMatrix4x4& projectedRotate(qreal angle, qreal x, qreal y, qreal z);
+
+ friend class QGraphicsRotation;
};
inline QMatrix4x4::QMatrix4x4