tests/auto/qgraphicstransform/tst_qgraphicstransform.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <qgraphicsitem.h>
       
    45 #include <qgraphicstransform.h>
       
    46 #include "../../shared/util.h"
       
    47 
       
    48 class tst_QGraphicsTransform : public QObject {
       
    49     Q_OBJECT
       
    50 
       
    51 public slots:
       
    52     void initTestCase();
       
    53     void cleanupTestCase();
       
    54     void init();
       
    55     void cleanup();
       
    56 
       
    57 private slots:
       
    58     void scale();
       
    59     void rotation();
       
    60     void rotation3d_data();
       
    61     void rotation3d();
       
    62 };
       
    63 
       
    64 
       
    65 // This will be called before the first test function is executed.
       
    66 // It is only called once.
       
    67 void tst_QGraphicsTransform::initTestCase()
       
    68 {
       
    69 }
       
    70 
       
    71 // This will be called after the last test function is executed.
       
    72 // It is only called once.
       
    73 void tst_QGraphicsTransform::cleanupTestCase()
       
    74 {
       
    75 }
       
    76 
       
    77 // This will be called before each test function is executed.
       
    78 void tst_QGraphicsTransform::init()
       
    79 {
       
    80 }
       
    81 
       
    82 // This will be called after every test function.
       
    83 void tst_QGraphicsTransform::cleanup()
       
    84 {
       
    85 }
       
    86 
       
    87 static QTransform transform2D(const QGraphicsTransform& t)
       
    88 {
       
    89     QMatrix4x4 m;
       
    90     t.applyTo(&m);
       
    91     return m.toTransform(0);
       
    92 }
       
    93 
       
    94 void tst_QGraphicsTransform::scale()
       
    95 {
       
    96     QGraphicsScale scale;
       
    97 
       
    98     // check initial conditions
       
    99     QCOMPARE(scale.xScale(), qreal(1));
       
   100     QCOMPARE(scale.yScale(), qreal(1));
       
   101     QCOMPARE(scale.zScale(), qreal(1));
       
   102     QCOMPARE(scale.origin(), QVector3D(0, 0, 0));
       
   103 
       
   104     scale.setOrigin(QVector3D(10, 10, 0));
       
   105 
       
   106     QCOMPARE(scale.xScale(), qreal(1));
       
   107     QCOMPARE(scale.yScale(), qreal(1));
       
   108     QCOMPARE(scale.zScale(), qreal(1));
       
   109     QCOMPARE(scale.origin(), QVector3D(10, 10, 0));
       
   110 
       
   111     QMatrix4x4 t;
       
   112     scale.applyTo(&t);
       
   113 
       
   114     QCOMPARE(t, QMatrix4x4());
       
   115     QCOMPARE(transform2D(scale), QTransform());
       
   116 
       
   117     scale.setXScale(10);
       
   118     scale.setOrigin(QVector3D(0, 0, 0));
       
   119 
       
   120     QCOMPARE(scale.xScale(), qreal(10));
       
   121     QCOMPARE(scale.yScale(), qreal(1));
       
   122     QCOMPARE(scale.zScale(), qreal(1));
       
   123     QCOMPARE(scale.origin(), QVector3D(0, 0, 0));
       
   124 
       
   125     QTransform res;
       
   126     res.scale(10, 1);
       
   127 
       
   128     QCOMPARE(transform2D(scale), res);
       
   129     QCOMPARE(transform2D(scale).map(QPointF(10, 10)), QPointF(100, 10));
       
   130 
       
   131     scale.setOrigin(QVector3D(10, 10, 0));
       
   132     QCOMPARE(transform2D(scale).map(QPointF(10, 10)), QPointF(10, 10));
       
   133     QCOMPARE(transform2D(scale).map(QPointF(11, 10)), QPointF(20, 10));
       
   134 
       
   135     scale.setYScale(2);
       
   136     scale.setZScale(4.5);
       
   137     scale.setOrigin(QVector3D(1, 2, 3));
       
   138 
       
   139     QCOMPARE(scale.xScale(), qreal(10));
       
   140     QCOMPARE(scale.yScale(), qreal(2));
       
   141     QCOMPARE(scale.zScale(), qreal(4.5));
       
   142     QCOMPARE(scale.origin(), QVector3D(1, 2, 3));
       
   143 
       
   144     QMatrix4x4 t2;
       
   145     scale.applyTo(&t2);
       
   146 
       
   147     QCOMPARE(t2.map(QVector3D(4, 5, 6)), QVector3D(31, 8, 16.5));
       
   148 
       
   149     // Because the origin has a non-zero z, mapping (4, 5) in 2D
       
   150     // will introduce a projective component into the result.
       
   151     QTransform t3 = t2.toTransform();
       
   152     QCOMPARE(t3.map(QPointF(4, 5)), QPointF(31 / t3.m33(), 8 / t3.m33()));
       
   153 }
       
   154 
       
   155 // QMatrix4x4 uses float internally, whereas QTransform uses qreal.
       
   156 // This can lead to issues with qFuzzyCompare() where it uses double
       
   157 // precision to compare values that have no more than float precision
       
   158 // after conversion from QMatrix4x4 to QTransform.  The following
       
   159 // definitions correct for the difference.
       
   160 static inline bool fuzzyCompare(qreal p1, qreal p2)
       
   161 {
       
   162     // increase delta on small machines using float instead of double
       
   163     if (sizeof(qreal) == sizeof(float))
       
   164         return (qAbs(p1 - p2) <= 0.00002f * qMin(qAbs(p1), qAbs(p2)));
       
   165     else
       
   166         return (qAbs(p1 - p2) <= 0.00001f * qMin(qAbs(p1), qAbs(p2)));
       
   167 }
       
   168 static bool fuzzyCompare(const QTransform& t1, const QTransform& t2)
       
   169 {
       
   170     return fuzzyCompare(t1.m11(), t2.m11()) &&
       
   171            fuzzyCompare(t1.m12(), t2.m12()) &&
       
   172            fuzzyCompare(t1.m13(), t2.m13()) &&
       
   173            fuzzyCompare(t1.m21(), t2.m21()) &&
       
   174            fuzzyCompare(t1.m22(), t2.m22()) &&
       
   175            fuzzyCompare(t1.m23(), t2.m23()) &&
       
   176            fuzzyCompare(t1.m31(), t2.m31()) &&
       
   177            fuzzyCompare(t1.m32(), t2.m32()) &&
       
   178            fuzzyCompare(t1.m33(), t2.m33());
       
   179 }
       
   180 
       
   181 void tst_QGraphicsTransform::rotation()
       
   182 {
       
   183     QGraphicsRotation rotation;
       
   184     QCOMPARE(rotation.axis(), QVector3D(0, 0, 1));
       
   185     QCOMPARE(rotation.origin(), QVector3D(0, 0, 0));
       
   186     QCOMPARE(rotation.angle(), (qreal)0);
       
   187 
       
   188     rotation.setOrigin(QVector3D(10, 10, 0));
       
   189 
       
   190     QCOMPARE(rotation.axis(), QVector3D(0, 0, 1));
       
   191     QCOMPARE(rotation.origin(), QVector3D(10, 10, 0));
       
   192     QCOMPARE(rotation.angle(), (qreal)0);
       
   193 
       
   194     QMatrix4x4 t;
       
   195     rotation.applyTo(&t);
       
   196 
       
   197     QCOMPARE(t, QMatrix4x4());
       
   198     QCOMPARE(transform2D(rotation), QTransform());
       
   199 
       
   200     rotation.setAngle(40);
       
   201     rotation.setOrigin(QVector3D(0, 0, 0));
       
   202 
       
   203     QCOMPARE(rotation.axis(), QVector3D(0, 0, 1));
       
   204     QCOMPARE(rotation.origin(), QVector3D(0, 0, 0));
       
   205     QCOMPARE(rotation.angle(), (qreal)40);
       
   206 
       
   207     QTransform res;
       
   208     res.rotate(40);
       
   209 
       
   210     QVERIFY(fuzzyCompare(transform2D(rotation), res));
       
   211 
       
   212     rotation.setOrigin(QVector3D(10, 10, 0));
       
   213     rotation.setAngle(90);
       
   214     QCOMPARE(transform2D(rotation).map(QPointF(10, 10)), QPointF(10, 10));
       
   215     QCOMPARE(transform2D(rotation).map(QPointF(20, 10)), QPointF(10, 20));
       
   216 }
       
   217 
       
   218 Q_DECLARE_METATYPE(Qt::Axis);
       
   219 void tst_QGraphicsTransform::rotation3d_data()
       
   220 {
       
   221     QTest::addColumn<Qt::Axis>("axis");
       
   222     QTest::addColumn<qreal>("angle");
       
   223 
       
   224     for (int angle = 0; angle <= 360; angle++) {
       
   225         QTest::newRow("test rotation on X") << Qt::XAxis << qreal(angle);
       
   226         QTest::newRow("test rotation on Y") << Qt::YAxis << qreal(angle);
       
   227         QTest::newRow("test rotation on Z") << Qt::ZAxis << qreal(angle);
       
   228     }
       
   229 }
       
   230 
       
   231 void tst_QGraphicsTransform::rotation3d()
       
   232 {
       
   233     QFETCH(Qt::Axis, axis);
       
   234     QFETCH(qreal, angle);
       
   235 
       
   236     QGraphicsRotation rotation;
       
   237     rotation.setAxis(axis);
       
   238 
       
   239     QMatrix4x4 t;
       
   240     rotation.applyTo(&t);
       
   241 
       
   242     QVERIFY(t.isIdentity());
       
   243     QVERIFY(transform2D(rotation).isIdentity());
       
   244 
       
   245     rotation.setAngle(angle);
       
   246 
       
   247     // QGraphicsRotation uses a correct mathematical rotation in 3D.
       
   248     // QTransform's Qt::YAxis rotation is inverted from the mathematical
       
   249     // version of rotation.  We correct for that here.
       
   250     QTransform expected;
       
   251     if (axis == Qt::YAxis && angle != 180.)
       
   252         expected.rotate(-angle, axis);
       
   253     else
       
   254         expected.rotate(angle, axis);
       
   255 
       
   256     QVERIFY(fuzzyCompare(transform2D(rotation), expected));
       
   257 
       
   258     //now let's check that a null vector will not change the transform
       
   259     rotation.setAxis(QVector3D(0, 0, 0));
       
   260     rotation.setOrigin(QVector3D(10, 10, 0));
       
   261 
       
   262     t.setIdentity();
       
   263     rotation.applyTo(&t);
       
   264 
       
   265     QVERIFY(t.isIdentity());
       
   266     QVERIFY(transform2D(rotation).isIdentity());
       
   267 
       
   268     rotation.setAngle(angle);
       
   269 
       
   270     QVERIFY(t.isIdentity());
       
   271     QVERIFY(transform2D(rotation).isIdentity());
       
   272 
       
   273     rotation.setOrigin(QVector3D(0, 0, 0));
       
   274 
       
   275     QVERIFY(t.isIdentity());
       
   276     QVERIFY(transform2D(rotation).isIdentity());
       
   277 }
       
   278 
       
   279 
       
   280 QTEST_MAIN(tst_QGraphicsTransform)
       
   281 #include "tst_qgraphicstransform.moc"
       
   282