Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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}) *
Expand Down
25 changes: 25 additions & 0 deletions impeller/geometry/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down