Debian Codegraph @

tests/auto/animation/animationutils/tst_animationutils.cpp

raw symbols SCIP
1 // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/QTest>
5 #include <Qt3DAnimation/private/animationclip_p.h>
6 #include <Qt3DAnimation/private/animationutils_p.h>
7 #include <Qt3DAnimation/private/blendedclipanimator_p.h>
8 #include <Qt3DAnimation/private/clock_p.h>
9 #include <Qt3DAnimation/private/channelmapper_p.h>
10 #include <Qt3DAnimation/private/channelmapping_p.h>
11 #include <Qt3DAnimation/private/clipblendvalue_p.h>
12 #include <Qt3DAnimation/private/handler_p.h>
13 #include <Qt3DAnimation/private/additiveclipblend_p.h>
14 #include <Qt3DAnimation/private/lerpclipblend_p.h>
15 #include <Qt3DAnimation/private/managers_p.h>
16 #include <QtGui/qvector2d.h>
17 #include <QtGui/qvector3d.h>
18 #include <QtGui/qvector4d.h>
19 #include <QtGui/qquaternion.h>
20 #include <QtGui/qcolor.h>
21 #include <QtCore/qbitarray.h>
22
23 #include <qbackendnodetester.h>
24 #include <testarbiter.h>
25
26 using namespace Qt3DAnimation::Animation;
27
28 Q_DECLARE_METATYPE(Qt3DAnimation::Animation::Handler*)
29 Q_DECLARE_METATYPE(QVector<ChannelMapping *>)
30 Q_DECLARE_METATYPE(Clock *)
31 Q_DECLARE_METATYPE(ChannelMapper *)
32 Q_DECLARE_METATYPE(AnimationClip *)
33 Q_DECLARE_METATYPE(QVector<MappingData>)
34 Q_DECLARE_METATYPE(Channel)
35 Q_DECLARE_METATYPE(AnimatorEvaluationData)
36 Q_DECLARE_METATYPE(ClipEvaluationData)
37 Q_DECLARE_METATYPE(ClipAnimator *)
38 Q_DECLARE_METATYPE(BlendedClipAnimator *)
39 Q_DECLARE_METATYPE(QVector<ChannelNameAndType>)
40 Q_DECLARE_METATYPE(QVector<AnimationCallbackAndValue>)
41 Q_DECLARE_METATYPE(ClipFormat)
42 Q_DECLARE_METATYPE(ChannelNameAndType)
43
44 namespace {
45
46 class MeanBlendNode : public ClipBlendNode
47 {
48 public:
49 MeanBlendNode()
50 : ClipBlendNode(ClipBlendNode::LerpBlendType)
51 {}
52
53 void setValueNodeIds(Qt3DCore::QNodeId value1Id,
54 Qt3DCore::QNodeId value2Id)
55 {
56 m_value1Id = value1Id;
57 m_value2Id = value2Id;
58 }
59
60 inline QList<Qt3DCore::QNodeId> allDependencyIds() const override
61 {
62 return currentDependencyIds();
63 }
64
65 QList<Qt3DCore::QNodeId> currentDependencyIds() const final
66 {
67 return { m_value1Id, m_value2Id };
68 }
69
70 using ClipBlendNode::setClipResults;
71
72 double duration() const final { return 0.0f; }
73
74 protected:
75 ClipResults doBlend(const QList<ClipResults> &blendData) const final
76 {
77 Q_ASSERT(blendData.size() == 2);
78 const qsizetype elementCount = blendData.first().size();
79 ClipResults blendResults(elementCount);
80
81 for (qsizetype i = 0; i < elementCount; ++i)
82 blendResults[i] = 0.5f * (blendData[0][i] + blendData[1][i]);
83
84 return blendResults;
85 }
86
87 private:
88 Qt3DCore::QNodeId m_value1Id;
89 Qt3DCore::QNodeId m_value2Id;
90 };
91
92 bool fuzzyCompare(float x1, float x2)
93 {
94 if (qFuzzyIsNull(x1) && qFuzzyIsNull(x2)) {
95 return true;
96 } else if ((qFuzzyIsNull(x1) && !qFuzzyIsNull(x2)) ||
97 (!qFuzzyIsNull(x1) && qFuzzyIsNull(x2))) {
98 return false;
99 } else {
100 return qFuzzyCompare(x1, x2);
101 }
102 }
103
104 class DummyCallback : public Qt3DAnimation::QAnimationCallback
105 {
106 public:
107 void valueChanged(const QVariant &) override { }
108 };
109
110 } // anonymous
111
112
113 class tst_AnimationUtils : public Qt3DCore::QBackendNodeTester
114 {
115 Q_OBJECT
116
117 public:
118 ChannelMapping *createChannelMapping(Handler *handler,
119 const QString &channelName,
120 const Qt3DCore::QNodeId targetId,
121 const char *propertyName,
122 int type,
123 int componentCount)
124 {
125 auto channelMappingId = Qt3DCore::QNodeId::createId();
126 ChannelMapping *channelMapping = handler->channelMappingManager()->getOrCreateResource(channelMappingId);
127 setPeerId(channelMapping, channelMappingId);
128 channelMapping->setHandler(handler);
129 channelMapping->setTargetId(targetId);
130 channelMapping->setPropertyName(propertyName);
131 channelMapping->setChannelName(channelName);
132 channelMapping->setType(type);
133 channelMapping->setMappingType(ChannelMapping::ChannelMappingType);
134 channelMapping->setComponentCount(componentCount);
135 return channelMapping;
136 }
137
138 ChannelMapping *createChannelMapping(Handler *handler,
139 const Qt3DCore::QNodeId skeletonId)
140 {
141 auto channelMappingId = Qt3DCore::QNodeId::createId();
142 ChannelMapping *channelMapping = handler->channelMappingManager()->getOrCreateResource(channelMappingId);
143 setPeerId(channelMapping, channelMappingId);
144 channelMapping->setHandler(handler);
145 channelMapping->setSkeletonId(skeletonId);
146 channelMapping->setMappingType(ChannelMapping::SkeletonMappingType);
147 return channelMapping;
148 }
149
150 ChannelMapper *createChannelMapper(Handler *handler,
151 const QList<Qt3DCore::QNodeId> &mappingIds)
152 {
153 auto channelMapperId = Qt3DCore::QNodeId::createId();
154 ChannelMapper *channelMapper = handler->channelMapperManager()->getOrCreateResource(channelMapperId);
155 setPeerId(channelMapper, channelMapperId);
156 channelMapper->setMappingIds(mappingIds);
157 return channelMapper;
158 }
159
160 AnimationClip *createAnimationClipLoader(Handler *handler,
161 const QUrl &source)
162 {
163 auto clipId = Qt3DCore::QNodeId::createId();
164 AnimationClip *clip = handler->animationClipLoaderManager()->getOrCreateResource(clipId);
165 setPeerId(clip, clipId);
166 clip->setDataType(AnimationClip::File);
167 clip->setSource(source);
168 clip->loadAnimation();
169 return clip;
170 }
171
172 ClipAnimator *createClipAnimator(Handler *handler,
173 qint64 globalStartTimeNS,
174 int loops)
175 {
176 auto animatorId = Qt3DCore::QNodeId::createId();
177 ClipAnimator *animator = handler->clipAnimatorManager()->getOrCreateResource(animatorId);
178 setPeerId(animator, animatorId);
179 animator->setStartTime(globalStartTimeNS);
180 animator->setLoops(loops);
181 return animator;
182 }
183
184 BlendedClipAnimator *createBlendedClipAnimator(Handler *handler,
185 qint64 globalStartTimeNS,
186 int loops)
187 {
188 auto animatorId = Qt3DCore::QNodeId::createId();
189 BlendedClipAnimator *animator = handler->blendedClipAnimatorManager()->getOrCreateResource(animatorId);
190 setPeerId(animator, animatorId);
191 animator->setStartTime(globalStartTimeNS);
192 animator->setLoops(loops);
193 return animator;
194 }
195
196 LerpClipBlend *createLerpClipBlend(Handler *handler)
197 {
198 auto lerpId = Qt3DCore::QNodeId::createId();
199 LerpClipBlend *lerp = new LerpClipBlend();
200 setPeerId(lerp, lerpId);
201 lerp->setClipBlendNodeManager(handler->clipBlendNodeManager());
202 lerp->setHandler(handler);
203 handler->clipBlendNodeManager()->appendNode(lerpId, lerp);
204 return lerp;
205 }
206
207 AdditiveClipBlend *createAdditiveClipBlend(Handler *handler)
208 {
209 auto additiveId = Qt3DCore::QNodeId::createId();
210 AdditiveClipBlend *additive = new AdditiveClipBlend();
211 setPeerId(additive, additiveId);
212 additive->setClipBlendNodeManager(handler->clipBlendNodeManager());
213 additive->setHandler(handler);
214 handler->clipBlendNodeManager()->appendNode(additiveId, additive);
215 return additive;
216 }
217
218 ClipBlendValue *createClipBlendValue(Handler *handler)
219 {
220 auto valueId = Qt3DCore::QNodeId::createId();
221 ClipBlendValue *value = new ClipBlendValue();
222 setPeerId(value, valueId);
223 value->setClipBlendNodeManager(handler->clipBlendNodeManager());
224 value->setHandler(handler);
225 handler->clipBlendNodeManager()->appendNode(valueId, value);
226 return value;
227 }
228
229 MeanBlendNode *createMeanBlendNode(Handler *handler)
230 {
231 auto id = Qt3DCore::QNodeId::createId();
232 MeanBlendNode *node = new MeanBlendNode();
233 setPeerId(node, id);
234 node->setClipBlendNodeManager(handler->clipBlendNodeManager());
235 node->setHandler(handler);
236 handler->clipBlendNodeManager()->appendNode(id, node);
237 return node;
238 }
239
240 Skeleton *createSkeleton(Handler *handler, int jointCount)
241 {
242 auto skeletonId = Qt3DCore::QNodeId::createId();
243 Skeleton *skeleton = handler->skeletonManager()->getOrCreateResource(skeletonId);
244 setPeerId(skeleton, skeletonId);
245 skeleton->setJointCount(jointCount);
246 return skeleton;
247 }
248
249 private Q_SLOTS:
250 void checkBuildPropertyMappings_data()
251 {
252 QTest::addColumn<Handler *>("handler");
253 QTest::addColumn<QVector<ChannelMapping *>>("channelMappings");
254 QTest::addColumn<QVector<ChannelNameAndType>>("channelNamesAndTypes");
255 QTest::addColumn<QVector<ComponentIndices>>("channelComponentIndices");
256 QTest::addColumn<QVector<QBitArray>>("sourceClipMask");
257 QTest::addColumn<QVector<MappingData>>("expectedResults");
258
259 // Single ChannelMapping
260 {
261 Handler *handler = new Handler();
262
263 auto channelMapping = createChannelMapping(handler,
264 QLatin1String("Location"),
265 Qt3DCore::QNodeId::createId(),
266 "translation",
267 static_cast<int>(QMetaType::QVector3D),
268 3);
269
270 const QVector<ChannelMapping *> channelMappings = { channelMapping };
271
272 // Create a few channels in the format description
273 const ChannelNameAndType rotation = { QLatin1String("Rotation"),
274 static_cast<int>(QMetaType::QQuaternion),
275 4,
276 channelMapping->peerId() };
277 const ChannelNameAndType location = { QLatin1String("Location"),
278 static_cast<int>(QMetaType::QVector3D),
279 3,
280 channelMapping->peerId() };
281 const ChannelNameAndType baseColor = { QLatin1String("BaseColor"),
282 static_cast<int>(QMetaType::QVector3D),
283 3,
284 channelMapping->peerId() };
285 const ChannelNameAndType metalness = { QLatin1String("Metalness"),
286 static_cast<int>(QMetaType::Double),
287 1,
288 channelMapping->peerId() };
289 const ChannelNameAndType roughness = { QLatin1String("Roughness"),
290 static_cast<int>(QMetaType::Double),
291 1,
292 channelMapping->peerId() };
293 const ChannelNameAndType morphTargetWeightsList = { QLatin1String("MorphTargetWeightsList"),
294 static_cast<int>(QMetaType::QVariantList),
295 5,
296 channelMapping->peerId() };
297 const ChannelNameAndType morphTargetWeightsVec = { QLatin1String("MorphTargetWeightsVec"),
298 qMetaTypeId<QVector<float>>(),
299 6,
300 channelMapping->peerId() };
301 const ChannelNameAndType rgbColor = { QLatin1String("rgbColor"),
302 static_cast<int>(QMetaType::QColor),
303 3,
304 channelMapping->peerId() };
305 const ChannelNameAndType rgbaColor = { QLatin1String("rgbaColor"),
306 static_cast<int>(QMetaType::QColor),
307 4,
308 channelMapping->peerId() };
309
310 const QVector<ChannelNameAndType> channelNamesAndTypes
311 = { rotation, location, baseColor, metalness, roughness,
312 morphTargetWeightsList, morphTargetWeightsVec, rgbColor, rgbaColor };
313
314 // And the matching indices
315 const ComponentIndices rotationIndices = { 0, 1, 2, 3 };
316 const ComponentIndices locationIndices = { 4, 5, 6 };
317 const ComponentIndices baseColorIndices = { 7, 8, 9 };
318 const ComponentIndices metalnessIndices = { 10 };
319 const ComponentIndices roughnessIndices = { 11 };
320 const ComponentIndices morphTargetListIndices = { 12, 13, 14, 15, 16 };
321 const ComponentIndices morphTargetVecIndices = { 17, 18, 19, 20, 21, 22 };
322 const ComponentIndices rgbColorIndices = { 23, 24, 25 };
323 const ComponentIndices rgbaColorIndices = { 26, 27, 28, 29 };
324 const QVector<ComponentIndices> channelComponentIndices
325 = { rotationIndices, locationIndices, baseColorIndices,
326 metalnessIndices, roughnessIndices, morphTargetListIndices,
327 morphTargetVecIndices, rgbColorIndices, rgbaColorIndices };
328
329 const QVector<QBitArray> sourceClipMask = { QBitArray(4, true),
330 QBitArray(3, true),
331 QBitArray(3, true),
332 QBitArray(1, true),
333 QBitArray(1, true),
334 QBitArray(5, true),
335 QBitArray(6, true),
336 QBitArray(3, true),
337 QBitArray(4, true) };
338
339 MappingData expectedMapping;
340 expectedMapping.targetId = channelMapping->targetId();
341 expectedMapping.propertyName = channelMapping->propertyName();
342 expectedMapping.type = channelMapping->type();
343 expectedMapping.channelIndices = locationIndices;
344 const QVector<MappingData> expectedResults = { expectedMapping };
345
346 QTest::newRow("single mapping")
347 << handler
348 << channelMappings
349 << channelNamesAndTypes
350 << channelComponentIndices
351 << sourceClipMask
352 << expectedResults;
353 }
354
355 // Multiple ChannelMappings
356 {
357 Handler *handler = new Handler();
358
359 auto locationMapping = createChannelMapping(handler,
360 QLatin1String("Location"),
361 Qt3DCore::QNodeId::createId(),
362 "translation",
363 static_cast<int>(QMetaType::QVector3D),
364 3);
365
366 auto metalnessMapping = createChannelMapping(handler,
367 QLatin1String("Metalness"),
368 Qt3DCore::QNodeId::createId(),
369 "metalness",
370 static_cast<int>(QMetaType::Double),
371 1);
372
373 auto baseColorMapping = createChannelMapping(handler,
374 QLatin1String("BaseColor"),
375 Qt3DCore::QNodeId::createId(),
376 "baseColor",
377 static_cast<int>(QMetaType::QVector3D),
378 3);
379
380 auto roughnessMapping = createChannelMapping(handler,
381 QLatin1String("Roughness"),
382 Qt3DCore::QNodeId::createId(),
383 "roughness",
384 static_cast<int>(QMetaType::Double),
385 1);
386
387 auto rotationMapping = createChannelMapping(handler,
388 QLatin1String("Rotation"),
389 Qt3DCore::QNodeId::createId(),
390 "rotation",
391 static_cast<int>(QMetaType::QQuaternion),
392 4);
393
394 auto morphTargetMapping = createChannelMapping(handler,
395 QLatin1String("MorphTargetWeights"),
396 Qt3DCore::QNodeId::createId(),
397 "weights",
398 static_cast<int>(QMetaType::QVariantList),
399 5);
400
401
402 const QVector<ChannelMapping *> channelMappings
403 = { locationMapping, metalnessMapping,
404 baseColorMapping, roughnessMapping,
405 rotationMapping, morphTargetMapping };
406
407 // Create a few channels in the format description
408 const ChannelNameAndType rotation = { QLatin1String("Rotation"),
409 static_cast<int>(QMetaType::QQuaternion),
410 4,
411 rotationMapping->peerId() };
412 const ChannelNameAndType location = { QLatin1String("Location"),
413 static_cast<int>(QMetaType::QVector3D),
414 3,
415 locationMapping->peerId() };
416 const ChannelNameAndType baseColor = { QLatin1String("BaseColor"),
417 static_cast<int>(QMetaType::QVector3D),
418 3,
419 baseColorMapping->peerId() };
420 const ChannelNameAndType metalness = { QLatin1String("Metalness"),
421 static_cast<int>(QMetaType::Double),
422 1,
423 metalnessMapping->peerId() };
424 const ChannelNameAndType roughness = { QLatin1String("Roughness"),
425 static_cast<int>(QMetaType::Double),
426 1,
427 roughnessMapping->peerId() };
428 const ChannelNameAndType morphTarget = { QLatin1String("MorphTargetWeights"),
429 static_cast<int>(QMetaType::QVariantList),
430 5,
431 morphTargetMapping->peerId() };
432 const QVector<ChannelNameAndType> channelNamesAndTypes
433 = { rotation, location, baseColor, metalness, roughness,
434 morphTarget };
435
436 // And the matching indices
437 const ComponentIndices rotationIndices = { 0, 1, 2, 3 };
438 const ComponentIndices locationIndices = { 4, 5, 6 };
439 const ComponentIndices baseColorIndices = { 7, 8, 9 };
440 const ComponentIndices metalnessIndices = { 10 };
441 const ComponentIndices roughnessIndices = { 11 };
442 const ComponentIndices morphTargetIndices = { 12, 13, 14, 15, 16 };
443 const QVector<ComponentIndices> channelComponentIndices
444 = { rotationIndices, locationIndices, baseColorIndices,
445 metalnessIndices, roughnessIndices, morphTargetIndices };
446
447 const QVector<QBitArray> sourceClipMask = { QBitArray(4, true),
448 QBitArray(3, true),
449 QBitArray(3, true),
450 QBitArray(1, true),
451 QBitArray(1, true),
452 QBitArray(5, true) };
453
454 MappingData expectedLocationMapping;
455 expectedLocationMapping.targetId = locationMapping->targetId();
456 expectedLocationMapping.propertyName = locationMapping->propertyName();
457 expectedLocationMapping.type = locationMapping->type();
458 expectedLocationMapping.channelIndices = locationIndices;
459
460 MappingData expectedMetalnessMapping;
461 expectedMetalnessMapping.targetId = metalnessMapping->targetId();
462 expectedMetalnessMapping.propertyName = metalnessMapping->propertyName();
463 expectedMetalnessMapping.type = metalnessMapping->type();
464 expectedMetalnessMapping.channelIndices = metalnessIndices;
465
466 MappingData expectedBaseColorMapping;
467 expectedBaseColorMapping.targetId = baseColorMapping->targetId();
468 expectedBaseColorMapping.propertyName = baseColorMapping->propertyName();
469 expectedBaseColorMapping.type = baseColorMapping->type();
470 expectedBaseColorMapping.channelIndices = baseColorIndices;
471
472 MappingData expectedRoughnessMapping;
473 expectedRoughnessMapping.targetId = roughnessMapping->targetId();
474 expectedRoughnessMapping.propertyName = roughnessMapping->propertyName();
475 expectedRoughnessMapping.type = roughnessMapping->type();
476 expectedRoughnessMapping.channelIndices = roughnessIndices;
477
478 MappingData expectedRotationMapping;
479 expectedRotationMapping.targetId = rotationMapping->targetId();
480 expectedRotationMapping.propertyName = rotationMapping->propertyName();
481 expectedRotationMapping.type = rotationMapping->type();
482 expectedRotationMapping.channelIndices = rotationIndices;
483
484 MappingData expectedMorphTargetMapping;
485 expectedMorphTargetMapping.targetId = morphTargetMapping->targetId();
486 expectedMorphTargetMapping.propertyName = morphTargetMapping->propertyName();
487 expectedMorphTargetMapping.type = morphTargetMapping->type();
488 expectedMorphTargetMapping.channelIndices = morphTargetIndices;
489
490 const QVector<MappingData> expectedResults
491 = { expectedLocationMapping,
492 expectedMetalnessMapping,
493 expectedBaseColorMapping,
494 expectedRoughnessMapping,
495 expectedRotationMapping,
496 expectedMorphTargetMapping };
497
498 QTest::newRow("multiple mappings")
499 << handler
500 << channelMappings
501 << channelNamesAndTypes
502 << channelComponentIndices
503 << sourceClipMask
504 << expectedResults;
505 }
506
507 // Single skeleton mapping
508 {
509 Handler *handler = new Handler();
510 const qsizetype jointCount = 4;
511 auto skeleton = createSkeleton(handler, jointCount);
512 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
513
514 const QVector<ChannelMapping *> channelMappings = { channelMapping };
515
516 // Create a few channels in the format description
517 QVector<ChannelNameAndType> channelNamesAndTypes;
518 for (qsizetype i = 0; i < jointCount; ++i) {
519 ChannelNameAndType locationDescription = { QLatin1String("Location"),
520 static_cast<int>(QMetaType::QVector3D),
521 3,
522 channelMapping->peerId() };
523 locationDescription.jointIndex = i;
524 locationDescription.jointTransformComponent = Translation;
525 channelNamesAndTypes.push_back(locationDescription);
526
527 ChannelNameAndType rotationDescription = { QLatin1String("Rotation"),
528 static_cast<int>(QMetaType::QQuaternion),
529 4,
530 channelMapping->peerId() };
531 rotationDescription.jointIndex = i;
532 rotationDescription.jointTransformComponent = Rotation;
533 channelNamesAndTypes.push_back(rotationDescription);
534
535 ChannelNameAndType scaleDescription = { QLatin1String("Scale"),
536 static_cast<int>(QMetaType::QVector3D),
537 3,
538 channelMapping->peerId() };
539 scaleDescription.jointIndex = i;
540 scaleDescription.jointTransformComponent = Scale;
541 channelNamesAndTypes.push_back(scaleDescription);
542 }
543
544 // And the matching indices
545 const QVector<ComponentIndices> channelComponentIndices = {
546 { 0, 1, 2 }, { 3, 4, 5, 6 }, { 7, 8, 9 },
547 { 10, 11, 12 }, { 13, 14, 15, 16 }, { 17, 18, 19 },
548 { 20, 21, 22 }, { 23, 24, 25, 26 }, { 27, 28, 29 },
549 { 30, 31, 32 }, { 33, 34, 35, 36 }, { 37, 38, 39 }
550 };
551
552 const QVector<QBitArray> sourceClipMask = { QBitArray(3, true),
553 QBitArray(4, true),
554 QBitArray(3, true),
555 QBitArray(3, true),
556 QBitArray(4, true),
557 QBitArray(3, true),
558 QBitArray(3, true),
559 QBitArray(4, true),
560 QBitArray(3, true),
561 QBitArray(3, true),
562 QBitArray(4, true),
563 QBitArray(3, true) };
564
565 QVector<MappingData> expectedResults;
566 int componentIndicesIndex = 0;
567 for (qsizetype i = 0; i < jointCount; ++i) {
568 MappingData locationMapping;
569 locationMapping.targetId = channelMapping->skeletonId();
570 locationMapping.propertyName = "translation";
571 locationMapping.type = static_cast<int>(QMetaType::QVector3D);
572 locationMapping.channelIndices = channelComponentIndices[componentIndicesIndex++];
573 locationMapping.jointIndex = i;
574
575 MappingData rotationMapping;
576 rotationMapping.targetId = channelMapping->skeletonId();
577 rotationMapping.propertyName = "rotation";
578 rotationMapping.type = static_cast<int>(QMetaType::QQuaternion);
579 rotationMapping.channelIndices = channelComponentIndices[componentIndicesIndex++];
580 rotationMapping.jointIndex = i;
581
582 MappingData scaleMapping;
583 scaleMapping.targetId = channelMapping->skeletonId();
584 scaleMapping.propertyName = "scale";
585 scaleMapping.type = static_cast<int>(QMetaType::QVector3D);
586 scaleMapping.channelIndices = channelComponentIndices[componentIndicesIndex++];
587 scaleMapping.jointIndex = i;
588
589 expectedResults << locationMapping << rotationMapping << scaleMapping;
590 }
591
592 QTest::newRow("single skeleton mapping")
593 << handler
594 << channelMappings
595 << channelNamesAndTypes
596 << channelComponentIndices
597 << sourceClipMask
598 << expectedResults;
599 }
600 }
601
602 void checkBuildPropertyMappings()
603 {
604 // GIVEN
605 QFETCH(Handler *, handler);
606 QFETCH(QVector<ChannelMapping *>, channelMappings);
607 QFETCH(QVector<ChannelNameAndType>, channelNamesAndTypes);
608 QFETCH(QVector<ComponentIndices>, channelComponentIndices);
609 QFETCH(QVector<QBitArray>, sourceClipMask);
610 QFETCH(QVector<MappingData>, expectedResults);
611
612 // WHEN
613 const auto actualResults = buildPropertyMappings(channelMappings,
614 channelNamesAndTypes,
615 channelComponentIndices,
616 sourceClipMask);
617
618 // THEN
619 QCOMPARE(actualResults.size(), expectedResults.size());
620 for (qsizetype i = 0; i < actualResults.size(); ++i) {
621 const auto actualMapping = actualResults[i];
622 const auto expectedMapping = expectedResults[i];
623
624 QCOMPARE(actualMapping.targetId, expectedMapping.targetId);
625 QCOMPARE(actualMapping.jointIndex, expectedMapping.jointIndex);
626 QCOMPARE(actualMapping.propertyName, expectedMapping.propertyName);
627 QCOMPARE(actualMapping.type, expectedMapping.type);
628 QCOMPARE(actualMapping.channelIndices.size(), expectedMapping.channelIndices.size());
629 for (int j = 0; j < actualMapping.channelIndices.size(); ++j) {
630 QCOMPARE(actualMapping.channelIndices[j], expectedMapping.channelIndices[j]);
631 }
632 }
633
634 // Cleanup
635 delete handler;
636 }
637
638 void checkLocalTimeFromElapsedTime_data()
639 {
640 QTest::addColumn<double>("elapsedTime");
641 QTest::addColumn<double>("currentTime");
642 QTest::addColumn<double>("playbackRate");
643 QTest::addColumn<double>("duration");
644 QTest::addColumn<int>("loopCount");
645 QTest::addColumn<int>("currentLoop");
646 QTest::addColumn<double>("expectedLocalTime");
647 QTest::addColumn<int>("expectedCurrentLoop");
648
649 double elapsedTime;
650 double currentTime;
651 double playbackRate;
652 double duration;
653 int loopCount;
654 int currentLoop;
655 double expectedLocalTime;
656 int expectedCurrentLoop;
657
658 elapsedTime = 0.0;
659 currentTime = 0.0;
660 playbackRate = 1.0;
661 duration = 1.0;
662 loopCount = 1;
663 currentLoop = 0;
664 expectedLocalTime = 0.0;
665 expectedCurrentLoop = 0;
666 QTest::newRow("simple, t_current = 0, t_elapsed = 0, loop_current = 0")
667 << elapsedTime << currentTime << playbackRate << duration << loopCount << currentLoop
668 << expectedLocalTime << expectedCurrentLoop;
669
670 elapsedTime = 0.5;
671 currentTime = 0.0;
672 playbackRate = 1.0;
673 duration = 1.0;
674 loopCount = 1;
675 currentLoop = 0;
676 expectedLocalTime = 0.5;
677 expectedCurrentLoop = 0;
678 QTest::newRow("simple, t_current = 0, t_elapsed = 0.5, loop_current = 0")
679 << elapsedTime << currentTime << playbackRate << duration << loopCount << currentLoop
680 << expectedLocalTime << expectedCurrentLoop;
681
682 elapsedTime = 1.5;
683 currentTime = 0.0;
684 playbackRate = 1.0;
685 duration = 1.0;
686 loopCount = 1;
687 currentLoop = 0;
688 expectedLocalTime = 1.0;
689 expectedCurrentLoop = 0;
690 QTest::newRow("simple, t_current = 0, t_elapsed = 1.5, loop_current = 0")
691 << elapsedTime << currentTime << playbackRate << duration << loopCount << currentLoop
692 << expectedLocalTime << expectedCurrentLoop;
693
694 elapsedTime = 0.5;
695 currentTime = 0.6;
696 playbackRate = 1.0;
697 duration = 1.0;
698 loopCount = 1;
699 currentLoop = 0;
700 expectedLocalTime = 1.0;
701 expectedCurrentLoop = 0;
702 QTest::newRow("simple, t_current = 0.5, t_elapsed = 0.6, loop_current = 0")
703 << elapsedTime << currentTime << playbackRate << duration << loopCount << currentLoop
704 << expectedLocalTime << expectedCurrentLoop;
705
706 elapsedTime = 0.5;
707 currentTime = 0.6;
708 playbackRate = 1.0;
709 duration = 1.0;
710 loopCount = 2;
711 currentLoop = 0;
712 expectedLocalTime = 0.1;
713 expectedCurrentLoop = 1;
714 QTest::newRow("simple, t_current = 0.5, t_elapsed = 0.6, loop_current = 0, loop_count = 2")
715 << elapsedTime << currentTime << playbackRate << duration << loopCount << currentLoop
716 << expectedLocalTime << expectedCurrentLoop;
717
718 elapsedTime = 0.5;
719 currentTime = 0.6;
720 playbackRate = 1.0;
721 duration = 1.0;
722 loopCount = 2;
723 currentLoop = 1;
724 expectedLocalTime = 1.0;
725 expectedCurrentLoop = 1; // We clamp at end of final loop
726 QTest::newRow("simple, t_current = 0.5, t_elapsed = 0.6, loop_current = 1, loop_count = 2")
727 << elapsedTime << currentTime << playbackRate << duration << loopCount << currentLoop
728 << expectedLocalTime << expectedCurrentLoop;
729
730 elapsedTime = 0.5;
731 currentTime = 0.6;
732 playbackRate = 0.1;
733 duration = 1.0;
734 loopCount = 2;
735 currentLoop = 1;
736 expectedLocalTime = 0.65;
737 expectedCurrentLoop = 1;
738 QTest::newRow("simple, t_current = 0.5, t_elapsed = 0.6, loop_current = 1, loop_count = 2")
739 << elapsedTime << currentTime << playbackRate << duration << loopCount << currentLoop
740 << expectedLocalTime << expectedCurrentLoop;
741 }
742
743 void checkLocalTimeFromElapsedTime()
744 {
745 // GIVEN
746 QFETCH(double, elapsedTime);
747 QFETCH(double, currentTime);
748 QFETCH(double, playbackRate);
749 QFETCH(double, duration);
750 QFETCH(int, loopCount);
751 QFETCH(int, currentLoop);
752 QFETCH(double, expectedLocalTime);
753 QFETCH(int, expectedCurrentLoop);
754
755 // WHEN
756 int actualCurrentLoop = currentLoop;
757 double actualLocalTime = localTimeFromElapsedTime(currentTime,
758 elapsedTime,
759 playbackRate,
760 duration,
761 loopCount,
762 actualCurrentLoop);
763
764 // THEN
765 QCOMPARE(actualCurrentLoop, expectedCurrentLoop);
766 QCOMPARE(actualLocalTime, expectedLocalTime);
767 }
768
769 void checkPreparePropertyChanges_data()
770 {
771 QTest::addColumn<Qt3DCore::QNodeId>("animatorId");
772 QTest::addColumn<QVector<MappingData>>("mappingData");
773 QTest::addColumn<QVector<float>>("channelResults");
774 QTest::addColumn<AnimationRecord>("expectedChanges");
775
776 Qt3DCore::QNodeId animatorId;
777 QVector<MappingData> mappingData;
778 QVector<float> channelResults;
779 AnimationRecord expectedChanges;
780
781 // Single property, vec3
782 {
783 animatorId = Qt3DCore::QNodeId::createId();
784 MappingData mapping;
785 mapping.targetId = Qt3DCore::QNodeId::createId();
786 mapping.propertyName = "translation";
787 mapping.type = static_cast<int>(QMetaType::QVector3D);
788 mapping.channelIndices = QVector<qsizetype>{ 0, 1, 2 };
789 mappingData.push_back(mapping);
790 channelResults = QVector<float> { 1.0f, 2.0f, 3.0f };
791 expectedChanges.normalizedTime = 1.1f; // Invalid
792 expectedChanges.finalFrame = false;
793 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(QVector3D(1.0f, 2.0f, 3.0f))});
794
795 QTest::newRow("vec3 translation, final = false")
796 << animatorId << mappingData << channelResults << expectedChanges;
797
798 expectedChanges.normalizedTime = 1.0f;
799 expectedChanges.finalFrame = true;
800
801 QTest::newRow("vec3 translation, final = true, normalizedTime = 1.0f")
802 << animatorId << mappingData << channelResults << expectedChanges;
803
804 mappingData.clear();
805 channelResults.clear();
806 expectedChanges.targetChanges.clear();
807 }
808
809 // Multiple properties, all vec3
810 {
811 animatorId = Qt3DCore::QNodeId::createId();
812 MappingData translationMapping;
813 translationMapping.targetId = Qt3DCore::QNodeId::createId();
814 translationMapping.propertyName = "translation";
815 translationMapping.type = static_cast<int>(QMetaType::QVector3D);
816 translationMapping.channelIndices = QVector<qsizetype>{ 0, 1, 2 };
817 mappingData.push_back(translationMapping);
818
819 MappingData scaleMapping;
820 scaleMapping.targetId = Qt3DCore::QNodeId::createId();
821 scaleMapping.propertyName = "scale";
822 scaleMapping.type = static_cast<int>(QMetaType::QVector3D);
823 scaleMapping.channelIndices = QVector<qsizetype>{ 3, 4, 5 };
824 mappingData.push_back(scaleMapping);
825
826 channelResults = QVector<float> { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
827 expectedChanges.finalFrame = false;
828 expectedChanges.normalizedTime = -0.1f; // Invalid
829
830 expectedChanges.targetChanges.push_back({translationMapping.targetId, translationMapping.propertyName, QVariant::fromValue(QVector3D(1.0f, 2.0f, 3.0f))});
831 expectedChanges.targetChanges.push_back({scaleMapping.targetId, scaleMapping.propertyName, QVariant::fromValue(QVector3D(4.0f, 5.0f, 6.0f))});
832
833 QTest::newRow("vec3 translation, vec3 scale, final = false")
834 << animatorId << mappingData << channelResults << expectedChanges;
835
836 expectedChanges.normalizedTime = 0.5f;
837 expectedChanges.finalFrame = true;
838
839 QTest::newRow("vec3 translation, vec3 scale, final = true")
840 << animatorId << mappingData << channelResults << expectedChanges;
841
842 mappingData.clear();
843 channelResults.clear();
844 expectedChanges.targetChanges.clear();
845 }
846
847 // Single property, double
848 {
849 animatorId = Qt3DCore::QNodeId::createId();
850 MappingData mapping;
851 mapping.targetId = Qt3DCore::QNodeId::createId();
852 mapping.propertyName = "mass";
853 mapping.type = static_cast<int>(QMetaType::Double);
854 mapping.channelIndices = QVector<qsizetype>{ 0 };
855 mappingData.push_back(mapping);
856 channelResults = QVector<float> { 3.5f };
857 expectedChanges.finalFrame = false;
858 expectedChanges.normalizedTime = -1.0f; // Invalid
859 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(3.5f)});
860
861 QTest::newRow("double mass")
862 << animatorId << mappingData << channelResults << expectedChanges;
863
864 mappingData.clear();
865 channelResults.clear();
866 expectedChanges.targetChanges.clear();
867 }
868
869 // Single property, vec2
870 {
871 animatorId = Qt3DCore::QNodeId::createId();
872 MappingData mapping;
873 mapping.targetId = Qt3DCore::QNodeId::createId();
874 mapping.propertyName = "pos";
875 mapping.type = static_cast<int>(QMetaType::QVector2D);
876 mapping.channelIndices = QVector<qsizetype>{ 0, 1 };
877 mappingData.push_back(mapping);
878 channelResults = QVector<float> { 2.0f, 1.0f };
879 expectedChanges.finalFrame = false;
880 expectedChanges.normalizedTime = 1.1f; // Invalid
881 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(QVector2D(2.0f, 1.0f))});
882
883 QTest::newRow("vec2 pos")
884 << animatorId << mappingData << channelResults << expectedChanges;
885
886 mappingData.clear();
887 channelResults.clear();
888 expectedChanges.targetChanges.clear();
889 }
890
891 // Single property, vec4
892 {
893 animatorId = Qt3DCore::QNodeId::createId();
894 MappingData mapping;
895 mapping.targetId = Qt3DCore::QNodeId::createId();
896 mapping.propertyName = "foo";
897 mapping.type = static_cast<int>(QMetaType::QVector4D);
898 mapping.channelIndices = QVector<qsizetype>{ 0, 1, 2, 3 };
899 mappingData.push_back(mapping);
900 channelResults = QVector<float> { 4.0f, 3.0f, 2.0f, 1.0f };
901 expectedChanges.finalFrame = false;
902 expectedChanges.normalizedTime = 1.1f; // Invalid
903 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(QVector4D(4.0f, 3.0f, 2.0f, 1.0f))});
904
905 QTest::newRow("vec4 foo")
906 << animatorId << mappingData << channelResults << expectedChanges;
907
908 mappingData.clear();
909 channelResults.clear();
910 expectedChanges.targetChanges.clear();
911 }
912
913 // Single property, quaternion
914 {
915 animatorId = Qt3DCore::QNodeId::createId();
916 MappingData mapping;
917 mapping.targetId = Qt3DCore::QNodeId::createId();
918 mapping.propertyName = "rotation";
919 mapping.type = static_cast<int>(QMetaType::QQuaternion);
920 mapping.channelIndices = QVector<qsizetype>{ 0, 1, 2, 3 };
921 mappingData.push_back(mapping);
922 channelResults = QVector<float> { 1.0f, 0.0f, 0.0f, 1.0f };
923 expectedChanges.finalFrame = false;
924 expectedChanges.normalizedTime = -0.1f; // Invalid
925 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(QQuaternion(1.0f, 0.0f, 0.0f, 1.0f).normalized())});
926
927 QTest::newRow("quaternion rotation")
928 << animatorId << mappingData << channelResults << expectedChanges;
929
930 mappingData.clear();
931 channelResults.clear();
932 expectedChanges.targetChanges.clear();
933 }
934
935 // Single property, QColor
936 {
937 animatorId = Qt3DCore::QNodeId::createId();
938 MappingData mapping;
939 mapping.targetId = Qt3DCore::QNodeId::createId();
940 mapping.propertyName = "color";
941 mapping.type = static_cast<int>(QMetaType::QColor);
942 mapping.channelIndices = QVector<qsizetype>{ 0, 1, 2 };
943 mappingData.push_back(mapping);
944 channelResults = QVector<float> { 0.5f, 0.4f, 0.3f };
945 expectedChanges.finalFrame = false;
946 expectedChanges.normalizedTime = 1.1f; // Invalid
947 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(QColor::fromRgbF(0.5f, 0.4f, 0.3f))});
948
949 QTest::newRow("QColor rgb color")
950 << animatorId << mappingData << channelResults << expectedChanges;
951
952 mappingData.clear();
953 channelResults.clear();
954 expectedChanges.targetChanges.clear();
955 }
956
957 // Single property, QColor
958 {
959 animatorId = Qt3DCore::QNodeId::createId();
960 MappingData mapping;
961 mapping.targetId = Qt3DCore::QNodeId::createId();
962 mapping.propertyName = "color";
963 mapping.type = static_cast<int>(QMetaType::QColor);
964 mapping.channelIndices = QVector<qsizetype>{ 0, 1, 2, 3 };
965 mappingData.push_back(mapping);
966 channelResults = QVector<float> { 0.5f, 0.4f, 0.3f, 0.2f };
967 expectedChanges.finalFrame = false;
968 expectedChanges.normalizedTime = 1.1f; // Invalid
969 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(QColor::fromRgbF(0.5f, 0.4f, 0.3f, 0.2f))});
970
971 QTest::newRow("QColor rgba color")
972 << animatorId << mappingData << channelResults << expectedChanges;
973
974 mappingData.clear();
975 channelResults.clear();
976 expectedChanges.targetChanges.clear();
977 }
978
979 // Single property, QVariantList
980 {
981 animatorId = Qt3DCore::QNodeId::createId();
982 MappingData mapping;
983 mapping.targetId = Qt3DCore::QNodeId::createId();
984 mapping.propertyName = "weights";
985 mapping.type = static_cast<int>(QMetaType::QVariantList);
986 mapping.channelIndices = QVector<qsizetype>{ 0, 1, 2, 3, 4, 5, 6 };
987 mappingData.push_back(mapping);
988 channelResults = QVector<float> { 0.5f, 0.4f, 0.3f, 0.0f, 1.0f, 0.6f, 0.9f };
989 expectedChanges.finalFrame = false;
990 expectedChanges.normalizedTime = 1.1f; // Invalid
991 QVariantList expectedValue = QVariantList { 0.5f, 0.4f, 0.3f, 0.0f, 1.0f, 0.6f, 0.9f };
992 expectedChanges.targetChanges.push_back({mapping.targetId, mapping.propertyName, QVariant::fromValue(expectedValue)});
993
994 QTest::newRow("QVariantList weights")
995 << animatorId << mappingData << channelResults << expectedChanges;
996
997 mappingData.clear();
998 channelResults.clear();
999 expectedChanges.targetChanges.clear();
1000 }
1001
1002 }
1003
1004 void checkPreparePropertyChanges()
1005 {
1006 // GIVEN
1007 QFETCH(Qt3DCore::QNodeId, animatorId);
1008 QFETCH(QVector<MappingData>, mappingData);
1009 QFETCH(QVector<float>, channelResults);
1010 QFETCH(AnimationRecord, expectedChanges);
1011
1012 // WHEN
1013 AnimationRecord actualChanges = prepareAnimationRecord(animatorId, mappingData, channelResults,
1014 expectedChanges.finalFrame, expectedChanges.normalizedTime);
1015
1016 // THEN
1017 QCOMPARE(actualChanges.targetChanges.size(), expectedChanges.targetChanges.size());
1018 for (int i = 0; i < actualChanges.targetChanges.size(); ++i) {
1019 const auto &expectedChange = expectedChanges.targetChanges[i];
1020 const auto &actualChange = actualChanges.targetChanges[i];
1021
1022 QCOMPARE(actualChange.targetId, expectedChange.targetId);
1023 QCOMPARE(actualChange.propertyName, expectedChange.propertyName);
1024 QCOMPARE(actualChange.value, expectedChange.value);
1025 }
1026 }
1027
1028 void checkPrepareCallbacks_data()
1029 {
1030 QTest::addColumn<QVector<MappingData>>("mappingData");
1031 QTest::addColumn<QVector<float>>("channelResults");
1032 QTest::addColumn<QVector<AnimationCallbackAndValue> >("expectedValues");
1033
1034 QVector<MappingData> mappingData;
1035 QVector<float> channelResults;
1036 QVector<AnimationCallbackAndValue> expectedValues;
1037
1038 // vec3
1039 {
1040 DummyCallback callback; // safe since the object is never used, just the address
1041 MappingData mapping;
1042 mapping.targetId = Qt3DCore::QNodeId::createId();
1043 mapping.propertyName = "translation";
1044 mapping.type = static_cast<int>(QMetaType::QVector3D);
1045 mapping.channelIndices = QVector<qsizetype>{ 0, 1, 2 };
1046 mapping.callback = &callback;
1047 mapping.callbackFlags = {};
1048 mappingData.push_back(mapping);
1049 channelResults = QVector<float> { 1.0f, 2.0f, 3.0f };
1050
1051 AnimationCallbackAndValue cbv;
1052 cbv.callback = mapping.callback;
1053 cbv.flags = mapping.callbackFlags;
1054 cbv.value = QVariant::fromValue<QVector3D>(QVector3D(1.0f, 2.0f, 3.0f));
1055 expectedValues.push_back(cbv);
1056
1057 QTest::newRow("vec3 translation, no flags") << mappingData << channelResults << expectedValues;
1058
1059 mappingData.clear();
1060 channelResults.clear();
1061 expectedValues.clear();
1062 }
1063
1064 // double
1065 {
1066 DummyCallback callback;
1067 MappingData mapping;
1068 mapping.targetId = Qt3DCore::QNodeId::createId();
1069 mapping.propertyName = "something";
1070 mapping.type = static_cast<int>(QMetaType::Double);
1071 mapping.channelIndices = QVector<qsizetype>{ 0 };
1072 mapping.callback = &callback;
1073 mapping.callbackFlags = {};
1074 mappingData.push_back(mapping);
1075 channelResults = QVector<float> { 1.0f };
1076
1077 AnimationCallbackAndValue cbv;
1078 cbv.callback = mapping.callback;
1079 cbv.flags = mapping.callbackFlags;
1080 cbv.value = QVariant(double(1.0));
1081 expectedValues.push_back(cbv);
1082
1083 QTest::newRow("double, no flags") << mappingData << channelResults << expectedValues;
1084
1085 mappingData.clear();
1086 channelResults.clear();
1087 expectedValues.clear();
1088 }
1089
1090 // float, set a flag
1091 {
1092 DummyCallback callback;
1093 MappingData mapping;
1094 mapping.targetId = Qt3DCore::QNodeId::createId();
1095 mapping.propertyName = "opacity";
1096 mapping.type = static_cast<int>(QMetaType::Float);
1097 mapping.channelIndices = QVector<qsizetype>{ 0 };
1098 mapping.callback = &callback;
1099 mapping.callbackFlags = Qt3DAnimation::QAnimationCallback::OnThreadPool;
1100 mappingData.push_back(mapping);
1101 channelResults = QVector<float> { 0.5f };
1102
1103 AnimationCallbackAndValue cbv;
1104 cbv.callback = mapping.callback;
1105 cbv.flags = mapping.callbackFlags;
1106 cbv.value = QVariant(float(0.5f));
1107 expectedValues.push_back(cbv);
1108
1109 QTest::newRow("float, OnThreadPool") << mappingData << channelResults << expectedValues;
1110
1111 mappingData.clear();
1112 channelResults.clear();
1113 expectedValues.clear();
1114 }
1115 }
1116
1117 void checkPrepareCallbacks()
1118 {
1119 // GIVEN
1120 QFETCH(QVector<MappingData>, mappingData);
1121 QFETCH(QVector<float>, channelResults);
1122 QFETCH(QVector<AnimationCallbackAndValue>, expectedValues);
1123
1124 // WHEN
1125 QVector<AnimationCallbackAndValue> callbacks = prepareCallbacks(mappingData, channelResults);
1126
1127 // THEN
1128 QCOMPARE(callbacks.size(), expectedValues.size());
1129 for (int i = 0; i < callbacks.size(); ++i) {
1130 auto expected = expectedValues[i];
1131 auto actual = callbacks[i];
1132
1133 QCOMPARE(actual.callback, expected.callback);
1134 QCOMPARE(actual.flags, expected.flags);
1135 QCOMPARE(actual.value, expected.value);
1136 }
1137 }
1138
1139 void checkEvaluateClipAtLocalTime_data()
1140 {
1141 QTest::addColumn<Handler *>("handler");
1142 QTest::addColumn<AnimationClip *>("clip");
1143 QTest::addColumn<float>("localTime");
1144 QTest::addColumn<ClipResults>("expectedResults");
1145
1146 Handler *handler;
1147 AnimationClip *clip;
1148 float localTime;
1149 ClipResults expectedResults;
1150
1151 {
1152 handler = new Handler();
1153 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1154 localTime = 0.0f;
1155 expectedResults = QVector<float> { 0.0f, 0.0f, 0.0f };
1156
1157 QTest::newRow("clip1.json, t = 0.0")
1158 << handler << clip << localTime << expectedResults;
1159 expectedResults.clear();
1160 }
1161
1162 {
1163 handler = new Handler();
1164 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1165 localTime = clip->duration();
1166 expectedResults = QVector<float> { 5.0f, 0.0f, 0.0f };
1167
1168 QTest::newRow("clip1.json, t = duration")
1169 << handler << clip << localTime << expectedResults;
1170 expectedResults.clear();
1171 }
1172
1173 {
1174 handler = new Handler();
1175 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1176 localTime = clip->duration() / 2.0f;
1177 expectedResults = QVector<float> { 2.5f, 0.0f, 0.0f };
1178
1179 QTest::newRow("clip1.json, t = duration/2")
1180 << handler << clip << localTime << expectedResults;
1181 expectedResults.clear();
1182 }
1183
1184 {
1185 handler = new Handler();
1186 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip2.json"));
1187 localTime = 0.0f;
1188 expectedResults = QVector<float> { 0.0f, 0.0f, 0.0f, // Translation
1189 1.0f, 0.0f, 0.0f, 0.0f }; // Rotation
1190
1191 QTest::newRow("clip2.json, t = 0.0")
1192 << handler << clip << localTime << expectedResults;
1193 expectedResults.clear();
1194 }
1195 {
1196 handler = new Handler();
1197 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip2.json"));
1198 localTime = clip->duration();
1199 expectedResults = QVector<float> { 5.0f, 0.0f, 0.0f, // Translation
1200 0.0f, 0.0f, -1.0f, 0.0f }; // Rotation
1201
1202 QTest::newRow("clip2.json, t = duration")
1203 << handler << clip << localTime << expectedResults;
1204 expectedResults.clear();
1205 }
1206 {
1207 handler = new Handler();
1208 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip2.json"));
1209 localTime = clip->duration() / 2.0f;
1210 expectedResults = QVector<float> { 2.5f, 0.0f, 0.0f, // Translation
1211 0.5f, 0.0f, -0.5f, 0.0f }; // Rotation
1212
1213 QTest::newRow("clip2.json, t = duration/2")
1214 << handler << clip << localTime << expectedResults;
1215 expectedResults.clear();
1216 }
1217 {
1218 // a clip with linear interpolation
1219 handler = new Handler();
1220 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip4.json"));
1221 localTime = clip->duration();
1222 expectedResults = QVector<float> { 5.0, -2.0f, 6.0f };
1223
1224 QTest::newRow("clip4.json, linear, t = duration")
1225 << handler << clip << localTime << expectedResults;
1226 expectedResults.clear();
1227 }
1228 {
1229 // a clip with linear interpolation
1230 handler = new Handler();
1231 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip4.json"));
1232 localTime = clip->duration() / 2.0f;
1233 expectedResults = QVector<float> { 2.5f, -1.0f, 3.0f };
1234
1235 QTest::newRow("clip4.json, linear, t = duration/2")
1236 << handler << clip << localTime << expectedResults;
1237 expectedResults.clear();
1238 }
1239 {
1240 // a clip with slerp interpolation
1241 handler = new Handler();
1242 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip6.json"));
1243 localTime = clip->duration() / 2.0f;
1244 expectedResults = QVector<float> { 0.923822f, 0.382626f, 0.0f, 0.0f };
1245
1246 QTest::newRow("clip6.json, slerp, t = duration/2")
1247 << handler << clip << localTime << expectedResults;
1248 expectedResults.clear();
1249 }
1250 }
1251
1252 void checkEvaluateClipAtLocalTime()
1253 {
1254 // GIVEN
1255 QFETCH(Handler *, handler);
1256 QFETCH(AnimationClip *, clip);
1257 QFETCH(float, localTime);
1258 QFETCH(ClipResults, expectedResults);
1259
1260 // WHEN
1261 const ClipResults actualResults = evaluateClipAtLocalTime(clip, localTime);
1262
1263 // THEN
1264 QCOMPARE(actualResults.size(), expectedResults.size());
1265 for (int i = 0; i < actualResults.size(); ++i) {
1266 auto actual = actualResults[i];
1267 auto expected = expectedResults[i];
1268
1269 QVERIFY(fuzzyCompare(actual, expected) == true);
1270 }
1271
1272 // Cleanup
1273 delete handler;
1274 }
1275
1276 void checkEvaluateClipAtPhase_data()
1277 {
1278 QTest::addColumn<Handler *>("handler");
1279 QTest::addColumn<AnimationClip *>("clip");
1280 QTest::addColumn<float>("phase");
1281 QTest::addColumn<ClipResults>("expectedResults");
1282
1283 Handler *handler;
1284 AnimationClip *clip;
1285 float phase;
1286 ClipResults expectedResults;
1287
1288 {
1289 handler = new Handler();
1290 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1291 phase = 0.0f;
1292 expectedResults = QVector<float> { 0.0f, 0.0f, 0.0f };
1293
1294 QTest::newRow("clip1.json, phi = 0.0")
1295 << handler << clip << phase << expectedResults;
1296 expectedResults.clear();
1297 }
1298
1299 {
1300 handler = new Handler();
1301 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1302 phase = 1.0f;
1303 expectedResults = QVector<float> { 5.0f, 0.0f, 0.0f };
1304
1305 QTest::newRow("clip1.json, phi = 1.0")
1306 << handler << clip << phase << expectedResults;
1307 expectedResults.clear();
1308 }
1309
1310 {
1311 handler = new Handler();
1312 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1313 phase = 0.5f;
1314 expectedResults = QVector<float> { 2.5f, 0.0f, 0.0f };
1315
1316 QTest::newRow("clip1.json, phi = 0.5")
1317 << handler << clip << phase << expectedResults;
1318 expectedResults.clear();
1319 }
1320
1321 {
1322 handler = new Handler();
1323 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip2.json"));
1324 phase = 0.0f;
1325 expectedResults = QVector<float> { 0.0f, 0.0f, 0.0f, // Translation
1326 1.0f, 0.0f, 0.0f, 0.0f }; // Rotation
1327
1328 QTest::newRow("clip2.json, phi = 0.0")
1329 << handler << clip << phase << expectedResults;
1330 expectedResults.clear();
1331 }
1332 {
1333 handler = new Handler();
1334 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip2.json"));
1335 phase = 1.0f;
1336 expectedResults = QVector<float> { 5.0f, 0.0f, 0.0f, // Translation
1337 0.0f, 0.0f, -1.0f, 0.0f }; // Rotation
1338
1339 QTest::newRow("clip2.json, t = 1.0")
1340 << handler << clip << phase << expectedResults;
1341 expectedResults.clear();
1342 }
1343 {
1344 handler = new Handler();
1345 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip2.json"));
1346 phase = 0.5f;
1347 expectedResults = QVector<float> { 2.5f, 0.0f, 0.0f, // Translation
1348 0.5f, 0.0f, -0.5f, 0.0f }; // Rotation
1349
1350 QTest::newRow("clip2.json, phi = 0.5")
1351 << handler << clip << phase << expectedResults;
1352 expectedResults.clear();
1353 }
1354 }
1355
1356 void checkEvaluateClipAtPhase()
1357 {
1358 // GIVEN
1359 QFETCH(Handler *, handler);
1360 QFETCH(AnimationClip *, clip);
1361 QFETCH(float, phase);
1362 QFETCH(ClipResults, expectedResults);
1363
1364 // WHEN
1365 const ClipResults actualResults = evaluateClipAtPhase(clip, phase);
1366
1367 // THEN
1368 QCOMPARE(actualResults.size(), expectedResults.size());
1369 for (int i = 0; i < actualResults.size(); ++i) {
1370 auto actual = actualResults[i];
1371 auto expected = expectedResults[i];
1372
1373 QVERIFY(fuzzyCompare(actual, expected) == true);
1374 }
1375
1376 // Cleanup
1377 delete handler;
1378 }
1379
1380 void checkChannelComponentsToIndicesHelper_data()
1381 {
1382 QTest::addColumn<Channel>("channel");
1383 QTest::addColumn<int>("dataType");
1384 QTest::addColumn<qsizetype>("expectedChannelComponentCount");
1385 QTest::addColumn<qsizetype>("offset");
1386 QTest::addColumn<QList<char>>("suffixes");
1387 QTest::addColumn<QList<qsizetype>>("expectedResults");
1388
1389 Channel channel;
1390 int dataType;
1391 qsizetype expectedChannelComponentCount;
1392 qsizetype offset;
1393 QList<char> suffixes;
1394 QList<qsizetype> expectedResults;
1395
1396 // already sorted vec3, no component names, with and without offset
1397 {
1398 channel = Channel();
1399 channel.name = QLatin1String("Location");
1400 channel.channelComponents.resize(3);
1401 // leave 'name' empty
1402
1403 dataType = static_cast<int>(QMetaType::QVector3D);
1404 expectedChannelComponentCount = 3;
1405 offset = 0;
1406 // suffixes expected to be ignored
1407 expectedResults = QList<qsizetype>{ 0, 1, 2 };
1408
1409 QTest::newRow("vec3 location, pre-sorted, no component names, offset = 0")
1410 << channel << dataType << expectedChannelComponentCount
1411 << offset << suffixes << expectedResults;
1412
1413 expectedResults.clear();
1414
1415 offset = 4;
1416 expectedResults = QList<qsizetype>{ 4, 5, 6 };
1417 QTest::newRow("vec3 location, pre-sorted, no component names, offset = 4")
1418 << channel << dataType << expectedChannelComponentCount
1419 << offset << suffixes << expectedResults;
1420 expectedResults.clear();
1421 }
1422
1423 // vec3 with and without offset
1424 {
1425 channel = Channel();
1426 channel.name = QLatin1String("Location");
1427 channel.channelComponents.resize(3);
1428 channel.channelComponents[0].name = QLatin1String("Location X");
1429 channel.channelComponents[1].name = QLatin1String("Location Y");
1430 channel.channelComponents[2].name = QLatin1String("Location Z");
1431
1432 dataType = static_cast<int>(QMetaType::QVector3D);
1433 expectedChannelComponentCount = 3;
1434 offset = 0;
1435 suffixes = QList<char> { 'X', 'Y', 'Z', 'W' };
1436 expectedResults = QList<qsizetype>{ 0, 1, 2 };
1437
1438 QTest::newRow("vec3 location, offset = 0")
1439 << channel << dataType << expectedChannelComponentCount
1440 << offset << suffixes << expectedResults;
1441
1442 expectedResults.clear();
1443
1444 offset = 4;
1445 expectedResults = QList<qsizetype>{ 4, 5, 6 };
1446 QTest::newRow("vec3 location, offset = 4")
1447 << channel << dataType << expectedChannelComponentCount
1448 << offset << suffixes << expectedResults;
1449
1450 suffixes.clear();
1451 expectedResults.clear();
1452 }
1453
1454 // vec2 with and without offset
1455 {
1456 channel = Channel();
1457 channel.name = QLatin1String("pos");
1458 channel.channelComponents.resize(2);
1459 channel.channelComponents[0].name = QLatin1String("pos X");
1460 channel.channelComponents[1].name = QLatin1String("pos Y");
1461
1462 dataType = static_cast<int>(QMetaType::QVector2D);
1463 expectedChannelComponentCount = 2;
1464 offset = 0;
1465 suffixes = QList<char> { 'X', 'Y', 'Z', 'W' };
1466 expectedResults = QList<qsizetype>{ 0, 1 };
1467
1468 QTest::newRow("vec2 pos, offset = 0")
1469 << channel << dataType << expectedChannelComponentCount
1470 << offset << suffixes << expectedResults;
1471
1472 expectedResults.clear();
1473
1474 offset = 2;
1475 expectedResults = QList<qsizetype>{ 2, 3 };
1476 QTest::newRow("vec2 pos, offset = 2")
1477 << channel << dataType << expectedChannelComponentCount
1478 << offset << suffixes << expectedResults;
1479
1480 suffixes.clear();
1481 expectedResults.clear();
1482 }
1483
1484 // vec4 with and without offset
1485 {
1486 channel = Channel();
1487 channel.name = QLatin1String("foo");
1488 channel.channelComponents.resize(4);
1489 channel.channelComponents[0].name = QLatin1String("foo X");
1490 channel.channelComponents[1].name = QLatin1String("foo Y");
1491 channel.channelComponents[2].name = QLatin1String("foo Z");
1492 channel.channelComponents[3].name = QLatin1String("foo W");
1493
1494 dataType = static_cast<int>(QMetaType::QVector4D);
1495 expectedChannelComponentCount = 4;
1496 offset = 0;
1497 suffixes = QList<char> { 'X', 'Y', 'Z', 'W' };
1498 expectedResults = QList<qsizetype>{ 0, 1, 2, 3 };
1499
1500 QTest::newRow("vec4 foo, offset = 0")
1501 << channel << dataType << expectedChannelComponentCount
1502 << offset << suffixes << expectedResults;
1503
1504 expectedResults.clear();
1505
1506 offset = 10;
1507 expectedResults = QList<qsizetype>{ 10, 11, 12, 13 };
1508 QTest::newRow("vec4 foo, offset = 10")
1509 << channel << dataType << expectedChannelComponentCount
1510 << offset << suffixes << expectedResults;
1511
1512 suffixes.clear();
1513 expectedResults.clear();
1514 }
1515
1516 // double with and without offset
1517 {
1518 channel = Channel();
1519 channel.name = QLatin1String("foo");
1520 channel.channelComponents.resize(1);
1521 channel.channelComponents[0].name = QLatin1String("Mass X");
1522
1523 dataType = static_cast<int>(QMetaType::Double);
1524 expectedChannelComponentCount = 1;
1525 offset = 0;
1526 suffixes = QList<char> { 'X', 'Y', 'Z', 'W' };
1527 expectedResults = QList<qsizetype>{ 0 };
1528
1529 QTest::newRow("double Mass, offset = 0")
1530 << channel << dataType << expectedChannelComponentCount
1531 << offset << suffixes << expectedResults;
1532
1533 expectedResults.clear();
1534
1535 offset = 5;
1536 expectedResults = QList<qsizetype>{ 5 };
1537 QTest::newRow("double Mass, offset = 5")
1538 << channel << dataType << expectedChannelComponentCount
1539 << offset << suffixes << expectedResults;
1540
1541 suffixes.clear();
1542 expectedResults.clear();
1543 }
1544
1545 // quaternion with and without offset
1546 {
1547 channel = Channel();
1548 channel.name = QLatin1String("Rotation");
1549 channel.channelComponents.resize(4);
1550 channel.channelComponents[0].name = QLatin1String("Rotation W");
1551 channel.channelComponents[1].name = QLatin1String("Rotation X");
1552 channel.channelComponents[2].name = QLatin1String("Rotation Y");
1553 channel.channelComponents[3].name = QLatin1String("Rotation Z");
1554
1555 dataType = static_cast<int>(QMetaType::QQuaternion);
1556 expectedChannelComponentCount = 4;
1557 offset = 0;
1558 suffixes = QList<char> { 'W', 'X', 'Y', 'Z' };
1559 expectedResults = QList<qsizetype>{ 0, 1, 2, 3 };
1560
1561 QTest::newRow("quaternion Rotation, offset = 0")
1562 << channel << dataType << expectedChannelComponentCount
1563 << offset << suffixes << expectedResults;
1564
1565 expectedResults.clear();
1566
1567 offset = 10;
1568 expectedResults = QList<qsizetype>{ 10, 11, 12, 13 };
1569 QTest::newRow("quaternion Rotation, offset = 10")
1570 << channel << dataType << expectedChannelComponentCount
1571 << offset << suffixes << expectedResults;
1572
1573 suffixes.clear();
1574 expectedResults.clear();
1575 }
1576
1577 // quaternion with and without offset, randomized
1578 {
1579 channel = Channel();
1580 channel.name = QLatin1String("Rotation");
1581 channel.channelComponents.resize(4);
1582 channel.channelComponents[0].name = QLatin1String("Rotation X");
1583 channel.channelComponents[1].name = QLatin1String("Rotation W");
1584 channel.channelComponents[2].name = QLatin1String("Rotation Z");
1585 channel.channelComponents[3].name = QLatin1String("Rotation Y");
1586
1587 dataType = static_cast<int>(QMetaType::QQuaternion);
1588 expectedChannelComponentCount = 4;
1589 offset = 0;
1590 suffixes = QList<char> { 'W', 'X', 'Y', 'Z' };
1591 expectedResults = QList<qsizetype>{ 1, 0, 3, 2 };
1592
1593 QTest::newRow("quaternion Rotation, offset = 0, randomized")
1594 << channel << dataType << expectedChannelComponentCount
1595 << offset << suffixes << expectedResults;
1596
1597 expectedResults.clear();
1598
1599 offset = 10;
1600 expectedResults = QList<qsizetype>{ 11, 10, 13, 12 };
1601 QTest::newRow("quaternion Rotation, offset = 10, randomized")
1602 << channel << dataType << expectedChannelComponentCount
1603 << offset << suffixes << expectedResults;
1604
1605 suffixes.clear();
1606 expectedResults.clear();
1607 }
1608
1609 // color with and without offset 3 components
1610 {
1611 channel = Channel();
1612 channel.name = QLatin1String("Color");
1613 channel.channelComponents.resize(3);
1614 channel.channelComponents[0].name = QLatin1String("Color R");
1615 channel.channelComponents[1].name = QLatin1String("Color G");
1616 channel.channelComponents[2].name = QLatin1String("Color B");
1617
1618 dataType = static_cast<int>(QMetaType::QColor);
1619 expectedChannelComponentCount = 3;
1620 offset = 0;
1621 suffixes = QList<char> { 'R', 'G', 'B' };
1622 expectedResults = QList<qsizetype>{ 0, 1, 2 };
1623
1624 QTest::newRow("QColor RGB Color, offset = 0")
1625 << channel << dataType << expectedChannelComponentCount
1626 << offset << suffixes << expectedResults;
1627
1628 expectedResults.clear();
1629
1630 offset = 10;
1631 expectedResults = QList<qsizetype>{ 10, 11, 12 };
1632 QTest::newRow("QColor RGB Color, offset = 10")
1633 << channel << dataType << expectedChannelComponentCount
1634 << offset << suffixes << expectedResults;
1635
1636 suffixes.clear();
1637 expectedResults.clear();
1638 }
1639
1640 // color with and without offset 4 components
1641 {
1642 channel = Channel();
1643 channel.name = QLatin1String("Color");
1644 channel.channelComponents.resize(4);
1645 channel.channelComponents[0].name = QLatin1String("Color R");
1646 channel.channelComponents[1].name = QLatin1String("Color G");
1647 channel.channelComponents[2].name = QLatin1String("Color B");
1648 channel.channelComponents[3].name = QLatin1String("Color A");
1649
1650 dataType = static_cast<int>(QMetaType::QColor);
1651 expectedChannelComponentCount = 4;
1652 offset = 0;
1653 suffixes = QList<char> { 'R', 'G', 'B', 'A' };
1654 expectedResults = QList<qsizetype>{ 0, 1, 2, 3 };
1655
1656 QTest::newRow("QColor RGBA Color, offset = 0")
1657 << channel << dataType << expectedChannelComponentCount
1658 << offset << suffixes << expectedResults;
1659
1660 expectedResults.clear();
1661
1662 offset = 10;
1663 expectedResults = QList<qsizetype>{ 10, 11, 12, 13 };
1664 QTest::newRow("QColor RGBA Color, offset = 10")
1665 << channel << dataType << expectedChannelComponentCount
1666 << offset << suffixes << expectedResults;
1667
1668 suffixes.clear();
1669 expectedResults.clear();
1670 }
1671
1672 // weights as list with and without offset
1673 {
1674 channel = Channel();
1675 channel.name = QLatin1String("MorphWeights");
1676 channel.channelComponents.resize(6);
1677 // leave channel component names empty
1678
1679 dataType = static_cast<int>(QMetaType::QVariantList);
1680 expectedChannelComponentCount = 6;
1681 offset = 0;
1682 // suffixes expected to be ignored
1683 expectedResults = QList<qsizetype>{ 0, 1, 2, 3, 4, 5 };
1684
1685 QTest::newRow("MorphWeights List count = 6, offset = 0")
1686 << channel << dataType << expectedChannelComponentCount
1687 << offset << suffixes << expectedResults;
1688 expectedResults.clear();
1689
1690 offset = 10;
1691 expectedResults = QList<qsizetype>{ 10, 11, 12, 13, 14, 15 };
1692 QTest::newRow("MorphWeights List count = 6, offset = 10")
1693 << channel << dataType << expectedChannelComponentCount
1694 << offset << suffixes << expectedResults;
1695 suffixes.clear();
1696 expectedResults.clear();
1697 }
1698
1699 // weights as vec of float with and without offset
1700 {
1701 channel = Channel();
1702 channel.name = QLatin1String("MorphWeights");
1703 channel.channelComponents.resize(6);
1704 // leave channel component names empty
1705
1706 dataType = qMetaTypeId<QList<float>>();
1707 expectedChannelComponentCount = 6;
1708 offset = 0;
1709 // suffixes expected to be ignored
1710 expectedResults = QList<qsizetype>{ 0, 1, 2, 3, 4, 5 };
1711
1712 QTest::newRow("MorphWeights Vec count = 6, offset = 0")
1713 << channel << dataType << expectedChannelComponentCount
1714 << offset << suffixes << expectedResults;
1715 expectedResults.clear();
1716
1717 offset = 10;
1718 expectedResults = QList<qsizetype>{ 10, 11, 12, 13, 14, 15 };
1719 QTest::newRow("MorphWeights Vec count = 6, offset = 10")
1720 << channel << dataType << expectedChannelComponentCount
1721 << offset << suffixes << expectedResults;
1722 suffixes.clear();
1723 expectedResults.clear();
1724 }
1725 }
1726
1727 void checkChannelComponentsToIndicesHelper()
1728 {
1729 // GIVEN
1730 QFETCH(Channel, channel);
1731 QFETCH(qsizetype, offset);
1732 QFETCH(qsizetype, expectedChannelComponentCount);
1733 QFETCH(QList<char>, suffixes);
1734 QFETCH(QList<qsizetype>, expectedResults);
1735
1736 // WHEN
1737 const QVector<qsizetype> actualResults = channelComponentsToIndicesHelper(channel,
1738 expectedChannelComponentCount, offset, suffixes);
1739
1740 // THEN
1741 QCOMPARE(actualResults.size(), expectedResults.size());
1742 for (qsizetype i = 0; i < actualResults.size(); ++i) {
1743 QCOMPARE(actualResults[i], expectedResults[i]);
1744 }
1745 }
1746
1747 void checkChannelComponentsToIndices_data()
1748 {
1749 QTest::addColumn<Channel>("channel");
1750 QTest::addColumn<int>("dataType");
1751 QTest::addColumn<qsizetype>("componentCount");
1752 QTest::addColumn<qsizetype>("offset");
1753 QTest::addColumn<QList<qsizetype>>("expectedResults");
1754
1755 Channel channel;
1756 int dataType;
1757 qsizetype componentCount;
1758 qsizetype offset;
1759 QList<qsizetype> expectedResults;
1760
1761 // Quaternion
1762 {
1763 channel = Channel();
1764 channel.name = QLatin1String("Rotation");
1765 channel.channelComponents.resize(4);
1766 channel.channelComponents[0].name = QLatin1String("Rotation W");
1767 channel.channelComponents[1].name = QLatin1String("Rotation X");
1768 channel.channelComponents[2].name = QLatin1String("Rotation Y");
1769 channel.channelComponents[3].name = QLatin1String("Rotation Z");
1770
1771 dataType = static_cast<int>(QMetaType::QQuaternion);
1772 componentCount = 4;
1773 offset = 0;
1774 expectedResults = QList<qsizetype>{ 0, 1, 2, 3 };
1775
1776 QTest::newRow("quaternion Rotation, offset = 0")
1777 << channel << dataType << componentCount << offset << expectedResults;
1778
1779 expectedResults.clear();
1780
1781 offset = 10;
1782 expectedResults = QList<qsizetype>{ 10, 11, 12, 13 };
1783 QTest::newRow("quaternion Rotation, offset = 10")
1784 << channel << dataType << componentCount << offset << expectedResults;
1785
1786 expectedResults.clear();
1787 }
1788
1789 // vec3 with and without offset
1790 {
1791 channel = Channel();
1792 channel.name = QLatin1String("Location");
1793 channel.channelComponents.resize(3);
1794 channel.channelComponents[0].name = QLatin1String("Location X");
1795 channel.channelComponents[1].name = QLatin1String("Location Y");
1796 channel.channelComponents[2].name = QLatin1String("Location Z");
1797
1798 dataType = static_cast<int>(QMetaType::QVector3D);
1799 componentCount = 3;
1800 offset = 0;
1801 expectedResults = QList<qsizetype>{ 0, 1, 2 };
1802
1803 QTest::newRow("vec3 location, offset = 0")
1804 << channel << dataType << componentCount << offset << expectedResults;
1805
1806 expectedResults.clear();
1807
1808 offset = 4;
1809 expectedResults = QList<qsizetype>{ 4, 5, 6 };
1810 QTest::newRow("vec3 location, offset = 4")
1811 << channel << dataType << componentCount << offset << expectedResults;
1812
1813 expectedResults.clear();
1814 }
1815
1816 // QColor
1817 {
1818 channel = Channel();
1819 channel.name = QLatin1String("Color");
1820 channel.channelComponents.resize(3);
1821 channel.channelComponents[0].name = QLatin1String("Color R");
1822 channel.channelComponents[1].name = QLatin1String("Color G");
1823 channel.channelComponents[2].name = QLatin1String("Color B");
1824
1825 dataType = static_cast<int>(QMetaType::QColor);
1826 componentCount = 3;
1827 offset = 0;
1828 expectedResults = QList<qsizetype>{ 0, 1, 2 };
1829
1830 QTest::newRow("QColor RGB Color, offset = 0")
1831 << channel << dataType << componentCount << offset << expectedResults;
1832
1833 expectedResults.clear();
1834
1835 offset = 10;
1836 expectedResults = QList<qsizetype>{ 10, 11, 12 };
1837 QTest::newRow("QColor RGB Color, offset = 10")
1838 << channel << dataType << componentCount << offset << expectedResults;
1839
1840 expectedResults.clear();
1841 }
1842
1843 {
1844 channel = Channel();
1845 channel.name = QLatin1String("Color");
1846 channel.channelComponents.resize(4);
1847 channel.channelComponents[0].name = QLatin1String("Color R");
1848 channel.channelComponents[1].name = QLatin1String("Color G");
1849 channel.channelComponents[2].name = QLatin1String("Color B");
1850 channel.channelComponents[3].name = QLatin1String("Color A");
1851
1852 dataType = static_cast<int>(QMetaType::QColor);
1853 componentCount = 4;
1854 offset = 0;
1855 expectedResults = QList<qsizetype>{ 0, 1, 2, 3 };
1856
1857 QTest::newRow("QColor RGBA Color, offset = 0")
1858 << channel << dataType << componentCount << offset << expectedResults;
1859
1860 expectedResults.clear();
1861
1862 offset = 10;
1863 expectedResults = QList<qsizetype>{ 10, 11, 12, 13 };
1864 QTest::newRow("QColor RGBA Color, offset = 10")
1865 << channel << dataType << componentCount << offset << expectedResults;
1866
1867 expectedResults.clear();
1868 }
1869 }
1870
1871 void checkChannelComponentsToIndices()
1872 {
1873 QFETCH(Channel, channel);
1874 QFETCH(int, dataType);
1875 QFETCH(qsizetype, componentCount);
1876 QFETCH(qsizetype, offset);
1877 QFETCH(QList<qsizetype>, expectedResults);
1878
1879 // WHEN
1880 const QVector<qsizetype> actualResults = channelComponentsToIndices(channel,
1881 dataType, componentCount, offset);
1882
1883 // THEN
1884 QCOMPARE(actualResults.size(), expectedResults.size());
1885 for (qsizetype i = 0; i < actualResults.size(); ++i) {
1886 QCOMPARE(actualResults[i], expectedResults[i]);
1887 }
1888 }
1889
1890 void checkEvaluationDataForClip_data()
1891 {
1892 QTest::addColumn<Handler *>("handler");
1893 QTest::addColumn<AnimationClip *>("clip");
1894 QTest::addColumn<AnimatorEvaluationData>("animatorData");
1895 QTest::addColumn<ClipEvaluationData>("expectedClipData");
1896
1897 Handler *handler;
1898 AnimationClip *clip;
1899 AnimatorEvaluationData animatorData;
1900 ClipEvaluationData clipData;
1901 auto* clock = new Clock;
1902
1903 {
1904 handler = new Handler();
1905 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1906 const qint64 globalStartTimeNS = 0;
1907 const int loops = 1;
1908 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
1909 animator->setCurrentLoop(0);
1910 clipData.currentLoop = animator->currentLoop();
1911 const qint64 elapsedTimeNS = 0;
1912 animatorData = evaluationDataForAnimator(animator, clock, elapsedTimeNS); // Tested elsewhere
1913
1914 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
1915 animatorData.elapsedTime,
1916 animatorData.playbackRate,
1917 clip->duration(),
1918 animatorData.loopCount,
1919 clipData.currentLoop); // Tested elsewhere
1920 clipData.isFinalFrame = false;
1921
1922 QTest::newRow("clip1.json, globalTime = 0")
1923 << handler << clip << animatorData << clipData;
1924 }
1925
1926 {
1927 handler = new Handler();
1928 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1929 const qint64 globalStartTimeNS = 0;
1930 const int loops = 1;
1931 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
1932 animator->setCurrentLoop(0);
1933 clipData.currentLoop = animator->currentLoop();
1934 const qint64 elapsedTimeNS = toNsecs(clip->duration()+1); // +1 to ensure beyond end
1935 animatorData = evaluationDataForAnimator(animator, nullptr, elapsedTimeNS); // Tested elsewhere
1936
1937 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
1938 animatorData.elapsedTime,
1939 animatorData.playbackRate,
1940 clip->duration(),
1941 animatorData.loopCount,
1942 clipData.currentLoop); // Tested elsewhere
1943 clipData.isFinalFrame = true;
1944
1945 QTest::newRow("clip1.json, elapsedTime = duration + 1")
1946 << handler << clip << animatorData << clipData;
1947 }
1948
1949 {
1950 handler = new Handler();
1951 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1952 const qint64 globalStartTimeNS = 0;
1953 const int loops = 0; // Infinite loops
1954 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
1955 animator->setCurrentLoop(0);
1956 clipData.currentLoop = animator->currentLoop();
1957 const qint64 elapsedTimeNS = toNsecs(2.0 * clip->duration());
1958 animatorData = evaluationDataForAnimator(animator, clock, elapsedTimeNS); // Tested elsewhere
1959
1960 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
1961 animatorData.elapsedTime,
1962 animatorData.playbackRate,
1963 clip->duration(),
1964 animatorData.loopCount,
1965 clipData.currentLoop); // Tested elsewhere
1966 clipData.isFinalFrame = false;
1967
1968 QTest::newRow("clip1.json, elapsedTime = 2 * duration, loops = infinite")
1969 << handler << clip << animatorData << clipData;
1970 }
1971
1972 {
1973 handler = new Handler();
1974 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1975 const qint64 globalStartTimeNS = 0;
1976 const int loops = 2;
1977 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
1978 animator->setCurrentLoop(0);
1979 clipData.currentLoop = animator->currentLoop();
1980 const qint64 elapsedTimeNS = toNsecs(2.0 * clip->duration() + 1.0); // +1 to ensure beyond end of clip
1981 animatorData = evaluationDataForAnimator(animator, nullptr, elapsedTimeNS); // Tested elsewhere
1982
1983 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
1984 animatorData.elapsedTime,
1985 animatorData.playbackRate,
1986 clip->duration(),
1987 animatorData.loopCount,
1988 clipData.currentLoop); // Tested elsewhere
1989 clipData.isFinalFrame = true;
1990
1991 QTest::newRow("clip1.json, elapsedTime = 2 * duration + 1, loops = 2")
1992 << handler << clip << animatorData << clipData;
1993 }
1994
1995 {
1996 handler = new Handler();
1997 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
1998 const qint64 globalStartTimeNS = 0;
1999 const int loops = 2;
2000 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
2001 animator->setCurrentLoop(1);
2002 clipData.currentLoop = animator->currentLoop();
2003 const qint64 elapsedTimeNS = toNsecs(clip->duration() + 1.0); // +1 to ensure beyond end of clip
2004 animatorData = evaluationDataForAnimator(animator, nullptr, elapsedTimeNS); // Tested elsewhere
2005
2006 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
2007 animatorData.elapsedTime,
2008 animatorData.playbackRate,
2009 clip->duration(),
2010 animatorData.loopCount,
2011 clipData.currentLoop); // Tested elsewhere
2012 clipData.isFinalFrame = true;
2013
2014 QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 2, current_loop = 1")
2015 << handler << clip << animatorData << clipData;
2016 }
2017
2018 {
2019 handler = new Handler();
2020 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
2021 const qint64 globalStartTimeNS = clip->duration();
2022 const int loops = 1;
2023 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
2024 animator->setCurrentLoop(1);
2025 clipData.currentLoop = animator->currentLoop();
2026 const qint64 elapsedTimeNS = toNsecs(clip->duration() * 0.5); // +1 to ensure beyond end of clip
2027
2028 Clock clock;
2029 clock.setPlaybackRate(-1.0);
2030
2031 animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere
2032
2033 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
2034 animatorData.elapsedTime,
2035 animatorData.playbackRate,
2036 clip->duration(),
2037 animatorData.loopCount,
2038 clipData.currentLoop); // Tested elsewhere
2039 clipData.isFinalFrame = false;
2040
2041 QTest::newRow("clip1.json, elapsedTime = duration / 2, loops = 1, current_loop = 1, playback_rate = -1")
2042 << handler << clip << animatorData << clipData;
2043 }
2044
2045 {
2046 handler = new Handler();
2047 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
2048 const qint64 globalStartTimeNS = clip->duration();
2049 const int loops = 1;
2050 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
2051 animator->setCurrentLoop(1);
2052 clipData.currentLoop = animator->currentLoop();
2053 const qint64 elapsedTimeNS = toNsecs(clip->duration() + 1); // +1 to ensure beyond end of clip
2054
2055 Clock clock;
2056 clock.setPlaybackRate(-1.0);
2057
2058 animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere
2059
2060 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
2061 animatorData.elapsedTime,
2062 animatorData.playbackRate,
2063 clip->duration(),
2064 animatorData.loopCount,
2065 clipData.currentLoop); // Tested elsewhere
2066 clipData.isFinalFrame = true;
2067
2068 QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 1, current_loop = 1, playback_rate = -1")
2069 << handler << clip << animatorData << clipData;
2070 }
2071
2072 {
2073 handler = new Handler();
2074 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
2075 const qint64 globalStartTimeNS = clip->duration();
2076 const int loops = 2;
2077 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
2078 animator->setCurrentLoop(0);
2079 clipData.currentLoop = animator->currentLoop();
2080 const qint64 elapsedTimeNS = toNsecs(clip->duration() + 1); // +1 to ensure beyond end of clip
2081
2082 Clock clock;
2083 clock.setPlaybackRate(-1.0);
2084
2085 animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere
2086
2087 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
2088 animatorData.elapsedTime,
2089 animatorData.playbackRate,
2090 clip->duration(),
2091 animatorData.loopCount,
2092 clipData.currentLoop); // Tested elsewhere
2093 clipData.isFinalFrame = true;
2094
2095 QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 2, current_loop = 0, playback_rate = -1")
2096 << handler << clip << animatorData << clipData;
2097 }
2098
2099 {
2100 handler = new Handler();
2101 clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));
2102 const qint64 globalStartTimeNS = clip->duration();
2103 const int loops = 2;
2104 auto animator = createClipAnimator(handler, globalStartTimeNS, loops);
2105 animator->setCurrentLoop(1);
2106 clipData.currentLoop = animator->currentLoop();
2107 const qint64 elapsedTimeNS = toNsecs(clip->duration() * 2.0 + 1); // +1 to ensure beyond end of clip
2108
2109 Clock clock;
2110 clock.setPlaybackRate(-1.0);
2111
2112 animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere
2113
2114 clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime,
2115 animatorData.elapsedTime,
2116 animatorData.playbackRate,
2117 clip->duration(),
2118 animatorData.loopCount,
2119 clipData.currentLoop); // Tested elsewhere
2120 clipData.isFinalFrame = true;
2121
2122 QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 2, current_loop = 1, playback_rate = -1")
2123 << handler << clip << animatorData << clipData;
2124 }
2125 }
2126
2127 void checkEvaluationDataForClip()
2128 {
2129 // GIVEN
2130 QFETCH(Handler *, handler);
2131 QFETCH(AnimationClip *, clip);
2132 QFETCH(AnimatorEvaluationData, animatorData);
2133 QFETCH(ClipEvaluationData, expectedClipData);
2134
2135 // WHEN
2136 ClipEvaluationData actualClipData = evaluationDataForClip(clip, animatorData);
2137
2138 // THEN
2139 QCOMPARE(actualClipData.currentLoop, expectedClipData.currentLoop);
2140 QVERIFY(fuzzyCompare(actualClipData.localTime, expectedClipData.localTime) == true);
2141 QCOMPARE(actualClipData.isFinalFrame, expectedClipData.isFinalFrame);
2142
2143 // Cleanup
2144 delete handler;
2145 }
2146
2147 void checkEvaluationDataForAnimator_data()
2148 {
2149 QTest::addColumn<Handler *>("handler");
2150 QTest::addColumn<ClipAnimator *>("animator");
2151 QTest::addColumn<qint64>("elapsedTime");
2152 QTest::addColumn<AnimatorEvaluationData>("expectedAnimatorData");
2153
2154 Handler *handler;
2155 ClipAnimator *animator;
2156 qint64 elapsedTimeNS;
2157 AnimatorEvaluationData expectedAnimatorData;
2158
2159 {
2160 handler = new Handler();
2161 const qint64 globalStartTimeNS = 0;
2162 const int loops = 1;
2163 animator = createClipAnimator(handler, globalStartTimeNS, loops);
2164 elapsedTimeNS = 0;
2165
2166 expectedAnimatorData.loopCount = loops;
2167 expectedAnimatorData.playbackRate = 1.0; // hard-wired for now
2168 expectedAnimatorData.elapsedTime = 0.0;
2169
2170 QTest::newRow("globalStartTime = 0, elapsedTime = 0, loops = 1")
2171 << handler << animator << elapsedTimeNS << expectedAnimatorData;
2172 }
2173
2174 {
2175 handler = new Handler();
2176 const qint64 globalStartTimeNS = 0;
2177 const int loops = 5;
2178 animator = createClipAnimator(handler, globalStartTimeNS, loops);
2179 elapsedTimeNS = 0;
2180
2181 expectedAnimatorData.loopCount = loops;
2182 expectedAnimatorData.playbackRate = 1.0; // hard-wired for now
2183 expectedAnimatorData.elapsedTime = 0.0;
2184
2185 QTest::newRow("globalStartTime = 0, elapsedTime = 0, loops = 5")
2186 << handler << animator << elapsedTimeNS << expectedAnimatorData;
2187 }
2188
2189 {
2190 handler = new Handler();
2191 const qint64 globalStartTimeNS = 0;
2192 const int loops = 1;
2193 animator = createClipAnimator(handler, globalStartTimeNS, loops);
2194 elapsedTimeNS = 5000000000;
2195
2196 expectedAnimatorData.loopCount = loops;
2197 expectedAnimatorData.playbackRate = 1.0; // hard-wired for now
2198 expectedAnimatorData.elapsedTime = 5.0;
2199
2200 QTest::newRow("globalStartTime = 0, elapsedTime = 5, loops = 1")
2201 << handler << animator << elapsedTimeNS << expectedAnimatorData;
2202 }
2203
2204 {
2205 handler = new Handler();
2206 const qint64 globalStartTimeNS = 3000000000;
2207 const int loops = 1;
2208 animator = createClipAnimator(handler, globalStartTimeNS, loops);
2209 elapsedTimeNS = 2000000000;
2210
2211 expectedAnimatorData.loopCount = loops;
2212 expectedAnimatorData.playbackRate = 1.0; // hard-wired for now
2213 expectedAnimatorData.elapsedTime = 2.0;
2214
2215 QTest::newRow("globalStartTime = 3, elapsedTime = 2, loops = 1")
2216 << handler << animator << elapsedTimeNS << expectedAnimatorData;
2217 }
2218 }
2219
2220 void checkEvaluationDataForAnimator()
2221 {
2222 // GIVEN
2223 QFETCH(Handler *, handler);
2224 QFETCH(ClipAnimator *, animator);
2225 QFETCH(qint64, elapsedTime);
2226 QFETCH(AnimatorEvaluationData, expectedAnimatorData);
2227
2228 // WHEN
2229 AnimatorEvaluationData actualAnimatorData = evaluationDataForAnimator(animator, nullptr, elapsedTime);
2230
2231 // THEN
2232 QCOMPARE(actualAnimatorData.loopCount, expectedAnimatorData.loopCount);
2233 QVERIFY(fuzzyCompare(actualAnimatorData.playbackRate, expectedAnimatorData.playbackRate) == true);
2234 QVERIFY(fuzzyCompare(actualAnimatorData.elapsedTime, expectedAnimatorData.elapsedTime) == true);
2235
2236 // Cleanup
2237 delete handler;
2238 }
2239
2240 void checkGatherValueNodesToEvaluate_data()
2241 {
2242 QTest::addColumn<Handler *>("handler");
2243 QTest::addColumn<Qt3DCore::QNodeId>("blendTreeRootId");
2244 QTest::addColumn<QList<Qt3DCore::QNodeId>>("expectedIds");
2245
2246 {
2247 Handler *handler = new Handler;
2248
2249 const auto lerp = createLerpClipBlend(handler);
2250 const auto value1 = createClipBlendValue(handler);
2251 const auto clip1Id = Qt3DCore::QNodeId::createId();
2252 value1->setClipId(clip1Id);
2253 lerp->setStartClipId(value1->peerId());
2254
2255 const auto value2 = createClipBlendValue(handler);
2256 const auto clip2Id = Qt3DCore::QNodeId::createId();
2257 value2->setClipId(clip2Id);
2258 lerp->setEndClipId(value2->peerId());
2259
2260 const QList<Qt3DCore::QNodeId> expectedIds = { value1->peerId(), value2->peerId() };
2261
2262 QTest::newRow("simple lerp") << handler << lerp->peerId() << expectedIds;
2263 }
2264
2265 {
2266 Handler *handler = new Handler;
2267
2268 const auto value1 = createClipBlendValue(handler);
2269 const auto clip1Id = Qt3DCore::QNodeId::createId();
2270 value1->setClipId(clip1Id);
2271
2272 const QList<Qt3DCore::QNodeId> expectedIds = { value1->peerId() };
2273
2274 QTest::newRow("value only") << handler << value1->peerId() << expectedIds;
2275 }
2276 }
2277
2278 void checkGatherValueNodesToEvaluate()
2279 {
2280 // GIVEN
2281 QFETCH(Handler *, handler);
2282 QFETCH(Qt3DCore::QNodeId, blendTreeRootId);
2283 QFETCH(QList<Qt3DCore::QNodeId>, expectedIds);
2284
2285 // WHEN
2286 const QVector<Qt3DCore::QNodeId> actualIds = gatherValueNodesToEvaluate(handler, blendTreeRootId);
2287
2288 // THEN
2289 QCOMPARE(actualIds.size(), expectedIds.size());
2290 for (int i = 0; i < actualIds.size(); ++i)
2291 QCOMPARE(actualIds[i], expectedIds[i]);
2292
2293 // Cleanup
2294 delete handler;
2295 }
2296
2297 void checkEvaluateBlendTree_data()
2298 {
2299 QTest::addColumn<Handler *>("handler");
2300 QTest::addColumn<BlendedClipAnimator *>("animator");
2301 QTest::addColumn<Qt3DCore::QNodeId>("blendNodeId");
2302 QTest::addColumn<ClipResults>("expectedResults");
2303
2304 {
2305 /*
2306 ValueNode1----
2307 |
2308 MeanBlendNode
2309 |
2310 ValueNode2----
2311 */
2312
2313 auto handler = new Handler();
2314 const qint64 globalStartTimeNS = 0;
2315 const int loopCount = 1;
2316 auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount);
2317
2318 // Set up the blend node and dependencies (evaluated clip results of the
2319 // dependent nodes in the animator indexed by their ids).
2320 MeanBlendNode *blendNode = createMeanBlendNode(handler);
2321
2322 // First clip to use in the mean
2323 auto valueNode1 = createClipBlendValue(handler);
2324 ClipResults valueNode1Results = { 0.0f, 0.0f, 0.0f };
2325 valueNode1->setClipResults(animator->peerId(), valueNode1Results);
2326
2327 // Second clip to use in the mean
2328 auto valueNode2 = createClipBlendValue(handler);
2329 ClipResults valueNode2Results = { 1.0f, 1.0f, 1.0f };
2330 valueNode2->setClipResults(animator->peerId(), valueNode2Results);
2331
2332 blendNode->setValueNodeIds(valueNode1->peerId(), valueNode2->peerId());
2333
2334 ClipResults expectedResults = { 0.5f, 0.5f, 0.5f };
2335
2336 QTest::newRow("mean node, 1 channel")
2337 << handler << animator << blendNode->peerId() << expectedResults;
2338 }
2339
2340 {
2341 /*
2342 ValueNode1----
2343 |
2344 MeanBlendNode
2345 |
2346 ValueNode2----
2347 */
2348
2349 auto handler = new Handler();
2350 const qint64 globalStartTimeNS = 0;
2351 const int loopCount = 1;
2352 auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount);
2353
2354 // Set up the blend node and dependencies (evaluated clip results of the
2355 // dependent nodes in the animator indexed by their ids).
2356 MeanBlendNode *blendNode = createMeanBlendNode(handler);
2357
2358 // First clip to use in the mean
2359 auto valueNode1 = createClipBlendValue(handler);
2360 ClipResults valueNode1Results = { 0.0f, 0.0f, 0.0f, 1.0f, 2.0f, 3.0f };
2361 valueNode1->setClipResults(animator->peerId(), valueNode1Results);
2362
2363 // Second clip to use in the mean
2364 auto valueNode2 = createClipBlendValue(handler);
2365 ClipResults valueNode2Results = { 1.0f, 1.0f, 1.0f, 2.0f, 4.0f, 6.0f };
2366 valueNode2->setClipResults(animator->peerId(), valueNode2Results);
2367
2368 blendNode->setValueNodeIds(valueNode1->peerId(), valueNode2->peerId());
2369
2370 ClipResults expectedResults = { 0.5f, 0.5f, 0.5f, 1.5f, 3.0f, 4.5f };
2371
2372 QTest::newRow("mean node, 2 channels")
2373 << handler << animator << blendNode->peerId() << expectedResults;
2374 }
2375
2376 {
2377 /*
2378 ValueNode1----
2379 |
2380 MeanBlendNode1------
2381 | |
2382 ValueNode2---- |
2383 MeanBlendNode3
2384 ValueNode3---- |
2385 | |
2386 MeanBlendNode2------
2387 |
2388 ValueNode4----
2389 */
2390
2391 auto handler = new Handler();
2392 const qint64 globalStartTimeNS = 0;
2393 const int loopCount = 1;
2394 auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount);
2395
2396 // Set up the blend node and dependencies (evaluated clip results of the
2397 // dependent nodes in the animator indexed by their ids).
2398
2399 // MeanBlendNode1
2400 MeanBlendNode *meanNode1 = createMeanBlendNode(handler);
2401
2402 // First clip to use in mean1
2403 auto valueNode1 = createClipBlendValue(handler);
2404 ClipResults valueNode1Results = { 0.0f, 0.0f, 0.0f };
2405 valueNode1->setClipResults(animator->peerId(), valueNode1Results);
2406
2407 // Second clip to use in mean1
2408 auto valueNode2 = createClipBlendValue(handler);
2409 ClipResults valueNode2Results = { 2.0f, 2.0f, 2.0f };
2410 valueNode2->setClipResults(animator->peerId(), valueNode2Results);
2411
2412 meanNode1->setValueNodeIds(valueNode1->peerId(), valueNode2->peerId());
2413
2414
2415 // MeanBlendNode2
2416 MeanBlendNode *meanNode2 = createMeanBlendNode(handler);
2417
2418 // First clip to use in mean1
2419 auto valueNode3 = createClipBlendValue(handler);
2420 ClipResults valueNode3Results = { 10.0f, 10.0f, 10.0f };
2421 valueNode3->setClipResults(animator->peerId(), valueNode3Results);
2422
2423 // Second clip to use in mean1
2424 auto valueNode4 = createClipBlendValue(handler);
2425 ClipResults valueNode4Results = { 20.0f, 20.0f, 20.0f };
2426 valueNode4->setClipResults(animator->peerId(), valueNode4Results);
2427
2428 meanNode2->setValueNodeIds(valueNode3->peerId(), valueNode4->peerId());
2429
2430
2431 // MeanBlendNode3
2432 MeanBlendNode *meanNode3 = createMeanBlendNode(handler);
2433 meanNode3->setValueNodeIds(meanNode1->peerId(), meanNode2->peerId());
2434
2435 // Mean1 = 1
2436 // Mean2 = 15
2437 // Mean3 = (1 + 15 ) / 2 = 8
2438 ClipResults expectedResults = { 8.0f, 8.0f, 8.0f };
2439
2440 QTest::newRow("3 mean nodes, 1 channel")
2441 << handler << animator << meanNode3->peerId() << expectedResults;
2442 }
2443
2444 {
2445 /*
2446 ValueNode1----
2447 |
2448 MeanBlendNode1------
2449 | |
2450 ValueNode2---- |
2451 MeanBlendNode3---
2452 ValueNode3---- | |
2453 | | |
2454 MeanBlendNode2------ AdditiveBlendNode1
2455 | |
2456 ValueNode4---- |
2457 ValueNode5-------
2458 */
2459
2460 auto handler = new Handler();
2461 const qint64 globalStartTimeNS = 0;
2462 const int loopCount = 1;
2463 auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount);
2464
2465 // Set up the blend node and dependencies (evaluated clip results of the
2466 // dependent nodes in the animator indexed by their ids).
2467
2468 // MeanBlendNode1
2469 MeanBlendNode *meanNode1 = createMeanBlendNode(handler);
2470
2471 // First clip to use in mean1
2472 auto valueNode1 = createClipBlendValue(handler);
2473 ClipResults valueNode1Results = { 0.0f, 0.0f, 0.0f };
2474 valueNode1->setClipResults(animator->peerId(), valueNode1Results);
2475
2476 // Second clip to use in mean1
2477 auto valueNode2 = createClipBlendValue(handler);
2478 ClipResults valueNode2Results = { 2.0f, 2.0f, 2.0f };
2479 valueNode2->setClipResults(animator->peerId(), valueNode2Results);
2480
2481 meanNode1->setValueNodeIds(valueNode1->peerId(), valueNode2->peerId());
2482
2483
2484 // MeanBlendNode2
2485 MeanBlendNode *meanNode2 = createMeanBlendNode(handler);
2486
2487 // First clip to use in mean2
2488 auto valueNode3 = createClipBlendValue(handler);
2489 ClipResults valueNode3Results = { 10.0f, 10.0f, 10.0f };
2490 valueNode3->setClipResults(animator->peerId(), valueNode3Results);
2491
2492 // Second clip to use in mean2
2493 auto valueNode4 = createClipBlendValue(handler);
2494 ClipResults valueNode4Results = { 20.0f, 20.0f, 20.0f };
2495 valueNode4->setClipResults(animator->peerId(), valueNode4Results);
2496
2497 meanNode2->setValueNodeIds(valueNode3->peerId(), valueNode4->peerId());
2498
2499
2500 // MeanBlendNode3
2501 MeanBlendNode *meanNode3 = createMeanBlendNode(handler);
2502 meanNode3->setValueNodeIds(meanNode1->peerId(), meanNode2->peerId());
2503
2504
2505 // AdditiveBlendNode1
2506 AdditiveClipBlend *additiveBlendNode1 = createAdditiveClipBlend(handler);
2507 auto valueNode5 = createClipBlendValue(handler);
2508 ClipResults valueNode5Results = { 1.0f, 2.0f, 3.0f };
2509 valueNode5->setClipResults(animator->peerId(), valueNode5Results);
2510
2511 additiveBlendNode1->setBaseClipId(meanNode3->peerId());
2512 additiveBlendNode1->setAdditiveClipId(valueNode5->peerId());
2513 additiveBlendNode1->setAdditiveFactor(0.5);
2514
2515 // Mean1 = 1
2516 // Mean2 = 15
2517 // Mean3 = (1 + 15 ) / 2 = 8
2518 // Additive1 = 8 + 0.5 * (1, 2, 3) = (8.5, 9, 9.5)
2519 ClipResults expectedResults = { 8.5f, 9.0f, 9.5f };
2520
2521 QTest::newRow("3 mean nodes + additive, 1 channel")
2522 << handler << animator << additiveBlendNode1->peerId() << expectedResults;
2523 }
2524 }
2525
2526 void checkEvaluateBlendTree()
2527 {
2528 // GIVEN
2529 QFETCH(Handler *, handler);
2530 QFETCH(BlendedClipAnimator *, animator);
2531 QFETCH(Qt3DCore::QNodeId, blendNodeId);
2532 QFETCH(ClipResults, expectedResults);
2533
2534 // WHEN
2535 const ClipResults actualResults = evaluateBlendTree(handler, animator, blendNodeId);
2536
2537 // THEN
2538 QCOMPARE(actualResults.size(), expectedResults.size());
2539 for (int i = 0; i < actualResults.size(); ++i)
2540 QCOMPARE(actualResults[i], expectedResults[i]);
2541
2542 // Cleanup
2543 delete handler;
2544 }
2545
2546 void checkFormatClipResults_data()
2547 {
2548 QTest::addColumn<ClipResults>("rawClipResults");
2549 QTest::addColumn<ComponentIndices>("format");
2550 QTest::addColumn<ClipResults>("expectedResults");
2551
2552 {
2553 ClipResults rawClipResults = { 1.0f, 2.0f, 3.0f };
2554 ComponentIndices format = { 0, 1, 2 };
2555 ClipResults expectedResults = { 1.0f, 2.0f, 3.0f };
2556
2557 QTest::newRow("identity")
2558 << rawClipResults << format << expectedResults;
2559 }
2560
2561 {
2562 ClipResults rawClipResults = { 1.0f, 2.0f };
2563 ComponentIndices format = { 1, 0 };
2564 ClipResults expectedResults = { 2.0f, 1.0f };
2565
2566 QTest::newRow("swap")
2567 << rawClipResults << format << expectedResults;
2568 }
2569
2570 {
2571 ClipResults rawClipResults = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
2572 ComponentIndices format = { 0, 2, 1, 3, 4 };
2573 ClipResults expectedResults = { 1.0f, 3.0f, 2.0f, 4.0f, 5.0f };
2574
2575 QTest::newRow("swap subset")
2576 << rawClipResults << format << expectedResults;
2577 }
2578
2579 {
2580 ClipResults rawClipResults = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
2581 ComponentIndices format = { 4, 3, 2, 1, 0 };
2582 ClipResults expectedResults = { 5.0f, 4.0f, 3.0f, 2.0f, 1.0f };
2583
2584 QTest::newRow("reverse")
2585 << rawClipResults << format << expectedResults;
2586 }
2587
2588 {
2589 ClipResults rawClipResults = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
2590 ComponentIndices format = { 0, 1, -1, 3, 4 };
2591 ClipResults expectedResults = { 1.0f, 2.0f, 0.0f, 4.0f, 5.0f };
2592
2593 QTest::newRow("include missing")
2594 << rawClipResults << format << expectedResults;
2595 }
2596 }
2597
2598 void checkFormatClipResults()
2599 {
2600 // GIVEN
2601 QFETCH(ClipResults, rawClipResults);
2602 QFETCH(ComponentIndices, format);
2603 QFETCH(ClipResults, expectedResults);
2604
2605 // WHEN
2606 const ClipResults actualResults = formatClipResults(rawClipResults, format);
2607
2608 // THEN
2609 QCOMPARE(actualResults.size(), expectedResults.size());
2610 for (int i = 0; i < actualResults.size(); ++i)
2611 QCOMPARE(actualResults[i], expectedResults[i]);
2612 }
2613
2614 void checkBuildRequiredChannelsAndTypes_data()
2615 {
2616 QTest::addColumn<Handler *>("handler");
2617 QTest::addColumn<ChannelMapper *>("mapper");
2618 QTest::addColumn<QList<ChannelNameAndType>>("expectedResults");
2619
2620 {
2621 auto handler = new Handler();
2622 auto channelMapping = createChannelMapping(handler,
2623 QLatin1String("Location"),
2624 Qt3DCore::QNodeId::createId(),
2625 "translation",
2626 static_cast<int>(QMetaType::QVector3D),
2627 3);
2628 auto channelMapper = createChannelMapper(handler,
2629 QList<Qt3DCore::QNodeId> { channelMapping->peerId() });
2630
2631 const QList<ChannelNameAndType> expectedResults = {
2632 { QLatin1String("Location"),
2633 static_cast<int>(QMetaType::QVector3D),
2634 3,
2635 channelMapping->peerId() }
2636 };
2637
2638 QTest::addRow("Location, vec3") << handler << channelMapper << expectedResults;
2639 }
2640
2641 {
2642 auto handler = new Handler();
2643 auto channelMapping1 = createChannelMapping(handler,
2644 QLatin1String("Location"),
2645 Qt3DCore::QNodeId::createId(),
2646 "translation",
2647 static_cast<int>(QMetaType::QVector3D),
2648 3);
2649 auto channelMapping2 = createChannelMapping(handler,
2650 QLatin1String("Rotation"),
2651 Qt3DCore::QNodeId::createId(),
2652 "rotation",
2653 static_cast<int>(QMetaType::QQuaternion),
2654 4);
2655
2656 const QList<Qt3DCore::QNodeId> channelMappingIds
2657 = { channelMapping1->peerId(), channelMapping2->peerId() };
2658 auto channelMapper = createChannelMapper(handler, channelMappingIds);
2659
2660 const QList<ChannelNameAndType> expectedResults = {
2661 { QLatin1String("Location"),
2662 static_cast<int>(QMetaType::QVector3D),
2663 3,
2664 channelMapping1->peerId() },
2665 { QLatin1String("Rotation"),
2666 static_cast<int>(QMetaType::QQuaternion),
2667 4,
2668 channelMapping2->peerId() }
2669 };
2670
2671 QTest::addRow("Multiple unique channels") << handler << channelMapper << expectedResults;
2672 }
2673
2674 {
2675 auto handler = new Handler();
2676 auto channelMapping1 = createChannelMapping(handler,
2677 QLatin1String("Location"),
2678 Qt3DCore::QNodeId::createId(),
2679 "translation",
2680 static_cast<int>(QMetaType::QVector3D),
2681 3);
2682 auto channelMapping2 = createChannelMapping(handler,
2683 QLatin1String("Rotation"),
2684 Qt3DCore::QNodeId::createId(),
2685 "rotation",
2686 static_cast<int>(QMetaType::QQuaternion),
2687 4);
2688 auto channelMapping3 = createChannelMapping(handler,
2689 QLatin1String("Location"),
2690 Qt3DCore::QNodeId::createId(),
2691 "translation",
2692 static_cast<int>(QMetaType::QVector3D),
2693 3);
2694 auto channelMapping4 = createChannelMapping(handler,
2695 QLatin1String("Location"),
2696 Qt3DCore::QNodeId::createId(),
2697 "translation",
2698 static_cast<int>(QMetaType::QVector3D),
2699 3);
2700
2701 const QList<Qt3DCore::QNodeId> channelMappingIds
2702 = { channelMapping1->peerId(), channelMapping2->peerId(),
2703 channelMapping3->peerId(), channelMapping4->peerId() };
2704 auto channelMapper = createChannelMapper(handler, channelMappingIds);
2705
2706 QList<ChannelNameAndType> expectedResults = {
2707 { QLatin1String("Location"),
2708 static_cast<int>(QMetaType::QVector3D),
2709 3,
2710 channelMapping1->peerId() },
2711 { QLatin1String("Rotation"),
2712 static_cast<int>(QMetaType::QQuaternion),
2713 4,
2714 channelMapping2->peerId() },
2715 { QLatin1String("Location"),
2716 static_cast<int>(QMetaType::QVector3D),
2717 3,
2718 channelMapping3->peerId() },
2719 { QLatin1String("Location"),
2720 static_cast<int>(QMetaType::QVector3D),
2721 3,
2722 channelMapping4->peerId() }
2723 };
2724
2725 QTest::addRow("Multiple channels with repeats") << handler << channelMapper << expectedResults;
2726 }
2727
2728 {
2729 auto handler = new Handler();
2730 const int jointCount = 10;
2731 auto skeleton = createSkeleton(handler, jointCount);
2732 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
2733 auto channelMapper = createChannelMapper(handler,
2734 QList<Qt3DCore::QNodeId> { channelMapping->peerId() });
2735
2736 QList<ChannelNameAndType> expectedResults;
2737 for (int i = 0; i < jointCount; ++i) {
2738 ChannelNameAndType locationDescription = { QLatin1String("Location"),
2739 static_cast<int>(QMetaType::QVector3D),
2740 3,
2741 channelMapping->peerId() };
2742 locationDescription.jointIndex = i;
2743 locationDescription.jointTransformComponent = Translation;
2744 expectedResults.push_back(locationDescription);
2745
2746 ChannelNameAndType rotationDescription = { QLatin1String("Rotation"),
2747 static_cast<int>(QMetaType::QQuaternion),
2748 4,
2749 channelMapping->peerId() };
2750 rotationDescription.jointIndex = i;
2751 rotationDescription.jointTransformComponent = Rotation;
2752 expectedResults.push_back(rotationDescription);
2753
2754 ChannelNameAndType scaleDescription = { QLatin1String("Scale"),
2755 static_cast<int>(QMetaType::QVector3D),
2756 3,
2757 channelMapping->peerId() };
2758 scaleDescription.jointIndex = i;
2759 scaleDescription.jointTransformComponent = Scale;
2760 expectedResults.push_back(scaleDescription);
2761 }
2762
2763 QTest::addRow("Skeleton, 10 joints") << handler << channelMapper << expectedResults;
2764 }
2765 }
2766
2767 void checkBuildRequiredChannelsAndTypes()
2768 {
2769 // GIVEN
2770 QFETCH(Handler *, handler);
2771 QFETCH(ChannelMapper *, mapper);
2772 QFETCH(QList<ChannelNameAndType>, expectedResults);
2773
2774 // WHEN
2775 const QVector<ChannelNameAndType> actualResults = buildRequiredChannelsAndTypes(handler, mapper);
2776
2777 // THEN
2778 QCOMPARE(actualResults.size(), expectedResults.size());
2779 for (int i = 0; i < actualResults.size(); ++i)
2780 QCOMPARE(actualResults[i], expectedResults[i]);
2781
2782 // Cleanup
2783 delete handler;
2784 }
2785
2786 void checkAssignChannelComponentIndices_data()
2787 {
2788 QTest::addColumn<QVector<ChannelNameAndType>>("allChannels");
2789 QTest::addColumn<QVector<ComponentIndices>>("expectedResults");
2790
2791 {
2792 const QVector<ChannelNameAndType> allChannels = {
2793 { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 }
2794 };
2795
2796 const QVector<ComponentIndices> expectedResults = { { 0, 1, 2 } };
2797
2798 QTest::newRow("vec3 location") << allChannels << expectedResults;
2799 }
2800
2801 {
2802 const QVector<ChannelNameAndType> allChannels = {
2803 { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 },
2804 { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 }
2805 };
2806
2807 const QVector<ComponentIndices> expectedResults = { { 0, 1, 2 },
2808 { 3, 4, 5, 6 } };
2809
2810 QTest::newRow("vec3 location, quaterion rotation") << allChannels << expectedResults;
2811 }
2812
2813 {
2814 const QVector<ChannelNameAndType> allChannels = {
2815 { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 },
2816 { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 },
2817 { QLatin1String("BaseColor"), static_cast<int>(QMetaType::QVector3D), 3 },
2818 { QLatin1String("Metalness"), static_cast<int>(QMetaType::Double), 1 },
2819 { QLatin1String("Roughness"), static_cast<int>(QMetaType::Double), 1 },
2820 { QLatin1String("MorphWeights"), static_cast<int>(QMetaType::QVariantList), 6 }
2821 };
2822
2823 const QVector<ComponentIndices> expectedResults = {
2824 { 0, 1, 2 },
2825 { 3, 4, 5, 6 },
2826 { 7, 8, 9 },
2827 { 10 },
2828 { 11 },
2829 { 12, 13, 14, 15, 16, 17 } };
2830
2831 QTest::newRow("vec3 location, quaterion rotation, pbr metal-rough morphweights") << allChannels << expectedResults;
2832 }
2833
2834 {
2835 QVector<ChannelNameAndType> allChannels;
2836 const int jointCount = 4;
2837 for (int i = 0; i < jointCount; ++i) {
2838 ChannelNameAndType locationDescription = { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 };
2839 locationDescription.jointIndex = i;
2840 allChannels.push_back(locationDescription);
2841
2842 ChannelNameAndType rotationDescription = { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 };
2843 rotationDescription.jointIndex = i;
2844 allChannels.push_back(rotationDescription);
2845
2846 ChannelNameAndType scaleDescription = { QLatin1String("Scale"), static_cast<int>(QMetaType::QVector3D), 3 };
2847 scaleDescription.jointIndex = i;
2848 allChannels.push_back(scaleDescription);
2849 }
2850
2851 const QVector<ComponentIndices> expectedResults = {
2852 { 0, 1, 2 }, { 3, 4, 5, 6 }, { 7, 8, 9 },
2853 { 10, 11, 12 }, { 13, 14, 15, 16 }, { 17, 18, 19 },
2854 { 20, 21, 22 }, { 23, 24, 25, 26 }, { 27, 28, 29 },
2855 { 30, 31, 32 }, { 33, 34, 35, 36 }, { 37, 38, 39 }
2856 };
2857
2858 QTest::newRow("skeleton, 4 joints") << allChannels << expectedResults;
2859 }
2860 }
2861
2862 void checkAssignChannelComponentIndices()
2863 {
2864 // GIVEN
2865 QFETCH(QVector<ChannelNameAndType>, allChannels);
2866 QFETCH(QVector<ComponentIndices>, expectedResults);
2867
2868 // WHEN
2869 const QVector<ComponentIndices> actualResults = assignChannelComponentIndices(allChannels);
2870
2871 // THEN
2872 QCOMPARE(actualResults.size(), expectedResults.size());
2873 for (int i = 0; i < actualResults.size(); ++i) {
2874 const ComponentIndices &actualResult = actualResults[i];
2875 const ComponentIndices &expectedResult = expectedResults[i];
2876
2877 for (int j = 0; j < actualResult.size(); ++j)
2878 QCOMPARE(actualResult[j], expectedResult[j]);
2879 }
2880 }
2881
2882 void checkGenerateClipFormatIndices_data()
2883 {
2884 QTest::addColumn<QVector<ChannelNameAndType>>("targetChannels");
2885 QTest::addColumn<QVector<ComponentIndices>>("targetIndices");
2886 QTest::addColumn<AnimationClip *>("clip");
2887 QTest::addColumn<ClipFormat>("expectedResults");
2888
2889 {
2890 const QVector<ChannelNameAndType> targetChannels = {
2891 { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 },
2892 { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 },
2893 { QLatin1String("Base Color"), static_cast<int>(QMetaType::QVector3D), 3 },
2894 { QLatin1String("Metalness"), static_cast<int>(QMetaType::Double), 1 },
2895 { QLatin1String("Roughness"), static_cast<int>(QMetaType::Double), 1 }
2896 };
2897
2898 const QVector<ComponentIndices> targetIndices = {
2899 { 0, 1, 2, 3 },
2900 { 4, 5, 6 },
2901 { 7, 8, 9 },
2902 { 10 },
2903 { 11 }
2904 };
2905
2906 auto *clip = new AnimationClip();
2907 clip->setDataType(AnimationClip::File);
2908 clip->setSource(QUrl("qrc:/clip3.json"));
2909 clip->loadAnimation();
2910
2911 ClipFormat expectedResults;
2912 expectedResults.sourceClipIndices = { 0, 1, 3, 2, // Rotation (y/z swapped in clip3.json)
2913 4, 6, 5, // Location (y/z swapped in clip3.json)
2914 7, 8, 9, // Base Color
2915 10, // Metalness
2916 11 }; // Roughness
2917 expectedResults.sourceClipMask = { QBitArray(4, true),
2918 QBitArray(3, true),
2919 QBitArray(3, true),
2920 QBitArray(1, true),
2921 QBitArray(1, true) };
2922 expectedResults.namesAndTypes = targetChannels;
2923 expectedResults.formattedComponentIndices = targetIndices;
2924
2925 QTest::newRow("rotation, location, pbr metal-rough")
2926 << targetChannels << targetIndices << clip << expectedResults;
2927 }
2928
2929 {
2930 const QVector<ChannelNameAndType> targetChannels = {
2931 { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 },
2932 { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 },
2933 { QLatin1String("Base Color"), static_cast<int>(QMetaType::QVector3D), 3 },
2934 { QLatin1String("Metalness"), static_cast<int>(QMetaType::Double), 1 },
2935 { QLatin1String("Roughness"), static_cast<int>(QMetaType::Double), 1 }
2936 };
2937
2938 const QVector<ComponentIndices> targetIndices = {
2939 { 0, 1, 2 },
2940 { 3, 4, 5, 6 },
2941 { 7, 8, 9 },
2942 { 10 },
2943 { 11 }
2944 };
2945
2946 auto *clip = new AnimationClip();
2947 clip->setDataType(AnimationClip::File);
2948 clip->setSource(QUrl("qrc:/clip3.json"));
2949 clip->loadAnimation();
2950
2951 ClipFormat expectedResults;
2952 expectedResults.sourceClipIndices = { 4, 6, 5, // Location (y/z swapped in clip3.json)
2953 0, 1, 3, 2, // Rotation (y/z swapped in clip3.json)
2954 7, 8, 9, // Base Color
2955 10, // Metalness
2956 11 }; // Roughness
2957 expectedResults.sourceClipMask = { QBitArray(3, true),
2958 QBitArray(4, true),
2959 QBitArray(3, true),
2960 QBitArray(1, true),
2961 QBitArray(1, true) };
2962 expectedResults.namesAndTypes = targetChannels;
2963 expectedResults.formattedComponentIndices = targetIndices;
2964
2965 QTest::newRow("location, rotation, pbr metal-rough")
2966 << targetChannels << targetIndices << clip << expectedResults;
2967 }
2968
2969 {
2970 const QVector<ChannelNameAndType> targetChannels = {
2971 { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 },
2972 { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 },
2973 { QLatin1String("Albedo"), static_cast<int>(QMetaType::QVector3D), 3 },
2974 { QLatin1String("Metalness"), static_cast<int>(QMetaType::Double), 1 },
2975 { QLatin1String("Roughness"), static_cast<int>(QMetaType::Double), 1 }
2976 };
2977
2978 const QVector<ComponentIndices> targetIndices = {
2979 { 0, 1, 2, 3 },
2980 { 4, 5, 6 },
2981 { 7, 8, 9 },
2982 { 10 },
2983 { 11 }
2984 };
2985
2986 auto *clip = new AnimationClip();
2987 clip->setDataType(AnimationClip::File);
2988 clip->setSource(QUrl("qrc:/clip3.json"));
2989 clip->loadAnimation();
2990
2991 ClipFormat expectedResults;
2992 expectedResults.sourceClipIndices = { 0, 1, 3, 2, // Rotation (y/z swapped in clip3.json)
2993 4, 6, 5, // Location (y/z swapped in clip3.json)
2994 -1, -1, -1, // Albedo (missing from clip)
2995 10, // Metalness
2996 11 }; // Roughness
2997 expectedResults.sourceClipMask = { QBitArray(4, true),
2998 QBitArray(3, true),
2999 QBitArray(3, false),
3000 QBitArray(1, true),
3001 QBitArray(1, true) };
3002 expectedResults.namesAndTypes = targetChannels;
3003 expectedResults.formattedComponentIndices = targetIndices;
3004
3005 QTest::newRow("rotation, location, albedo (missing), metal-rough")
3006 << targetChannels << targetIndices << clip << expectedResults;
3007 }
3008
3009 {
3010 const QVector<ChannelNameAndType> targetChannels = {
3011 { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 },
3012 { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 },
3013 { QLatin1String("Albedo"), static_cast<int>(QMetaType::QVector3D), 3 },
3014 { QLatin1String("Metalness"), static_cast<int>(QMetaType::Double), 1 },
3015 { QLatin1String("Roughness"), static_cast<int>(QMetaType::Double), 1 }
3016 };
3017
3018 const QVector<ComponentIndices> targetIndices = {
3019 { 0, 1, 2 },
3020 { 3, 4, 5, 6 },
3021 { 7, 8, 9 },
3022 { 10 },
3023 { 11 }
3024 };
3025
3026 auto *clip = new AnimationClip();
3027 clip->setDataType(AnimationClip::File);
3028 clip->setSource(QUrl("qrc:/clip3.json"));
3029 clip->loadAnimation();
3030
3031 ClipFormat expectedResults;
3032 expectedResults.sourceClipIndices = { 4, 6, 5, // Location (y/z swapped in clip3.json)
3033 0, 1, 3, 2, // Rotation (y/z swapped in clip3.json)
3034 -1, -1, -1, // Albedo (missing from clip)
3035 10, // Metalness
3036 11 }; // Roughness
3037 expectedResults.sourceClipMask = { QBitArray(3, true),
3038 QBitArray(4, true),
3039 QBitArray(3, false),
3040 QBitArray(1, true),
3041 QBitArray(1, true) };
3042 expectedResults.namesAndTypes = targetChannels;
3043 expectedResults.formattedComponentIndices = targetIndices;
3044
3045 QTest::newRow("location, rotation, albedo (missing), metal-rough")
3046 << targetChannels << targetIndices << clip << expectedResults;
3047 }
3048
3049 {
3050 QVector<ChannelNameAndType> targetChannels;
3051 const int jointCount = 4;
3052 for (int i = 0; i < jointCount; ++i) {
3053 ChannelNameAndType locationDescription = { QLatin1String("Location"), static_cast<int>(QMetaType::QVector3D), 3 };
3054 locationDescription.jointIndex = i;
3055 targetChannels.push_back(locationDescription);
3056
3057 ChannelNameAndType rotationDescription = { QLatin1String("Rotation"), static_cast<int>(QMetaType::QQuaternion), 4 };
3058 rotationDescription.jointIndex = i;
3059 targetChannels.push_back(rotationDescription);
3060
3061 ChannelNameAndType scaleDescription = { QLatin1String("Scale"), static_cast<int>(QMetaType::QVector3D), 3 };
3062 scaleDescription.jointIndex = i;
3063 targetChannels.push_back(scaleDescription);
3064 }
3065
3066 const QVector<ComponentIndices> targetIndices = {
3067 { 0, 1, 2 }, { 3, 4, 5, 6 }, { 7, 8, 9 },
3068 { 10, 11, 12 }, { 13, 14, 15, 16 }, { 17, 18, 19 },
3069 { 20, 21, 22 }, { 23, 24, 25, 26 }, { 27, 28, 29 },
3070 { 30, 31, 32 }, { 33, 34, 35, 36 }, { 37, 38, 39 }
3071 };
3072
3073 auto *clip = new AnimationClip();
3074 clip->setDataType(AnimationClip::File);
3075 clip->setSource(QUrl("qrc:/clip5.json"));
3076 clip->loadAnimation();
3077
3078 ClipFormat expectedResults;
3079 expectedResults.sourceClipIndices = { 4, 6, 5, // Location, joint 0 (y/z swapped in clip5.json)
3080 0, 1, 3, 2, // Rotation, joint 0 (y/z swapped in clip5.json)
3081 7, 8, 9, // Scale, joint 0
3082 14, 16, 15, // Location, joint 1 (y/z swapped in clip5.json)
3083 10, 11, 13, 12, // Rotation, joint 1 (y/z swapped in clip5.json)
3084 17, 18, 19, // Scale, joint 1
3085 24, 26, 25, // Location, joint 2 (y/z swapped in clip5.json)
3086 20, 21, 23, 22, // Rotation, joint 2 (y/z swapped in clip5.json)
3087 27, 28, 29, // Scale, joint 2
3088 34, 36, 35, // Location, joint 3 (y/z swapped in clip5.json)
3089 30, 31, 33, 32, // Rotation, joint 3 (y/z swapped in clip5.json)
3090 37, 38, 39 }; // Scale, joint 3
3091 expectedResults.sourceClipMask = { QBitArray(3, true),
3092 QBitArray(4, true),
3093 QBitArray(3, true),
3094 QBitArray(3, true),
3095 QBitArray(4, true),
3096 QBitArray(3, true),
3097 QBitArray(3, true),
3098 QBitArray(4, true),
3099 QBitArray(3, true),
3100 QBitArray(3, true),
3101 QBitArray(4, true),
3102 QBitArray(3, true) };
3103 expectedResults.namesAndTypes = targetChannels;
3104 expectedResults.formattedComponentIndices = targetIndices;
3105
3106 QTest::newRow("skeleton (SQT), 4 joints")
3107 << targetChannels << targetIndices << clip << expectedResults;
3108 }
3109 }
3110
3111 void checkGenerateClipFormatIndices()
3112 {
3113 // GIVEN
3114 QFETCH(QVector<ChannelNameAndType>, targetChannels);
3115 QFETCH(QVector<ComponentIndices>, targetIndices);
3116 QFETCH(AnimationClip *, clip);
3117 QFETCH(ClipFormat, expectedResults);
3118
3119 // WHEN
3120 const ClipFormat actualResults = generateClipFormatIndices(targetChannels,
3121 targetIndices,
3122 clip);
3123
3124 // THEN
3125 QCOMPARE(actualResults.sourceClipIndices.size(), expectedResults.sourceClipIndices.size());
3126 for (int i = 0; i < actualResults.sourceClipIndices.size(); ++i)
3127 QCOMPARE(actualResults.sourceClipIndices[i], expectedResults.sourceClipIndices[i]);
3128
3129 QCOMPARE(actualResults.sourceClipMask.size(), expectedResults.sourceClipMask.size());
3130 for (int i = 0; i < actualResults.sourceClipMask.size(); ++i)
3131 QCOMPARE(actualResults.sourceClipMask[i], expectedResults.sourceClipMask[i]);
3132
3133 QCOMPARE(actualResults.formattedComponentIndices.size(), expectedResults.formattedComponentIndices.size());
3134 for (int i = 0; i < actualResults.formattedComponentIndices.size(); ++i)
3135 QCOMPARE(actualResults.formattedComponentIndices[i], expectedResults.formattedComponentIndices[i]);
3136
3137 QCOMPARE(actualResults.namesAndTypes.size(), expectedResults.namesAndTypes.size());
3138 for (int i = 0; i < actualResults.namesAndTypes.size(); ++i)
3139 QCOMPARE(actualResults.namesAndTypes[i], expectedResults.namesAndTypes[i]);
3140
3141 // Cleanup
3142 delete clip;
3143 }
3144
3145 void checkDefaultValueForChannel_data()
3146 {
3147 QTest::addColumn<Handler *>("handler");
3148 QTest::addColumn<ChannelNameAndType>("channelDescription");
3149 QTest::addColumn<QVector<float>>("expectedResults");
3150
3151 {
3152 auto handler = new Handler();
3153 auto channelMapping = createChannelMapping(handler,
3154 QLatin1String("Location"),
3155 Qt3DCore::QNodeId::createId(),
3156 "translation",
3157 static_cast<int>(QMetaType::QVector3D),
3158 3);
3159 ChannelNameAndType channelDescription;
3160 channelDescription.mappingId = channelMapping->peerId();
3161 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3162 channelDescription.name = QLatin1String("translation");
3163 const QVector<float> expectedResults = { 0.0f, 0.0f, 0.0f };
3164 QTest::newRow("translation") << handler << channelDescription << expectedResults;
3165 }
3166
3167 {
3168 auto handler = new Handler();
3169 auto channelMapping = createChannelMapping(handler,
3170 QLatin1String("Rotation"),
3171 Qt3DCore::QNodeId::createId(),
3172 "rotation",
3173 static_cast<int>(QMetaType::QQuaternion),
3174 4);
3175 ChannelNameAndType channelDescription;
3176 channelDescription.mappingId = channelMapping->peerId();
3177 channelDescription.type = static_cast<int>(QMetaType::QQuaternion);
3178 channelDescription.name = QLatin1String("rotation");
3179 const QVector<float> expectedResults = { 1.0f, 0.0f, 0.0f, 0.0f };
3180 QTest::newRow("rotation") << handler << channelDescription << expectedResults;
3181 }
3182
3183 {
3184 auto handler = new Handler();
3185 auto channelMapping = createChannelMapping(handler,
3186 QLatin1String("Scale"),
3187 Qt3DCore::QNodeId::createId(),
3188 "scale",
3189 static_cast<int>(QMetaType::QVector3D),
3190 3);
3191 ChannelNameAndType channelDescription;
3192 channelDescription.mappingId = channelMapping->peerId();
3193 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3194 channelDescription.name = QLatin1String("scale");
3195 const QVector<float> expectedResults = { 1.0f, 1.0f, 1.0f };
3196 QTest::newRow("scale") << handler << channelDescription << expectedResults;
3197 }
3198
3199 // Test skeleton cases
3200 {
3201 auto handler = new Handler();
3202 auto skeleton = createSkeleton(handler, 2);
3203 skeleton->setJointScale(0, QVector3D(2.0f, 3.0f, 4.0f));
3204
3205 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
3206 ChannelNameAndType channelDescription;
3207 channelDescription.mappingId = channelMapping->peerId();
3208 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3209 channelDescription.jointIndex = 0;
3210 channelDescription.jointTransformComponent = Scale;
3211 const QVector<float> expectedResults = { 2.0f, 3.0f, 4.0f };
3212 QTest::newRow("joint 0 scale") << handler << channelDescription << expectedResults;
3213 }
3214
3215 {
3216 auto handler = new Handler();
3217 auto skeleton = createSkeleton(handler, 2);
3218 skeleton->setJointRotation(0, QQuaternion(1.0f, 0.0f, 0.0f, 0.0f));
3219
3220 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
3221 ChannelNameAndType channelDescription;
3222 channelDescription.mappingId = channelMapping->peerId();
3223 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3224 channelDescription.jointIndex = 0;
3225 channelDescription.jointTransformComponent = Rotation;
3226 const QVector<float> expectedResults = { 1.0f, 0.0f, 0.0f, 0.0f };
3227 QTest::newRow("joint 0 rotation") << handler << channelDescription << expectedResults;
3228 }
3229
3230 {
3231 auto handler = new Handler();
3232 auto skeleton = createSkeleton(handler, 2);
3233 skeleton->setJointTranslation(0, QVector3D(2.0f, 3.0f, 4.0f));
3234
3235 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
3236 ChannelNameAndType channelDescription;
3237 channelDescription.mappingId = channelMapping->peerId();
3238 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3239 channelDescription.jointIndex = 0;
3240 channelDescription.jointTransformComponent = Translation;
3241 const QVector<float> expectedResults = { 2.0f, 3.0f, 4.0f };
3242 QTest::newRow("joint 0 translation") << handler << channelDescription << expectedResults;
3243 }
3244
3245 {
3246 auto handler = new Handler();
3247 auto skeleton = createSkeleton(handler, 2);
3248 skeleton->setJointScale(1, QVector3D(20.0f, 30.0f, 40.0f));
3249
3250 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
3251 ChannelNameAndType channelDescription;
3252 channelDescription.mappingId = channelMapping->peerId();
3253 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3254 channelDescription.jointIndex = 1;
3255 channelDescription.jointTransformComponent = Scale;
3256 const QVector<float> expectedResults = { 20.0f, 30.0f, 40.0f };
3257 QTest::newRow("joint 1 scale") << handler << channelDescription << expectedResults;
3258 }
3259
3260 {
3261 auto handler = new Handler();
3262 auto skeleton = createSkeleton(handler, 2);
3263 skeleton->setJointRotation(1, QQuaternion(1.0f, 0.0f, 0.0f, 0.0f));
3264
3265 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
3266 ChannelNameAndType channelDescription;
3267 channelDescription.mappingId = channelMapping->peerId();
3268 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3269 channelDescription.jointIndex = 1;
3270 channelDescription.jointTransformComponent = Rotation;
3271 const QVector<float> expectedResults = { 1.0f, 0.0f, 0.0f, 0.0f };
3272 QTest::newRow("joint 1 rotation") << handler << channelDescription << expectedResults;
3273 }
3274
3275 {
3276 auto handler = new Handler();
3277 auto skeleton = createSkeleton(handler, 2);
3278 skeleton->setJointTranslation(1, QVector3D(4.0f, 5.0f, 6.0f));
3279
3280 auto channelMapping = createChannelMapping(handler, skeleton->peerId());
3281 ChannelNameAndType channelDescription;
3282 channelDescription.mappingId = channelMapping->peerId();
3283 channelDescription.type = static_cast<int>(QMetaType::QVector3D);
3284 channelDescription.jointIndex = 1;
3285 channelDescription.jointTransformComponent = Translation;
3286 const QVector<float> expectedResults = { 4.0f, 5.0f, 6.0f };
3287 QTest::newRow("joint 1 translation") << handler << channelDescription << expectedResults;
3288 }
3289 }
3290
3291 void checkDefaultValueForChannel()
3292 {
3293 // GIVEN
3294 QFETCH(Handler *, handler);
3295 QFETCH(ChannelNameAndType, channelDescription);
3296 QFETCH(QVector<float>, expectedResults);
3297
3298 // WHEN
3299 auto actualResults = defaultValueForChannel(handler, channelDescription);
3300
3301 // THEN
3302 QCOMPARE(actualResults.size(), expectedResults.size());
3303 for (int i = 0; i < actualResults.size(); ++i) {
3304 QCOMPARE(actualResults[i], expectedResults[i]);
3305 }
3306 }
3307 };
3308
3309 QTEST_MAIN(tst_AnimationUtils)
3310
3311 #include "tst_animationutils.moc"