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
47 changes: 47 additions & 0 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,53 @@ TEST(GeometryTest, MatrixMakePerspective) {
}
}

TEST(GeometryTest, MatrixGetBasisVectors) {
{
auto m = Matrix();
Vector3 x = m.GetBasisX();
Vector3 y = m.GetBasisY();
Vector3 z = m.GetBasisZ();
ASSERT_VECTOR3_NEAR(x, Vector3(1, 0, 0));
ASSERT_VECTOR3_NEAR(y, Vector3(0, 1, 0));
ASSERT_VECTOR3_NEAR(z, Vector3(0, 0, 1));
}

{
auto m = Matrix::MakeRotationZ(Radians{kPiOver2}) *
Matrix::MakeRotationX(Radians{kPiOver2}) *
Matrix::MakeScale(Vector3(2, 3, 4));
Vector3 x = m.GetBasisX();
Vector3 y = m.GetBasisY();
Vector3 z = m.GetBasisZ();
ASSERT_VECTOR3_NEAR(x, Vector3(0, 2, 0));
ASSERT_VECTOR3_NEAR(y, Vector3(0, 0, 3));
ASSERT_VECTOR3_NEAR(z, Vector3(4, 0, 0));
}
}

TEST(GeometryTest, MatrixGetDirectionScale) {
{
auto m = Matrix();
Scalar result = m.GetDirectionScale(Vector3{1, 0, 0});
ASSERT_FLOAT_EQ(result, 1);
}

{
auto m = Matrix::MakeRotationX(Degrees{10}) *
Matrix::MakeRotationY(Degrees{83}) *
Matrix::MakeRotationZ(Degrees{172});
Scalar result = m.GetDirectionScale(Vector3{0, 1, 0});
ASSERT_FLOAT_EQ(result, 1);
}

{
auto m = Matrix::MakeRotationZ(Radians{kPiOver2}) *
Matrix::MakeScale(Vector3(3, 4, 5));
Scalar result = m.GetDirectionScale(Vector3{2, 0, 0});
ASSERT_FLOAT_EQ(result, 8);
}
}

TEST(GeometryTest, QuaternionLerp) {
auto q1 = Quaternion{{0.0, 0.0, 1.0}, 0.0};
auto q2 = Quaternion{{0.0, 0.0, 1.0}, kPiOver4};
Expand Down
16 changes: 13 additions & 3 deletions impeller/geometry/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,20 @@ struct Matrix {

Scalar GetMaxBasisLength() const;

constexpr Vector3 GetBasisX() const { return Vector3(m[0], m[1], m[2]); }

constexpr Vector3 GetBasisY() const { return Vector3(m[4], m[5], m[6]); }

constexpr Vector3 GetBasisZ() const { return Vector3(m[8], m[9], m[10]); }

constexpr Vector3 GetScale() const {
return Vector3(Vector3(m[0], m[1], m[2]).Length(),
Vector3(m[4], m[5], m[6]).Length(),
Vector3(m[8], m[9], m[10]).Length());
return Vector3(GetBasisX().Length(), GetBasisY().Length(),
GetBasisZ().Length());
}

constexpr Scalar GetDirectionScale(Vector3 direction) const {
return 1.0 / (this->Invert() * direction.Normalize()).Length() *
direction.Length();
}

constexpr bool IsAffine() const {
Expand Down
14 changes: 13 additions & 1 deletion impeller/geometry/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Vector3 {
*
* @return the calculated length.
*/
Scalar Length() const { return sqrt(x * x + y * y + z * z); }
constexpr Scalar Length() const { return sqrt(x * x + y * y + z * z); }

constexpr Vector3 Normalize() const {
const auto len = Length();
Expand Down Expand Up @@ -130,6 +130,18 @@ struct Vector3 {
std::string ToString() const;
};

// RHS algebraic operations with arithmetic types.

template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr Vector3 operator*(U s, const Vector3& p) {
return p * s;
}

template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr Vector3 operator/(U s, const Vector3& p) {
return {static_cast<Scalar>(s) / p.x, static_cast<Scalar>(s) / p.y};
}

struct Vector4 {
union {
struct {
Expand Down