| 1 | // Copyright (C) 2017 Paul Lemire <[email protected]> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QTest> |
| 6 | #include <Qt3DAnimation/qadditiveclipblend.h> |
| 7 | #include <Qt3DAnimation/qanimationcliploader.h> |
| 8 | #include <Qt3DAnimation/private/qadditiveclipblend_p.h> |
| 9 | #include <Qt3DAnimation/private/additiveclipblend_p.h> |
| 10 | #include "qbackendnodetester.h" |
| 11 | |
| 12 | using namespace Qt3DAnimation::Animation; |
| 13 | |
| 14 | Q_DECLARE_METATYPE(Handler *) |
| 15 | Q_DECLARE_METATYPE(AdditiveClipBlend *) |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | class TestClipBlendNode : public ClipBlendNode |
| 20 | { |
| 21 | public: |
| 22 | TestClipBlendNode(double duration) |
| 23 | : ClipBlendNode(ClipBlendNode::LerpBlendType) |
| 24 | , m_duration(duration) |
| 25 | {} |
| 26 | |
| 27 | inline QList<Qt3DCore::QNodeId> allDependencyIds() const override |
| 28 | { |
| 29 | return currentDependencyIds(); |
| 30 | } |
| 31 | |
| 32 | QList<Qt3DCore::QNodeId> currentDependencyIds() const final |
| 33 | { |
| 34 | return {}; |
| 35 | } |
| 36 | |
| 37 | using ClipBlendNode::setClipResults; |
| 38 | |
| 39 | double duration() const final { return m_duration; } |
| 40 | |
| 41 | protected: |
| 42 | ClipResults doBlend(const QList<ClipResults> &) const final { return ClipResults(); } |
| 43 | |
| 44 | private: |
| 45 | double m_duration; |
| 46 | }; |
| 47 | |
| 48 | } // anonymous |
| 49 | |
| 50 | class tst_AdditiveClipBlend : public Qt3DCore::QBackendNodeTester |
| 51 | { |
| 52 | Q_OBJECT |
| 53 | public: |
| 54 | TestClipBlendNode *createTestBlendNode(Handler *handler, |
| 55 | double duration) |
| 56 | { |
| 57 | auto id = Qt3DCore::QNodeId::createId(); |
| 58 | TestClipBlendNode *node = new TestClipBlendNode(duration); |
| 59 | setPeerId(node, id); |
| 60 | node->setHandler(handler); |
| 61 | node->setClipBlendNodeManager(handler->clipBlendNodeManager()); |
| 62 | handler->clipBlendNodeManager()->appendNode(id, node); |
| 63 | return node; |
| 64 | } |
| 65 | |
| 66 | AdditiveClipBlend *createAdditiveClipBlendNode(Handler *handler, const float &blendFactor) |
| 67 | { |
| 68 | auto id = Qt3DCore::QNodeId::createId(); |
| 69 | AdditiveClipBlend *node = new AdditiveClipBlend(); |
| 70 | node->setAdditiveFactor(blendFactor); |
| 71 | setPeerId(node, id); |
| 72 | node->setHandler(handler); |
| 73 | node->setClipBlendNodeManager(handler->clipBlendNodeManager()); |
| 74 | handler->clipBlendNodeManager()->appendNode(id, node); |
| 75 | return node; |
| 76 | } |
| 77 | |
| 78 | BlendedClipAnimator *createBlendedClipAnimator(Handler *handler, |
| 79 | qint64 globalStartTimeNS, |
| 80 | int loops) |
| 81 | { |
| 82 | auto animatorId = Qt3DCore::QNodeId::createId(); |
| 83 | BlendedClipAnimator *animator = handler->blendedClipAnimatorManager()->getOrCreateResource(animatorId); |
| 84 | setPeerId(animator, animatorId); |
| 85 | animator->setStartTime(globalStartTimeNS); |
| 86 | animator->setLoops(loops); |
| 87 | return animator; |
| 88 | } |
| 89 | |
| 90 | private Q_SLOTS: |
| 91 | |
| 92 | void checkInitialState() |
| 93 | { |
| 94 | // GIVEN |
| 95 | AdditiveClipBlend backendAdditiveBlend; |
| 96 | |
| 97 | // THEN |
| 98 | QCOMPARE(backendAdditiveBlend.isEnabled(), false); |
| 99 | QVERIFY(backendAdditiveBlend.peerId().isNull()); |
| 100 | QCOMPARE(backendAdditiveBlend.baseClipId(), Qt3DCore::QNodeId()); |
| 101 | QCOMPARE(backendAdditiveBlend.additiveClipId(), Qt3DCore::QNodeId()); |
| 102 | QCOMPARE(backendAdditiveBlend.additiveFactor(), 0.0f); |
| 103 | QCOMPARE(backendAdditiveBlend.blendType(), ClipBlendNode::AdditiveBlendType); |
| 104 | } |
| 105 | |
| 106 | void checkInitializeFromPeer() |
| 107 | { |
| 108 | // GIVEN |
| 109 | Qt3DAnimation::QAdditiveClipBlend additiveBlend; |
| 110 | Qt3DAnimation::QAdditiveClipBlend baseClip; |
| 111 | Qt3DAnimation::QAdditiveClipBlend additiveClip; |
| 112 | Qt3DAnimation::QAnimationClipLoader clip; |
| 113 | additiveBlend.setBaseClip(&baseClip); |
| 114 | additiveBlend.setAdditiveClip(&additiveClip); |
| 115 | additiveBlend.setAdditiveFactor(0.8f); |
| 116 | |
| 117 | { |
| 118 | // WHEN |
| 119 | AdditiveClipBlend backendAdditiveBlend; |
| 120 | simulateInitializationSync(&additiveBlend, &backendAdditiveBlend); |
| 121 | |
| 122 | // THEN |
| 123 | QCOMPARE(backendAdditiveBlend.isEnabled(), true); |
| 124 | QCOMPARE(backendAdditiveBlend.peerId(), additiveBlend.id()); |
| 125 | QCOMPARE(backendAdditiveBlend.baseClipId(), baseClip.id()); |
| 126 | QCOMPARE(backendAdditiveBlend.additiveClipId(), additiveClip.id()); |
| 127 | QCOMPARE(backendAdditiveBlend.additiveFactor(), 0.8f); |
| 128 | } |
| 129 | { |
| 130 | // WHEN |
| 131 | AdditiveClipBlend backendAdditiveBlend; |
| 132 | additiveBlend.setEnabled(false); |
| 133 | simulateInitializationSync(&additiveBlend, &backendAdditiveBlend); |
| 134 | |
| 135 | // THEN |
| 136 | QCOMPARE(backendAdditiveBlend.peerId(), additiveBlend.id()); |
| 137 | QCOMPARE(backendAdditiveBlend.isEnabled(), false); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void checkSceneChangeEvents() |
| 142 | { |
| 143 | // GIVEN |
| 144 | Qt3DAnimation::QAdditiveClipBlend additiveBlend; |
| 145 | AdditiveClipBlend backendAdditiveBlend; |
| 146 | simulateInitializationSync(&additiveBlend, &backendAdditiveBlend); |
| 147 | { |
| 148 | // WHEN |
| 149 | const bool newValue = false; |
| 150 | additiveBlend.setEnabled(newValue); |
| 151 | backendAdditiveBlend.syncFromFrontEnd(&additiveBlend, false); |
| 152 | |
| 153 | // THEN |
| 154 | QCOMPARE(backendAdditiveBlend.isEnabled(), newValue); |
| 155 | } |
| 156 | { |
| 157 | // WHEN |
| 158 | const float newValue = 0.883f; |
| 159 | additiveBlend.setAdditiveFactor(newValue); |
| 160 | backendAdditiveBlend.syncFromFrontEnd(&additiveBlend, false); |
| 161 | |
| 162 | // THEN |
| 163 | QCOMPARE(backendAdditiveBlend.additiveFactor(), newValue); |
| 164 | } |
| 165 | { |
| 166 | // WHEN |
| 167 | Qt3DAnimation::QAdditiveClipBlend newValue; |
| 168 | additiveBlend.setBaseClip(&newValue); |
| 169 | backendAdditiveBlend.syncFromFrontEnd(&additiveBlend, false); |
| 170 | |
| 171 | // THEN |
| 172 | QCOMPARE(backendAdditiveBlend.baseClipId(), newValue.id()); |
| 173 | } |
| 174 | { |
| 175 | // WHEN |
| 176 | Qt3DAnimation::QAdditiveClipBlend newValue; |
| 177 | additiveBlend.setAdditiveClip(&newValue); |
| 178 | backendAdditiveBlend.syncFromFrontEnd(&additiveBlend, false); |
| 179 | |
| 180 | // THEN |
| 181 | QCOMPARE(backendAdditiveBlend.additiveClipId(), newValue.id()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void checkDependencyIds() |
| 186 | { |
| 187 | // GIVEN |
| 188 | AdditiveClipBlend addBlend; |
| 189 | auto baseClipId = Qt3DCore::QNodeId::createId(); |
| 190 | auto additiveClipId = Qt3DCore::QNodeId::createId(); |
| 191 | |
| 192 | // WHEN |
| 193 | addBlend.setBaseClipId(baseClipId); |
| 194 | addBlend.setAdditiveClipId(additiveClipId); |
| 195 | QList<Qt3DCore::QNodeId> actualIds = addBlend.currentDependencyIds(); |
| 196 | |
| 197 | // THEN |
| 198 | QCOMPARE(actualIds.size(), 2); |
| 199 | QCOMPARE(actualIds[0], baseClipId); |
| 200 | QCOMPARE(actualIds[1], additiveClipId); |
| 201 | |
| 202 | // WHEN |
| 203 | auto anotherAdditiveClipId = Qt3DCore::QNodeId::createId(); |
| 204 | addBlend.setAdditiveClipId(anotherAdditiveClipId); |
| 205 | actualIds = addBlend.currentDependencyIds(); |
| 206 | |
| 207 | // THEN |
| 208 | QCOMPARE(actualIds.size(), 2); |
| 209 | QCOMPARE(actualIds[0], baseClipId); |
| 210 | QCOMPARE(actualIds[1], anotherAdditiveClipId); |
| 211 | } |
| 212 | |
| 213 | void checkDuration() |
| 214 | { |
| 215 | // GIVEN |
| 216 | auto handler = new Handler(); |
| 217 | const double expectedDuration = 123.5; |
| 218 | const double baseNodeDuration = expectedDuration; |
| 219 | const double additiveNodeDuration = 5.0; |
| 220 | |
| 221 | auto baseNode = createTestBlendNode(handler, baseNodeDuration); |
| 222 | auto additiveNode = createTestBlendNode(handler, additiveNodeDuration); |
| 223 | |
| 224 | AdditiveClipBlend blendNode; |
| 225 | blendNode.setHandler(handler); |
| 226 | blendNode.setClipBlendNodeManager(handler->clipBlendNodeManager()); |
| 227 | blendNode.setBaseClipId(baseNode->peerId()); |
| 228 | blendNode.setAdditiveClipId(additiveNode->peerId()); |
| 229 | |
| 230 | // WHEN |
| 231 | double actualDuration = blendNode.duration(); |
| 232 | |
| 233 | // THEN |
| 234 | QCOMPARE(actualDuration, expectedDuration); |
| 235 | } |
| 236 | |
| 237 | void checkDoBlend_data() |
| 238 | { |
| 239 | QTest::addColumn<Handler *>("handler"); |
| 240 | QTest::addColumn<AdditiveClipBlend *>("blendNode"); |
| 241 | QTest::addColumn<Qt3DCore::QNodeId>("animatorId"); |
| 242 | QTest::addColumn<ClipResults>("expectedResults"); |
| 243 | |
| 244 | { |
| 245 | auto handler = new Handler(); |
| 246 | |
| 247 | const qint64 globalStartTimeNS = 0; |
| 248 | const int loopCount = 1; |
| 249 | auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount); |
| 250 | |
| 251 | const double duration = 1.0; |
| 252 | auto baseNode = createTestBlendNode(handler, duration); |
| 253 | baseNode->setClipResults(animator->peerId(), { 0.0f, 0.0f, 0.0f }); |
| 254 | auto additiveNode = createTestBlendNode(handler, duration); |
| 255 | additiveNode->setClipResults(animator->peerId(), { 1.0f, 1.0f, 1.0f }); |
| 256 | |
| 257 | const float additiveFactor = 0.0f; |
| 258 | auto blendNode = createAdditiveClipBlendNode(handler, additiveFactor); |
| 259 | blendNode->setBaseClipId(baseNode->peerId()); |
| 260 | blendNode->setAdditiveClipId(additiveNode->peerId()); |
| 261 | blendNode->setAdditiveFactor(additiveFactor); |
| 262 | |
| 263 | ClipResults expectedResults = { 0.0f, 0.0f, 0.0f }; |
| 264 | |
| 265 | QTest::addRow("unit additive, beta = 0.0") |
| 266 | << handler << blendNode << animator->peerId() << expectedResults; |
| 267 | } |
| 268 | |
| 269 | { |
| 270 | auto handler = new Handler(); |
| 271 | |
| 272 | const qint64 globalStartTimeNS = 0; |
| 273 | const int loopCount = 1; |
| 274 | auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount); |
| 275 | |
| 276 | const double duration = 1.0; |
| 277 | auto baseNode = createTestBlendNode(handler, duration); |
| 278 | baseNode->setClipResults(animator->peerId(), { 0.0f, 0.0f, 0.0f }); |
| 279 | auto additiveNode = createTestBlendNode(handler, duration); |
| 280 | additiveNode->setClipResults(animator->peerId(), { 1.0f, 1.0f, 1.0f }); |
| 281 | |
| 282 | const float additiveFactor = 0.5f; |
| 283 | auto blendNode = createAdditiveClipBlendNode(handler, additiveFactor); |
| 284 | blendNode->setBaseClipId(baseNode->peerId()); |
| 285 | blendNode->setAdditiveClipId(additiveNode->peerId()); |
| 286 | blendNode->setAdditiveFactor(additiveFactor); |
| 287 | |
| 288 | ClipResults expectedResults = { 0.5f, 0.5f, 0.5f }; |
| 289 | |
| 290 | QTest::addRow("unit additive, beta = 0.5") |
| 291 | << handler << blendNode << animator->peerId() << expectedResults; |
| 292 | } |
| 293 | |
| 294 | { |
| 295 | auto handler = new Handler(); |
| 296 | |
| 297 | const qint64 globalStartTimeNS = 0; |
| 298 | const int loopCount = 1; |
| 299 | auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount); |
| 300 | |
| 301 | const double duration = 1.0; |
| 302 | auto baseNode = createTestBlendNode(handler, duration); |
| 303 | baseNode->setClipResults(animator->peerId(), { 0.0f, 0.0f, 0.0f }); |
| 304 | auto additiveNode = createTestBlendNode(handler, duration); |
| 305 | additiveNode->setClipResults(animator->peerId(), { 1.0f, 1.0f, 1.0f }); |
| 306 | |
| 307 | const float additiveFactor = 1.0f; |
| 308 | auto blendNode = createAdditiveClipBlendNode(handler, additiveFactor); |
| 309 | blendNode->setBaseClipId(baseNode->peerId()); |
| 310 | blendNode->setAdditiveClipId(additiveNode->peerId()); |
| 311 | blendNode->setAdditiveFactor(additiveFactor); |
| 312 | |
| 313 | ClipResults expectedResults = { 1.0f, 1.0f, 1.0f }; |
| 314 | |
| 315 | QTest::addRow("unit additive, beta = 1.0") |
| 316 | << handler << blendNode << animator->peerId() << expectedResults; |
| 317 | } |
| 318 | |
| 319 | { |
| 320 | auto handler = new Handler(); |
| 321 | |
| 322 | const qint64 globalStartTimeNS = 0; |
| 323 | const int loopCount = 1; |
| 324 | auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount); |
| 325 | |
| 326 | const double duration = 1.0; |
| 327 | auto baseNode = createTestBlendNode(handler, duration); |
| 328 | baseNode->setClipResults(animator->peerId(), { 0.0f, 1.0f, 2.0f }); |
| 329 | auto additiveNode = createTestBlendNode(handler, duration); |
| 330 | additiveNode->setClipResults(animator->peerId(), { 1.0f, 2.0f, 3.0f }); |
| 331 | |
| 332 | const float blendFactor = 0.5f; |
| 333 | auto blendNode = createAdditiveClipBlendNode(handler, blendFactor); |
| 334 | blendNode->setBaseClipId(baseNode->peerId()); |
| 335 | blendNode->setAdditiveClipId(additiveNode->peerId()); |
| 336 | blendNode->setAdditiveFactor(blendFactor); |
| 337 | |
| 338 | ClipResults expectedResults = { 0.5f, 2.0f, 3.5f }; |
| 339 | |
| 340 | QTest::addRow("lerp varying data, beta = 0.5") |
| 341 | << handler << blendNode << animator->peerId() << expectedResults; |
| 342 | } |
| 343 | |
| 344 | { |
| 345 | auto handler = new Handler(); |
| 346 | |
| 347 | const qint64 globalStartTimeNS = 0; |
| 348 | const int loopCount = 1; |
| 349 | auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount); |
| 350 | |
| 351 | const double duration = 1.0; |
| 352 | const int dataCount = 1000; |
| 353 | ClipResults baseData(dataCount); |
| 354 | ClipResults additiveData(dataCount); |
| 355 | ClipResults expectedResults(dataCount); |
| 356 | for (int i = 0; i < dataCount; ++i) { |
| 357 | baseData[i] = float(i); |
| 358 | additiveData[i] = 2.0f * float(i); |
| 359 | expectedResults[i] = 2.0f * float(i); |
| 360 | } |
| 361 | auto baseNode = createTestBlendNode(handler, duration); |
| 362 | baseNode->setClipResults(animator->peerId(), baseData); |
| 363 | auto additiveNode = createTestBlendNode(handler, duration); |
| 364 | additiveNode->setClipResults(animator->peerId(), additiveData); |
| 365 | |
| 366 | const float blendFactor = 0.5f; |
| 367 | auto blendNode = createAdditiveClipBlendNode(handler, blendFactor); |
| 368 | blendNode->setBaseClipId(baseNode->peerId()); |
| 369 | blendNode->setAdditiveClipId(additiveNode->peerId()); |
| 370 | blendNode->setAdditiveFactor(blendFactor); |
| 371 | |
| 372 | QTest::addRow("lerp lots of data, beta = 0.5") |
| 373 | << handler << blendNode << animator->peerId() << expectedResults; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | void checkDoBlend() |
| 378 | { |
| 379 | // GIVEN |
| 380 | QFETCH(Handler *, handler); |
| 381 | QFETCH(AdditiveClipBlend *, blendNode); |
| 382 | QFETCH(Qt3DCore::QNodeId, animatorId); |
| 383 | QFETCH(ClipResults, expectedResults); |
| 384 | |
| 385 | // WHEN |
| 386 | blendNode->blend(animatorId); |
| 387 | |
| 388 | // THEN |
| 389 | const ClipResults actualResults = blendNode->clipResults(animatorId); |
| 390 | QCOMPARE(actualResults.size(), expectedResults.size()); |
| 391 | for (int i = 0; i < actualResults.size(); ++i) |
| 392 | QCOMPARE(actualResults[i], expectedResults[i]); |
| 393 | |
| 394 | // Cleanup |
| 395 | delete handler; |
| 396 | } |
| 397 | }; |
| 398 | |
| 399 | QTEST_MAIN(tst_AdditiveClipBlend) |
| 400 | |
| 401 | #include "tst_additiveclipblend.moc" |