Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e10bf9c

Browse files
authored
[Impeller] Add Quaternion to Matrix conversion (#38056)
1 parent 662c5f8 commit e10bf9c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

impeller/geometry/geometry_unittests.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,26 @@ TEST(GeometryTest, MatrixVectorMultiplication) {
265265
}
266266
}
267267

268+
TEST(GeometryTest, MatrixMakeRotationFromQuaternion) {
269+
{
270+
auto matrix = Matrix::MakeRotation(Quaternion({1, 0, 0}, kPiOver2));
271+
auto expected = Matrix::MakeRotationX(Radians(kPiOver2));
272+
ASSERT_MATRIX_NEAR(matrix, expected);
273+
}
274+
275+
{
276+
auto matrix = Matrix::MakeRotation(Quaternion({0, 1, 0}, kPiOver2));
277+
auto expected = Matrix::MakeRotationY(Radians(kPiOver2));
278+
ASSERT_MATRIX_NEAR(matrix, expected);
279+
}
280+
281+
{
282+
auto matrix = Matrix::MakeRotation(Quaternion({0, 0, 1}, kPiOver2));
283+
auto expected = Matrix::MakeRotationZ(Radians(kPiOver2));
284+
ASSERT_MATRIX_NEAR(matrix, expected);
285+
}
286+
}
287+
268288
TEST(GeometryTest, MatrixTransformDirection) {
269289
{
270290
auto matrix = Matrix::MakeTranslation({100, 100, 100}) *

impeller/geometry/matrix.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ struct Matrix {
122122
// clang-format on
123123
}
124124

125+
static Matrix MakeRotation(Quaternion q) {
126+
// clang-format off
127+
return Matrix(
128+
1.0 - 2.0 * q.y * q.y - 2.0 * q.z * q.z,
129+
2.0 * q.x * q.y + 2.0 * q.z * q.w,
130+
2.0 * q.x * q.z - 2.0 * q.y * q.w,
131+
0.0,
132+
133+
2.0 * q.x * q.y - 2.0 * q.z * q.w,
134+
1.0 - 2.0 * q.x * q.x - 2.0 * q.z * q.z,
135+
2.0 * q.y * q.z + 2.0 * q.x * q.w,
136+
0.0,
137+
138+
2.0 * q.x * q.z + 2.0 * q.y * q.w,
139+
2.0 * q.y * q.z - 2.0 * q.x * q.w,
140+
1.0 - 2.0 * q.x * q.x - 2.0 * q.y * q.y,
141+
0.0,
142+
143+
0.0,
144+
0.0,
145+
0.0,
146+
1.0);
147+
// clang-format on
148+
}
149+
125150
static Matrix MakeRotation(Scalar radians, const Vector4& r) {
126151
const Vector4 v = r.Normalize();
127152

0 commit comments

Comments
 (0)