From 516d2f21d7f2bd3158229982ebce4fe178fd3fd8 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Mon, 26 Sep 2022 17:33:34 -0700 Subject: [PATCH] [Impeller] Add lerps (flutter/engine#36430) --- .../impeller/geometry/geometry_unittests.cc | 21 +++++++++++++++++++ engine/src/flutter/impeller/geometry/point.h | 4 ++++ engine/src/flutter/impeller/geometry/vector.h | 10 ++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index 47ef5878530..f2fed24cf4d 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -965,6 +965,27 @@ TEST(GeometryTest, PointAngleTo) { } } +TEST(GeometryTest, PointLerp) { + Point p(1, 2); + Point result = p.Lerp({5, 10}, 0.75); + Point expected(4, 8); + ASSERT_POINT_NEAR(result, expected); +} + +TEST(GeometryTest, Vector3Lerp) { + Vector3 p(1, 2, 3); + Vector3 result = p.Lerp({5, 10, 15}, 0.75); + Vector3 expected(4, 8, 12); + ASSERT_VECTOR3_NEAR(result, expected); +} + +TEST(GeometryTest, Vector4Lerp) { + Vector4 p(1, 2, 3, 4); + Vector4 result = p.Lerp({5, 10, 15, 20}, 0.75); + Vector4 expected(4, 8, 12, 16); + ASSERT_VECTOR4_NEAR(result, expected); +} + TEST(GeometryTest, CanUseVector3AssignmentOperators) { { Vector3 p(1, 2, 4); diff --git a/engine/src/flutter/impeller/geometry/point.h b/engine/src/flutter/impeller/geometry/point.h index 04b5461dbe0..42b97e2ca70 100644 --- a/engine/src/flutter/impeller/geometry/point.h +++ b/engine/src/flutter/impeller/geometry/point.h @@ -208,6 +208,10 @@ struct TPoint { return Radians{std::atan2(this->Cross(p), this->Dot(p))}; } + constexpr TPoint Lerp(const TPoint& p, Scalar t) const { + return *this + (p - *this) * t; + } + constexpr bool IsZero() const { return x == 0 && y == 0; } }; diff --git a/engine/src/flutter/impeller/geometry/vector.h b/engine/src/flutter/impeller/geometry/vector.h index 2456b896b83..10066340a5b 100644 --- a/engine/src/flutter/impeller/geometry/vector.h +++ b/engine/src/flutter/impeller/geometry/vector.h @@ -140,6 +140,10 @@ struct Vector3 { return Vector3(x / scale, y / scale, z / scale); } + constexpr Vector3 Lerp(const Vector3& v, Scalar t) const { + return *this + (v - *this) * t; + } + /** * Make a linear combination of two vectors and return the result. * @@ -224,10 +228,14 @@ struct Vector4 { return Vector4(x - v.x, y - v.y, z - v.z, w - v.w); } - constexpr Vector4 operator*(float f) const { + constexpr Vector4 operator*(Scalar f) const { return Vector4(x * f, y * f, z * f, w * f); } + constexpr Vector4 Lerp(const Vector4& v, Scalar t) const { + return *this + (v - *this) * t; + } + std::string ToString() const; };