| 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 <Qt3DCore/qjoint.h> |
| 6 | #include <Qt3DCore/qskeleton.h> |
| 7 | #include <Qt3DAnimation/private/skeleton_p.h> |
| 8 | #include <Qt3DAnimation/private/handler_p.h> |
| 9 | #include <Qt3DCore/private/qscene_p.h> |
| 10 | #include <Qt3DCore/private/qbackendnode_p.h> |
| 11 | #include <qbackendnodetester.h> |
| 12 | #include <testarbiter.h> |
| 13 | |
| 14 | #include <QtGui/qquaternion.h> |
| 15 | |
| 16 | using namespace Qt3DCore; |
| 17 | using namespace Qt3DAnimation; |
| 18 | using namespace Qt3DAnimation::Animation; |
| 19 | |
| 20 | Q_DECLARE_METATYPE(Skeleton*) |
| 21 | |
| 22 | class tst_Skeleton : public QBackendNodeTester |
| 23 | { |
| 24 | Q_OBJECT |
| 25 | |
| 26 | private Q_SLOTS: |
| 27 | void checkPeerPropertyMirroring() |
| 28 | { |
| 29 | // GIVEN |
| 30 | Handler handler; |
| 31 | Skeleton backendSkeleton; |
| 32 | backendSkeleton.setHandler(&handler); |
| 33 | QSkeleton skeleton; |
| 34 | auto rootJoint = new QJoint(); |
| 35 | rootJoint->setName(QLatin1String("rootJoint")); |
| 36 | auto childJoint = new QJoint(); |
| 37 | childJoint->setName(QLatin1String("childJoint")); |
| 38 | rootJoint->addChildJoint(childJoint); |
| 39 | skeleton.setRootJoint(rootJoint); |
| 40 | |
| 41 | // WHEN |
| 42 | simulateInitializationSync(&skeleton, &backendSkeleton); |
| 43 | |
| 44 | // THEN - nothing mirrored from frontend |
| 45 | QCOMPARE(backendSkeleton.peerId(), skeleton.id()); |
| 46 | QCOMPARE(backendSkeleton.isEnabled(), skeleton.isEnabled()); |
| 47 | QCOMPARE(backendSkeleton.jointNames().size(), 0); |
| 48 | QCOMPARE(backendSkeleton.jointLocalPoses().size(), 0); |
| 49 | } |
| 50 | |
| 51 | void checkJointName() |
| 52 | { |
| 53 | // GIVEN |
| 54 | Skeleton backendSkeleton; |
| 55 | const QVector<QString> jointNames = { QLatin1String("rootJoint"), |
| 56 | QLatin1String("child1Joint"), |
| 57 | QLatin1String("child2Joint") }; |
| 58 | |
| 59 | // WHEN |
| 60 | backendSkeleton.setJointNames(jointNames); |
| 61 | |
| 62 | // THEN |
| 63 | const int jointNameCount = jointNames.size(); |
| 64 | for (int i = 0; i < jointNameCount; ++i) { |
| 65 | QCOMPARE(jointNames[i], backendSkeleton.jointName(i)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void checkInitialAndCleanedUpState() |
| 70 | { |
| 71 | // GIVEN |
| 72 | Handler handler; |
| 73 | Skeleton backendSkeleton; |
| 74 | backendSkeleton.setHandler(&handler); |
| 75 | |
| 76 | // THEN |
| 77 | QVERIFY(backendSkeleton.peerId().isNull()); |
| 78 | QCOMPARE(backendSkeleton.isEnabled(), false); |
| 79 | QCOMPARE(backendSkeleton.jointNames().size(), 0); |
| 80 | QCOMPARE(backendSkeleton.jointLocalPoses().size(), 0); |
| 81 | |
| 82 | // GIVEN |
| 83 | const QVector<QString> names = { QLatin1String("root"), |
| 84 | QLatin1String("child1"), |
| 85 | QLatin1String("child2") }; |
| 86 | const QVector<Sqt> localPoses = { Sqt(), Sqt(), Sqt() }; |
| 87 | |
| 88 | // WHEN |
| 89 | backendSkeleton.setJointNames(names); |
| 90 | backendSkeleton.setJointLocalPoses(localPoses); |
| 91 | backendSkeleton.cleanup(); |
| 92 | |
| 93 | // THEN |
| 94 | QCOMPARE(backendSkeleton.isEnabled(), false); |
| 95 | QCOMPARE(backendSkeleton.jointNames().size(), 0); |
| 96 | QCOMPARE(backendSkeleton.jointLocalPoses().size(), 0); |
| 97 | } |
| 98 | |
| 99 | void checkPropertyChanges() |
| 100 | { |
| 101 | // GIVEN |
| 102 | Handler handler; |
| 103 | Skeleton backendSkeleton; |
| 104 | backendSkeleton.setHandler(&handler); |
| 105 | QSkeleton skeleton; |
| 106 | simulateInitializationSync(&skeleton, &backendSkeleton); |
| 107 | |
| 108 | // WHEN |
| 109 | skeleton.setEnabled(false); |
| 110 | backendSkeleton.syncFromFrontEnd(&skeleton, false); |
| 111 | |
| 112 | // THEN |
| 113 | QCOMPARE(backendSkeleton.isEnabled(), false); |
| 114 | } |
| 115 | |
| 116 | void checkJointTransforms_data() |
| 117 | { |
| 118 | QTest::addColumn<Skeleton*>("skeleton"); |
| 119 | QTest::addColumn<QList<Sqt>>("jointTransforms"); |
| 120 | |
| 121 | const int count = 5; |
| 122 | auto skeleton = new Skeleton; |
| 123 | skeleton->setJointCount(count); |
| 124 | QList<Sqt> jointTransforms; |
| 125 | jointTransforms.reserve(count); |
| 126 | for (int i = 0; i < count; ++i) { |
| 127 | const float f = float(i); |
| 128 | Sqt transform; |
| 129 | transform.scale = QVector3D(f, f, f); |
| 130 | transform.rotation = QQuaternion(f, 1.0f, 0.0f, 0.0f).normalized(); |
| 131 | transform.translation = QVector3D(1.0 * f, 2.0 * f, 3.0 * f); |
| 132 | skeleton->setJointScale(i, transform.scale); |
| 133 | skeleton->setJointRotation(i, transform.rotation); |
| 134 | skeleton->setJointTranslation(i, transform.translation); |
| 135 | jointTransforms.push_back(transform); |
| 136 | } |
| 137 | |
| 138 | QTest::newRow("5 joints") << skeleton << jointTransforms; |
| 139 | } |
| 140 | |
| 141 | void checkJointTransforms() |
| 142 | { |
| 143 | // GIVEN |
| 144 | QFETCH(Skeleton*, skeleton); |
| 145 | QFETCH(QList<Sqt>, jointTransforms); |
| 146 | |
| 147 | const int count = skeleton->jointCount(); |
| 148 | for (int i = 0; i < count; ++i) { |
| 149 | // WHEN |
| 150 | const QVector3D s = skeleton->jointScale(i); |
| 151 | |
| 152 | // THEN |
| 153 | QCOMPARE(s, jointTransforms[i].scale); |
| 154 | |
| 155 | // WHEN |
| 156 | const QQuaternion q = skeleton->jointRotation(i); |
| 157 | |
| 158 | // THEN |
| 159 | QCOMPARE(q, jointTransforms[i].rotation); |
| 160 | |
| 161 | // WHEN |
| 162 | const QVector3D t = skeleton->jointTranslation(i); |
| 163 | |
| 164 | // THEN |
| 165 | QCOMPARE(t, jointTransforms[i].translation); |
| 166 | } |
| 167 | |
| 168 | // Cleanup |
| 169 | delete skeleton; |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | QTEST_APPLESS_MAIN(tst_Skeleton) |
| 174 | |
| 175 | #include "tst_skeleton.moc" |