[Impeller] Add Quaternion to Matrix conversion (flutter/engine#38056)

This commit is contained in:
Brandon DeRosier 2022-12-03 19:17:51 -08:00 committed by GitHub
parent cfccb9ed0b
commit 39362b24b5
2 changed files with 45 additions and 0 deletions

View File

@ -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}) *

View File

@ -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();