From a41edcb25e50a0064492067360748aaf857822f0 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Sat, 3 Dec 2022 17:56:27 -0800 Subject: [PATCH] [Impeller] Add Quaternion version of Matrix::MakeRotation --- impeller/geometry/geometry_unittests.cc | 20 ++++++++++++++++++++ impeller/geometry/matrix.h | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/impeller/geometry/geometry_unittests.cc b/impeller/geometry/geometry_unittests.cc index 95a0287eef600..f60cf50bfeaeb 100644 --- a/impeller/geometry/geometry_unittests.cc +++ b/impeller/geometry/geometry_unittests.cc @@ -265,6 +265,26 @@ TEST(GeometryTest, MatrixVectorMultiplication) { } } +TEST(GeometryTest, MatrixMakeRotationFromQuaternion) { + { + auto matrix = Matrix::MakeRotation(Quaternion({1, 0, 0}, kPiOver2)); + auto expected = Matrix::MakeRotationX(Radians(kPiOver2)); + ASSERT_MATRIX_NEAR(matrix, expected); + } + + { + auto matrix = Matrix::MakeRotation(Quaternion({0, 1, 0}, kPiOver2)); + auto expected = Matrix::MakeRotationY(Radians(kPiOver2)); + ASSERT_MATRIX_NEAR(matrix, expected); + } + + { + auto matrix = Matrix::MakeRotation(Quaternion({0, 0, 1}, kPiOver2)); + auto expected = Matrix::MakeRotationZ(Radians(kPiOver2)); + ASSERT_MATRIX_NEAR(matrix, expected); + } +} + TEST(GeometryTest, MatrixTransformDirection) { { auto matrix = Matrix::MakeTranslation({100, 100, 100}) * diff --git a/impeller/geometry/matrix.h b/impeller/geometry/matrix.h index 9829317deb939..c739dae66faa5 100644 --- a/impeller/geometry/matrix.h +++ b/impeller/geometry/matrix.h @@ -122,6 +122,31 @@ struct Matrix { // clang-format on } + static Matrix MakeRotation(Quaternion q) { + // clang-format off + return Matrix( + 1.0 - 2.0 * q.y * q.y - 2.0 * q.z * q.z, + 2.0 * q.x * q.y + 2.0 * q.z * q.w, + 2.0 * q.x * q.z - 2.0 * q.y * q.w, + 0.0, + + 2.0 * q.x * q.y - 2.0 * q.z * q.w, + 1.0 - 2.0 * q.x * q.x - 2.0 * q.z * q.z, + 2.0 * q.y * q.z + 2.0 * q.x * q.w, + 0.0, + + 2.0 * q.x * q.z + 2.0 * q.y * q.w, + 2.0 * q.y * q.z - 2.0 * q.x * q.w, + 1.0 - 2.0 * q.x * q.x - 2.0 * q.y * q.y, + 0.0, + + 0.0, + 0.0, + 0.0, + 1.0); + // clang-format on + } + static Matrix MakeRotation(Scalar radians, const Vector4& r) { const Vector4 v = r.Normalize();