summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-06-02 02:49:08 (GMT)
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-06-02 02:53:33 (GMT)
commitcef61c5c660410e2db8418c8a23b68d9e523c75d (patch)
tree421128bd0f003ec8ec35f5983c3d777e305c25f7 /tests/auto
parent6e8bafefb9d8484c2fcd0b26c02d1af0e74e25e1 (diff)
downloadQt-cef61c5c660410e2db8418c8a23b68d9e523c75d.zip
Qt-cef61c5c660410e2db8418c8a23b68d9e523c75d.tar.gz
Qt-cef61c5c660410e2db8418c8a23b68d9e523c75d.tar.bz2
Optimize QMatrix4x4::mapRect() for translation and scale operations
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp b/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
index bb510fc..936f9d6 100644
--- a/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
+++ b/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
@@ -167,6 +167,9 @@ private slots:
void fill();
+ void mapRect_data();
+ void mapRect();
+
private:
static void setMatrix(QMatrix2x2& m, const qreal *values);
static void setMatrixFixed(QMatrix2x2& m, const qreal *values);
@@ -3180,6 +3183,117 @@ void tst_QMatrix::fill()
QVERIFY(isSame(m2, fillValues4x3));
}
+// Test the mapRect() function for QRect and QRectF.
+void tst_QMatrix::mapRect_data()
+{
+ QTest::addColumn<qreal>("x");
+ QTest::addColumn<qreal>("y");
+ QTest::addColumn<qreal>("width");
+ QTest::addColumn<qreal>("height");
+
+ QTest::newRow("null")
+ << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
+ QTest::newRow("rect")
+ << (qreal)1.0f << (qreal)-20.5f << (qreal)100.0f << (qreal)63.75f;
+}
+void tst_QMatrix::mapRect()
+{
+ QFETCH(qreal, x);
+ QFETCH(qreal, y);
+ QFETCH(qreal, width);
+ QFETCH(qreal, height);
+
+ QRectF rect(x, y, width, height);
+ QRect recti(qRound(x), qRound(y), qRound(width), qRound(height));
+
+ QMatrix4x4 m1;
+ QVERIFY(m1.mapRect(rect) == rect);
+ QVERIFY(m1.mapRect(recti) == recti);
+
+ QMatrix4x4 m2;
+ m2.translate(-100.5f, 64.0f);
+ QRectF translated = rect.translated(-100.5f, 64.0f);
+ QRect translatedi = QRect(qRound(recti.x() - 100.5f), recti.y() + 64,
+ recti.width(), recti.height());
+ QVERIFY(m2.mapRect(rect) == translated);
+ QVERIFY(m2.mapRect(recti) == translatedi);
+
+ QMatrix4x4 m3;
+ m3.scale(-100.5f, 64.0f);
+ qreal scalex = x * -100.5f;
+ qreal scaley = y * 64.0f;
+ qreal scalewid = width * -100.5f;
+ qreal scaleht = height * 64.0f;
+ if (scalewid < 0.0f) {
+ scalewid = -scalewid;
+ scalex -= scalewid;
+ }
+ if (scaleht < 0.0f) {
+ scaleht = -scaleht;
+ scaley -= scaleht;
+ }
+ QRectF scaled(scalex, scaley, scalewid, scaleht);
+ QVERIFY(m3.mapRect(rect) == scaled);
+ scalex = recti.x() * -100.5f;
+ scaley = recti.y() * 64.0f;
+ scalewid = recti.width() * -100.5f;
+ scaleht = recti.height() * 64.0f;
+ if (scalewid < 0.0f) {
+ scalewid = -scalewid;
+ scalex -= scalewid;
+ }
+ if (scaleht < 0.0f) {
+ scaleht = -scaleht;
+ scaley -= scaleht;
+ }
+ QRect scaledi(qRound(scalex), qRound(scaley),
+ qRound(scalewid), qRound(scaleht));
+ QVERIFY(m3.mapRect(recti) == scaledi);
+
+ QMatrix4x4 m4;
+ m4.translate(-100.5f, 64.0f);
+ m4.scale(-2.5f, 4.0f);
+ qreal transx1 = x * -2.5f - 100.5f;
+ qreal transy1 = y * 4.0f + 64.0f;
+ qreal transx2 = (x + width) * -2.5f - 100.5f;
+ qreal transy2 = (y + height) * 4.0f + 64.0f;
+ if (transx1 > transx2)
+ qSwap(transx1, transx2);
+ if (transy1 > transy2)
+ qSwap(transy1, transy2);
+ QRectF trans(transx1, transy1, transx2 - transx1, transy2 - transy1);
+ QVERIFY(m4.mapRect(rect) == trans);
+ transx1 = recti.x() * -2.5f - 100.5f;
+ transy1 = recti.y() * 4.0f + 64.0f;
+ transx2 = (recti.x() + recti.width()) * -2.5f - 100.5f;
+ transy2 = (recti.y() + recti.height()) * 4.0f + 64.0f;
+ if (transx1 > transx2)
+ qSwap(transx1, transx2);
+ if (transy1 > transy2)
+ qSwap(transy1, transy2);
+ QRect transi(qRound(transx1), qRound(transy1),
+ qRound(transx2) - qRound(transx1),
+ qRound(transy2) - qRound(transy1));
+ QVERIFY(m4.mapRect(recti) == transi);
+
+ m4.rotate(45.0f, 0.0f, 0.0f, 1.0f);
+
+ QTransform t4;
+ t4.translate(-100.5f, 64.0f);
+ t4.scale(-2.5f, 4.0f);
+ t4.rotate(45.0f);
+ QRectF mr = m4.mapRect(rect);
+ QRectF tr = t4.mapRect(rect);
+ QVERIFY(fuzzyCompare(mr.x(), tr.x()));
+ QVERIFY(fuzzyCompare(mr.y(), tr.y()));
+ QVERIFY(fuzzyCompare(mr.width(), tr.width()));
+ QVERIFY(fuzzyCompare(mr.height(), tr.height()));
+
+ QRect mri = m4.mapRect(recti);
+ QRect tri = t4.mapRect(recti);
+ QVERIFY(mri == tri);
+}
+
QTEST_APPLESS_MAIN(tst_QMatrix)
#include "tst_qmatrixnxn.moc"