[Impeller] Add quaternion vector multiplication (flutter/engine#36374)
This commit is contained in:
@@ -404,6 +404,48 @@ TEST(GeometryTest, QuaternionLerp) {
|
||||
ASSERT_QUATERNION_NEAR(q3, expected);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, QuaternionVectorMultiply) {
|
||||
{
|
||||
Quaternion q({0, 0, 1}, 0);
|
||||
Vector3 v(0, 1, 0);
|
||||
|
||||
Vector3 result = q * v;
|
||||
Vector3 expected(0, 1, 0);
|
||||
|
||||
ASSERT_VECTOR3_NEAR(result, expected);
|
||||
}
|
||||
|
||||
{
|
||||
Quaternion q({0, 0, 1}, k2Pi);
|
||||
Vector3 v(1, 0, 0);
|
||||
|
||||
Vector3 result = q * v;
|
||||
Vector3 expected(1, 0, 0);
|
||||
|
||||
ASSERT_VECTOR3_NEAR(result, expected);
|
||||
}
|
||||
|
||||
{
|
||||
Quaternion q({0, 0, 1}, kPiOver4);
|
||||
Vector3 v(0, 1, 0);
|
||||
|
||||
Vector3 result = q * v;
|
||||
Vector3 expected(-k1OverSqrt2, k1OverSqrt2, 0);
|
||||
|
||||
ASSERT_VECTOR3_NEAR(result, expected);
|
||||
}
|
||||
|
||||
{
|
||||
Quaternion q(Vector3(1, 0, 1).Normalize(), kPi);
|
||||
Vector3 v(0, 0, -1);
|
||||
|
||||
Vector3 result = q * v;
|
||||
Vector3 expected(-1, 0, 0);
|
||||
|
||||
ASSERT_VECTOR3_NEAR(result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(GeometryTest, EmptyPath) {
|
||||
auto path = PathBuilder{}.TakePath();
|
||||
ASSERT_EQ(path.GetComponentCount(), 1u);
|
||||
|
||||
@@ -60,6 +60,13 @@ struct Quaternion {
|
||||
return {scale * x, scale * y, scale * z, scale * w};
|
||||
}
|
||||
|
||||
Vector3 operator*(Vector3 vector) const {
|
||||
Vector3 v(x, y, z);
|
||||
return v * v.Dot(vector) * 2 + //
|
||||
vector * (w * w - v.Dot(v)) + //
|
||||
v.Cross(vector) * 2 * w;
|
||||
}
|
||||
|
||||
Quaternion operator+(const Quaternion& o) const {
|
||||
return {x + o.x, y + o.y, z + o.z, w + o.w};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user