From 48df46cd38e69fed9454d97eeaf8cf0c0489acfa Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Mon, 26 Oct 2009 08:04:23 +1000 Subject: Add an extra overload for QMatrix4x4::toTransform() Change 100afe8d fixed a bug in QGraphicsRotation related to when the "distance to plane" projection needed to be performed. As a side effect it made the toTransform() API not do the expected thing when the function is called with no argument. This change makes the default no-argument version of toTransform() do the simple "drop row 3 and column 3" orthographic transformation that normal users of the class expect, and adds a new overload for the "distance to plane" projection case for the special case. Reviewed-by: trustme --- src/gui/graphicsview/qgraphicsitem_p.h | 2 +- src/gui/graphicsview/qgraphicstransform.cpp | 2 +- src/gui/math3d/qmatrix4x4.cpp | 22 ++++++++++++++++++++-- src/gui/math3d/qmatrix4x4.h | 3 ++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsitem_p.h b/src/gui/graphicsview/qgraphicsitem_p.h index 046f5dc..7c3c4f0 100644 --- a/src/gui/graphicsview/qgraphicsitem_p.h +++ b/src/gui/graphicsview/qgraphicsitem_p.h @@ -542,7 +542,7 @@ struct QGraphicsItemPrivate::TransformData QMatrix4x4 m; for (int i = 0; i < graphicsTransforms.size(); ++i) graphicsTransforms.at(i)->applyTo(&m); - x *= m.toTransform(0); + x *= m.toTransform(); } x.translate(xOrigin, yOrigin); x.rotate(rotation); diff --git a/src/gui/graphicsview/qgraphicstransform.cpp b/src/gui/graphicsview/qgraphicstransform.cpp index 49d8999..a0b5493 100644 --- a/src/gui/graphicsview/qgraphicstransform.cpp +++ b/src/gui/graphicsview/qgraphicstransform.cpp @@ -549,7 +549,7 @@ void QGraphicsRotation::applyTo(QMatrix4x4 *matrix) const matrix->translate(d->origin); QMatrix4x4 m; m.rotate(d->angle, d->axis.x(), d->axis.y(), d->axis.z()); - *matrix *= m.toTransform(); + *matrix *= m.toTransform(1024.0f); // Project back to 2D. matrix->translate(-d->origin); } diff --git a/src/gui/math3d/qmatrix4x4.cpp b/src/gui/math3d/qmatrix4x4.cpp index ed1b13d..00e8f15 100644 --- a/src/gui/math3d/qmatrix4x4.cpp +++ b/src/gui/math3d/qmatrix4x4.cpp @@ -1430,6 +1430,24 @@ QMatrix QMatrix4x4::toAffine() const m[3][0], m[3][1]); } +/*! + Returns the conventional Qt 2D transformation matrix that + corresponds to this matrix. + + 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 be dropped rather than projected. + + \sa toAffine() +*/ +QTransform QMatrix4x4::toTransform() const +{ + return QTransform(m[0][0], m[0][1], m[0][3], + m[1][0], m[1][1], m[1][3], + m[3][0], m[3][1], m[3][3]); +} + static const qreal inv_dist_to_plane = 1. / 1024.; /*! @@ -1437,8 +1455,8 @@ static const qreal inv_dist_to_plane = 1. / 1024.; 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 default - value of 1024 corresponds to the projection factor used + factor to use to adjust for the z co-ordinate. 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 diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h index b32e00a..42d992e 100644 --- a/src/gui/math3d/qmatrix4x4.h +++ b/src/gui/math3d/qmatrix4x4.h @@ -159,7 +159,8 @@ public: void toValueArray(qreal *values) const; QMatrix toAffine() const; - QTransform toTransform(qreal distanceToPlane = 1024.0f) const; + QTransform toTransform() const; + QTransform toTransform(qreal distanceToPlane) const; QPoint map(const QPoint& point) const; QPointF map(const QPointF& point) const; -- cgit v0.12