From 957635b4841679ddf7cb385ad4186f84523994a0 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sun, 29 Nov 2020 23:17:09 -0600 Subject: [PATCH] feat(matrix): add C44Matrix::RotationAroundZ --- tempest/matrix/C44Matrix.cpp | 28 ++++++++++++++++++++++++ tempest/matrix/C44Matrix.hpp | 3 +++ test/Matrix.cpp | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/tempest/matrix/C44Matrix.cpp b/tempest/matrix/C44Matrix.cpp index 90b3419..548ea58 100644 --- a/tempest/matrix/C44Matrix.cpp +++ b/tempest/matrix/C44Matrix.cpp @@ -1,4 +1,32 @@ #include "tempest/matrix/C44Matrix.hpp" +#include + +C44Matrix C44Matrix::RotationAroundZ(float angle) { + float cosAngle = cos(angle); + float sinAngle = sin(angle); + + float a0 = cosAngle; + float a1 = sinAngle; + float a2 = 0.0f; + float a3 = 0.0f; + + float b0 = -sinAngle; + float b1 = cosAngle; + float b2 = 0.0f; + float b3 = 0.0f; + + float c0 = 0.0f; + float c1 = 0.0f; + float c2 = 1.0f; + float c3 = 0.0f; + + float d0 = 0.0f; + float d1 = 0.0f; + float d2 = 0.0f; + float d3 = 1.0f; + + return { a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, d0, d1, d2, d3 }; +} C44Matrix C44Matrix::Adjoint() const { float a0 = this->b2 * this->c3 * this->d1 + this->b3 * this->c1 * this->d2 + this->b1 * this->c2 * this->d3 - this->b3 * this->c2 * this->d1 - this->b2 * this->c1 * this->d3 - this->b1 * this->c3 * this->d2; diff --git a/tempest/matrix/C44Matrix.hpp b/tempest/matrix/C44Matrix.hpp index 9283f66..2766115 100644 --- a/tempest/matrix/C44Matrix.hpp +++ b/tempest/matrix/C44Matrix.hpp @@ -3,6 +3,9 @@ class C44Matrix { public: + // Static functions + static C44Matrix RotationAroundZ(float angle); + // Member variables float a0 = 1.0f; float a1 = 0.0f; diff --git a/test/Matrix.cpp b/test/Matrix.cpp index 4193632..c2de9f0 100644 --- a/test/Matrix.cpp +++ b/test/Matrix.cpp @@ -1,6 +1,48 @@ #include "tempest/Matrix.hpp" #include "test/Test.hpp" +TEST_CASE("C44Matrix::RotationAroundZ", "[matrix]") { + SECTION("returns rotation matrix for 0 degree angle") { + auto rotation = C44Matrix::RotationAroundZ(0.0f); + CHECK(rotation.a0 == 1.0f); + CHECK(rotation.a1 == 0.0f); + CHECK(rotation.a2 == 0.0f); + CHECK(rotation.a3 == 0.0f); + CHECK(rotation.b0 == 0.0f); + CHECK(rotation.b1 == 1.0f); + CHECK(rotation.b2 == 0.0f); + CHECK(rotation.b3 == 0.0f); + CHECK(rotation.c0 == 0.0f); + CHECK(rotation.c1 == 0.0f); + CHECK(rotation.c2 == 1.0f); + CHECK(rotation.c3 == 0.0f); + CHECK(rotation.d0 == 0.0f); + CHECK(rotation.d1 == 0.0f); + CHECK(rotation.d2 == 0.0f); + CHECK(rotation.d3 == 1.0f); + } + + SECTION("returns rotation matrix for 180 degree angle") { + auto rotation = C44Matrix::RotationAroundZ(3.1415927f); + CHECK(rotation.a0 == Approx(-1.0f).margin(0.0000001f)); + CHECK(rotation.a1 == Approx(0.0f).margin(0.0000001f)); + CHECK(rotation.a2 == 0.0f); + CHECK(rotation.a3 == 0.0f); + CHECK(rotation.b0 == Approx(0.0f).margin(0.0000001f)); + CHECK(rotation.b1 == Approx(-1.0f).margin(0.0000001f)); + CHECK(rotation.b2 == 0.0f); + CHECK(rotation.b3 == 0.0f); + CHECK(rotation.c0 == 0.0f); + CHECK(rotation.c1 == 0.0f); + CHECK(rotation.c2 == 1.0f); + CHECK(rotation.c3 == 0.0f); + CHECK(rotation.d0 == 0.0f); + CHECK(rotation.d1 == 0.0f); + CHECK(rotation.d2 == 0.0f); + CHECK(rotation.d3 == 1.0f); + } +} + TEST_CASE("C44Matrix", "[matrix]") { SECTION("constructs with default constructor") { C44Matrix matrix;