fixed matrix naming

This commit is contained in:
2024-08-18 08:55:28 +03:00
parent 67e28a5a2e
commit 62f471e04a
3 changed files with 78 additions and 78 deletions

View File

@@ -8,7 +8,7 @@ namespace omath
{ {
class Vector3; class Vector3;
class Matrix class Matrix final
{ {
public: public:
Matrix(size_t rows, size_t columns); Matrix(size_t rows, size_t columns);
@@ -16,7 +16,7 @@ namespace omath
explicit Matrix(const std::vector<std::vector<float>> &rows); explicit Matrix(const std::vector<std::vector<float>> &rows);
[[nodiscard]] [[nodiscard]]
static Matrix to_screen_matrix(float screenWidth, float screenHeight); static Matrix ToScreenMatrix(float screenWidth, float screenHeight);
Matrix(const Matrix &other); Matrix(const Matrix &other);
@@ -25,29 +25,29 @@ namespace omath
Matrix(Matrix &&other) noexcept; Matrix(Matrix &&other) noexcept;
[[nodiscard]] [[nodiscard]]
size_t get_rows_count() const noexcept; size_t RowCount() const noexcept;
[[nodiscard]] [[nodiscard]]
size_t get_columns_count() const noexcept; size_t ColumnsCount() const noexcept;
[[nodiscard]] [[nodiscard]]
std::pair<size_t, size_t> get_size() const noexcept; std::pair<size_t, size_t> Size() const noexcept;
[[nodiscard]] [[nodiscard]]
float &at(size_t iRow, size_t iCol); float &At(size_t iRow, size_t iCol);
[[nodiscard]] [[nodiscard]]
float get_sum(); float Sum();
void set_from_raw(const float* pRawMatrix); void SetDataFromRaw(const float* pRawMatrix);
[[nodiscard]] [[nodiscard]]
Matrix transpose(); Matrix Transpose();
void set(float val); void Set(float val);
[[nodiscard]] [[nodiscard]]
const float &at(size_t iRow, size_t iCol) const; const float &At(size_t iRow, size_t iCol) const;
Matrix operator*(const Matrix &other) const; Matrix operator*(const Matrix &other) const;
@@ -59,22 +59,22 @@ namespace omath
Matrix &operator/=(float f); Matrix &operator/=(float f);
void clear(); void Clear();
[[nodiscard]] [[nodiscard]]
Matrix strip(size_t row, size_t column) const; Matrix Strip(size_t row, size_t column) const;
[[nodiscard]] [[nodiscard]]
float minor(size_t i, size_t j) const; float Minor(size_t i, size_t j) const;
[[nodiscard]] [[nodiscard]]
float alg_complement(size_t i, size_t j) const; float AlgComplement(size_t i, size_t j) const;
[[nodiscard]] [[nodiscard]]
float det() const; float Determinant() const;
[[nodiscard]] [[nodiscard]]
const float* raw() const; const float* Raw() const;
Matrix &operator=(const Matrix &other); Matrix &operator=(const Matrix &other);
@@ -83,13 +83,13 @@ namespace omath
Matrix operator/(float f) const; Matrix operator/(float f) const;
[[nodiscard]] [[nodiscard]]
std::string to_string() const; std::string ToSrtring() const;
~Matrix(); ~Matrix();
private: private:
size_t m_rows = 0; size_t m_rows = 0;
size_t m_columns = 0; size_t m_columns = 0;
std::unique_ptr<float[]> m_pData = nullptr; std::unique_ptr<float[]> m_data = nullptr;
}; };
} }

View File

@@ -17,9 +17,9 @@ namespace omath
m_rows = rows; m_rows = rows;
m_columns = columns; m_columns = columns;
m_pData = std::make_unique<float[]>(m_rows * m_columns); m_data = std::make_unique<float[]>(m_rows * m_columns);
set(0.f); Set(0.f);
} }
Matrix::Matrix(const std::vector<std::vector<float>> &rows) Matrix::Matrix(const std::vector<std::vector<float>> &rows)
@@ -28,11 +28,11 @@ namespace omath
m_columns = rows[0].size(); m_columns = rows[0].size();
m_pData = std::make_unique<float[]>(m_rows * m_columns); m_data = std::make_unique<float[]>(m_rows * m_columns);
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
at(i,j) = rows[i][j]; At(i,j) = rows[i][j];
} }
Matrix::Matrix(const Matrix &other) Matrix::Matrix(const Matrix &other)
@@ -40,11 +40,11 @@ namespace omath
m_rows = other.m_rows; m_rows = other.m_rows;
m_columns = other.m_columns; m_columns = other.m_columns;
m_pData = std::make_unique<float[]>(m_rows * m_columns); m_data = std::make_unique<float[]>(m_rows * m_columns);
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
at(i, j) = other.at(i, j); At(i, j) = other.At(i, j);
} }
Matrix::Matrix(const size_t rows, const size_t columns, const float *pRaw) Matrix::Matrix(const size_t rows, const size_t columns, const float *pRaw)
@@ -53,14 +53,14 @@ namespace omath
m_columns = columns; m_columns = columns;
m_pData = std::make_unique<float[]>(m_rows * m_columns); m_data = std::make_unique<float[]>(m_rows * m_columns);
for (size_t i = 0; i < rows*columns; ++i) for (size_t i = 0; i < rows*columns; ++i)
at(i / rows, i % columns) = pRaw[i]; At(i / rows, i % columns) = pRaw[i];
} }
size_t Matrix::get_rows_count() const noexcept size_t Matrix::RowCount() const noexcept
{ {
return m_rows; return m_rows;
} }
@@ -69,39 +69,39 @@ namespace omath
{ {
m_rows = other.m_rows; m_rows = other.m_rows;
m_columns = other.m_columns; m_columns = other.m_columns;
m_pData = std::move(other.m_pData); m_data = std::move(other.m_data);
} }
size_t Matrix::get_columns_count() const noexcept size_t Matrix::ColumnsCount() const noexcept
{ {
return m_columns; return m_columns;
} }
std::pair<size_t, size_t> Matrix::get_size() const noexcept std::pair<size_t, size_t> Matrix::Size() const noexcept
{ {
return {get_rows_count(), get_columns_count()}; return {RowCount(), ColumnsCount()};
} }
float &Matrix::at(const size_t iRow, const size_t iCol) float &Matrix::At(const size_t iRow, const size_t iCol)
{ {
return const_cast<float&>(std::as_const(*this).at(iRow, iCol)); return const_cast<float&>(std::as_const(*this).At(iRow, iCol));
} }
float Matrix::get_sum() float Matrix::Sum()
{ {
float sum = 0; float sum = 0;
for (size_t i = 0; i < get_rows_count(); i++) for (size_t i = 0; i < RowCount(); i++)
for (size_t j = 0; j < get_columns_count(); j++) for (size_t j = 0; j < ColumnsCount(); j++)
sum += at(i, j); sum += At(i, j);
return sum; return sum;
} }
const float &Matrix::at(const size_t iRow, const size_t iCol) const const float &Matrix::At(const size_t iRow, const size_t iCol) const
{ {
return m_pData[iRow * m_columns + iCol]; return m_data[iRow * m_columns + iCol];
} }
Matrix Matrix::operator*(const Matrix &other) const Matrix Matrix::operator*(const Matrix &other) const
@@ -114,7 +114,7 @@ namespace omath
for (size_t d = 0; d < m_rows; ++d) for (size_t d = 0; d < m_rows; ++d)
for (size_t i = 0; i < other.m_columns; ++i) for (size_t i = 0; i < other.m_columns; ++i)
for (size_t j = 0; j < other.m_rows; ++j) for (size_t j = 0; j < other.m_rows; ++j)
outMat.at(d, i) += at(d, j) * other.at(j, i); outMat.At(d, i) += At(d, j) * other.At(j, i);
return outMat; return outMat;
@@ -125,31 +125,31 @@ namespace omath
auto out = *this; auto out = *this;
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
out.at(i, j) *= f; out.At(i, j) *= f;
return out; return out;
} }
Matrix &Matrix::operator*=(const float f) Matrix &Matrix::operator*=(const float f)
{ {
for (size_t i = 0; i < get_rows_count(); i++) for (size_t i = 0; i < RowCount(); i++)
for (size_t j = 0; j < get_columns_count(); j++) for (size_t j = 0; j < ColumnsCount(); j++)
at(i, j) *= f; At(i, j) *= f;
return *this; return *this;
} }
void Matrix::clear() void Matrix::Clear()
{ {
set(0.f); Set(0.f);
} }
Matrix Matrix::operator*(const Vector3 &vec3) const Matrix Matrix::operator*(const Vector3 &vec3) const
{ {
auto vecmatrix = Matrix(m_rows, 1); auto vecmatrix = Matrix(m_rows, 1);
vecmatrix.set(1.f); vecmatrix.Set(1.f);
vecmatrix.at(0, 0) = vec3.x; vecmatrix.At(0, 0) = vec3.x;
vecmatrix.at(1, 0) = vec3.y; vecmatrix.At(1, 0) = vec3.y;
vecmatrix.at(2, 0) = vec3.z; vecmatrix.At(2, 0) = vec3.z;
return *this * vecmatrix; return *this * vecmatrix;
@@ -163,7 +163,7 @@ namespace omath
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
at(i, j) = other.at(i, j); At(i, j) = other.At(i, j);
return *this; return *this;
@@ -176,7 +176,7 @@ namespace omath
m_rows = other.m_rows; m_rows = other.m_rows;
m_columns = other.m_columns; m_columns = other.m_columns;
m_pData = std::move(other.m_pData); m_data = std::move(other.m_data);
return *this; return *this;
@@ -186,7 +186,7 @@ namespace omath
{ {
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
at(i, j) /= f; At(i, j) /= f;
return *this; return *this;
} }
@@ -196,12 +196,12 @@ namespace omath
auto out = *this; auto out = *this;
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
out.at(i, j) /= f; out.At(i, j) /= f;
return out; return out;
} }
std::string Matrix::to_string() const std::string Matrix::ToSrtring() const
{ {
std::string str; std::string str;
@@ -209,7 +209,7 @@ namespace omath
{ {
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
{ {
str += std::format("{:.1f}",at(i, j)); str += std::format("{:.1f}",At(i, j));
if (j == m_columns-1) if (j == m_columns-1)
str += '\n'; str += '\n';
@@ -220,48 +220,48 @@ namespace omath
return str; return str;
} }
float Matrix::det() const float Matrix::Determinant() const
{ {
if (m_rows + m_columns == 2) if (m_rows + m_columns == 2)
return at(0, 0); return At(0, 0);
if (m_rows == 2 and m_columns == 2) if (m_rows == 2 and m_columns == 2)
return at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0); return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
float fDet = 0; float fDet = 0;
for (size_t i = 0; i < m_columns; i++) for (size_t i = 0; i < m_columns; i++)
fDet += alg_complement(0, i) * at(0, i); fDet += AlgComplement(0, i) * At(0, i);
return fDet; return fDet;
} }
float Matrix::alg_complement(const size_t i, const size_t j) const float Matrix::AlgComplement(const size_t i, const size_t j) const
{ {
const auto tmp = minor(i, j); const auto tmp = Minor(i, j);
return ((i + j + 2) % 2 == 0) ? tmp : -tmp; return ((i + j + 2) % 2 == 0) ? tmp : -tmp;
} }
Matrix Matrix::transpose() Matrix Matrix::Transpose()
{ {
Matrix transposed = {m_columns, m_rows}; Matrix transposed = {m_columns, m_rows};
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
transposed.at(j, i) = at(i, j); transposed.At(j, i) = At(i, j);
return transposed; return transposed;
} }
Matrix::~Matrix() = default; Matrix::~Matrix() = default;
void Matrix::set(const float val) void Matrix::Set(const float val)
{ {
for (size_t i = 0; i < m_rows; ++i) for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j) for (size_t j = 0; j < m_columns; ++j)
at(i, j) = val; At(i, j) = val;
} }
Matrix Matrix::strip(const size_t row, const size_t column) const Matrix Matrix::Strip(const size_t row, const size_t column) const
{ {
Matrix stripped = {m_rows - 1, m_columns - 1}; Matrix stripped = {m_rows - 1, m_columns - 1};
size_t iStripRowIndex = 0; size_t iStripRowIndex = 0;
@@ -277,7 +277,7 @@ namespace omath
if (j == column) if (j == column)
continue; continue;
stripped.at(iStripRowIndex, iStripColumnIndex) = at(i, j); stripped.At(iStripRowIndex, iStripColumnIndex) = At(i, j);
iStripColumnIndex++; iStripColumnIndex++;
} }
@@ -287,12 +287,12 @@ namespace omath
return stripped; return stripped;
} }
float Matrix::minor(const size_t i, const size_t j) const float Matrix::Minor(const size_t i, const size_t j) const
{ {
return strip(i, j).det(); return Strip(i, j).Determinant();
} }
Matrix Matrix::to_screen_matrix(float screenWidth, float screenHeight) Matrix Matrix::ToScreenMatrix(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},
@@ -302,14 +302,14 @@ namespace omath
}); });
} }
const float * Matrix::raw() const const float * Matrix::Raw() const
{ {
return m_pData.get(); return m_data.get();
} }
void Matrix::set_from_raw(const float *pRawMatrix) void Matrix::SetDataFromRaw(const float *pRawMatrix)
{ {
for (size_t i = 0; i < m_columns*m_rows; ++i) for (size_t i = 0; i < m_columns*m_rows; ++i)
at(i / m_rows, i % m_columns) = pRawMatrix[i]; At(i / m_rows, i % m_columns) = pRawMatrix[i];
} }
} }

View File

@@ -9,8 +9,8 @@
TEST(UnitTestMatrix, ToString) TEST(UnitTestMatrix, ToString)
{ {
omath::Matrix matrix(2, 2); omath::Matrix matrix(2, 2);
matrix.set(1.1); matrix.Set(1.1);
const auto str = matrix.to_string(); const auto str = matrix.ToSrtring();
std::cout << str; std::cout << str;