From b21236e5dbb53eae17125bceb286e40fb0789945 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 9 Feb 2025 11:32:46 -0500 Subject: [PATCH] Fixed quaternion equals operator --- src/Quaternion.cpp | 4 ++-- src/Quaternion.h | 2 +- unit-tests/matrix-tests.cpp | 11 ++++------- unit-tests/quaternion-tests.cpp | 18 ++++++++++++++---- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Quaternion.cpp b/src/Quaternion.cpp index 5420075..4ea9748 100644 --- a/src/Quaternion.cpp +++ b/src/Quaternion.cpp @@ -30,9 +30,9 @@ float Quaternion::operator[](uint8_t index) const return 1e+6; } -void Quaternion::operator=(const Quaternion &other) const +void Quaternion::operator=(const Quaternion &other) { - static_cast>(this->matrix) = static_cast>(other.matrix); + memcpy(&(this->matrix), &(other.matrix), 4 * sizeof(float)); } Quaternion Quaternion::operator*(const Quaternion &other) const diff --git a/src/Quaternion.h b/src/Quaternion.h index 5c958e2..0e66a7b 100644 --- a/src/Quaternion.h +++ b/src/Quaternion.h @@ -29,7 +29,7 @@ public: /** * @brief Assign one quaternion to another */ - void operator=(const Quaternion &other) const; + void operator=(const Quaternion &other); /** * @brief Do quaternion multiplication diff --git a/unit-tests/matrix-tests.cpp b/unit-tests/matrix-tests.cpp index dc01a08..0486d23 100644 --- a/unit-tests/matrix-tests.cpp +++ b/unit-tests/matrix-tests.cpp @@ -323,28 +323,25 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") 4, 5, 6, 7, 8, 9}; - Matrix<2, 2> mat5{0}; + Matrix<2, 2> mat5 = mat4.SubMatrix<2, 2, 0, 0>(); - mat4.SubMatrix(mat5, 0, 0); REQUIRE(mat5.Get(0, 0) == 1); REQUIRE(mat5.Get(0, 1) == 2); REQUIRE(mat5.Get(1, 0) == 4); REQUIRE(mat5.Get(1, 1) == 5); - mat4.SubMatrix(mat5, 1, 1); + mat5 = mat4.SubMatrix<2, 2, 1, 1>(); REQUIRE(mat5.Get(0, 0) == 5); REQUIRE(mat5.Get(0, 1) == 6); REQUIRE(mat5.Get(1, 0) == 8); REQUIRE(mat5.Get(1, 1) == 9); - Matrix<3, 1> mat6{0}; - mat4.SubMatrix(mat6, 0, 0); + Matrix<3, 1> mat6 = mat4.SubMatrix<3, 1, 0, 0>(); REQUIRE(mat6.Get(0, 0) == 1); REQUIRE(mat6.Get(1, 0) == 4); REQUIRE(mat6.Get(2, 0) == 7); - Matrix<1, 3> mat7{0}; - mat4.SubMatrix(mat7, 0, 0); + Matrix<1, 3> mat7 = mat4.SubMatrix<1, 3, 0, 0>(); REQUIRE(mat7.Get(0, 0) == 1); REQUIRE(mat7.Get(0, 1) == 2); REQUIRE(mat7.Get(0, 2) == 3); diff --git a/unit-tests/quaternion-tests.cpp b/unit-tests/quaternion-tests.cpp index 2551754..6211680 100644 --- a/unit-tests/quaternion-tests.cpp +++ b/unit-tests/quaternion-tests.cpp @@ -53,6 +53,16 @@ TEST_CASE("Vector Math", "Vector") REQUIRE(q6.v3 == 4); } + SECTION("Equals") + { + Quaternion q3{0, 0, 0, 0}; + q3 = q1; + REQUIRE(q3.w == 1); + REQUIRE(q3.v1 == 2); + REQUIRE(q3.v2 == 3); + REQUIRE(q3.v3 == 4); + } + SECTION("Array access") { REQUIRE(q1[0] == 1); @@ -64,10 +74,10 @@ TEST_CASE("Vector Math", "Vector") SECTION("Addition") { Quaternion q3 = q1 + q2; - REQUIRE(q3.w == 5); - REQUIRE(q3.v1 == 12); - REQUIRE(q3.v2 == 21); - REQUIRE(q3.v3 == 32); + REQUIRE(q3.w == 6); + REQUIRE(q3.v1 == 8); + REQUIRE(q3.v2 == 10); + REQUIRE(q3.v3 == 12); } SECTION("Multiplication")