From 9c11551c9a7498e34c3372e184042b7fa6608cd7 Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 8 Sep 2025 19:56:04 +0300 Subject: [PATCH] Removes deprecated Matrix class Removes the deprecated `Matrix` class and its associated source files and unit tests. This change is to ensure code cleanliness and prevent accidental usage of the slow and outdated `Matrix` class. The `Mat` class should be used instead. --- include/omath/linear_algebra/matrix.hpp | 127 --------- source/matrix.cpp | 364 ------------------------ tests/general/unit_test_matrix.cpp | 184 ------------ 3 files changed, 675 deletions(-) delete mode 100644 include/omath/linear_algebra/matrix.hpp delete mode 100644 source/matrix.cpp delete mode 100644 tests/general/unit_test_matrix.cpp diff --git a/include/omath/linear_algebra/matrix.hpp b/include/omath/linear_algebra/matrix.hpp deleted file mode 100644 index 32e9a87f..00000000 --- a/include/omath/linear_algebra/matrix.hpp +++ /dev/null @@ -1,127 +0,0 @@ -#pragma once - -/* -THIS CODE IS DEPRECATED NEVER EVER USE Matrix CLASS -AND VERY SLOW USE Mat INSTEAD!!!!!!!!!!! -⠛⠛⣿⣿⣿⣿⣿⡷⢶⣦⣶⣶⣤⣤⣤⣀⠀⠀⠀ -⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀ -⠀⠀⠀⠉⠉⠉⠙⠻⣿⣿⠿⠿⠛⠛⠛⠻⣿⣿⣇⠀ -⠀⠀⢤⣀⣀⣀⠀⠀⢸⣷⡄⠀⣁⣀⣤⣴⣿⣿⣿⣆ -⠀⠀⠀⠀⠹⠏⠀⠀⠀⣿⣧⠀⠹⣿⣿⣿⣿⣿⡿⣿ -⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠿⠇⢀⣼⣿⣿⠛⢯⡿⡟ -⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠦⠴⢿⢿⣿⡿⠷⠀⣿⠀ -⠀⠀⠀⠀⠀⠀⠀⠙⣷⣶⣶⣤⣤⣤⣤⣤⣶⣦⠃⠀ -⠀⠀⠀⠀⠀⠀⠀⢐⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀ -⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀ -⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠻⢿⣿⣿⣿⣿⠟⠁ -*/ - -#ifdef OMATH_ENABLE_LEGACY -#include "omath/vector3.hpp" -#include -#include -#include - -namespace omath -{ - - class Matrix final - { - public: - Matrix(); - Matrix(size_t rows, size_t columns); - - Matrix(const std::initializer_list>& rows); - - [[nodiscard]] - static Matrix to_screen_matrix(float screen_width, float screen_height); - - [[nodiscard]] - static Matrix translation_matrix(const Vector3& diff); - - [[nodiscard]] - static Matrix orientation_matrix(const Vector3& forward, const Vector3& right, - const Vector3& up); - - [[nodiscard]] - static Matrix projection_matrix(float field_of_view, float aspect_ratio, float near, float far); - - Matrix(const Matrix& other); - - Matrix(size_t rows, size_t columns, const float* raw_data); - - Matrix(Matrix&& other) noexcept; - - [[nodiscard]] - size_t row_count() const noexcept; - - [[nodiscard]] - float& operator[](size_t row, size_t column); - - [[nodiscard]] - size_t columns_count() const noexcept; - - [[nodiscard]] - std::pair size() const noexcept; - - [[nodiscard]] - float& at(size_t row, size_t col); - - [[nodiscard]] - float sum(); - - void set_data_from_raw(const float* raw_matrix); - - [[nodiscard]] - Matrix transpose() const; - - void set(float val); - - [[nodiscard]] - const float& at(size_t row, size_t col) const; - - Matrix operator*(const Matrix& other) const; - - Matrix& operator*=(const Matrix& other); - - Matrix operator*(float f) const; - - Matrix& operator*=(float f); - - Matrix& operator/=(float f); - - void clear(); - - [[nodiscard]] - Matrix strip(size_t row, size_t column) const; - - [[nodiscard]] - float minor(size_t i, size_t j) const; - - [[nodiscard]] - float alg_complement(size_t i, size_t j) const; - - [[nodiscard]] - float determinant() 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: - size_t m_rows; - size_t m_columns; - std::unique_ptr m_data; - }; -} // namespace omath -#endif diff --git a/source/matrix.cpp b/source/matrix.cpp deleted file mode 100644 index 322ea967..00000000 --- a/source/matrix.cpp +++ /dev/null @@ -1,364 +0,0 @@ -#ifdef OMATH_ENABLE_LEGACY - -#include "omath/matrix.hpp" -#include "omath/angles.hpp" -#include "omath/vector3.hpp" -#include -#include -#include -#include - -namespace omath -{ - Matrix::Matrix(const size_t rows, const size_t columns) - { - if (rows == 0 and columns == 0) - throw std::runtime_error("Matrix cannot be 0x0"); - - m_rows = rows; - m_columns = columns; - - m_data = std::make_unique(m_rows * m_columns); - - set(0.f); - } - - Matrix::Matrix(const std::initializer_list>& rows) - { - m_rows = rows.size(); - m_columns = rows.begin()->size(); - - for (const auto& row: rows) - if (row.size() != m_columns) - throw std::invalid_argument("All rows must have the same number of columns."); - - m_data = std::make_unique(m_rows * m_columns); - - size_t i = 0; - for (const auto& row: rows) - { - size_t j = 0; - for (const auto& value: row) - at(i, j++) = value; - ++i; - } - } - - Matrix::Matrix(const Matrix& other) - { - m_rows = other.m_rows; - m_columns = other.m_columns; - - m_data = std::make_unique(m_rows * m_columns); - - for (size_t i = 0; i < m_rows; ++i) - for (size_t j = 0; j < m_columns; ++j) - at(i, j) = other.at(i, j); - } - - Matrix::Matrix(const size_t rows, const size_t columns, const float* raw_data) - { - m_rows = rows; - m_columns = columns; - - m_data = std::make_unique(m_rows * m_columns); - - for (size_t i = 0; i < rows * columns; ++i) - at(i / rows, i % columns) = raw_data[i]; - } - - size_t Matrix::row_count() const noexcept - { - return m_rows; - } - - float& Matrix::operator[](const size_t row, const size_t column) - { - return at(row, column); - } - - Matrix::Matrix(Matrix&& other) noexcept - { - m_rows = other.m_rows; - m_columns = other.m_columns; - m_data = std::move(other.m_data); - - other.m_rows = 0; - other.m_columns = 0; - - other.m_data = nullptr; - } - - size_t Matrix::columns_count() const noexcept - { - return m_columns; - } - - std::pair Matrix::size() const noexcept - { - return {row_count(), columns_count()}; - } - - float& Matrix::at(const size_t row, const size_t col) - { - return const_cast(std::as_const(*this).at(row, col)); - } - - float Matrix::sum() - { - float sum = 0; - - for (size_t i = 0; i < row_count(); i++) - for (size_t j = 0; j < columns_count(); j++) - sum += at(i, j); - - return sum; - } - - const float& Matrix::at(const size_t row, const size_t col) const - { - return m_data[row * m_columns + col]; - } - - Matrix Matrix::operator*(const Matrix& other) const - { - if (m_columns != other.m_rows) - throw std::runtime_error("n != m"); - - auto out_mat = Matrix(m_rows, other.m_columns); - - for (size_t d = 0; d < m_rows; ++d) - for (size_t i = 0; i < other.m_columns; ++i) - for (size_t j = 0; j < other.m_rows; ++j) - out_mat.at(d, i) += at(d, j) * other.at(j, i); - - return out_mat; - } - - Matrix& Matrix::operator*=(const Matrix& other) - { - *this = *this * other; - return *this; - } - - Matrix Matrix::operator*(const float f) const - { - auto out = *this; - for (size_t i = 0; i < m_rows; ++i) - for (size_t j = 0; j < m_columns; ++j) - out.at(i, j) *= f; - - return out; - } - - Matrix& Matrix::operator*=(const float f) - { - for (size_t i = 0; i < row_count(); i++) - for (size_t j = 0; j < columns_count(); j++) - at(i, j) *= f; - return *this; - } - - void Matrix::clear() - { - set(0.f); - } - - Matrix& Matrix::operator=(const Matrix& other) - { - if (this == &other) - return *this; - - for (size_t i = 0; i < m_rows; ++i) - for (size_t j = 0; j < m_columns; ++j) - at(i, j) = other.at(i, j); - - return *this; - } - - Matrix& Matrix::operator=(Matrix&& other) noexcept - { - if (this == &other) - return *this; - - m_rows = other.m_rows; - m_columns = other.m_columns; - m_data = std::move(other.m_data); - - other.m_rows = 0; - other.m_columns = 0; - - return *this; - } - - Matrix& Matrix::operator/=(const float f) - { - for (size_t i = 0; i < m_rows; ++i) - for (size_t j = 0; j < m_columns; ++j) - at(i, j) /= f; - - return *this; - } - - Matrix Matrix::operator/(const float f) const - { - auto out = *this; - for (size_t i = 0; i < m_rows; ++i) - for (size_t j = 0; j < m_columns; ++j) - out.at(i, j) /= f; - - 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::determinant() const // NOLINT(*-no-recursion) - { - if (m_rows + m_columns == 2) - return at(0, 0); - - if (m_rows == 2 and m_columns == 2) - return at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0); - - float det = 0; - for (size_t i = 0; i < m_columns; i++) - det += alg_complement(0, i) * at(0, i); - - return det; - } - - float Matrix::alg_complement(const size_t i, const size_t j) const // NOLINT(*-no-recursion) - { - const auto tmp = minor(i, j); - return ((i + j + 2) % 2 == 0) ? tmp : -tmp; - } - - Matrix Matrix::transpose() const - { - Matrix transposed = {m_columns, m_rows}; - - for (size_t i = 0; i < m_rows; ++i) - for (size_t j = 0; j < m_columns; ++j) - transposed.at(j, i) = at(i, j); - - return transposed; - } - - Matrix::~Matrix() = default; - - void Matrix::set(const float val) - { - for (size_t i = 0; i < m_rows; ++i) - for (size_t j = 0; j < m_columns; ++j) - at(i, j) = val; - } - - Matrix Matrix::strip(const size_t row, const size_t column) const - { - Matrix stripped = {m_rows - 1, m_columns - 1}; - size_t strip_row_index = 0; - - for (size_t i = 0; i < m_rows; i++) - { - if (i == row) - continue; - - size_t strip_column_index = 0; - for (size_t j = 0; j < m_columns; ++j) - { - if (j == column) - continue; - - stripped.at(strip_row_index, strip_column_index) = at(i, j); - strip_column_index++; - } - - strip_row_index++; - } - - return stripped; - } - - float Matrix::minor(const size_t i, const size_t j) const // NOLINT(*-no-recursion) - { - return strip(i, j).determinant(); - } - - Matrix Matrix::to_screen_matrix(const float screen_width, const float screen_height) - { - return { - {screen_width / 2.f, 0.f, 0.f, 0.f}, - {0.f, -screen_height / 2.f, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {screen_width / 2.f, screen_height / 2.f, 0.f, 1.f}, - }; - } - - Matrix Matrix::translation_matrix(const Vector3& diff) - { - return { - {1.f, 0.f, 0.f, 0.f}, - {0.f, 1.f, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {diff.x, diff.y, diff.z, 1.f}, - }; - } - - Matrix Matrix::orientation_matrix(const Vector3& forward, const Vector3& right, - const Vector3& up) - { - return { - {right.x, up.x, forward.x, 0.f}, - {right.y, up.y, forward.y, 0.f}, - {right.z, up.z, forward.z, 0.f}, - {0.f, 0.f, 0.f, 1.f}, - }; - } - - Matrix Matrix::projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) - { - const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f); - - return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f}, - {0.f, 1.f / fov_half_tan, 0.f, 0.f}, - {0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)}, - {0.f, 0.f, -1.f, 0.f}}; - } - - const float* Matrix::raw() const - { - return m_data.get(); - } - - void Matrix::set_data_from_raw(const float* raw_matrix) - { - for (size_t i = 0; i < m_columns * m_rows; ++i) - at(i / m_rows, i % m_columns) = raw_matrix[i]; - } - - Matrix::Matrix() - { - m_columns = 0; - m_rows = 0; - m_data = nullptr; - } -} // namespace omath -#endif diff --git a/tests/general/unit_test_matrix.cpp b/tests/general/unit_test_matrix.cpp deleted file mode 100644 index b7543c6b..00000000 --- a/tests/general/unit_test_matrix.cpp +++ /dev/null @@ -1,184 +0,0 @@ -// -// Created by vlad on 5/18/2024. -// - -#ifdef OMATH_ENABLE_LEGACY - -#include -#include -#include "omath/vector3.hpp" - -using namespace omath; - - -class UnitTestMatrix : public ::testing::Test -{ -protected: - Matrix m1; - Matrix m2; - - void SetUp() override - { - m1 = Matrix(2, 2); - m2 = Matrix{{1.0f, 2.0f}, {3.0f, 4.0f}}; - } -}; - -// Test constructors -TEST_F(UnitTestMatrix, Constructor_Size) -{ - const Matrix m(3, 3); - EXPECT_EQ(m.row_count(), 3); - EXPECT_EQ(m.columns_count(), 3); -} - -TEST_F(UnitTestMatrix, Operator_SquareBrackets) -{ - EXPECT_EQ((m2[0, 0]), 1.0f); - EXPECT_EQ((m2[0, 1]), 2.0f); - EXPECT_EQ((m2[1, 0]), 3.0f); - EXPECT_EQ((m2[1, 1]), 4.0f); -} - - -TEST_F(UnitTestMatrix, Constructor_InitializerList) -{ - Matrix m{{1.0f, 2.0f}, {3.0f, 4.0f}}; - EXPECT_EQ(m.row_count(), 2); - EXPECT_EQ(m.columns_count(), 2); - EXPECT_FLOAT_EQ(m.at(0, 0), 1.0f); - EXPECT_FLOAT_EQ(m.at(1, 1), 4.0f); -} - -TEST_F(UnitTestMatrix, Constructor_Copy) -{ - Matrix m3 = m2; - EXPECT_EQ(m3.row_count(), m2.row_count()); - EXPECT_EQ(m3.columns_count(), m2.columns_count()); - EXPECT_FLOAT_EQ(m3.at(0, 0), m2.at(0, 0)); -} - -TEST_F(UnitTestMatrix, Constructor_Move) -{ - Matrix m3 = std::move(m2); - EXPECT_EQ(m3.row_count(), 2); - EXPECT_EQ(m3.columns_count(), 2); - EXPECT_FLOAT_EQ(m3.at(0, 0), 1.0f); - EXPECT_EQ(m2.row_count(), 0); // m2 should be empty after the move - EXPECT_EQ(m2.columns_count(), 0); -} - -// Test matrix operations -TEST_F(UnitTestMatrix, Operator_Multiplication_Matrix) -{ - Matrix m3 = m2 * m2; - EXPECT_EQ(m3.row_count(), 2); - EXPECT_EQ(m3.columns_count(), 2); - EXPECT_FLOAT_EQ(m3.at(0, 0), 7.0f); - EXPECT_FLOAT_EQ(m3.at(1, 1), 22.0f); -} - -TEST_F(UnitTestMatrix, Operator_Multiplication_Scalar) -{ - Matrix m3 = m2 * 2.0f; - EXPECT_FLOAT_EQ(m3.at(0, 0), 2.0f); - EXPECT_FLOAT_EQ(m3.at(1, 1), 8.0f); -} - -TEST_F(UnitTestMatrix, Operator_Division_Scalar) -{ - Matrix m3 = m2 / 2.0f; - EXPECT_FLOAT_EQ(m3.at(0, 0), 0.5f); - EXPECT_FLOAT_EQ(m3.at(1, 1), 2.0f); -} - -// Test matrix functions -TEST_F(UnitTestMatrix, Transpose) -{ - Matrix m3 = m2.transpose(); - EXPECT_FLOAT_EQ(m3.at(0, 1), 3.0f); - EXPECT_FLOAT_EQ(m3.at(1, 0), 2.0f); -} - -TEST_F(UnitTestMatrix, Determinant) -{ - const float det = m2.determinant(); - EXPECT_FLOAT_EQ(det, -2.0f); -} - -TEST_F(UnitTestMatrix, Minor) -{ - const float minor = m2.minor(0, 0); - EXPECT_FLOAT_EQ(minor, 4.0f); -} - -TEST_F(UnitTestMatrix, AlgComplement) -{ - const float algComp = m2.alg_complement(0, 0); - EXPECT_FLOAT_EQ(algComp, 4.0f); -} - -TEST_F(UnitTestMatrix, Strip) -{ - Matrix m3 = m2.strip(0, 0); - EXPECT_EQ(m3.row_count(), 1); - EXPECT_EQ(m3.columns_count(), 1); - EXPECT_FLOAT_EQ(m3.at(0, 0), 4.0f); -} - -TEST_F(UnitTestMatrix, ProjectionMatrix) -{ - const Matrix proj = Matrix::projection_matrix(45.0f, 1.33f, 0.1f, 100.0f); - EXPECT_EQ(proj.row_count(), 4); - EXPECT_EQ(proj.columns_count(), 4); - // Further checks on projection matrix elements could be added -} - -// Test other member functions -TEST_F(UnitTestMatrix, Set) -{ - m1.set(3.0f); - EXPECT_FLOAT_EQ(m1.at(0, 0), 3.0f); - EXPECT_FLOAT_EQ(m1.at(1, 1), 3.0f); -} - -TEST_F(UnitTestMatrix, Sum) -{ - const float sum = m2.sum(); - EXPECT_FLOAT_EQ(sum, 10.0f); -} - -TEST_F(UnitTestMatrix, Clear) -{ - m2.clear(); - EXPECT_FLOAT_EQ(m2.at(0, 0), 0.0f); - EXPECT_FLOAT_EQ(m2.at(1, 1), 0.0f); -} - -TEST_F(UnitTestMatrix, ToString) -{ - const std::string str = m2.to_string(); - EXPECT_FALSE(str.empty()); -} - -// Test assignment operators -TEST_F(UnitTestMatrix, AssignmentOperator_Copy) -{ - Matrix m3(2, 2); - m3 = m2; - EXPECT_EQ(m3.row_count(), m2.row_count()); - EXPECT_EQ(m3.columns_count(), m2.columns_count()); - EXPECT_FLOAT_EQ(m3.at(0, 0), m2.at(0, 0)); -} - -TEST_F(UnitTestMatrix, AssignmentOperator_Move) -{ - Matrix m3(2, 2); - m3 = std::move(m2); - EXPECT_EQ(m3.row_count(), 2); - EXPECT_EQ(m3.columns_count(), 2); - EXPECT_FLOAT_EQ(m3.at(0, 0), 1.0f); - EXPECT_EQ(m2.row_count(), 0); // m2 should be empty after the move - EXPECT_EQ(m2.columns_count(), 0); -} -#endif