[Impeller] Improved numerical stability in cubic path boundary calculations (flutter/engine#42720)

fix https://github.com/flutter/flutter/issues/128532

In this commit, we refined the method for calculating cubic path boundaries in CubicPathBoundingPopulateValues function. By adopting an alternative quadratic formula from Numerical Recipes in C (The code is ported from skia's `SkFindUnitQuadRoots`),  we mitigated numerical errors, hence boosting precision and reliability in handling cubic paths.
This commit is contained in:
ColdPaleLight
2023-06-11 12:02:05 +08:00
committed by GitHub
parent 9f6caec38a
commit 4f49c89943
2 changed files with 20 additions and 2 deletions

View File

@@ -671,6 +671,16 @@ TEST(GeometryTest, BoundingBoxOfCompositePathIsCorrect) {
ASSERT_RECT_NEAR(actual.value(), expected);
}
TEST(GeometryTest, ExtremaOfCubicPathComponentIsCorrect) {
CubicPathComponent cubic{{11.769268, 252.883148},
{-6.2857933, 204.356461},
{-4.53997231, 156.552902},
{17.0067291, 109.472488}};
auto points = cubic.Extrema();
ASSERT_EQ(points.size(), static_cast<size_t>(3));
ASSERT_POINT_NEAR(points[2], cubic.Solve(0.455916));
}
TEST(GeometryTest, PathGetBoundingBoxForCubicWithNoDerivativeRootsIsCorrect) {
PathBuilder builder;
// Straight diagonal line.

View File

@@ -284,15 +284,23 @@ static void CubicPathBoundingPopulateValues(std::vector<Scalar>& values,
Scalar rootB2Minus4AC = ::sqrt(b2Minus4AC);
/* From Numerical Recipes in C.
*
* q = -1/2 (b + sign(b) sqrt[b^2 - 4ac])
* x1 = q / a
* x2 = c / q
*/
Scalar q = (b < 0) ? -(b - rootB2Minus4AC) / 2 : -(b + rootB2Minus4AC) / 2;
{
Scalar t = (-b + rootB2Minus4AC) / (2.0 * a);
Scalar t = q / a;
if (t >= 0.0 && t <= 1.0) {
values.emplace_back(t);
}
}
{
Scalar t = (-b - rootB2Minus4AC) / (2.0 * a);
Scalar t = c / q;
if (t >= 0.0 && t <= 1.0) {
values.emplace_back(t);
}