Skip to content

Commit

Permalink
Matrix4::getIdentity() implemented by Eigen
Browse files Browse the repository at this point in the history
Also moved the method into the .h file and changed it to return by value rather
than const reference. Although I haven't profiled, I doubt that there is much
(if any) performance benefit of returning by reference rather than simply
inlining the method body + RVO.
  • Loading branch information
Matthew Mott committed Mar 31, 2021
1 parent 2a79f63 commit b8d1012
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 14 deletions.
12 changes: 0 additions & 12 deletions libs/math/Matrix4.cpp
Expand Up @@ -48,18 +48,6 @@ Matrix4::Matrix4(double xx_, double xy_, double xz_, double xw_,

// Named constructors

// Identity matrix
const Matrix4& Matrix4::getIdentity()
{
static const Matrix4 _identity(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
return _identity;
}

// Get a translation matrix for the given vector
Matrix4 Matrix4::getTranslation(const Vector3& translation)
{
Expand Down
5 changes: 4 additions & 1 deletion libs/math/Matrix4.h
Expand Up @@ -72,7 +72,10 @@ class Matrix4
const Eigen::Projective3d& eigen() const { return _transform; }

/// Obtain the identity matrix.
static const Matrix4& getIdentity();
static Matrix4 getIdentity()
{
return Matrix4(Eigen::Projective3d::Identity());
}

/// Get a matrix representing the given 3D translation.
static Matrix4 getTranslation(const Vector3& translation);
Expand Down
3 changes: 2 additions & 1 deletion libs/transformlib.h
Expand Up @@ -13,7 +13,8 @@ class IdentityTransform :
/// \brief Returns the identity matrix.
const Matrix4& localToParent() const
{
return Matrix4::getIdentity();
static const Matrix4 ID = Matrix4::getIdentity();
return ID;
}
};

Expand Down

0 comments on commit b8d1012

Please sign in to comment.