Debian Codegraph @

tests/auto/animation/bezierevaluator/tst_bezierevaluator.cpp

raw symbols SCIP
1 // Copyright (C) 2015 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/bezierevaluator_p.h>
6 #include <Qt3DAnimation/private/keyframe_p.h>
7 #include <QtCore/qlist.h>
8
9 #include <cmath>
10
11 Q_DECLARE_METATYPE(Qt3DAnimation::Animation::Keyframe)
12
13 using namespace Qt3DAnimation;
14 using namespace Qt3DAnimation::Animation;
15
16 static const QList<float> globalTimes = { 0.0f, 1.00375f, 2.48f,
17 4.37625f, 6.64f, 9.21875f,
18 12.06f, 15.11125f, 18.32f,
19 21.63375f, 25.0f, 28.36625f,
20 31.68f, 34.88875f, 37.94f,
21 40.78125f, 43.36f, 45.62375f,
22 47.52f, 48.99625f, 50.0f };
23
24 static const QList<float> globalValues = { 0.0f, 0.03625f, 0.14f,
25 0.30375f, 0.52f, 0.78125f,
26 1.08f, 1.40875f, 1.76f,
27 2.12625f, 2.5f, 2.87375f,
28 3.24f, 3.59125f, 3.92f,
29 4.21875f, 4.48f, 4.69625f,
30 4.86f, 4.96375f, 5.0f };
31
32
33 class tst_BezierEvaluator : public QObject
34 {
35 Q_OBJECT
36
37 private Q_SLOTS:
38 void checkFindCubicRoots_data()
39 {
40 // Test data verified on Wolfram Alpha with snippets such as:
41 // Plot[x^3-5x^2+x+3,{x,-3,6}]
42 // Solve[x^3-5x^2+x+3,x]
43 // If you need more, try these at https://www.wolframalpha.com/
44
45 QTest::addColumn<float>("a");
46 QTest::addColumn<float>("b");
47 QTest::addColumn<float>("c");
48 QTest::addColumn<float>("d");
49 QTest::addColumn<int>("rootCount");
50 QTest::addColumn<QList<float>>("roots");
51
52 float a = 1.0f;
53 float b = 0.0f;
54 float c = 0.0f;
55 float d = 0.0f;
56 int rootCount = 1;
57 QVector<float> roots = { 0.0f };
58 QTest::newRow("a=1, b=0, c=0, d=0") << a << b << c << d << rootCount << roots;
59 roots.clear();
60
61 a = 1.0f;
62 b = -1.0f;
63 c = 1.0f;
64 d = -1.0f;
65 rootCount = 1;
66 roots.resize(1);
67 roots[0] = 1.0f;
68 QTest::newRow("a=1, b=-1, c=1, d=-1") << a << b << c << d << rootCount << roots;
69 roots.clear();
70
71 a = 1.0f;
72 b = -2.0f;
73 c = 1.0f;
74 d = -1.0f;
75 rootCount = 1;
76 roots.resize(1);
77 roots[0] = 1.7548776f;
78 QTest::newRow("a=1, b=-2, c=1, d=-1") << a << b << c << d << rootCount << roots;
79 roots.clear();
80
81 a = 1.0f;
82 b = -5.0f;
83 c = 1.0f;
84 d = 3.0f;
85 rootCount = 3;
86 roots.resize(3);
87 roots[0] = 2.0f + std::sqrt(7.0f);
88 roots[1] = 1.0f;
89 roots[2] = 2.0f - std::sqrt(7.0f);
90 QTest::newRow("a=1, b=-5, c=1, d=3") << a << b << c << d << rootCount << roots;
91 roots.clear();
92
93 // quadratic equation
94 a = 0.0f;
95 b = 9.0f;
96 c = 11.0f;
97 d = 3.0f;
98 roots.resize(2);
99 roots[0] = -11.0f/18.0f + std::sqrt(13.0f) / 18.0f;
100 roots[1] = -11.0f/18.0f - std::sqrt(13.0f) / 18.0f;
101 QTest::newRow("a=0, b=9, c=11, d=3") << a << b << c << d << roots.size() << roots;
102 roots.clear();
103
104 // quadratic equation with discriminant = 0
105 a = 0.0f;
106 b = 1.0f;
107 c = 2.0f;
108 d = 1.0f;
109 roots.resize(1);
110 roots[0] = -1.f;
111 QTest::newRow("a=0, b=1, c=2, d=1") << a << b << c << d << roots.size() << roots;
112 roots.clear();
113
114 // quadratic equation with discriminant < 0
115 a = 0.0f;
116 b = 1.0f;
117 c = 4.0f;
118 d = 8.0f;
119 roots.resize(0);
120 QTest::newRow("a=0, b=1, c=4, d=8") << a << b << c << d << roots.size() << roots;
121 roots.clear();
122
123 // linear equation
124 a = 0.0f;
125 b = 0.0f;
126 c = 2.0f;
127 d = 1.0f;
128 roots.resize(1);
129 roots[0] = -0.5f;
130 QTest::newRow("a=0, b=0, c=2, d=1") << a << b << c << d << roots.size() << roots;
131 roots.clear();
132
133 // linear equation
134 a = 0.0f;
135 b = 0.0f;
136 c = 8.0f;
137 d = -5.0f;
138 roots.resize(1);
139 roots[0] = -d/c;
140 QTest::newRow("a=0, b=0, c=8, d=-5") << a << b << c << d << roots.size() << roots;
141 roots.clear();
142
143 // invalid equation
144 a = 0.0f;
145 b = 0.0f;
146 c = 0.0f;
147 d = -5.0f;
148 roots.resize(0);
149 QTest::newRow("a=0, b=0, c=0, d=-5") << a << b << c << d << roots.size() << roots;
150 roots.clear();
151
152 // Invalid equation
153 a = 0.0f;
154 b = 0.0f;
155 c = 0.0f;
156 d = 42.0f;
157 roots.resize(0);
158 QTest::newRow("a=0, b=0, c=0, d=42") << a << b << c << d << roots.size() << roots;
159 roots.clear();
160
161 // almost linear equation
162 a = 1.90735e-06f;
163 b = -2.86102e-06f;
164 c = 5.0;
165 d = 0.0;
166 roots.resize(1);
167 roots[0] = -d/c;
168 QTest::newRow("a=~0, b=~0, c=5, d=0") << a << b << c << d << roots.size() << roots;
169 roots.clear();
170
171 // case that produces a result just below zero, that should be evaluated as zero
172 a = -0.75f;
173 b = 0.75f;
174 c = 2.5;
175 d = 0.0;
176 roots.resize(3);
177 roots[0] = 2.39297f;
178 roots[1] = 0.f;
179 roots[2] = -1.39297f;
180 QTest::newRow("a=-0.75, b=0.75, c=2.5, d=0") << a << b << c << d << roots.size() << roots;
181 roots.clear();
182
183 // Case that produces a discriminant that is close enough to zero that it should be
184 // evaluated as zero.
185 // Expected roots = 0.0, ~1.5
186 a = -3.998f;
187 b = 5.997f;
188 c = 0.0f;
189 d = 0.0f;
190 roots.resize(2);
191 roots[0] = 1.5f;
192 roots[1] = 0.0f;
193 QTest::newRow("a=-3.998, b=5.997, c=0, d=0") << a << b << c << d << roots.size() << roots;
194 roots.clear();
195 }
196
197 void checkFindCubicRoots()
198 {
199 QFETCH(float, a);
200 QFETCH(float, b);
201 QFETCH(float, c);
202 QFETCH(float, d);
203 QFETCH(int, rootCount);
204 QFETCH(QList<float>, roots);
205
206 float coeffs[4];
207 coeffs[0] = d;
208 coeffs[1] = c;
209 coeffs[2] = b;
210 coeffs[3] = a;
211
212 float results[3];
213 const int foundRootCount = BezierEvaluator::findCubicRoots(coeffs, results);
214
215 QCOMPARE(foundRootCount, rootCount);
216 for (int i = 0; i < rootCount; ++i)
217 QCOMPARE(results[i], roots[i]);
218 }
219
220 void checkParameterForTime_data()
221 {
222 QTest::addColumn<float>("t0");
223 QTest::addColumn<Keyframe>("kf0");
224 QTest::addColumn<float>("t1");
225 QTest::addColumn<Keyframe>("kf1");
226 QTest::addColumn<QList<float>>("times");
227 QTest::addColumn<QList<float>>("bezierParamters");
228
229 {
230 float t0 = 0.0f;
231 Keyframe kf0{0.0f, {-5.0f, 0.0f}, {5.0f, 0.0f}, QKeyFrame::BezierInterpolation};
232 float t1 = 50.0f;
233 Keyframe kf1{5.0f, {45.0f, 5.0f}, {55.0f, 5.0f}, QKeyFrame::BezierInterpolation};
234 const int count = 21;
235
236 QList<float> bezierParameters;
237 float deltaU = 1.0f / float(count - 1);
238 for (int i = 0; i < count; ++i)
239 bezierParameters.push_back(float(i) * deltaU);
240
241 QTest::newRow("t=0 to t=50, default easing") << t0 << kf0
242 << t1 << kf1
243 << globalTimes << bezierParameters;
244 }
245 {
246 // This test creates a case where the coefficients for finding
247 // the cubic roots will be a = 0, b = 0, c ~= 6.28557 d ~= -6.28557
248 // Because c ~= d, the answer should be one root = 1, but
249 // because of numerical imprecision, it will be slightly larger.
250 // We have a fuzzy check in parameterForTime that takes care of this.
251 float t0 = 3.71443009f;
252 Keyframe kf0{150.0f, {0.0f, 0.0f}, {5.80961999f, 150.0f}, QKeyFrame::BezierInterpolation};
253 float t1 = 10.0f;
254 Keyframe kf1{-150.0f, {7.904809959f, 150.0f}, {0.f, 0.f}, QKeyFrame::BezierInterpolation};
255 QList<float> times = { 10.f };
256 QList<float> results = { 1.0f };
257 QTest::newRow("t=0 to t=10, regression") << t0 << kf0
258 << t1 << kf1
259 << times << results;
260 }
261 }
262
263 void checkParameterForTime()
264 {
265 // GIVEN
266 QFETCH(float, t0);
267 QFETCH(Keyframe, kf0);
268 QFETCH(float, t1);
269 QFETCH(Keyframe, kf1);
270 QFETCH(QList<float>, times);
271 QFETCH(QList<float>, bezierParamters);
272
273 // WHEN
274 BezierEvaluator bezier(t0, kf0, t1, kf1);
275
276 // THEN
277 for (int i = 0; i < times.size(); ++i) {
278 const float time = times[i];
279 const float u = bezier.parameterForTime(time);
280 QCOMPARE(u, bezierParamters[i]);
281 }
282 }
283
284 void checkValueForTime_data()
285 {
286 QTest::addColumn<float>("t0");
287 QTest::addColumn<Keyframe>("kf0");
288 QTest::addColumn<float>("t1");
289 QTest::addColumn<Keyframe>("kf1");
290 QTest::addColumn<QList<float>>("times");
291 QTest::addColumn<QList<float>>("values");
292
293 float t0 = 0.0f;
294 Keyframe kf0{0.0f, {-5.0f, 0.0f}, {5.0f, 0.0f}, QKeyFrame::BezierInterpolation};
295 float t1 = 50.0f;
296 Keyframe kf1{5.0f, {45.0f, 5.0f}, {55.0f, 5.0f}, QKeyFrame::BezierInterpolation};
297
298 QTest::newRow("t=0, value=0 to t=50, value=5, default easing") << t0 << kf0
299 << t1 << kf1
300 << globalTimes << globalValues;
301 }
302
303 void checkValueForTime()
304 {
305 // GIVEN
306 QFETCH(float, t0);
307 QFETCH(Keyframe, kf0);
308 QFETCH(float, t1);
309 QFETCH(Keyframe, kf1);
310 QFETCH(QList<float>, times);
311 QFETCH(QList<float>, values);
312
313 // WHEN
314 BezierEvaluator bezier(t0, kf0, t1, kf1);
315
316 // THEN
317 for (int i = 0; i < times.size(); ++i) {
318 const float time = times[i];
319 const float value = bezier.valueForTime(time);
320 QCOMPARE(value, values[i]);
321 }
322 }
323 };
324
325 QTEST_APPLESS_MAIN(tst_BezierEvaluator)
326
327 #include "tst_bezierevaluator.moc"