added to_string method to matrix

This commit is contained in:
2024-05-18 13:28:04 +03:00
parent 53bd451072
commit 4e01f3ee09
4 changed files with 75 additions and 16 deletions

View File

@@ -1,6 +1,8 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <string>
namespace uml namespace uml
{ {
@@ -13,7 +15,8 @@ namespace uml
explicit matrix(const std::vector<std::vector<float>> &rows); explicit matrix(const std::vector<std::vector<float>> &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); matrix(const matrix &other);
@@ -21,21 +24,30 @@ namespace uml
matrix(matrix &&other) noexcept; 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<size_t, size_t> get_size() const noexcept; [[nodiscard]]
std::pair<size_t, size_t> get_size() const noexcept;
[[nodiscard]]
float &at(size_t iRow, size_t iCol); float &at(size_t iRow, size_t iCol);
[[nodiscard]]
float get_sum(); float get_sum();
void set_from_raw(const float* pRawMatrix); void set_from_raw(const float* pRawMatrix);
[[nodiscard]]
matrix transpose(); matrix transpose();
void set(float val); 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; matrix operator*(const matrix &other) const;
@@ -49,20 +61,30 @@ namespace uml
void clear(); 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=(const matrix &other);
matrix &operator=(matrix &&other) noexcept; matrix &operator=(matrix &&other) noexcept;
matrix operator/(float f) const; matrix operator/(float f) const;
[[nodiscard]]
std::string to_string() const;
~matrix(); ~matrix();
private: private:

View File

@@ -4,6 +4,9 @@
*/ */
#include "uml/matrix.h" #include "uml/matrix.h"
#include <format>
#include "uml/Vector3.h" #include "uml/Vector3.h"
#include <utility> #include <utility>
#include <stdexcept> #include <stdexcept>
@@ -204,6 +207,25 @@ namespace uml
return out; 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 float matrix::det() const
{ {
if (m_rows + m_columns == 2) if (m_rows + m_columns == 2)
@@ -279,11 +301,11 @@ namespace uml
matrix matrix::to_screen_matrix(float screenWidth, float screenHeight) matrix matrix::to_screen_matrix(float screenWidth, float screenHeight)
{ {
return matrix({ return matrix({
{screenWidth / 2.f, 0.f, 0.f, 0.f}, {screenWidth / 2.f, 0.f, 0.f, 0.f},
{0.f, -screenHeight / 2.f, 0.f, 0.f}, {0.f, -screenHeight / 2.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f}, {0.f, 0.f, 1.f, 0.f},
{screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f}, {screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f},
}); });
} }
const float * matrix::raw() const const float * matrix::raw() const

View File

@@ -4,6 +4,6 @@ project(unit-tests)
file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
include(GoogleTest) 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) target_link_libraries(unit-tests PRIVATE gtest gtest_main uml)

15
tests/UnitTestMatrix.cpp Normal file
View File

@@ -0,0 +1,15 @@
//
// Created by vlad on 5/18/2024.
//
#include <gtest/gtest.h>
#include <uml/matrix.h>
#include <print>
TEST(UnitTestMatrix, ToString)
{
uml::matrix matrix(2, 2);
matrix.set(1.1);
const auto str = matrix.to_string();
std::cout << str;
}