summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Strømme <christian.stromme@qt.io>2018-07-04 14:46:34 +0200
committerLaszlo Agocs <laszlo.agocs@qt.io>2018-07-10 09:41:04 +0000
commita772c0f3cc85baed7434c0e0c8d2a6a7b3601364 (patch)
treef1a4491fb0797b5a067e6ba5fd61312e08bc719d
parent67c08c0fb7101291bb44998f52ea8b087e33b1f2 (diff)
More fuzzy evaluation when calculating roots
If the discriminant is close to zero, evaluate it as being zero. Change-Id: Ief2624dd512d703e44d8a0c63431baef3cee7c45 Reviewed-by: Sean Harmer <sean.harmer@kdab.com> Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
-rw-r--r--src/animation/backend/bezierevaluator.cpp2
-rw-r--r--tests/auto/animation/bezierevaluator/tst_bezierevaluator.cpp13
2 files changed, 14 insertions, 1 deletions
diff --git a/src/animation/backend/bezierevaluator.cpp b/src/animation/backend/bezierevaluator.cpp
index 8a7f0139b..d211c584b 100644
--- a/src/animation/backend/bezierevaluator.cpp
+++ b/src/animation/backend/bezierevaluator.cpp
@@ -230,7 +230,7 @@ int BezierEvaluator::findCubicRoots(const float coeffs[4], float roots[3])
const double pCubed = p * p * p;
const double discriminant = q * q + pCubed;
- if (qIsNull(discriminant)) {
+ if (almostZero(discriminant, 1e-6f)) {
if (qIsNull(q)) {
// One repeated triple root
roots[0] = 0.0;
diff --git a/tests/auto/animation/bezierevaluator/tst_bezierevaluator.cpp b/tests/auto/animation/bezierevaluator/tst_bezierevaluator.cpp
index c61ce2d7e..06389bd36 100644
--- a/tests/auto/animation/bezierevaluator/tst_bezierevaluator.cpp
+++ b/tests/auto/animation/bezierevaluator/tst_bezierevaluator.cpp
@@ -187,6 +187,19 @@ private Q_SLOTS:
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()