Skip to content

Commit

Permalink
#5740: Set up unit test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 6, 2021
1 parent 1368551 commit c6a8881
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
102 changes: 99 additions & 3 deletions libs/math/Matrix3.h
Expand Up @@ -20,7 +20,12 @@ class alignas(16) Matrix3
using Transform = Eigen::Projective2d;
Transform _transform;

private:
// Initialising constructor, elements are passed in column-wise order
Matrix3(double xx_, double xy_, double xz_,
double yx_, double yy_, double yz_,
double zx_, double zy_, double zz_);

public:
/// Construct a matrix with uninitialised values.
Matrix3() { }

Expand All @@ -30,14 +35,105 @@ class alignas(16) Matrix3
{}

/// Get the underlying Eigen transform
Transform& eigen() { return _transform; }
Eigen::Projective2d& eigen() { return _transform; }

/// Get the underlying const Eigen transform
const Transform& eigen() const { return _transform; }
const Eigen::Projective2d& eigen() const { return _transform; }

/**
* Return matrix elements
* \{
*/
double& xx() { return _transform.matrix()(0, 0); }
const double& xx() const { return _transform.matrix()(0, 0); }
double& xy() { return _transform.matrix()(1, 0); }
const double& xy() const { return _transform.matrix()(1, 0); }
double& xz() { return _transform.matrix()(2, 0); }
const double& xz() const { return _transform.matrix()(2, 0); }

double& yx() { return _transform.matrix()(0, 1); }
const double& yx() const { return _transform.matrix()(0, 1); }
double& yy() { return _transform.matrix()(1, 1); }
const double& yy() const { return _transform.matrix()(1, 1); }
double& yz() { return _transform.matrix()(2, 1); }
const double& yz() const { return _transform.matrix()(2, 1); }

double& zx() { return _transform.matrix()(0, 2); }
const double& zx() const { return _transform.matrix()(0, 2); }
double& zy() { return _transform.matrix()(1, 2); }
const double& zy() const { return _transform.matrix()(1, 2); }
double& zz() { return _transform.matrix()(2, 2); }
const double& zz() const { return _transform.matrix()(2, 2); }
/**
* \}
*/

/// Obtain the identity matrix.
static Matrix3 getIdentity()
{
return Matrix3(Eigen::Projective2d::Identity());
}

/**
* \brief
* Construct a matrix containing the given elements.
*
* The elements are specified column-wise, starting with the left-most
* column.
*/
static Matrix3 byColumns(double xx, double xy, double xz,
double yx, double yy, double yz,
double zx, double zy, double zz);

/**
* \brief
* Construct a matrix containing the given elements.
*
* The elements are specified row-wise, starting with the top row.
*/
static Matrix3 byRows(double xx, double yx, double zx,
double xy, double yy, double zy,
double xz, double yz, double zz);
};

// Private constructor
Matrix3::Matrix3(double xx_, double xy_, double xz_,
double yx_, double yy_, double yz_,
double zx_, double zy_, double zz_)
{
xx() = xx_;
xy() = xy_;
xz() = xz_;
yx() = yx_;
yy() = yy_;
yz() = yz_;
zx() = zx_;
zy() = zy_;
zz() = zz_;
}

// Construct a matrix with given column elements
inline Matrix3 Matrix3::byColumns(double xx, double xy, double xz,
double yx, double yy, double yz,
double zx, double zy, double zz)
{
return Matrix3(xx, xy, xz,
yx, yy, yz,
zx, zy, zz);
}

// Construct a matrix with given row elements
inline Matrix3 Matrix3::byRows(double xx, double yx, double zx,
double xy, double yy, double zy,
double xz, double yz, double zz)
{
return Matrix3(xx, xy, xz,
yx, yy, yz,
zx, zy, zz);
}

/// Compare two matrices elementwise for equality
inline bool operator==(const Matrix3& l, const Matrix3& r)
{
return l.eigen().matrix() == r.eigen().matrix();
}
16 changes: 16 additions & 0 deletions test/math/Matrix3.cpp
@@ -0,0 +1,16 @@
#include "gtest/gtest.h"

#include "math/Matrix3.h"

namespace test
{

TEST(Matrix3Test, CreateIdentityMatrix)
{
const auto identity = Matrix3::getIdentity();
EXPECT_EQ(identity, Matrix3::byRows(1, 0, 0,
0, 1, 0,
0, 0, 1));
}

}
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Expand Up @@ -88,6 +88,7 @@
<ClCompile Include="..\..\..\test\MapSavingLoading.cpp" />
<ClCompile Include="..\..\..\test\MaterialExport.cpp" />
<ClCompile Include="..\..\..\test\Materials.cpp" />
<ClCompile Include="..\..\..\test\math\Matrix3.cpp" />
<ClCompile Include="..\..\..\test\math\Matrix4.cpp" />
<ClCompile Include="..\..\..\test\math\Plane3.cpp" />
<ClCompile Include="..\..\..\test\math\Quaternion.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Expand Up @@ -44,6 +44,9 @@
<ClCompile Include="..\..\..\test\Transformation.cpp" />
<ClCompile Include="..\..\..\test\MapMerging.cpp" />
<ClCompile Include="..\..\..\test\PointTrace.cpp" />
<ClCompile Include="..\..\..\test\math\Matrix3.cpp">
<Filter>math</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\HeadlessOpenGLContext.h" />
Expand Down

0 comments on commit c6a8881

Please sign in to comment.