From 39274eb9640486a6270fa9cc8f01b5232cab0ef2 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 6 Feb 2025 22:02:50 -0500 Subject: [PATCH] Refactored the src dir layout --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 40 +-------- src/Matrix/CMakeLists.txt | 26 ++++++ src/{ => Matrix}/Matrix.cpp | 0 src/{ => Matrix}/Matrix.hpp | 0 src/Quaternion/CMakeLists.txt | 0 src/Vector3D/CMakeLists.txt | 21 +++++ src/{ => Vector3D}/Vector3D.cpp | 0 src/{ => Vector3D}/Vector3D.hpp | 0 unit-tests/CMakeLists.txt | 3 +- unit-tests/matrix-tests.cpp | 155 +++++++++++++++++++++----------- 11 files changed, 157 insertions(+), 90 deletions(-) create mode 100644 src/Matrix/CMakeLists.txt rename src/{ => Matrix}/Matrix.cpp (100%) rename src/{ => Matrix}/Matrix.hpp (100%) create mode 100644 src/Quaternion/CMakeLists.txt create mode 100644 src/Vector3D/CMakeLists.txt rename src/{ => Vector3D}/Vector3D.cpp (100%) rename src/{ => Vector3D}/Vector3D.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d27ca0..5932a0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,4 +6,4 @@ add_subdirectory(src) set(CMAKE_CXX_STANDARD 11) -add_compile_options(-fdiagnostics-color=always) \ No newline at end of file +add_compile_options(-fdiagnostics-color=always -Wall -Wextra -Wpedantic) \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 33460ef..db28747 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,38 +1,4 @@ -# Vector 3D Interface -add_library(vector-3d-intf - INTERFACE -) +add_subdirectory(Matrix) +add_subdirectory(Quaternion) +add_subdirectory(Vector3D) -target_include_directories(vector-3d-intf - INTERFACE - . -) - -# Matrix -add_library(matrix - STATIC - Matrix.cpp -) - -target_link_libraries(matrix - PUBLIC - vector-3d-intf - PRIVATE -) - -set_target_properties(matrix - PROPERTIES - LINKER_LANGUAGE CXX -) - -# Vector3d -add_library(vector-3d - STATIC - Vector3D.cpp -) - -target_link_libraries(vector-3d - PUBLIC - vector-3d-intf - PRIVATE -) \ No newline at end of file diff --git a/src/Matrix/CMakeLists.txt b/src/Matrix/CMakeLists.txt new file mode 100644 index 0000000..cbf86f0 --- /dev/null +++ b/src/Matrix/CMakeLists.txt @@ -0,0 +1,26 @@ +# Vector 3D Interface +add_library(matrix-intf + INTERFACE +) + +target_include_directories(matrix-intf + INTERFACE + . +) + +# Matrix +add_library(matrix + STATIC + Matrix.cpp +) + +target_link_libraries(matrix + PUBLIC + vector-3d-intf + PRIVATE +) + +set_target_properties(matrix + PROPERTIES + LINKER_LANGUAGE CXX +) \ No newline at end of file diff --git a/src/Matrix.cpp b/src/Matrix/Matrix.cpp similarity index 100% rename from src/Matrix.cpp rename to src/Matrix/Matrix.cpp diff --git a/src/Matrix.hpp b/src/Matrix/Matrix.hpp similarity index 100% rename from src/Matrix.hpp rename to src/Matrix/Matrix.hpp diff --git a/src/Quaternion/CMakeLists.txt b/src/Quaternion/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/Vector3D/CMakeLists.txt b/src/Vector3D/CMakeLists.txt new file mode 100644 index 0000000..ccd0ae4 --- /dev/null +++ b/src/Vector3D/CMakeLists.txt @@ -0,0 +1,21 @@ +# Vector 3D Interface +add_library(vector-3d-intf + INTERFACE +) + +target_include_directories(vector-3d-intf + INTERFACE + . +) + +# Vector3d +add_library(vector-3d + STATIC + Vector3D.cpp +) + +target_link_libraries(vector-3d + PUBLIC + vector-3d-intf + PRIVATE +) \ No newline at end of file diff --git a/src/Vector3D.cpp b/src/Vector3D/Vector3D.cpp similarity index 100% rename from src/Vector3D.cpp rename to src/Vector3D/Vector3D.cpp diff --git a/src/Vector3D.hpp b/src/Vector3D/Vector3D.hpp similarity index 100% rename from src/Vector3D.hpp rename to src/Vector3D/Vector3D.hpp diff --git a/unit-tests/CMakeLists.txt b/unit-tests/CMakeLists.txt index b464674..5cb60ea 100644 --- a/unit-tests/CMakeLists.txt +++ b/unit-tests/CMakeLists.txt @@ -17,7 +17,7 @@ add_executable(matrix-tests matrix-tests.cpp) target_link_libraries(matrix-tests PRIVATE - vector-3d-intf + matrix-intf Catch2::Catch2WithMain ) @@ -26,6 +26,7 @@ add_executable(vector-tests vector-tests.cpp) target_link_libraries(vector-tests PRIVATE + matrix-intf vector-3d-intf Catch2::Catch2WithMain ) \ No newline at end of file diff --git a/unit-tests/matrix-tests.cpp b/unit-tests/matrix-tests.cpp index 09a3da9..eac240d 100644 --- a/unit-tests/matrix-tests.cpp +++ b/unit-tests/matrix-tests.cpp @@ -10,13 +10,15 @@ #include #include -TEST_CASE("Elementary Matrix Operations", "Matrix") { +TEST_CASE("Elementary Matrix Operations", "Matrix") +{ std::array arr2{5, 6, 7, 8}; Matrix<2, 2> mat1{1, 2, 3, 4}; Matrix<2, 2> mat2{arr2}; Matrix<2, 2> mat3{}; - SECTION("Initialization") { + SECTION("Initialization") + { // array initialization REQUIRE(mat1.Get(0, 0) == 1); REQUIRE(mat1.Get(0, 1) == 2); @@ -38,14 +40,17 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { // large matrix Matrix<255, 255> mat6{}; mat6.Fill(4); - for (uint8_t row{0}; row < 255; row++) { - for (uint8_t column{0}; column < 255; column++) { + for (uint8_t row{0}; row < 255; row++) + { + for (uint8_t column{0}; column < 255; column++) + { REQUIRE(mat6.Get(row, column) == 4); } } } - SECTION("Fill") { + SECTION("Fill") + { mat1.Fill(0); REQUIRE(mat1.Get(0, 0) == 0); REQUIRE(mat1.Get(0, 1) == 0); @@ -65,10 +70,12 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat3.Get(1, 1) == -20); } - SECTION("Addition") { + SECTION("Addition") + { std::string strBuf1 = ""; mat1.ToString(strBuf1); - std::cout << "Matrix 1:\n" << strBuf1 << std::endl; + std::cout << "Matrix 1:\n" + << strBuf1 << std::endl; mat1.Add(mat2, mat3); @@ -86,7 +93,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat3.Get(1, 1) == 12); } - SECTION("Subtraction") { + SECTION("Subtraction") + { mat1.Sub(mat2, mat3); REQUIRE(mat3.Get(0, 0) == -4); @@ -103,7 +111,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat3.Get(1, 1) == -4); } - SECTION("Multiplication") { + SECTION("Multiplication") + { mat1.Mult(mat2, mat3); REQUIRE(mat3.Get(0, 0) == 19); @@ -118,9 +127,12 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat3.Get(0, 1) == 22); REQUIRE(mat3.Get(1, 0) == 43); REQUIRE(mat3.Get(1, 1) == 50); + + // TODO: You need to add non-square multiplications to this. } - SECTION("Scalar Multiplication") { + SECTION("Scalar Multiplication") + { mat1.Mult(2, mat3); REQUIRE(mat3.Get(0, 0) == 2); @@ -129,7 +141,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat3.Get(1, 1) == 8); } - SECTION("Element Multiply") { + SECTION("Element Multiply") + { mat1.ElementMultiply(mat2, mat3); REQUIRE(mat3.Get(0, 0) == 5); @@ -138,7 +151,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat3.Get(1, 1) == 32); } - SECTION("Element Divide") { + SECTION("Element Divide") + { mat1.ElementDivide(mat2, mat3); REQUIRE_THAT(mat3.Get(0, 0), Catch::Matchers::WithinRel(0.2f, 1e-6f)); @@ -147,7 +161,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE_THAT(mat3.Get(1, 1), Catch::Matchers::WithinRel(0.5f, 1e-6f)); } - SECTION("Minor Matrix") { + SECTION("Minor Matrix") + { // what about matrices of 0,0 or 1,1? // minor matrix for 2x2 matrix Matrix<1, 1> minorMat1{}; @@ -183,7 +198,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(minorMat4.Get(1, 1) == 5); } - SECTION("Determinant") { + SECTION("Determinant") + { float det1 = mat1.Det(); REQUIRE_THAT(det1, Catch::Matchers::WithinRel(-2.0F, 1e-6f)); @@ -200,7 +216,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE_THAT(det5, Catch::Matchers::WithinRel(6.0F, 1e-6f)); } - SECTION("Matrix of Minors") { + SECTION("Matrix of Minors") + { mat1.MatrixOfMinors(mat3); REQUIRE_THAT(mat3.Get(0, 0), Catch::Matchers::WithinRel(4.0F, 1e-6f)); REQUIRE_THAT(mat3.Get(0, 1), Catch::Matchers::WithinRel(3.0F, 1e-6f)); @@ -221,7 +238,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE_THAT(mat5.Get(2, 2), Catch::Matchers::WithinRel(-3.0F, 1e-6f)); } - SECTION("Invert") { + SECTION("Invert") + { mat1.Invert(mat3); REQUIRE_THAT(mat3.Get(0, 0), Catch::Matchers::WithinRel(-2.0F, 1e-6f)); REQUIRE_THAT(mat3.Get(0, 1), Catch::Matchers::WithinRel(1.0F, 1e-6f)); @@ -229,7 +247,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE_THAT(mat3.Get(1, 1), Catch::Matchers::WithinRel(-0.5F, 1e-6f)); }; - SECTION("Transpose") { + SECTION("Transpose") + { // transpose a square matrix mat1.Transpose(mat3); @@ -252,7 +271,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat5.Get(2, 1) == 6); } - SECTION("Normalize") { + SECTION("Normalize") + { mat1.Normalize(mat3); float sqrt_30{sqrt(30)}; @@ -272,7 +292,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { Catch::Matchers::WithinRel(0.957591346325f, 1e-6f)); } - SECTION("GET ROW") { + SECTION("GET ROW") + { Matrix<1, 2> mat1Rows{}; mat1.GetRow(0, mat1Rows); REQUIRE(mat1Rows.Get(0, 0) == 1); @@ -283,7 +304,8 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { REQUIRE(mat1Rows.Get(0, 1) == 4); } - SECTION("GET COLUMN") { + SECTION("GET COLUMN") + { Matrix<2, 1> mat1Columns{}; mat1.GetColumn(0, mat1Columns); REQUIRE(mat1Columns.Get(0, 0) == 1); @@ -297,13 +319,16 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") { // basically re-run all of the previous tests with huge matrices and time the // results. -TEST_CASE("Timing Tests", "Matrix") { +TEST_CASE("Timing Tests", "Matrix") +{ std::array arr1{}; - for (uint16_t i{0}; i < 50 * 50; i++) { + for (uint16_t i{0}; i < 50 * 50; i++) + { arr1[i] = i; } std::array arr2{5, 6, 7, 8}; - for (uint16_t i{50 * 50}; i < 2 * 50 * 50; i++) { + for (uint16_t i{50 * 50}; i < 2 * 50 * 50; i++) + { arr2[i] = i; } Matrix<50, 50> mat1{arr1}; @@ -314,91 +339,119 @@ TEST_CASE("Timing Tests", "Matrix") { Matrix<4, 4> mat4{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; Matrix<4, 4> mat5{}; - SECTION("Addition") { - for (uint32_t i{0}; i < 10000; i++) { + SECTION("Addition") + { + for (uint32_t i{0}; i < 10000; i++) + { mat3 = mat1 + mat2; } } - SECTION("Subtraction") { - for (uint32_t i{0}; i < 10000; i++) { + SECTION("Subtraction") + { + for (uint32_t i{0}; i < 10000; i++) + { mat3 = mat1 - mat2; } } - SECTION("Multiplication") { - for (uint32_t i{0}; i < 1000; i++) { + SECTION("Multiplication") + { + for (uint32_t i{0}; i < 1000; i++) + { mat3 = mat1 * mat2; } } - SECTION("Scalar Multiplication") { - for (uint32_t i{0}; i < 10000; i++) { + SECTION("Scalar Multiplication") + { + for (uint32_t i{0}; i < 10000; i++) + { mat3 = mat1 * 3; } } - SECTION("Element Multiply") { - for (uint32_t i{0}; i < 10000; i++) { + SECTION("Element Multiply") + { + for (uint32_t i{0}; i < 10000; i++) + { mat1.ElementMultiply(mat2, mat3); } } - SECTION("Element Divide") { - for (uint32_t i{0}; i < 10000; i++) { + SECTION("Element Divide") + { + for (uint32_t i{0}; i < 10000; i++) + { mat1.ElementDivide(mat2, mat3); } } - SECTION("Minor Matrix") { + SECTION("Minor Matrix") + { // what about matrices of 0,0 or 1,1? // minor matrix for 2x2 matrix Matrix<49, 49> minorMat1{}; - for (uint32_t i{0}; i < 10000; i++) { + for (uint32_t i{0}; i < 10000; i++) + { mat1.MinorMatrix(minorMat1, 0, 0); } } - SECTION("Determinant") { - for (uint32_t i{0}; i < 100000; i++) { + SECTION("Determinant") + { + for (uint32_t i{0}; i < 100000; i++) + { float det1 = mat4.Det(); } } - SECTION("Matrix of Minors") { - for (uint32_t i{0}; i < 100000; i++) { + SECTION("Matrix of Minors") + { + for (uint32_t i{0}; i < 100000; i++) + { mat4.MatrixOfMinors(mat5); } } - SECTION("Invert") { - for (uint32_t i{0}; i < 100000; i++) { + SECTION("Invert") + { + for (uint32_t i{0}; i < 100000; i++) + { mat4.Invert(mat5); } }; - SECTION("Transpose") { - for (uint32_t i{0}; i < 10000; i++) { + SECTION("Transpose") + { + for (uint32_t i{0}; i < 10000; i++) + { mat1.Transpose(mat3); } } - SECTION("Normalize") { - for (uint32_t i{0}; i < 10000; i++) { + SECTION("Normalize") + { + for (uint32_t i{0}; i < 10000; i++) + { mat1.Normalize(mat3); } } - SECTION("GET ROW") { + SECTION("GET ROW") + { Matrix<1, 50> mat1Rows{}; - for (uint32_t i{0}; i < 1000000; i++) { + for (uint32_t i{0}; i < 1000000; i++) + { mat1.GetRow(0, mat1Rows); } } - SECTION("GET COLUMN") { + SECTION("GET COLUMN") + { Matrix<50, 1> mat1Columns{}; - for (uint32_t i{0}; i < 1000000; i++) { + for (uint32_t i{0}; i < 1000000; i++) + { mat1.GetColumn(0, mat1Columns); } }