-
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 3 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,59 @@ | ||
// 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. | ||
constexpr static Scalar kPrecision = 4; | ||
|
||
static inline Scalar length(Point n) { | ||
Point nn = n * n; | ||
return std::sqrt(nn.x + nn.y); | ||
} | ||
|
||
static inline Point Max(Point a, Point b) { | ||
return Point{ | ||
a.x > b.x ? a.x : b.x, // | ||
a.y > b.y ? a.y : b.y // | ||
}; | ||
} | ||
|
||
} // namespace | ||
|
||
Scalar ComputeCubicSubdivisions(Scalar intolerance, | ||
Point p0, | ||
Point p1, | ||
Point p2, | ||
Point p3) { | ||
Scalar k = intolerance * .75f * kPrecision; | ||
Point a = (p0 - p1 * 2 + p2).Abs(); | ||
Point b = (p1 - p2 * 2 + p3).Abs(); | ||
return std::sqrt(k * length(Max(a, b))); | ||
} | ||
|
||
Scalar ComputeQuadradicSubdivisions(Scalar intolerance, | ||
Point p0, | ||
Point p1, | ||
Point p2) { | ||
Scalar k = intolerance * .25f * kPrecision; | ||
return std::sqrt(k * length(p0 - p1 * 2 + p2)); | ||
} | ||
|
||
Scalar ComputeQuadradicSubdivisions(Scalar intolerance, | ||
const QuadraticPathComponent& quad) { | ||
return ComputeQuadradicSubdivisions(intolerance, quad.p1, quad.cp, quad.p2); | ||
} | ||
|
||
Scalar ComputeCubicSubdivisions(float intolerance, | ||
const CubicPathComponent& cub) { | ||
return ComputeCubicSubdivisions(intolerance, 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,57 @@ | ||
// 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. | ||
Scalar ComputeCubicSubdivisions(Scalar intolerance, | ||
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. | ||
Scalar ComputeQuadradicSubdivisions(Scalar intolerance, | ||
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. | ||
Scalar ComputeQuadradicSubdivisions(Scalar intolerance, | ||
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. | ||
Scalar ComputeCubicSubdivisions(float intolerance, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,30 +78,6 @@ TEST(TessellatorTest, TessellatorBuilderReturnsCorrectResultStatus) { | |
|
||
ASSERT_EQ(result, Tessellator::Result::kInputError); | ||
} | ||
|
||
// More than uint16 points, odd fill mode. | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
Tessellator t; | ||
PathBuilder builder = {}; | ||
for (auto i = 0; i < 1000; i++) { | ||
builder.AddCircle(Point(i, i), 4); | ||
} | ||
auto path = builder.TakePath(FillType::kOdd); | ||
bool no_indices = false; | ||
size_t count = 0u; | ||
Tessellator::Result result = t.Tessellate( | ||
path, 1.0f, | ||
[&no_indices, &count](const float* vertices, size_t vertices_count, | ||
const uint16_t* indices, size_t indices_count) { | ||
no_indices = indices == nullptr; | ||
count = vertices_count; | ||
return true; | ||
}); | ||
|
||
ASSERT_TRUE(no_indices); | ||
ASSERT_TRUE(count >= USHRT_MAX); | ||
ASSERT_EQ(result, Tessellator::Result::kSuccess); | ||
} | ||
} | ||
|
||
TEST(TessellatorTest, TessellateConvex) { | ||
|
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.
Don't we already have
Point::Max
andPoint::GetLength
?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.
Switched to Point::Max. The length measurement is different however.