Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 46184a5

Browse files
committed
[Impeller] Add Rect::Project
1 parent adfc3af commit 46184a5

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

impeller/geometry/geometry_unittests.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,48 @@ TEST(GeometryTest, RectGetPositive) {
21152115
}
21162116
}
21172117

2118+
TEST(GeometryTest, RectScale) {
2119+
{
2120+
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
2121+
auto actual = r.Scale(0);
2122+
auto expected = Rect::MakeLTRB(0, 0, 0, 0);
2123+
ASSERT_RECT_NEAR(expected, actual);
2124+
}
2125+
{
2126+
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
2127+
auto actual = r.Scale(-2);
2128+
auto expected = Rect::MakeLTRB(200, 200, -200, -200);
2129+
ASSERT_RECT_NEAR(expected, actual);
2130+
}
2131+
{
2132+
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
2133+
auto actual = r.Scale(Point{0, 0});
2134+
auto expected = Rect::MakeLTRB(0, 0, 0, 0);
2135+
ASSERT_RECT_NEAR(expected, actual);
2136+
}
2137+
{
2138+
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
2139+
auto actual = r.Scale(Size{-1, -2});
2140+
auto expected = Rect::MakeLTRB(100, 200, -100, -200);
2141+
ASSERT_RECT_NEAR(expected, actual);
2142+
}
2143+
}
2144+
2145+
TEST(GeometryTest, RectProject) {
2146+
{
2147+
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
2148+
auto actual = r.Project(r);
2149+
auto expected = Rect::MakeLTRB(0, 0, 1, 1);
2150+
ASSERT_RECT_NEAR(expected, actual);
2151+
}
2152+
{
2153+
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
2154+
auto actual = r.Project(Rect::MakeLTRB(0, 0, 100, 100));
2155+
auto expected = Rect::MakeLTRB(0.5, 0.5, 1, 1);
2156+
ASSERT_RECT_NEAR(expected, actual);
2157+
}
2158+
}
2159+
21182160
TEST(GeometryTest, CubicPathComponentPolylineDoesNotIncludePointOne) {
21192161
CubicPathComponent component({10, 10}, {20, 35}, {35, 20}, {40, 40});
21202162
auto polyline = component.CreatePolyline(1.0f);

impeller/geometry/rect.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ struct TRect {
9494
{size.width - r.size.width, size.height - r.size.height});
9595
}
9696

97-
constexpr TRect operator*(Type scale) const {
98-
return TRect({origin.x * scale, origin.y * scale},
99-
{size.width * scale, size.height * scale});
100-
}
97+
constexpr TRect operator*(Type scale) const { return Scale(scale); }
10198

10299
constexpr TRect operator*(const TRect& r) const {
103100
return TRect({origin.x * r.origin.x, origin.y * r.origin.y},
@@ -108,6 +105,20 @@ struct TRect {
108105
return origin == r.origin && size == r.size;
109106
}
110107

108+
constexpr TRect Scale(Type scale) const {
109+
return TRect({origin.x * scale, origin.y * scale},
110+
{size.width * scale, size.height * scale});
111+
}
112+
113+
constexpr TRect Scale(TPoint<T> scale) const {
114+
return TRect({origin.x * scale.x, origin.y * scale.y},
115+
{size.width * scale.x, size.height * scale.y});
116+
}
117+
118+
constexpr TRect Scale(TSize<T> scale) const {
119+
return Scale(TPoint<T>(scale));
120+
}
121+
111122
constexpr bool Contains(const TPoint<Type>& p) const {
112123
return p.x >= GetLeft() && p.x < GetRight() && p.y >= GetTop() &&
113124
p.y < GetBottom();
@@ -255,7 +266,7 @@ struct TRect {
255266

256267
/// @brief Returns a rectangle with expanded edges. Negative expansion
257268
/// results in shrinking.
258-
constexpr TRect<T> Expand(T left, T top, T right, T bottom) {
269+
constexpr TRect<T> Expand(T left, T top, T right, T bottom) const {
259270
return TRect(origin.x - left, //
260271
origin.y - top, //
261272
size.width + left + right, //
@@ -264,12 +275,22 @@ struct TRect {
264275

265276
/// @brief Returns a rectangle with expanded edges in all directions.
266277
/// Negative expansion results in shrinking.
267-
constexpr TRect<T> Expand(T amount) {
278+
constexpr TRect<T> Expand(T amount) const {
268279
return TRect(origin.x - amount, //
269280
origin.y - amount, //
270281
size.width + amount * 2, //
271282
size.height + amount * 2);
272283
}
284+
285+
/// @brief Returns a new rectangle that represents the projection of the
286+
/// source rectangle onto this rectangle. In other words, the source
287+
/// rectangle is redefined in terms of the corrdinate space of this
288+
/// rectangle.
289+
constexpr TRect<T> Project(TRect<T> source) const {
290+
return source.Shift(-origin).Scale(
291+
TSize<T>(1.0 / static_cast<Scalar>(size.width),
292+
1.0 / static_cast<Scalar>(size.height)));
293+
}
273294
};
274295

275296
using Rect = TRect<Scalar>;

0 commit comments

Comments
 (0)