From 4e01f3ee0985508dfb3ef3582f545ca05d76492b Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 18 May 2024 13:28:04 +0300 Subject: [PATCH] added to_string method to matrix --- include/uml/matrix.h | 42 ++++++++++++++++++++++++++++++---------- source/matrix.cpp | 32 +++++++++++++++++++++++++----- tests/CMakeLists.txt | 2 +- tests/UnitTestMatrix.cpp | 15 ++++++++++++++ 4 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 tests/UnitTestMatrix.cpp diff --git a/include/uml/matrix.h b/include/uml/matrix.h index 25a1305..afb09f6 100644 --- a/include/uml/matrix.h +++ b/include/uml/matrix.h @@ -1,6 +1,8 @@ #pragma once #include #include +#include + namespace uml { @@ -13,7 +15,8 @@ namespace uml explicit matrix(const std::vector> &rows); - [[nodiscard]] static matrix to_screen_matrix(float screenWidth, float screenHeight); + [[nodiscard]] + static matrix to_screen_matrix(float screenWidth, float screenHeight); matrix(const matrix &other); @@ -21,21 +24,30 @@ namespace uml matrix(matrix &&other) noexcept; - [[nodiscard]] size_t get_rows_count() const noexcept; + [[nodiscard]] + size_t get_rows_count() const noexcept; - [[nodiscard]] size_t get_columns_count() const noexcept; + [[nodiscard]] + size_t get_columns_count() const noexcept; - [[nodiscard]] std::pair get_size() const noexcept; + [[nodiscard]] + std::pair get_size() const noexcept; + [[nodiscard]] float &at(size_t iRow, size_t iCol); + [[nodiscard]] float get_sum(); + void set_from_raw(const float* pRawMatrix); + + [[nodiscard]] matrix transpose(); void set(float val); - [[nodiscard]] const float &at(size_t iRow, size_t iCol) const; + [[nodiscard]] + const float &at(size_t iRow, size_t iCol) const; matrix operator*(const matrix &other) const; @@ -49,20 +61,30 @@ namespace uml void clear(); - [[nodiscard]] matrix strip(size_t row, size_t column) const; + [[nodiscard]] + matrix strip(size_t row, size_t column) const; - [[nodiscard]] float minor(size_t i, size_t j) const; + [[nodiscard]] + float minor(size_t i, size_t j) const; - [[nodiscard]] float alg_complement(size_t i, size_t j) const; + [[nodiscard]] + float alg_complement(size_t i, size_t j) const; + + [[nodiscard]] + float det() const; + + [[nodiscard]] + const float* raw() const; - [[nodiscard]] float det() const; - [[nodiscard]] const float* raw() const; matrix &operator=(const matrix &other); matrix &operator=(matrix &&other) noexcept; matrix operator/(float f) const; + [[nodiscard]] + std::string to_string() const; + ~matrix(); private: diff --git a/source/matrix.cpp b/source/matrix.cpp index bb511ff..b606beb 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -4,6 +4,9 @@ */ #include "uml/matrix.h" + +#include + #include "uml/Vector3.h" #include #include @@ -204,6 +207,25 @@ namespace uml return out; } + std::string matrix::to_string() const + { + std::string str; + + for (size_t i = 0; i < m_rows; i++) + { + for (size_t j = 0; j < m_columns; ++j) + { + str += std::format("{:.1f}",at(i, j)); + + if (j == m_columns-1) + str += '\n'; + else + str += ' '; + } + } + return str; + } + float matrix::det() const { if (m_rows + m_columns == 2) @@ -279,11 +301,11 @@ namespace uml matrix matrix::to_screen_matrix(float screenWidth, float screenHeight) { return matrix({ - {screenWidth / 2.f, 0.f, 0.f, 0.f}, - {0.f, -screenHeight / 2.f, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f}, - }); + {screenWidth / 2.f, 0.f, 0.f, 0.f}, + {0.f, -screenHeight / 2.f, 0.f, 0.f}, + {0.f, 0.f, 1.f, 0.f}, + {screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f}, + }); } const float * matrix::raw() const diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index aaefac7..12feaf5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,6 +4,6 @@ project(unit-tests) file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) include(GoogleTest) -add_executable(unit-tests UnitTestColor.cpp) +add_executable(unit-tests UnitTestColor.cpp UnitTestMatrix.cpp) target_link_libraries(unit-tests PRIVATE gtest gtest_main uml) \ No newline at end of file diff --git a/tests/UnitTestMatrix.cpp b/tests/UnitTestMatrix.cpp new file mode 100644 index 0000000..cad8f5d --- /dev/null +++ b/tests/UnitTestMatrix.cpp @@ -0,0 +1,15 @@ +// +// Created by vlad on 5/18/2024. +// +#include +#include +#include +TEST(UnitTestMatrix, ToString) +{ + uml::matrix matrix(2, 2); + matrix.set(1.1); + const auto str = matrix.to_string(); + + std::cout << str; + +} \ No newline at end of file