summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qeasingcurve
diff options
context:
space:
mode:
authorSvenn-Arne Dragly <svenn-arne.dragly@qt.io>2018-03-14 16:36:09 +0100
committerSvenn-Arne Dragly <svenn-arne.dragly@qt.io>2018-03-26 15:53:10 +0000
commit50cfbd6112a2682228cdf34cd72b5abae967cdb2 (patch)
tree369c97df16b15c5f3bd86cc32e888571b1f3cf53 /tests/auto/corelib/tools/qeasingcurve
parent411a4cb67cd3d976ddbd94b37a0ce936bfb223e5 (diff)
Animation: Fix case where QEasingCurve::valueForProgress returns nan
Previously, we would divide by zero in BezierEase::findTForX if factorT3 was zero when solving the cubic equation. This change fixes the problem by adding solutions for the special cases where the cubic equation can be reduced to a quadratic or linear equation. This change also adds tests that cover cases where the equation becomes quadratic, linear or invalid. Task-number: QTBUG-67061 Change-Id: I2b59f7e0392eb807663c3c8927509fd8b226ebc7 Reviewed-by: Christian Stromme <christian.stromme@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools/qeasingcurve')
-rw-r--r--tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp b/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp
index 0714883855..79309f960d 100644
--- a/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp
+++ b/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp
@@ -54,6 +54,7 @@ private slots:
void testCbrtDouble();
void testCbrtFloat();
void cpp11();
+ void quadraticEquation();
};
void tst_QEasingCurve::type()
@@ -804,5 +805,74 @@ void tst_QEasingCurve::cpp11()
#endif
}
+void tst_QEasingCurve::quadraticEquation() {
+ // We find the value for a given time by solving a cubic equation.
+ // ax^3 + bx^2 + cx + d = 0
+ // However, the solver also needs to take care of cases where a = 0,
+ // b = 0 or c = 0, and the equation becomes quadratic, linear or invalid.
+ // A naive cubic solver might divide by zero and return nan, even
+ // when the solution is a real number.
+ // This test should triggers those cases.
+
+ {
+ // If the control points are spaced 1/3 apart of the distance of the
+ // start- and endpoint, the equation becomes linear.
+ QEasingCurve test(QEasingCurve::BezierSpline);
+ const qreal p1 = 1.0 / 3.0;
+ const qreal p2 = 1.0 - 1.0 / 3.0;
+ const qreal p3 = 1.0;
+
+ test.addCubicBezierSegment(QPointF(p1, 0.0), QPointF(p2, 1.0), QPointF(p3, 1.0));
+ QVERIFY(qAbs(test.valueForProgress(0.25) - 0.15625) < 1e-6);
+ QVERIFY(qAbs(test.valueForProgress(0.5) - 0.5) < 1e-6);
+ QVERIFY(qAbs(test.valueForProgress(0.75) - 0.84375) < 1e-6);
+ }
+
+ {
+ // If both the start point and the first control point
+ // are placed a 0.0, and the second control point is
+ // placed at 1/3, we get a case where a = 0 and b != 0
+ // i.e. a quadratic equation.
+ QEasingCurve test(QEasingCurve::BezierSpline);
+ const qreal p1 = 0.0;
+ const qreal p2 = 1.0 / 3.0;
+ const qreal p3 = 1.0;
+ test.addCubicBezierSegment(QPointF(p1, 0.0), QPointF(p2, 1.0), QPointF(p3, 1.0));
+ QVERIFY(qAbs(test.valueForProgress(0.25) - 0.5) < 1e-6);
+ QVERIFY(qAbs(test.valueForProgress(0.5) - 0.792893) < 1e-6);
+ QVERIFY(qAbs(test.valueForProgress(0.75) - 0.950962) < 1e-6);
+ }
+
+ {
+ // If both the start point and the first control point
+ // are placed a 0.0, and the second control point is
+ // placed close to 1/3, we get a case where a = ~0 and b != 0.
+ // It's not truly a quadratic equation, but should be treated
+ // as one, because it causes some cubic solvers to fail.
+ QEasingCurve test(QEasingCurve::BezierSpline);
+ const qreal p1 = 0.0;
+ const qreal p2 = 1.0 / 3.0 + 1e-6;
+ const qreal p3 = 1.0;
+ test.addCubicBezierSegment(QPointF(p1, 0.0), QPointF(p2, 1.0), QPointF(p3, 1.0));
+ QVERIFY(qAbs(test.valueForProgress(0.25) - 0.499999) < 1e-6);
+ QVERIFY(qAbs(test.valueForProgress(0.5) - 0.792892) < 1e-6);
+ QVERIFY(qAbs(test.valueForProgress(0.75) - 0.950961) < 1e-6);
+ }
+
+ {
+ // A bad case, where the segment is of zero length.
+ // However, it might still happen in user code,
+ // and we should return a sensible answer.
+ QEasingCurve test(QEasingCurve::BezierSpline);
+ const qreal p0 = 0.0;
+ const qreal p1 = p0;
+ const qreal p2 = p0;
+ const qreal p3 = p0;
+ test.addCubicBezierSegment(QPointF(p1, 0.0), QPointF(p2, 1.0), QPointF(p3, 1.0));
+ test.addCubicBezierSegment(QPointF(p3, 1.0), QPointF(1.0, 1.0), QPointF(1.0, 1.0));
+ QCOMPARE(test.valueForProgress(0.0), 0.0);
+ }
+}
+
QTEST_MAIN(tst_QEasingCurve)
#include "tst_qeasingcurve.moc"