1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
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.
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.
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ** If you have questions regarding the use of this file, please contact
37 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
44 #include <qgraphicsitem.h>
45 #include <qgraphicstransform.h>
46 #include "../../shared/util.h"
48 class tst_QGraphicsTransform : public QObject {
53 void cleanupTestCase();
60 void rotation3d_data();
62 void rotation3dArbitraryAxis_data();
63 void rotation3dArbitraryAxis();
67 // This will be called before the first test function is executed.
68 // It is only called once.
69 void tst_QGraphicsTransform::initTestCase()
73 // This will be called after the last test function is executed.
74 // It is only called once.
75 void tst_QGraphicsTransform::cleanupTestCase()
79 // This will be called before each test function is executed.
80 void tst_QGraphicsTransform::init()
84 // This will be called after every test function.
85 void tst_QGraphicsTransform::cleanup()
89 static QTransform transform2D(const QGraphicsTransform& t)
93 return m.toTransform();
96 void tst_QGraphicsTransform::scale()
100 // check initial conditions
101 QCOMPARE(scale.xScale(), qreal(1));
102 QCOMPARE(scale.yScale(), qreal(1));
103 QCOMPARE(scale.zScale(), qreal(1));
104 QCOMPARE(scale.origin(), QVector3D(0, 0, 0));
106 scale.setOrigin(QVector3D(10, 10, 0));
108 QCOMPARE(scale.xScale(), qreal(1));
109 QCOMPARE(scale.yScale(), qreal(1));
110 QCOMPARE(scale.zScale(), qreal(1));
111 QCOMPARE(scale.origin(), QVector3D(10, 10, 0));
116 QCOMPARE(t, QMatrix4x4());
117 QCOMPARE(transform2D(scale), QTransform());
120 scale.setOrigin(QVector3D(0, 0, 0));
122 QCOMPARE(scale.xScale(), qreal(10));
123 QCOMPARE(scale.yScale(), qreal(1));
124 QCOMPARE(scale.zScale(), qreal(1));
125 QCOMPARE(scale.origin(), QVector3D(0, 0, 0));
130 QCOMPARE(transform2D(scale), res);
131 QCOMPARE(transform2D(scale).map(QPointF(10, 10)), QPointF(100, 10));
133 scale.setOrigin(QVector3D(10, 10, 0));
134 QCOMPARE(transform2D(scale).map(QPointF(10, 10)), QPointF(10, 10));
135 QCOMPARE(transform2D(scale).map(QPointF(11, 10)), QPointF(20, 10));
138 scale.setZScale(4.5);
139 scale.setOrigin(QVector3D(1, 2, 3));
141 QCOMPARE(scale.xScale(), qreal(10));
142 QCOMPARE(scale.yScale(), qreal(2));
143 QCOMPARE(scale.zScale(), qreal(4.5));
144 QCOMPARE(scale.origin(), QVector3D(1, 2, 3));
149 QCOMPARE(t2.map(QVector3D(4, 5, 6)), QVector3D(31, 8, 16.5));
151 // Because the origin has a non-zero z, mapping (4, 5) in 2D
152 // will introduce a projective component into the result.
153 QTransform t3 = t2.toTransform();
154 QCOMPARE(t3.map(QPointF(4, 5)), QPointF(31 / t3.m33(), 8 / t3.m33()));
157 // QMatrix4x4 uses float internally, whereas QTransform uses qreal.
158 // This can lead to issues with qFuzzyCompare() where it uses double
159 // precision to compare values that have no more than float precision
160 // after conversion from QMatrix4x4 to QTransform. The following
161 // definitions correct for the difference.
162 static inline bool fuzzyCompare(qreal p1, qreal p2)
164 // increase delta on small machines using float instead of double
165 if (sizeof(qreal) == sizeof(float))
166 return (qAbs(p1 - p2) <= 0.00003f * qMin(qAbs(p1), qAbs(p2)));
168 return (qAbs(p1 - p2) <= 0.00001f * qMin(qAbs(p1), qAbs(p2)));
171 static bool fuzzyCompare(const QTransform& t1, const QTransform& t2)
173 return fuzzyCompare(t1.m11(), t2.m11()) &&
174 fuzzyCompare(t1.m12(), t2.m12()) &&
175 fuzzyCompare(t1.m13(), t2.m13()) &&
176 fuzzyCompare(t1.m21(), t2.m21()) &&
177 fuzzyCompare(t1.m22(), t2.m22()) &&
178 fuzzyCompare(t1.m23(), t2.m23()) &&
179 fuzzyCompare(t1.m31(), t2.m31()) &&
180 fuzzyCompare(t1.m32(), t2.m32()) &&
181 fuzzyCompare(t1.m33(), t2.m33());
184 static inline bool fuzzyCompare(const QMatrix4x4& m1, const QMatrix4x4& m2)
187 for (int y = 0; y < 4; ++y)
188 for (int x = 0; x < 4; ++x)
189 ok &= fuzzyCompare(m1(y, x), m2(y, x));
193 void tst_QGraphicsTransform::rotation()
195 QGraphicsRotation rotation;
196 QCOMPARE(rotation.axis(), QVector3D(0, 0, 1));
197 QCOMPARE(rotation.origin(), QVector3D(0, 0, 0));
198 QCOMPARE(rotation.angle(), (qreal)0);
200 rotation.setOrigin(QVector3D(10, 10, 0));
202 QCOMPARE(rotation.axis(), QVector3D(0, 0, 1));
203 QCOMPARE(rotation.origin(), QVector3D(10, 10, 0));
204 QCOMPARE(rotation.angle(), (qreal)0);
207 rotation.applyTo(&t);
209 QCOMPARE(t, QMatrix4x4());
210 QCOMPARE(transform2D(rotation), QTransform());
212 rotation.setAngle(40);
213 rotation.setOrigin(QVector3D(0, 0, 0));
215 QCOMPARE(rotation.axis(), QVector3D(0, 0, 1));
216 QCOMPARE(rotation.origin(), QVector3D(0, 0, 0));
217 QCOMPARE(rotation.angle(), (qreal)40);
222 QVERIFY(fuzzyCompare(transform2D(rotation), res));
224 rotation.setOrigin(QVector3D(10, 10, 0));
225 rotation.setAngle(90);
226 QCOMPARE(transform2D(rotation).map(QPointF(10, 10)), QPointF(10, 10));
227 QCOMPARE(transform2D(rotation).map(QPointF(20, 10)), QPointF(10, 20));
230 Q_DECLARE_METATYPE(Qt::Axis);
231 void tst_QGraphicsTransform::rotation3d_data()
233 QTest::addColumn<Qt::Axis>("axis");
234 QTest::addColumn<qreal>("angle");
236 for (int angle = 0; angle <= 360; angle++) {
237 QTest::newRow("test rotation on X") << Qt::XAxis << qreal(angle);
238 QTest::newRow("test rotation on Y") << Qt::YAxis << qreal(angle);
239 QTest::newRow("test rotation on Z") << Qt::ZAxis << qreal(angle);
243 void tst_QGraphicsTransform::rotation3d()
245 QFETCH(Qt::Axis, axis);
246 QFETCH(qreal, angle);
248 QGraphicsRotation rotation;
249 rotation.setAxis(axis);
252 rotation.applyTo(&t);
254 QVERIFY(t.isIdentity());
255 QVERIFY(transform2D(rotation).isIdentity());
257 rotation.setAngle(angle);
259 // QGraphicsRotation uses a correct mathematical rotation in 3D.
260 // QTransform's Qt::YAxis rotation is inverted from the mathematical
261 // version of rotation. We correct for that here.
263 if (axis == Qt::YAxis && angle != 180.)
264 expected.rotate(-angle, axis);
266 expected.rotate(angle, axis);
268 QVERIFY(fuzzyCompare(transform2D(rotation), expected));
270 // Check that "rotation" produces the 4x4 form of the 3x3 matrix.
271 // i.e. third row and column are 0 0 1 0.
273 rotation.applyTo(&t);
274 QMatrix4x4 r(expected);
275 if (sizeof(qreal) == sizeof(float) && angle == 268) {
276 // This test fails, on only this angle, when qreal == float
277 // because the deg2rad value in QTransform is not accurate
278 // enough to match what QMatrix4x4 is doing.
280 QVERIFY(fuzzyCompare(t, r));
283 //now let's check that a null vector will not change the transform
284 rotation.setAxis(QVector3D(0, 0, 0));
285 rotation.setOrigin(QVector3D(10, 10, 0));
288 rotation.applyTo(&t);
290 QVERIFY(t.isIdentity());
291 QVERIFY(transform2D(rotation).isIdentity());
293 rotation.setAngle(angle);
295 QVERIFY(t.isIdentity());
296 QVERIFY(transform2D(rotation).isIdentity());
298 rotation.setOrigin(QVector3D(0, 0, 0));
300 QVERIFY(t.isIdentity());
301 QVERIFY(transform2D(rotation).isIdentity());
304 void tst_QGraphicsTransform::rotation3dArbitraryAxis_data()
306 QTest::addColumn<QVector3D>("axis");
307 QTest::addColumn<qreal>("angle");
309 QVector3D axis1 = QVector3D(1.0f, 1.0f, 1.0f);
310 QVector3D axis2 = QVector3D(2.0f, -3.0f, 0.5f);
311 QVector3D axis3 = QVector3D(-2.0f, 0.0f, -0.5f);
312 QVector3D axis4 = QVector3D(0.0001f, 0.0001f, 0.0001f);
313 QVector3D axis5 = QVector3D(0.01f, 0.01f, 0.01f);
315 for (int angle = 0; angle <= 360; angle++) {
316 QTest::newRow("test rotation on (1, 1, 1)") << axis1 << qreal(angle);
317 QTest::newRow("test rotation on (2, -3, .5)") << axis2 << qreal(angle);
318 QTest::newRow("test rotation on (-2, 0, -.5)") << axis3 << qreal(angle);
319 QTest::newRow("test rotation on (.0001, .0001, .0001)") << axis4 << qreal(angle);
320 QTest::newRow("test rotation on (.01, .01, .01)") << axis5 << qreal(angle);
324 void tst_QGraphicsTransform::rotation3dArbitraryAxis()
326 QFETCH(QVector3D, axis);
327 QFETCH(qreal, angle);
329 QGraphicsRotation rotation;
330 rotation.setAxis(axis);
333 rotation.applyTo(&t);
335 QVERIFY(t.isIdentity());
336 QVERIFY(transform2D(rotation).isIdentity());
338 rotation.setAngle(angle);
340 // Compute the expected answer using QMatrix4x4 and a projection.
341 // These two steps are performed in one hit by QGraphicsRotation.
343 exp.rotate(angle, axis);
344 QTransform expected = exp.toTransform(1024.0f);
346 QVERIFY(fuzzyCompare(transform2D(rotation), expected));
348 // Check that "rotation" produces the 4x4 form of the 3x3 matrix.
349 // i.e. third row and column are 0 0 1 0.
351 rotation.applyTo(&t);
352 QMatrix4x4 r(expected);
353 QVERIFY(qFuzzyCompare(t, r));
357 QTEST_MAIN(tst_QGraphicsTransform)
358 #include "tst_qgraphicstransform.moc"