[Impeller] Add Rect::Project (flutter/engine#43026)

- Utility to project a rectangle onto another rectangle.
- Fix constness for Rect::Expand.
- Add named methods for scaling.
This commit is contained in:
Brandon DeRosier
2023-06-21 17:17:51 -07:00
committed by GitHub
parent e2c36cac36
commit cec295a528
2 changed files with 69 additions and 6 deletions

View File

@@ -2115,6 +2115,48 @@ TEST(GeometryTest, RectGetPositive) {
}
}
TEST(GeometryTest, RectScale) {
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(0);
auto expected = Rect::MakeLTRB(0, 0, 0, 0);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(-2);
auto expected = Rect::MakeLTRB(200, 200, -200, -200);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(Point{0, 0});
auto expected = Rect::MakeLTRB(0, 0, 0, 0);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(Size{-1, -2});
auto expected = Rect::MakeLTRB(100, 200, -100, -200);
ASSERT_RECT_NEAR(expected, actual);
}
}
TEST(GeometryTest, RectProject) {
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Project(r);
auto expected = Rect::MakeLTRB(0, 0, 1, 1);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Project(Rect::MakeLTRB(0, 0, 100, 100));
auto expected = Rect::MakeLTRB(0.5, 0.5, 1, 1);
ASSERT_RECT_NEAR(expected, actual);
}
}
TEST(GeometryTest, CubicPathComponentPolylineDoesNotIncludePointOne) {
CubicPathComponent component({10, 10}, {20, 35}, {35, 20}, {40, 40});
auto polyline = component.CreatePolyline(1.0f);

View File

@@ -94,10 +94,7 @@ struct TRect {
{size.width - r.size.width, size.height - r.size.height});
}
constexpr TRect operator*(Type scale) const {
return TRect({origin.x * scale, origin.y * scale},
{size.width * scale, size.height * scale});
}
constexpr TRect operator*(Type scale) const { return Scale(scale); }
constexpr TRect operator*(const TRect& r) const {
return TRect({origin.x * r.origin.x, origin.y * r.origin.y},
@@ -108,6 +105,20 @@ struct TRect {
return origin == r.origin && size == r.size;
}
constexpr TRect Scale(Type scale) const {
return TRect({origin.x * scale, origin.y * scale},
{size.width * scale, size.height * scale});
}
constexpr TRect Scale(TPoint<T> scale) const {
return TRect({origin.x * scale.x, origin.y * scale.y},
{size.width * scale.x, size.height * scale.y});
}
constexpr TRect Scale(TSize<T> scale) const {
return Scale(TPoint<T>(scale));
}
constexpr bool Contains(const TPoint<Type>& p) const {
return p.x >= GetLeft() && p.x < GetRight() && p.y >= GetTop() &&
p.y < GetBottom();
@@ -255,7 +266,7 @@ struct TRect {
/// @brief Returns a rectangle with expanded edges. Negative expansion
/// results in shrinking.
constexpr TRect<T> Expand(T left, T top, T right, T bottom) {
constexpr TRect<T> Expand(T left, T top, T right, T bottom) const {
return TRect(origin.x - left, //
origin.y - top, //
size.width + left + right, //
@@ -264,12 +275,22 @@ struct TRect {
/// @brief Returns a rectangle with expanded edges in all directions.
/// Negative expansion results in shrinking.
constexpr TRect<T> Expand(T amount) {
constexpr TRect<T> Expand(T amount) const {
return TRect(origin.x - amount, //
origin.y - amount, //
size.width + amount * 2, //
size.height + amount * 2);
}
/// @brief Returns a new rectangle that represents the projection of the
/// source rectangle onto this rectangle. In other words, the source
/// rectangle is redefined in terms of the corrdinate space of this
/// rectangle.
constexpr TRect<T> Project(TRect<T> source) const {
return source.Shift(-origin).Scale(
TSize<T>(1.0 / static_cast<Scalar>(size.width),
1.0 / static_cast<Scalar>(size.height)));
}
};
using Rect = TRect<Scalar>;