Debian Codegraph @

tests/auto/core/qtransform/tst_qtransform.cpp

raw symbols SCIP
1 // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/QTest>
5 #include <QtTest/QSignalSpy>
6
7 #include <Qt3DCore/qtransform.h>
8 #include <Qt3DCore/qcomponent.h>
9 #include <Qt3DCore/private/qtransform_p.h>
10
11 #include <QtGui/qquaternion.h>
12
13 #include <QtCore/qscopedpointer.h>
14
15 #include "testarbiter.h"
16
17 using namespace Qt3DCore;
18
19 class FakeTransform : public Qt3DCore::QTransform
20 {
21 public:
22 };
23
24 class tst_QTransform : public QObject
25 {
26 Q_OBJECT
27
28 private Q_SLOTS:
29 void defaultConstruction()
30 {
31 // GIVEN
32 Qt3DCore::QTransform transform;
33
34 // THEN
35 QCOMPARE(transform.isShareable(), false);
36 QCOMPARE(transform.matrix(), QMatrix4x4());
37 QCOMPARE(transform.worldMatrix(), QMatrix4x4());
38 QCOMPARE(transform.scale(), 1.0f);
39 QCOMPARE(transform.scale3D(), QVector3D(1.0f, 1.0f, 1.0f));
40 QCOMPARE(transform.rotation(), QQuaternion());
41 QCOMPARE(transform.rotationX(), 0.0f);
42 QCOMPARE(transform.rotationY(), 0.0f);
43 QCOMPARE(transform.rotationZ(), 0.0f);
44 QCOMPARE(transform.translation(), QVector3D(0.0f, 0.0f, 0.0f));
45 }
46
47 void checkPropertyUpdates()
48 {
49 // GIVEN
50 TestArbiter arbiter;
51 QScopedPointer<Qt3DCore::QTransform> transform(new Qt3DCore::QTransform());
52 arbiter.setArbiterOnNode(transform.data());
53
54 // WHEN
55 transform->setTranslation(QVector3D(454.0f, 427.0f, 383.0f));
56
57 // THEN
58 QCOMPARE(arbiter.dirtyNodes().size(), 1);
59 QCOMPARE(arbiter.dirtyNodes().front(), transform.data());
60
61 arbiter.clear();
62
63 // WHEN
64 QQuaternion q = Qt3DCore::QTransform::fromAxisAndAngle(QVector3D(0.0f, 1.0f, 0.0f), 90.0f);
65 transform->setRotation(q);
66
67 // THEN
68 QCOMPARE(arbiter.dirtyNodes().size(), 1);
69 QCOMPARE(arbiter.dirtyNodes().front(), transform.data());
70
71 arbiter.clear();
72
73 // WHEN
74 transform->setScale3D(QVector3D(883.0f, 1200.0f, 1340.0f));
75 QCoreApplication::processEvents();
76
77 // THEN
78 QCOMPARE(arbiter.dirtyNodes().size(), 1);
79 QCOMPARE(arbiter.dirtyNodes().front(), transform.data());
80
81 arbiter.clear();
82
83 // WHEN
84 // Force the transform to update its matrix
85 (void)transform->matrix();
86
87 transform->setMatrix(QMatrix4x4());
88
89 // THEN
90 QCOMPARE(arbiter.dirtyNodes().size(), 1);
91 QCOMPARE(arbiter.dirtyNodes().front(), transform.data());
92
93 arbiter.clear();
94
95 // WHEN
96 transform->setRotationX(20.0f);
97 QCoreApplication::processEvents();
98
99 // THEN
100 QCOMPARE(arbiter.dirtyNodes().size(), 1);
101 QCOMPARE(arbiter.dirtyNodes().front(), transform.data());
102
103 arbiter.clear();
104 }
105
106 void checkSignalEmittion()
107 {
108 // GIVEN
109 QScopedPointer<Qt3DCore::QTransform> transform(new Qt3DCore::QTransform());
110
111 int rotationXChangedCount = 0;
112 int rotationYChangedCount = 0;
113 int rotationZChangedCount = 0;
114 int rotationChangedCount = 0;
115 int matrixChangedCount = 0;
116 int scaleChangedCount = 0;
117 int scale3DChangedCount = 0;
118 int translationChangedCount = 0;
119
120 QObject::connect(transform.data(), &Qt3DCore::QTransform::rotationChanged, this, [&] { ++rotationChangedCount; });
121 QObject::connect(transform.data(), &Qt3DCore::QTransform::rotationXChanged, this, [&] { ++rotationXChangedCount; });
122 QObject::connect(transform.data(), &Qt3DCore::QTransform::rotationYChanged, this, [&] { ++rotationYChangedCount; });
123 QObject::connect(transform.data(), &Qt3DCore::QTransform::rotationZChanged, this, [&] { ++rotationZChangedCount; });
124 QObject::connect(transform.data(), &Qt3DCore::QTransform::matrixChanged, this, [&] { ++matrixChangedCount; });
125 QObject::connect(transform.data(), &Qt3DCore::QTransform::scale3DChanged, this, [&] { ++scale3DChangedCount; });
126 QObject::connect(transform.data(), &Qt3DCore::QTransform::scaleChanged, this, [&] { ++scaleChangedCount; });
127 QObject::connect(transform.data(), &Qt3DCore::QTransform::translationChanged, this, [&] { ++translationChangedCount; });
128
129 // WHEN
130 transform->setRotationX(180.0f);
131
132 // THEN
133 QCOMPARE(rotationXChangedCount, 1);
134 QCOMPARE(rotationYChangedCount, 0);
135 QCOMPARE(rotationZChangedCount, 0);
136 QCOMPARE(rotationChangedCount, 1);
137 QCOMPARE(matrixChangedCount, 1);
138 QCOMPARE(scaleChangedCount, 0);
139 QCOMPARE(scale3DChangedCount, 0);
140 QCOMPARE(translationChangedCount, 0);
141
142 // WHEN
143 transform->setRotationY(180.0f);
144
145 // THEN
146 QCOMPARE(rotationXChangedCount, 1);
147 QCOMPARE(rotationYChangedCount, 1);
148 QCOMPARE(rotationZChangedCount, 0);
149 QCOMPARE(rotationChangedCount, 2);
150 QCOMPARE(matrixChangedCount, 2);
151 QCOMPARE(scaleChangedCount, 0);
152 QCOMPARE(scale3DChangedCount, 0);
153 QCOMPARE(translationChangedCount, 0);
154
155 // WHEN
156 transform->setRotationZ(180.0f);
157
158 // THEN
159 QCOMPARE(rotationXChangedCount, 1);
160 QCOMPARE(rotationYChangedCount, 1);
161 QCOMPARE(rotationZChangedCount, 1);
162 QCOMPARE(rotationChangedCount, 3);
163 QCOMPARE(matrixChangedCount, 3);
164 QCOMPARE(scaleChangedCount, 0);
165 QCOMPARE(scale3DChangedCount, 0);
166 QCOMPARE(translationChangedCount, 0);
167
168 // WHEN
169 transform->setRotation(Qt3DCore::QTransform::fromEulerAngles(15.0f, 25.0f, 84.0f));
170
171 // THEN
172 QCOMPARE(rotationXChangedCount, 2);
173 QCOMPARE(rotationYChangedCount, 2);
174 QCOMPARE(rotationZChangedCount, 2);
175 QCOMPARE(rotationChangedCount, 4);
176 QCOMPARE(matrixChangedCount, 4);
177 QCOMPARE(scaleChangedCount, 0);
178 QCOMPARE(scale3DChangedCount, 0);
179 QCOMPARE(translationChangedCount, 0);
180
181 // WHEN
182 transform->setMatrix(QMatrix4x4());
183
184 // THEN
185 QCOMPARE(rotationXChangedCount, 3);
186 QCOMPARE(rotationYChangedCount, 3);
187 QCOMPARE(rotationZChangedCount, 3);
188 QCOMPARE(rotationChangedCount, 5);
189 QCOMPARE(matrixChangedCount, 5);
190 QCOMPARE(scaleChangedCount, 1);
191 QCOMPARE(scale3DChangedCount, 1);
192 QCOMPARE(translationChangedCount, 1);
193
194 // WHEN
195 transform->setScale(18.0f);
196
197 // THEN
198 QCOMPARE(rotationXChangedCount, 3);
199 QCOMPARE(rotationYChangedCount, 3);
200 QCOMPARE(rotationZChangedCount, 3);
201 QCOMPARE(rotationChangedCount, 5);
202 QCOMPARE(matrixChangedCount, 6);
203 QCOMPARE(scaleChangedCount, 2);
204 QCOMPARE(scale3DChangedCount, 2);
205 QCOMPARE(translationChangedCount, 1);
206
207 // WHEN
208 transform->setScale3D(QVector3D(15.0f, 18.0f, 15.0f));
209
210 // THEN
211 QCOMPARE(rotationXChangedCount, 3);
212 QCOMPARE(rotationYChangedCount, 3);
213 QCOMPARE(rotationZChangedCount, 3);
214 QCOMPARE(rotationChangedCount, 5);
215 QCOMPARE(matrixChangedCount, 7);
216 QCOMPARE(scaleChangedCount, 2);
217 QCOMPARE(scale3DChangedCount, 3);
218 QCOMPARE(translationChangedCount, 1);
219
220 // WHEN
221 transform->setTranslation(QVector3D(350.0f, 383.0f, 454.0f));
222
223 // THEN
224 QCOMPARE(rotationXChangedCount, 3);
225 QCOMPARE(rotationYChangedCount, 3);
226 QCOMPARE(rotationZChangedCount, 3);
227 QCOMPARE(rotationChangedCount, 5);
228 QCOMPARE(matrixChangedCount, 8);
229 QCOMPARE(scaleChangedCount, 2);
230 QCOMPARE(scale3DChangedCount, 3);
231 QCOMPARE(translationChangedCount, 2);
232 }
233
234 void checkCompositionDecomposition()
235 {
236 // GIVEN
237 Qt3DCore::QTransform t;
238 Qt3DCore::QTransform t2;
239 QMatrix4x4 m = Qt3DCore::QTransform::rotateAround(QVector3D(0.1877f, 0.6868f, 0.3884f), 45.0f, QVector3D(0.0f, 0.0f, 1.0f));
240
241 // WHEN
242 t.setMatrix(m);
243 t2.setScale3D(t.scale3D());
244 t2.setRotation(t.rotation());
245 t2.setTranslation(t.translation());
246
247 // THEN
248 QCOMPARE(t.scale3D(), t2.scale3D());
249 QCOMPARE(t.rotation(), t2.rotation());
250 QCOMPARE(t.translation(), t2.translation());
251
252 // Note: t.matrix() != t2.matrix() since different matrices
253 // can result in the same scale, rotation, translation
254 }
255
256 void checkUpdateWorldTransform()
257 {
258 // GIVEN
259 TestArbiter arbiter;
260 FakeTransform t;
261 arbiter.setArbiterOnNode(&t);
262
263 // WHEN
264 QSignalSpy spy(&t, SIGNAL(worldMatrixChanged(QMatrix4x4)));
265
266 // THEN
267 QVERIFY(spy.isValid());
268
269 // WHEN
270 QTransformPrivate *dT = static_cast<QTransformPrivate *>(QNodePrivate::get(&t));
271 const QMatrix4x4 newValue(1.0f, 2.0f, 3.0f, 4.0f,
272 5.0f, 6.0f, 7.0f, 8.0f,
273 9.0f, 10.0f, 11.0f, 12.0f,
274 13.0f, 14.0f, 15.0f, 16.0f);
275 dT->setWorldMatrix(newValue);
276
277
278 // THEN
279 QCOMPARE(spy.size(), 1);
280 QCOMPARE(arbiter.dirtyNodes().size(), 0);
281 QCOMPARE(t.worldMatrix(), newValue);
282
283 // WHEN
284 spy.clear();
285 dT->setWorldMatrix(newValue);
286
287 // THEN
288 QCOMPARE(spy.size(), 0);
289 QCOMPARE(arbiter.dirtyNodes().size(), 0);
290 QCOMPARE(t.worldMatrix(), newValue);
291 }
292 };
293
294 QTEST_MAIN(tst_QTransform)
295
296 #include "tst_qtransform.moc"