-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Impeller] Use Wang's formula for quad/cubic subdivision. #52079
Merged
auto-submit
merged 4 commits into
flutter:main
from
jonahwilliams:use_wangs_formula_for_division
Apr 15, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "impeller/geometry/wangs_formula.h" | ||
|
||
namespace impeller { | ||
|
||
namespace { | ||
|
||
// Don't allow linearized segments to be off by more than 1/4th of a pixel from | ||
// the true curve. This value should be scaled by the max basis of the | ||
// X and Y directions. | ||
constexpr static Scalar kPrecision = 4; | ||
|
||
constexpr Scalar length(Point n) { | ||
Point nn = n * n; | ||
return std::sqrt(nn.x + nn.y); | ||
} | ||
|
||
} // namespace | ||
|
||
Scalar ComputeCubicSubdivisions(Scalar scale_factor, | ||
Point p0, | ||
Point p1, | ||
Point p2, | ||
Point p3) { | ||
Scalar k = scale_factor * .75f * kPrecision; | ||
Point a = (p0 - p1 * 2 + p2).Abs(); | ||
Point b = (p1 - p2 * 2 + p3).Abs(); | ||
return std::sqrt(k * length(a.Max(b))); | ||
} | ||
|
||
Scalar ComputeQuadradicSubdivisions(Scalar scale_factor, | ||
Point p0, | ||
Point p1, | ||
Point p2) { | ||
Scalar k = scale_factor * .25f * kPrecision; | ||
return std::sqrt(k * length(p0 - p1 * 2 + p2)); | ||
} | ||
|
||
Scalar ComputeQuadradicSubdivisions(Scalar scale_factor, | ||
const QuadraticPathComponent& quad) { | ||
return ComputeQuadradicSubdivisions(scale_factor, quad.p1, quad.cp, quad.p2); | ||
} | ||
|
||
Scalar ComputeCubicSubdivisions(float scale_factor, | ||
const CubicPathComponent& cub) { | ||
return ComputeCubicSubdivisions(scale_factor, cub.p1, cub.cp1, cub.cp2, | ||
cub.p2); | ||
} | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_IMPELLER_GEOMETRY_WANGS_FORMULA_H_ | ||
#define FLUTTER_IMPELLER_GEOMETRY_WANGS_FORMULA_H_ | ||
|
||
#include "impeller/geometry/path_component.h" | ||
#include "impeller/geometry/point.h" | ||
#include "impeller/geometry/scalar.h" | ||
|
||
// Skia GPU Ports | ||
|
||
// Wang's formula gives the minimum number of evenly spaced (in the parametric | ||
// sense) line segments that a bezier curve must be chopped into in order to | ||
// guarantee all lines stay within a distance of "1/precision" pixels from the | ||
// true curve. Its definition for a bezier curve of degree "n" is as follows: | ||
// | ||
// maxLength = max([length(p[i+2] - 2p[i+1] + p[i]) for (0 <= i <= n-2)]) | ||
// numParametricSegments = sqrt(maxLength * precision * n*(n - 1)/8) | ||
// | ||
// (Goldman, Ron. (2003). 5.6.3 Wang's Formula. "Pyramid Algorithms: A Dynamic | ||
// Programming Approach to Curves and Surfaces for Geometric Modeling". Morgan | ||
// Kaufmann Publishers.) | ||
namespace impeller { | ||
|
||
/// Returns the minimum number of evenly spaced (in the parametric sense) line | ||
/// segments that the cubic must be chopped into in order to guarantee all lines | ||
/// stay within a distance of "1/intolerance" pixels from the true curve. | ||
/// | ||
/// The scale_factor should be the max basis XY of the current transform. | ||
Scalar ComputeCubicSubdivisions(Scalar scale_factor, | ||
Point p0, | ||
Point p1, | ||
Point p2, | ||
Point p3); | ||
|
||
/// Returns the minimum number of evenly spaced (in the parametric sense) line | ||
/// segments that the quadratic must be chopped into in order to guarantee all | ||
/// lines stay within a distance of "1/intolerance" pixels from the true curve. | ||
/// | ||
/// The scale_factor should be the max basis XY of the current transform. | ||
Scalar ComputeQuadradicSubdivisions(Scalar scale_factor, | ||
Point p0, | ||
Point p1, | ||
Point p2); | ||
|
||
/// Returns the minimum number of evenly spaced (in the parametric sense) line | ||
/// segments that the quadratic must be chopped into in order to guarantee all | ||
/// lines stay within a distance of "1/intolerance" pixels from the true curve. | ||
/// | ||
/// The scale_factor should be the max basis XY of the current transform. | ||
Scalar ComputeQuadradicSubdivisions(Scalar scale_factor, | ||
const QuadraticPathComponent& quad); | ||
|
||
/// Returns the minimum number of evenly spaced (in the parametric sense) line | ||
/// segments that the cubic must be chopped into in order to guarantee all lines | ||
/// stay within a distance of "1/intolerance" pixels from the true curve. | ||
/// | ||
/// The scale_factor should be the max basis XY of the current transform. | ||
Scalar ComputeCubicSubdivisions(float scale_factor, | ||
const CubicPathComponent& cub); | ||
} // namespace impeller | ||
|
||
#endif // FLUTTER_IMPELLER_GEOMETRY_WANGS_FORMULA_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing a case that no longer matters since we don't use the tessellator. i suspect it broke because we're now generating fewer line segments, since our cubic division isn't as cranky.
As a follow up I plan to delete most of this code.