Skip to content

Commit

Permalink
#5740: Add named Matrix3 constructors for the simple transform types
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 8, 2021
1 parent 330f124 commit 97156f2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libs/math/Matrix3.h
Expand Up @@ -107,6 +107,24 @@ class alignas(16) Matrix3
return Matrix3(Eigen::Projective2d::Identity());
}

/// Get a matrix representing the given 2D translation.
static Matrix3 getTranslation(const Vector2& translation)
{
return Matrix3(Transform(Eigen::Translation2d(translation.x(), translation.y())));
}

/// Get a matrix representing the given 2D rotation (counter-clockwise, angle in radians)
static Matrix3 getRotation(double angle)
{
return Matrix3(Transform(Eigen::Rotation2D(angle)));
}

/// Get a matrix representing the given 2D scale
static Matrix3 getScale(const Vector2& scale)
{
return Matrix3(Transform(Eigen::Scaling(scale.x(), scale.y())));
}

/**
* \brief
* Construct a matrix containing the given elements.
Expand Down
31 changes: 31 additions & 0 deletions test/math/Matrix3.cpp
Expand Up @@ -154,4 +154,35 @@ TEST(Matrix3Test, MatrixTransformation)
}
}

TEST(Matrix3Test, ConstructTranslationMatrix)
{
const Vector2 translation(1.5, -2939);
auto tm = Matrix3::getTranslation(translation);

EXPECT_EQ(tm, Matrix3::byRows(1, 0, translation.x(),
0, 1, translation.y(),
0, 0, 1));
EXPECT_EQ(tm.zCol(), Vector3(translation.x(), translation.y(), 1));
}

TEST(Matrix3Test, ConstructRotationMatrix)
{
const double angle = degrees_to_radians(15);
auto tm = Matrix3::getRotation(angle);

EXPECT_EQ(tm, Matrix3::byRows(cos(angle), -sin(angle), 0,
sin(angle), cos(angle), 0,
0, 0, 1));
}

TEST(Matrix3Test, ConstructScaleMatrix)
{
const Vector2 scale(1.5, -3.2);
auto tm = Matrix3::getScale(scale);

EXPECT_EQ(tm, Matrix3::byRows(scale.x(), 0, 0,
0, scale.y(), 0,
0, 0, 1));
}

}

0 comments on commit 97156f2

Please sign in to comment.