From f220d6ce4a57dcc0b5d16d073d129e0e9b481fbe Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Tue, 2 Aug 2022 15:07:23 -0700 Subject: [PATCH] [Impeller] Add Rect::GetPositive (flutter/engine#35083) --- .../impeller/geometry/geometry_unittests.cc | 14 ++++++++++++++ engine/src/flutter/impeller/geometry/rect.h | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index 31e876d9b8..c2b10a32d6 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -1049,6 +1049,20 @@ TEST(GeometryTest, RectMakePointBounds) { } } +TEST(GeometryTest, RectGetPositive) { + { + Rect r{100, 200, 300, 400}; + auto actual = r.GetPositive(); + ASSERT_RECT_NEAR(r, actual); + } + { + Rect r{100, 200, -100, -100}; + auto actual = r.GetPositive(); + Rect expected(0, 100, 100, 100); + ASSERT_RECT_NEAR(expected, actual); + } +} + TEST(GeometryTest, CubicPathComponentPolylineDoesNotIncludePointOne) { CubicPathComponent component({10, 10}, {20, 35}, {35, 20}, {40, 40}); SmoothingApproximation approximation; diff --git a/engine/src/flutter/impeller/geometry/rect.h b/engine/src/flutter/impeller/geometry/rect.h index 3165681f89..8b0a169d02 100644 --- a/engine/src/flutter/impeller/geometry/rect.h +++ b/engine/src/flutter/impeller/geometry/rect.h @@ -139,6 +139,14 @@ struct TRect { return {left, top, right, bottom}; } + /// @brief Get a version of this rectangle that has a non-negative size. + constexpr TRect GetPositive() const { + auto ltrb = GetLTRB(); + return MakeLTRB(ltrb[0], ltrb[1], ltrb[2], ltrb[3]); + } + + /// @brief Get the points that represent the 4 corners of this rectangle. The + /// order is: Top left, top right, bottom left, bottom right. constexpr std::array, 4> GetPoints() const { auto [left, top, right, bottom] = GetLTRB(); return {TPoint(left, top), TPoint(right, top), TPoint(left, bottom),