From 7c6aeace6162bbf2776697f1e248d72733a07ab5 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 2 Mar 2022 12:39:34 -0800 Subject: [PATCH] Point: Add reflect and make scalar ops more flexible (flutter/engine#51) --- .../impeller/geometry/geometry_unittests.cc | 58 +++++++++++++++++++ engine/src/flutter/impeller/geometry/point.h | 33 ++++++++--- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index c663764512..3c3d9314ee 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "impeller/geometry/geometry_unittests.h" +#include #include "flutter/testing/testing.h" #include "impeller/geometry/path.h" #include "impeller/geometry/path_builder.h" @@ -279,6 +280,38 @@ TEST(GeometryTest, CanPerformAlgebraicPointOps) { } } +TEST(GeometryTest, CanPerformAlgebraicPointOpsWithArithmeticTypes) { + // LHS + { + IPoint p1(1, 2); + IPoint p2 = p1 * 2.0f; + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + IPoint p1(2, 6); + IPoint p2 = p1 / 2.0f; + ASSERT_EQ(p2.x, 1u); + ASSERT_EQ(p2.y, 3u); + } + + // RHS + { + IPoint p1(1, 2); + IPoint p2 = 2.0f * p1; + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + IPoint p1(2, 6); + IPoint p2 = 12.0f / p1; + ASSERT_EQ(p2.x, 6u); + ASSERT_EQ(p2.y, 2u); + } +} + TEST(GeometryTest, PointIntegerCoercesToFloat) { // Integer on LHS, float on RHS { @@ -499,6 +532,31 @@ TEST(GeometryTest, PointCrossProduct) { } } +TEST(GeometryTest, PointReflect) { + { + Point axis = Point(0, 1); + Point a(2, 3); + auto reflected = a.Reflect(axis); + auto expected = Point(2, -3); + ASSERT_POINT_NEAR(reflected, expected); + } + + { + Point axis = Point(1, 1).Normalize(); + Point a(1, 0); + auto reflected = a.Reflect(axis); + auto expected = Point(0, -1); + ASSERT_POINT_NEAR(reflected, expected); + } + + { + Point axis = Point(1, 1).Normalize(); + Point a(-1, -1); + auto reflected = a.Reflect(axis); + ASSERT_POINT_NEAR(reflected, -a); + } +} + TEST(GeometryTest, CanConvertBetweenDegressAndRadians) { { auto deg = Degrees{90.0}; diff --git a/engine/src/flutter/impeller/geometry/point.h b/engine/src/flutter/impeller/geometry/point.h index f41184f0b8..c334993c2d 100644 --- a/engine/src/flutter/impeller/geometry/point.h +++ b/engine/src/flutter/impeller/geometry/point.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "impeller/geometry/scalar.h" #include "impeller/geometry/size.h" @@ -121,8 +122,9 @@ struct TPoint { return {x - static_cast(s.width), y - static_cast(s.height)}; } - constexpr TPoint operator*(Scalar scale) const { - return {x * scale, y * scale}; + template >> + constexpr TPoint operator*(U scale) const { + return {static_cast(x * scale), static_cast(y * scale)}; } constexpr TPoint operator*(const TPoint& p) const { @@ -134,7 +136,10 @@ struct TPoint { return {x * static_cast(s.width), y * static_cast(s.height)}; } - constexpr TPoint operator/(Scalar d) const { return {x / d, y / d}; } + template >> + constexpr TPoint operator/(U d) const { + return {static_cast(x / d), static_cast(y / d)}; + } constexpr TPoint operator/(const TPoint& p) const { return {x / p.x, y / p.y}; @@ -175,11 +180,13 @@ struct TPoint { return {x / length, y / length}; } - constexpr Scalar Cross(const TPoint& p) const { - return (x * p.y) - (y * p.x); - } + constexpr Type Cross(const TPoint& p) const { return (x * p.y) - (y * p.x); } - constexpr Scalar Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); } + constexpr Type Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); } + + constexpr TPoint Reflect(const TPoint& axis) const { + return *this - axis * this->Dot(axis) * 2; + } constexpr bool IsZero() const { return x == 0 && y == 0; } }; @@ -226,6 +233,18 @@ constexpr TPoint operator/(const TPoint& p1, const TPoint& p2) { return {static_cast(p1.x) / p2.x, static_cast(p1.y) / p2.y}; } +// RHS algebraic operations with arithmetic types. + +template >> +constexpr TPoint operator*(U s, const TPoint& p) { + return p * s; +} + +template >> +constexpr TPoint operator/(U s, const TPoint& p) { + return {static_cast(s) / p.x, static_cast(s) / p.y}; +} + // RHS algebraic operations with TSize. template