This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] add support for superellipse. #54562
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ac0765
[Impeller] add support for rectellipse.
jonahwilliams ffddf05
full configuration.
jonahwilliams 190dfc8
linting.
jonahwilliams ded4ae8
quick fix
jonahwilliams 0d161ad
move to entity tests.
jonahwilliams e369328
add doc comment.
jonahwilliams 2e9d74b
Update superellipse_geometry.h
jonahwilliams 4cc92e0
Update superellipse_geometry.cc
jonahwilliams a037a7d
Update superellipse_geometry.cc
jonahwilliams e014ce9
inline radius.
jonahwilliams d684295
Merge branch 'add_rectiple' of github.com:jonahwilliams/engine into a…
jonahwilliams 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
// 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 <vector> | ||
|
||
#include "flutter/impeller/entity/geometry/superellipse_geometry.h" | ||
|
||
#include "impeller/geometry/constants.h" | ||
|
||
namespace impeller { | ||
|
||
SuperellipseGeometry::SuperellipseGeometry(const Point& center, | ||
Scalar radius, | ||
Scalar degree, | ||
Scalar alpha, | ||
Scalar beta) | ||
: center_(center), | ||
degree_(degree), | ||
radius_(radius), | ||
alpha_(alpha), | ||
beta_(beta) {} | ||
|
||
GeometryResult SuperellipseGeometry::GetPositionBuffer( | ||
const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const { | ||
// https://math.stackexchange.com/questions/2573746/superellipse-parametric-equation | ||
Scalar a = alpha_; | ||
Scalar b = beta_; | ||
Scalar n = degree_; | ||
|
||
// TODO(jonahwilliams): determine parameter values based on scaling factor. | ||
Scalar step = kPi / 80; | ||
|
||
// Generate the points for the top left quadrant, and then mirror to the other | ||
// quadrants. | ||
std::vector<Point> points; | ||
points.reserve(41); | ||
for (int i = 0; i <= 40; i++) { | ||
Scalar t = i * step; | ||
Scalar x = a * pow(abs(cos(t)), 2 / n); | ||
Scalar y = b * pow(abs(sin(t)), 2 / n); | ||
points.emplace_back(x * radius_, y * radius_); | ||
} | ||
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. Nit: final points are always computed from 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. Done |
||
|
||
static constexpr Point reflection[4] = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}; | ||
|
||
// Reflect into the 4 quadrants and generate the tessellated mesh. The | ||
// iteration order is reversed so that the trianges are continuous from | ||
// quadrant to quadrant. | ||
std::vector<Point> geometry; | ||
geometry.reserve(1 + 4 * points.size()); | ||
geometry.push_back(center_); | ||
for (auto i = 0u; i < points.size(); i++) { | ||
geometry.push_back(center_ + (reflection[0] * points[i])); | ||
} | ||
for (auto i = 0u; i < points.size(); i++) { | ||
geometry.push_back(center_ + | ||
(reflection[1] * points[points.size() - i - 1])); | ||
} | ||
for (auto i = 0u; i < points.size(); i++) { | ||
geometry.push_back(center_ + (reflection[2] * points[i])); | ||
} | ||
for (auto i = 0u; i < points.size(); i++) { | ||
geometry.push_back(center_ + | ||
(reflection[3] * points[points.size() - i - 1])); | ||
} | ||
|
||
std::vector<uint16_t> indices; | ||
indices.reserve(geometry.size() * 3); | ||
for (auto i = 2u; i < geometry.size(); i++) { | ||
indices.push_back(0); | ||
indices.push_back(i - 1); | ||
indices.push_back(i); | ||
} | ||
|
||
auto& host_buffer = renderer.GetTransientsBuffer(); | ||
return GeometryResult{ | ||
.type = PrimitiveType::kTriangle, | ||
.vertex_buffer = | ||
{ | ||
.vertex_buffer = host_buffer.Emplace( | ||
geometry.data(), geometry.size() * sizeof(Point), | ||
alignof(Point)), | ||
.index_buffer = host_buffer.Emplace( | ||
indices.data(), indices.size() * sizeof(uint16_t), | ||
alignof(uint16_t)), | ||
.vertex_count = indices.size(), | ||
.index_type = IndexType::k16bit, | ||
}, | ||
.transform = entity.GetShaderTransform(pass), | ||
}; | ||
} | ||
|
||
std::optional<Rect> SuperellipseGeometry::GetCoverage( | ||
const Matrix& transform) const { | ||
return Rect::MakeOriginSize(center_ - Point(radius_, radius_), | ||
Size(radius_ * 2, radius_ * 2)); | ||
} | ||
|
||
bool SuperellipseGeometry::CoversArea(const Matrix& transform, | ||
const Rect& rect) const { | ||
return false; | ||
} | ||
|
||
bool SuperellipseGeometry::IsAxisAlignedRect() const { | ||
return false; | ||
} | ||
|
||
} // namespace impeller |
This file contains hidden or 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,62 @@ | ||
// 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_ENTITY_GEOMETRY_SUPERELLIPSE_GEOMETRY_H_ | ||
#define FLUTTER_IMPELLER_ENTITY_GEOMETRY_SUPERELLIPSE_GEOMETRY_H_ | ||
|
||
#include "impeller/entity/geometry/geometry.h" | ||
|
||
namespace impeller { | ||
|
||
/// Geometry class that can generate vertices for a superellipse. | ||
/// | ||
/// A Superellipse is an ellipse-like shape that is defined by the parameters N, | ||
/// alpha, and beta: | ||
/// | ||
/// 1 = |x / b| ^n + |y / a| ^n | ||
/// | ||
/// The radius and center apply a uniform scaling and offset that is separate | ||
/// from alpha or beta. When n = 4, the shape is referred to as a rectellipse. | ||
/// | ||
/// See also: https://en.wikipedia.org/wiki/Superellipse | ||
class SuperellipseGeometry final : public Geometry { | ||
public: | ||
explicit SuperellipseGeometry(const Point& center, | ||
Scalar radius, | ||
Scalar degree, | ||
Scalar alpha, | ||
Scalar beta); | ||
|
||
~SuperellipseGeometry() = default; | ||
|
||
// |Geometry| | ||
bool CoversArea(const Matrix& transform, const Rect& rect) const override; | ||
|
||
// |Geometry| | ||
bool IsAxisAlignedRect() const override; | ||
|
||
private: | ||
// |Geometry| | ||
GeometryResult GetPositionBuffer(const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const override; | ||
|
||
// |Geometry| | ||
std::optional<Rect> GetCoverage(const Matrix& transform) const override; | ||
|
||
Point center_; | ||
// 4 is a rectellipse | ||
Scalar degree_; | ||
Scalar radius_; | ||
Scalar alpha_; | ||
Scalar beta_; | ||
|
||
SuperellipseGeometry(const SuperellipseGeometry&) = delete; | ||
|
||
SuperellipseGeometry& operator=(const SuperellipseGeometry&) = delete; | ||
}; | ||
|
||
} // namespace impeller | ||
|
||
#endif // FLUTTER_IMPELLER_ENTITY_GEOMETRY_SUPERELLIPSE_GEOMETRY_H_ |
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.
Putting this part in the Tessellator would allow it to leverage the "GetTrigsForDivisions" caching.
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.
I'm going to leave this out for now so its stays separate before we decide how to ship it, but I agree it should eventually live in the tessellator.
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.
Good call if it isolates it until we figure out its future. It will matter a little for benchmarks, but not for testing its usefulness.