From 9792f9203dd08fabc7ee919ac23faa904c4f2558 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 6 Jul 2021 16:44:53 -0700 Subject: [PATCH] Geometry utils for unsigned types. --- .../impeller/geometry/geometry_unittests.cc | 27 ++++++ engine/src/flutter/impeller/geometry/point.h | 58 +++++++----- engine/src/flutter/impeller/geometry/rect.cc | 31 +------ engine/src/flutter/impeller/geometry/rect.h | 90 +++++++++++++------ engine/src/flutter/impeller/geometry/size.h | 5 ++ 5 files changed, 132 insertions(+), 79 deletions(-) diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index bed8c9987f..414c49622e 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -3,6 +3,8 @@ // found in the LICENSE file. #include "flutter/testing/testing.h" +#include "impeller/geometry/point.h" +#include "impeller/geometry/rect.h" #include "impeller/geometry/size.h" namespace impeller { @@ -19,5 +21,30 @@ TEST(GeometryTest, CanGenerateMipCounts) { ASSERT_EQ((Size{-128, 25}.MipCount()), 1u); } +TEST(GeometryTest, CanConvertTTypesExplicitly) { + { + Point p1(1.0, 2.0); + IPoint p2 = static_cast(p1); + ASSERT_EQ(p2.x, 1u); + ASSERT_EQ(p2.y, 2u); + } + + { + Size s1(1.0, 2.0); + ISize s2 = static_cast(s1); + ASSERT_EQ(s2.width, 1u); + ASSERT_EQ(s2.height, 2u); + } + + { + Rect r1(1.0, 2.0, 3.0, 4.0); + IRect r2 = static_cast(r1); + ASSERT_EQ(r2.origin.x, 1u); + ASSERT_EQ(r2.origin.y, 2u); + ASSERT_EQ(r2.size.width, 3u); + ASSERT_EQ(r2.size.height, 4u); + } +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/point.h b/engine/src/flutter/impeller/geometry/point.h index 10c25d996e..ec3e4b5adc 100644 --- a/engine/src/flutter/impeller/geometry/point.h +++ b/engine/src/flutter/impeller/geometry/point.h @@ -12,65 +12,81 @@ namespace impeller { -struct Point { - Scalar x = 0.0; - Scalar y = 0.0; +template +struct TPoint { + using Type = T; - constexpr Point() = default; + Type x = {}; + Type y = {}; - constexpr Point(Scalar x, Scalar y) : x(x), y(y) {} + constexpr TPoint() = default; - constexpr bool operator==(const Point& p) const { + template + explicit constexpr TPoint(const TPoint& other) + : TPoint(static_cast(other.x), static_cast(other.y)) {} + + constexpr TPoint(Type x, Type y) : x(x), y(y) {} + + constexpr bool operator==(const TPoint& p) const { return p.x == x && p.y == y; } - constexpr bool operator!=(const Point& p) const { + constexpr bool operator!=(const TPoint& p) const { return p.x != x || p.y != y; } - constexpr Point operator-() const { return {-x, -y}; } + constexpr TPoint operator-() const { return {-x, -y}; } - constexpr Point operator+(const Point& p) const { return {x + p.x, y + p.y}; } + constexpr TPoint operator+(const TPoint& p) const { + return {x + p.x, y + p.y}; + } - constexpr Point operator+(const Size& s) const { + constexpr TPoint operator+(const TSize& s) const { return {x + s.width, y + s.height}; } - constexpr Point operator-(const Point& p) const { return {x - p.x, y - p.y}; } + constexpr TPoint operator-(const TPoint& p) const { + return {x - p.x, y - p.y}; + } - constexpr Point operator-(const Size& s) const { + constexpr TPoint operator-(const TSize& s) const { return {x - s.width, y - s.height}; } - constexpr Point operator*(Scalar scale) const { + constexpr TPoint operator*(Type scale) const { return {x * scale, y * scale}; } - constexpr Point operator*(const Point& p) const { return {x * p.x, y * p.y}; } + constexpr TPoint operator*(const TPoint& p) const { + return {x * p.x, y * p.y}; + } - constexpr Point operator*(const Size& s) const { + constexpr TPoint operator*(const TSize& s) const { return {x * s.width, y * s.height}; } - constexpr Point operator/(Scalar d) const { return {x / d, y / d}; } + constexpr TPoint operator/(Type d) const { return {x / d, y / d}; } - constexpr Point operator/(const Point& p) const { return {x / p.x, y / p.y}; } + constexpr TPoint operator/(const TPoint& p) const { + return {x / p.x, y / p.y}; + } - constexpr Point operator/(const Size& s) const { + constexpr TPoint operator/(const TSize& s) const { return {x / s.width, y / s.height}; } - constexpr Scalar GetDistanceSquared(const Point& p) const { + constexpr Type GetDistanceSquared(const TPoint& p) const { double dx = p.x - x; double dy = p.y - y; return dx * dx + dy * dy; } - constexpr Scalar GetDistance(const Point& p) const { + constexpr Type GetDistance(const TPoint& p) const { return sqrt(GetDistanceSquared(p)); } }; -static_assert(sizeof(Point) == 2 * sizeof(Scalar)); +using Point = TPoint; +using IPoint = TPoint; } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/rect.cc b/engine/src/flutter/impeller/geometry/rect.cc index 9d7e28fbcc..6b0192ad6f 100644 --- a/engine/src/flutter/impeller/geometry/rect.cc +++ b/engine/src/flutter/impeller/geometry/rect.cc @@ -7,35 +7,6 @@ namespace impeller { -Rect Rect::WithPoint(const Point& p) const { - Rect copy = *this; - if (p.x < origin.x) { - copy.origin.x = p.x; - copy.size.width += (origin.x - p.x); - } - - if (p.y < origin.y) { - copy.origin.y = p.y; - copy.size.height += (origin.y - p.y); - } - - if (p.x > (size.width + origin.x)) { - copy.size.width += p.x - (size.width + origin.x); - } - - if (p.y > (size.height + origin.y)) { - copy.size.height += p.y - (size.height + origin.y); - } - - return copy; -} - -Rect Rect::WithPoints(const std::vector& points) const { - Rect box = *this; - for (const auto& point : points) { - box = box.WithPoint(point); - } - return box; -} +// } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/rect.h b/engine/src/flutter/impeller/geometry/rect.h index 52ad8a54ee..02a51a3642 100644 --- a/engine/src/flutter/impeller/geometry/rect.h +++ b/engine/src/flutter/impeller/geometry/rect.h @@ -12,62 +12,96 @@ namespace impeller { -struct Rect { - Point origin; - Size size; +template +struct TRect { + using Type = T; - constexpr Rect() : origin({0.0, 0.0}), size({0.0, 0.0}) {} + TPoint origin; + TSize size; - constexpr Rect(Size size) : origin({0.0, 0.0}), size(size) {} + constexpr TRect() : origin({0.0, 0.0}), size({0.0, 0.0}) {} - constexpr Rect(Point origin, Size size) : origin(origin), size(size) {} + constexpr TRect(TSize size) : origin({0.0, 0.0}), size(size) {} - constexpr Rect(const Scalar components[4]) + constexpr TRect(TPoint origin, TSize size) + : origin(origin), size(size) {} + + constexpr TRect(const Type components[4]) : origin(components[0], components[1]), size(components[2], components[3]) {} - constexpr Rect(Scalar x, Scalar y, Scalar width, Scalar height) + constexpr TRect(Type x, Type y, Type width, Type height) : origin(x, y), size(width, height) {} - /* - * Operator overloads - */ - constexpr Rect operator+(const Rect& r) const { - return Rect({origin.x + r.origin.x, origin.y + r.origin.y}, - {size.width + r.size.width, size.height + r.size.height}); + template + constexpr explicit TRect(const TRect& other) + : origin(static_cast>(other.origin)), + size(static_cast>(other.size)) {} + + constexpr TRect operator+(const TRect& r) const { + return TRect({origin.x + r.origin.x, origin.y + r.origin.y}, + {size.width + r.size.width, size.height + r.size.height}); } - constexpr Rect operator-(const Rect& r) const { - return Rect({origin.x - r.origin.x, origin.y - r.origin.y}, - {size.width - r.size.width, size.height - r.size.height}); + constexpr TRect operator-(const TRect& r) const { + return TRect({origin.x - r.origin.x, origin.y - r.origin.y}, + {size.width - r.size.width, size.height - r.size.height}); } - constexpr Rect operator*(Scalar scale) const { - return Rect({origin.x * scale, origin.y * scale}, - {size.width * scale, size.height * scale}); + constexpr TRect operator*(Type scale) const { + return TRect({origin.x * scale, origin.y * scale}, + {size.width * scale, size.height * scale}); } - constexpr Rect operator*(const Rect& r) const { - return Rect({origin.x * r.origin.x, origin.y * r.origin.y}, - {size.width * r.size.width, size.height * r.size.height}); + constexpr TRect operator*(const TRect& r) const { + return TRect({origin.x * r.origin.x, origin.y * r.origin.y}, + {size.width * r.size.width, size.height * r.size.height}); } - constexpr bool operator==(const Rect& r) const { + constexpr bool operator==(const TRect& r) const { return origin == r.origin && size == r.size; } - constexpr bool Contains(const Point& p) const { + constexpr bool Contains(const TPoint& p) const { return p.x >= origin.x && p.x <= size.width && p.y >= origin.y && p.y <= size.height; } constexpr bool IsZero() const { return size.IsZero(); } - Rect WithPoint(const Point& p) const; + constexpr TRect WithPoint(const TPoint& p) const { + TRect copy = *this; + if (p.x < origin.x) { + copy.origin.x = p.x; + copy.size.width += (origin.x - p.x); + } - Rect WithPoints(const std::vector& points) const; + if (p.y < origin.y) { + copy.origin.y = p.y; + copy.size.height += (origin.y - p.y); + } + + if (p.x > (size.width + origin.x)) { + copy.size.width += p.x - (size.width + origin.x); + } + + if (p.y > (size.height + origin.y)) { + copy.size.height += p.y - (size.height + origin.y); + } + + return copy; + } + + constexpr TRect WithPoints(const std::vector>& points) const { + TRect box = *this; + for (const auto& point : points) { + box = box.WithPoint(point); + } + return box; + } }; -static_assert(sizeof(Rect) == 4 * sizeof(Scalar)); +using Rect = TRect; +using IRect = TRect; } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/size.h b/engine/src/flutter/impeller/geometry/size.h index ac32ffdd9f..2f0957660c 100644 --- a/engine/src/flutter/impeller/geometry/size.h +++ b/engine/src/flutter/impeller/geometry/size.h @@ -23,6 +23,11 @@ struct TSize { constexpr TSize(Type width, Type height) : width(width), height(height) {} + template + explicit constexpr TSize(const TSize& other) + : TSize(static_cast(other.width), static_cast(other.height)) { + } + static constexpr TSize Infinite() { return TSize{std::numeric_limits::max(), std::numeric_limits::max()};