File indexing completed on 2024-05-19 15:26:48

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include <QtTest/QtTest>
0008 
0009 #include "model/animation/keyframe_transition.hpp"
0010 
0011 using namespace glaxnimate;
0012 
0013 class TestCase: public QObject
0014 {
0015     Q_OBJECT
0016 
0017 private Q_SLOTS:
0018 
0019     void test_set_handle()
0020     {
0021         model::KeyframeTransition kft;
0022         kft.set_before(QPointF(.46, .94));
0023         kft.set_after(QPointF(.89, .34));
0024         QCOMPARE(kft.bezier().points()[0], QPointF(0, 0));
0025         QCOMPARE(kft.bezier().points()[1], QPointF(.46, .94));
0026         QCOMPARE(kft.bezier().points()[2], QPointF(.89, .34));
0027         QCOMPARE(kft.bezier().points()[3], QPointF(1, 1));
0028 
0029 //         double step = 0.1;
0030 //         int count = 10;
0031 //         for ( int i = 0; i <= count; i++ )
0032 //         {
0033 //             double x = i * step;
0034 //             qDebug() << i * 10 << kft.lerp_factor(x);
0035 //         }
0036 // QDEBUG : TestCase::test_lerp_factor() 0 0
0037 // QDEBUG : TestCase::test_lerp_factor() 10 0.182092
0038 // QDEBUG : TestCase::test_lerp_factor() 20 0.323608
0039 // QDEBUG : TestCase::test_lerp_factor() 30 0.430119
0040 // QDEBUG : TestCase::test_lerp_factor() 40 0.507039
0041 // QDEBUG : TestCase::test_lerp_factor() 50 0.560079
0042 // QDEBUG : TestCase::test_lerp_factor() 60 0.595965
0043 // QDEBUG : TestCase::test_lerp_factor() 70 0.62387
0044 // QDEBUG : TestCase::test_lerp_factor() 80 0.659026
0045 // QDEBUG : TestCase::test_lerp_factor() 90 0.735291
0046 
0047 
0048     }
0049 
0050     void test_lerp_factor_linear()
0051     {
0052         model::KeyframeTransition kft;
0053 
0054         QCOMPARE(kft.lerp_factor(0), 0);
0055         QCOMPARE(kft.lerp_factor(0.1), 0.1);
0056         QCOMPARE(kft.lerp_factor(0.2), 0.2);
0057         QCOMPARE(kft.lerp_factor(0.3), 0.3);
0058         QCOMPARE(kft.lerp_factor(0.4), 0.4);
0059         QCOMPARE(kft.lerp_factor(0.5), 0.5);
0060         QCOMPARE(kft.lerp_factor(0.6), 0.6);
0061         QCOMPARE(kft.lerp_factor(0.7), 0.7);
0062         QCOMPARE(kft.lerp_factor(0.8), 0.8);
0063         QCOMPARE(kft.lerp_factor(0.9), 0.9);
0064         QCOMPARE(kft.lerp_factor(1), 1);
0065     }
0066 
0067     void benchmark_lerp_factor()
0068     {
0069         model::KeyframeTransition kft;
0070         kft.set_before(QPointF(.46, .94));
0071         kft.set_after(QPointF(.89, .34));
0072         QCOMPARE(kft.bezier().points()[0], QPointF(0, 0));
0073         QCOMPARE(kft.bezier().points()[1], QPointF(.46, .94));
0074         QCOMPARE(kft.bezier().points()[2], QPointF(.89, .34));
0075         QCOMPARE(kft.bezier().points()[3], QPointF(1, 1));
0076 
0077         double step = 0.1;
0078         int count = 10;
0079         QBENCHMARK{
0080             for ( int i = 0; i <= count; i++ )
0081                 kft.lerp_factor(i * step);
0082         }
0083 
0084     }
0085 
0086     void test_lerp_factor()
0087     {
0088         model::KeyframeTransition kft;
0089 
0090         kft.set_before(QPointF(.46, .94));
0091         kft.set_after(QPointF(.89, .34));
0092         QCOMPARE(qRound(kft.lerp_factor(0.1)*100), 18);
0093     }
0094 
0095     void test_lerp_factor_cache_flush()
0096     {
0097         model::KeyframeTransition kft;
0098         QCOMPARE(kft.lerp_factor(0.1), 0.1);
0099 
0100         kft.set_before(QPointF(.46, .94));
0101         kft.set_after(QPointF(.89, .34));
0102         QCOMPARE(qRound(kft.lerp_factor(0.1)*100), 18);
0103     }
0104 
0105     void test_split_hold()
0106     {
0107         model::KeyframeTransition kft({0, 0}, {1, 1}, true);
0108         auto split = kft.split(0.2);
0109         QCOMPARE(split.first.hold(), true);
0110         QCOMPARE(split.second.hold(), true);
0111     }
0112 
0113     void test_split_linear()
0114     {
0115         model::KeyframeTransition kft({1./3., 1./3.}, {2./3., 2./3.}, false);
0116         auto split = kft.split(0.2);
0117 
0118         QCOMPARE(split.first.hold(), false);
0119         QCOMPARE(split.first.before().x(), 1./3.);
0120         QCOMPARE(split.first.before().y(), 1./3.);
0121         QCOMPARE(split.first.after().x(), 2./3.);
0122         QCOMPARE(split.first.after().y(), 2./3.);
0123 
0124         QCOMPARE(split.second.hold(), false);
0125         QCOMPARE(split.second.before().x(), 1./3.);
0126         QCOMPARE(split.second.before().y(), 1./3.);
0127         QCOMPARE(split.second.after().x(), 2./3.);
0128         QCOMPARE(split.second.after().y(), 2./3.);
0129     }
0130 
0131     void test_split_ease()
0132     {
0133         model::KeyframeTransition kft({1./3., 0}, {2./3., 1}, false);
0134         auto split = kft.split(0.5);
0135 
0136         QCOMPARE(split.first.hold(), false);
0137         QCOMPARE(split.first.before().x(), 1./3.);
0138         QCOMPARE(split.first.before().y(), 0);
0139         QCOMPARE(split.first.after().x(), 2./3.);
0140         QCOMPARE(split.first.after().y(), 0.5);
0141 
0142         QCOMPARE(split.second.hold(), false);
0143         QCOMPARE(split.second.before().x(), 1./3.);
0144         QCOMPARE(split.second.before().y(), 0.5);
0145         QCOMPARE(split.second.after().x(), 2./3.);
0146         QCOMPARE(split.second.after().y(), 1.);
0147     }
0148 
0149     void test_split_left()
0150     {
0151         model::KeyframeTransition kft({1, -1}, {0, 2});
0152         float t = 0.146446609407;
0153         auto split = kft.split_t(t).first;
0154 
0155         QCOMPARE(split.hold(), false);
0156         QCOMPARE(float(split.before().x()), 0.453082f);
0157         QCOMPARE(float(split.before().y()), 0.707107f);
0158         QCOMPARE(float(split.after().x()), 0.773459f);
0159         QCOMPARE(float(split.after().y()), 1.f);
0160     }
0161 
0162     void test_split_right()
0163     {
0164         model::KeyframeTransition kft({1, -1}, {0, 2});
0165         float t = 0.146446609407;
0166         auto split = kft.split_t(1-t).second;
0167 
0168         QCOMPARE(split.hold(), false);
0169         QCOMPARE(float(split.before().x()), 1-0.773459f);
0170         QCOMPARE(float(split.before().y()), 0.f);
0171         QCOMPARE(float(split.after().x()), 1-0.453082f);
0172         QCOMPARE(float(split.after().y()), 1-0.707107f);
0173     }
0174 };
0175 
0176 
0177 QTEST_GUILESS_MAIN(TestCase)
0178 #include "test_keyframe_transition.moc"