From c6a8881e5a32db94f77fe2df4d968b3885cadd92 Mon Sep 17 00:00:00 2001 From: codereader Date: Mon, 6 Sep 2021 18:16:15 +0200 Subject: [PATCH] #5740: Set up unit test suite --- libs/math/Matrix3.h | 102 ++++++++++++++++++++++++- test/math/Matrix3.cpp | 16 ++++ tools/msvc/Tests/Tests.vcxproj | 1 + tools/msvc/Tests/Tests.vcxproj.filters | 3 + 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 test/math/Matrix3.cpp diff --git a/libs/math/Matrix3.h b/libs/math/Matrix3.h index 7a5168b485..e80590a07f 100644 --- a/libs/math/Matrix3.h +++ b/libs/math/Matrix3.h @@ -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() { } @@ -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(); +} diff --git a/test/math/Matrix3.cpp b/test/math/Matrix3.cpp new file mode 100644 index 0000000000..2737813187 --- /dev/null +++ b/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)); +} + +} diff --git a/tools/msvc/Tests/Tests.vcxproj b/tools/msvc/Tests/Tests.vcxproj index 5d568593e4..759fa2064e 100644 --- a/tools/msvc/Tests/Tests.vcxproj +++ b/tools/msvc/Tests/Tests.vcxproj @@ -88,6 +88,7 @@ + diff --git a/tools/msvc/Tests/Tests.vcxproj.filters b/tools/msvc/Tests/Tests.vcxproj.filters index 7868f35514..cd7d8b8faa 100644 --- a/tools/msvc/Tests/Tests.vcxproj.filters +++ b/tools/msvc/Tests/Tests.vcxproj.filters @@ -44,6 +44,9 @@ + + math +