summaryrefslogtreecommitdiffstats
path: root/tests/auto/animation/bezierevaluator/tst_bezierevaluator.cpp
blob: bc9f5b0339fd97502bb92d27230c99c5961e8c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtTest/QTest>
#include <Qt3DAnimation/private/bezierevaluator_p.h>
#include <Qt3DAnimation/private/keyframe_p.h>
#include <QtCore/qlist.h>

#include <cmath>

Q_DECLARE_METATYPE(Qt3DAnimation::Animation::Keyframe)

using namespace Qt3DAnimation;
using namespace Qt3DAnimation::Animation;

static const QList<float> globalTimes = { 0.0f,      1.00375f, 2.48f,
                                          4.37625f,  6.64f,    9.21875f,
                                         12.06f,    15.11125f, 18.32f,
                                         21.63375f, 25.0f,     28.36625f,
                                         31.68f,    34.88875f, 37.94f,
                                         40.78125f, 43.36f,    45.62375f,
                                         47.52f,    48.99625f, 50.0f };

static const QList<float> globalValues = { 0.0f,     0.03625f, 0.14f,
                                           0.30375f, 0.52f,    0.78125f,
                                           1.08f,    1.40875f, 1.76f,
                                           2.12625f, 2.5f,     2.87375f,
                                           3.24f,    3.59125f, 3.92f,
                                           4.21875f, 4.48f,    4.69625f,
                                           4.86f,    4.96375f, 5.0f };


class tst_BezierEvaluator : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void checkFindCubicRoots_data()
    {
        // Test data verified on Wolfram Alpha with snippets such as:
        // Plot[x^3-5x^2+x+3,{x,-3,6}]
        // Solve[x^3-5x^2+x+3,x]
        // If you need more, try these at https://www.wolframalpha.com/

        QTest::addColumn<float>("a");
        QTest::addColumn<float>("b");
        QTest::addColumn<float>("c");
        QTest::addColumn<float>("d");
        QTest::addColumn<int>("rootCount");
        QTest::addColumn<QList<float>>("roots");

        float a = 1.0f;
        float b = 0.0f;
        float c = 0.0f;
        float d = 0.0f;
        int rootCount = 1;
        QVector<float> roots = { 0.0f };
        QTest::newRow("a=1, b=0, c=0, d=0") << a << b << c << d << rootCount << roots;
        roots.clear();

        a = 1.0f;
        b = -1.0f;
        c = 1.0f;
        d = -1.0f;
        rootCount = 1;
        roots.resize(1);
        roots[0] = 1.0f;
        QTest::newRow("a=1, b=-1, c=1, d=-1") << a << b << c << d << rootCount << roots;
        roots.clear();

        a = 1.0f;
        b = -2.0f;
        c = 1.0f;
        d = -1.0f;
        rootCount = 1;
        roots.resize(1);
        roots[0] = 1.7548776f;
        QTest::newRow("a=1, b=-2, c=1, d=-1") << a << b << c << d << rootCount << roots;
        roots.clear();

        a = 1.0f;
        b = -5.0f;
        c = 1.0f;
        d = 3.0f;
        rootCount = 3;
        roots.resize(3);
        roots[0] = 2.0f + std::sqrt(7.0f);
        roots[1] = 1.0f;
        roots[2] = 2.0f - std::sqrt(7.0f);
        QTest::newRow("a=1, b=-5, c=1, d=3") << a << b << c << d << rootCount << roots;
        roots.clear();

        // quadratic equation
        a = 0.0f;
        b = 9.0f;
        c = 11.0f;
        d = 3.0f;
        roots.resize(2);
        roots[0] = -11.0f/18.0f + std::sqrt(13.0f) / 18.0f;
        roots[1] = -11.0f/18.0f - std::sqrt(13.0f) / 18.0f;
        QTest::newRow("a=0, b=9, c=11, d=3") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // quadratic equation with discriminant = 0
        a = 0.0f;
        b = 1.0f;
        c = 2.0f;
        d = 1.0f;
        roots.resize(1);
        roots[0] = -1.f;
        QTest::newRow("a=0, b=1, c=2, d=1") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // quadratic equation with discriminant < 0
        a = 0.0f;
        b = 1.0f;
        c = 4.0f;
        d = 8.0f;
        roots.resize(0);
        QTest::newRow("a=0, b=1, c=4, d=8") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // linear equation
        a = 0.0f;
        b = 0.0f;
        c = 2.0f;
        d = 1.0f;
        roots.resize(1);
        roots[0] = -0.5f;
        QTest::newRow("a=0, b=0, c=2, d=1") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // linear equation
        a = 0.0f;
        b = 0.0f;
        c = 8.0f;
        d = -5.0f;
        roots.resize(1);
        roots[0] = -d/c;
        QTest::newRow("a=0, b=0, c=8, d=-5") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // invalid equation
        a = 0.0f;
        b = 0.0f;
        c = 0.0f;
        d = -5.0f;
        roots.resize(0);
        QTest::newRow("a=0, b=0, c=0, d=-5") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // Invalid equation
        a = 0.0f;
        b = 0.0f;
        c = 0.0f;
        d = 42.0f;
        roots.resize(0);
        QTest::newRow("a=0, b=0, c=0, d=42") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // almost linear equation
        a = 1.90735e-06f;
        b = -2.86102e-06f;
        c = 5.0;
        d = 0.0;
        roots.resize(1);
        roots[0] = -d/c;
        QTest::newRow("a=~0, b=~0, c=5, d=0") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // case that produces a result just below zero, that should be evaluated as zero
        a = -0.75f;
        b = 0.75f;
        c = 2.5;
        d = 0.0;
        roots.resize(3);
        roots[0] = 2.39297f;
        roots[1] = 0.f;
        roots[2] = -1.39297f;
        QTest::newRow("a=-0.75, b=0.75, c=2.5, d=0") << a << b << c << d << roots.size() << roots;
        roots.clear();

        // Case that produces a discriminant that is close enough to zero that it should be
        // evaluated as zero.
        // Expected roots = 0.0, ~1.5
        a = -3.998f;
        b = 5.997f;
        c = 0.0f;
        d = 0.0f;
        roots.resize(2);
        roots[0] = 1.5f;
        roots[1] = 0.0f;
        QTest::newRow("a=-3.998, b=5.997, c=0, d=0") << a << b << c << d << roots.size() << roots;
        roots.clear();
    }

    void checkFindCubicRoots()
    {
        QFETCH(float, a);
        QFETCH(float, b);
        QFETCH(float, c);
        QFETCH(float, d);
        QFETCH(int, rootCount);
        QFETCH(QList<float>, roots);

        float coeffs[4];
        coeffs[0] = d;
        coeffs[1] = c;
        coeffs[2] = b;
        coeffs[3] = a;

        float results[3];
        const int foundRootCount = BezierEvaluator::findCubicRoots(coeffs, results);

        QCOMPARE(foundRootCount, rootCount);
        for (int i = 0; i < rootCount; ++i)
            QCOMPARE(results[i], roots[i]);
    }

    void checkParameterForTime_data()
    {
        QTest::addColumn<float>("t0");
        QTest::addColumn<Keyframe>("kf0");
        QTest::addColumn<float>("t1");
        QTest::addColumn<Keyframe>("kf1");
        QTest::addColumn<QList<float>>("times");
        QTest::addColumn<QList<float>>("bezierParamters");

        {
            float t0 = 0.0f;
            Keyframe kf0{0.0f, {-5.0f, 0.0f}, {5.0f, 0.0f}, QKeyFrame::BezierInterpolation};
            float t1 = 50.0f;
            Keyframe kf1{5.0f, {45.0f, 5.0f}, {55.0f, 5.0f}, QKeyFrame::BezierInterpolation};
            const int count = 21;

            QList<float> bezierParameters;
            float deltaU = 1.0f / float(count - 1);
            for (int i = 0; i < count; ++i)
                bezierParameters.push_back(float(i) * deltaU);

            QTest::newRow("t=0 to t=50, default easing") << t0 << kf0
                                                         << t1 << kf1
                                                         << globalTimes << bezierParameters;
        }
        {
            // This test creates a case where the coefficients for finding
            // the cubic roots will be a = 0, b = 0, c ~= 6.28557 d ~= -6.28557
            // Because c ~= d, the answer should be one root = 1, but
            // because of numerical imprecision, it will be slightly larger.
            // We have a fuzzy check in parameterForTime that takes care of this.
            float t0 = 3.71443009f;
            Keyframe kf0{150.0f, {0.0f, 0.0f}, {5.80961999f, 150.0f}, QKeyFrame::BezierInterpolation};
            float t1 = 10.0f;
            Keyframe kf1{-150.0f, {7.904809959f, 150.0f}, {0.f, 0.f}, QKeyFrame::BezierInterpolation};
            QList<float> times = { 10.f };
            QList<float> results = { 1.0f };
            QTest::newRow("t=0 to t=10, regression") << t0 << kf0
                                                     << t1 << kf1
                                                     << times << results;
        }
    }

    void checkParameterForTime()
    {
        // GIVEN
        QFETCH(float, t0);
        QFETCH(Keyframe, kf0);
        QFETCH(float, t1);
        QFETCH(Keyframe, kf1);
        QFETCH(QList<float>, times);
        QFETCH(QList<float>, bezierParamters);

        // WHEN
        BezierEvaluator bezier(t0, kf0, t1, kf1);

        // THEN
        for (int i = 0; i < times.size(); ++i) {
            const float time = times[i];
            const float u = bezier.parameterForTime(time);
            QCOMPARE(u, bezierParamters[i]);
        }
    }

    void checkValueForTime_data()
    {
        QTest::addColumn<float>("t0");
        QTest::addColumn<Keyframe>("kf0");
        QTest::addColumn<float>("t1");
        QTest::addColumn<Keyframe>("kf1");
        QTest::addColumn<QList<float>>("times");
        QTest::addColumn<QList<float>>("values");

        float t0 = 0.0f;
        Keyframe kf0{0.0f, {-5.0f, 0.0f}, {5.0f, 0.0f}, QKeyFrame::BezierInterpolation};
        float t1 = 50.0f;
        Keyframe kf1{5.0f, {45.0f, 5.0f}, {55.0f, 5.0f}, QKeyFrame::BezierInterpolation};

        QTest::newRow("t=0, value=0 to t=50, value=5, default easing") << t0 << kf0
                                                     << t1 << kf1
                                                     << globalTimes << globalValues;
    }

    void checkValueForTime()
    {
        // GIVEN
        QFETCH(float, t0);
        QFETCH(Keyframe, kf0);
        QFETCH(float, t1);
        QFETCH(Keyframe, kf1);
        QFETCH(QList<float>, times);
        QFETCH(QList<float>, values);

        // WHEN
        BezierEvaluator bezier(t0, kf0, t1, kf1);

        // THEN
        for (int i = 0; i < times.size(); ++i) {
            const float time = times[i];
            const float value = bezier.valueForTime(time);
            QCOMPARE(value, values[i]);
        }
    }
};

QTEST_APPLESS_MAIN(tst_BezierEvaluator)

#include "tst_bezierevaluator.moc"