mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
updated matrix name
This commit is contained in:
@@ -8,21 +8,21 @@ namespace omath
|
|||||||
{
|
{
|
||||||
class Vector3;
|
class Vector3;
|
||||||
|
|
||||||
class matrix
|
class Matrix
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
matrix(size_t rows, size_t columns);
|
Matrix(size_t rows, size_t columns);
|
||||||
|
|
||||||
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 to_screen_matrix(float screenWidth, float screenHeight);
|
||||||
|
|
||||||
matrix(const matrix &other);
|
Matrix(const Matrix &other);
|
||||||
|
|
||||||
matrix(size_t rows, size_t columns, const float *pRaw);
|
Matrix(size_t rows, size_t columns, const float *pRaw);
|
||||||
|
|
||||||
matrix(matrix &&other) noexcept;
|
Matrix(Matrix &&other) noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
size_t get_rows_count() const noexcept;
|
size_t get_rows_count() const noexcept;
|
||||||
@@ -42,27 +42,27 @@ namespace omath
|
|||||||
void set_from_raw(const float* pRawMatrix);
|
void set_from_raw(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;
|
||||||
|
|
||||||
matrix operator*(float f) const;
|
Matrix operator*(float f) const;
|
||||||
|
|
||||||
matrix operator*(const Vector3 &vec3) const;
|
Matrix operator*(const Vector3 &vec3) const;
|
||||||
|
|
||||||
matrix &operator*=(float f);
|
Matrix &operator*=(float f);
|
||||||
|
|
||||||
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;
|
||||||
@@ -76,16 +76,16 @@ namespace omath
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
const float* raw() const;
|
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]]
|
[[nodiscard]]
|
||||||
std::string to_string() const;
|
std::string to_string() const;
|
||||||
|
|
||||||
~matrix();
|
~Matrix();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t m_rows = 0;
|
size_t m_rows = 0;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
matrix::matrix(const size_t rows, const size_t columns)
|
Matrix::Matrix(const size_t rows, const size_t columns)
|
||||||
{
|
{
|
||||||
if (rows == 0 and columns == 0)
|
if (rows == 0 and columns == 0)
|
||||||
throw std::runtime_error("Matrix cannot be 0x0");
|
throw std::runtime_error("Matrix cannot be 0x0");
|
||||||
@@ -22,7 +22,7 @@ namespace omath
|
|||||||
set(0.f);
|
set(0.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix::matrix(const std::vector<std::vector<float>> &rows)
|
Matrix::Matrix(const std::vector<std::vector<float>> &rows)
|
||||||
{
|
{
|
||||||
m_rows = rows.size();
|
m_rows = rows.size();
|
||||||
m_columns = rows[0].size();
|
m_columns = rows[0].size();
|
||||||
@@ -35,7 +35,7 @@ namespace omath
|
|||||||
at(i,j) = rows[i][j];
|
at(i,j) = rows[i][j];
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix::matrix(const matrix &other)
|
Matrix::Matrix(const Matrix &other)
|
||||||
{
|
{
|
||||||
m_rows = other.m_rows;
|
m_rows = other.m_rows;
|
||||||
m_columns = other.m_columns;
|
m_columns = other.m_columns;
|
||||||
@@ -47,7 +47,7 @@ namespace omath
|
|||||||
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)
|
||||||
{
|
{
|
||||||
m_rows = rows;
|
m_rows = rows;
|
||||||
m_columns = columns;
|
m_columns = columns;
|
||||||
@@ -60,12 +60,12 @@ namespace omath
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t matrix::get_rows_count() const noexcept
|
size_t Matrix::get_rows_count() const noexcept
|
||||||
{
|
{
|
||||||
return m_rows;
|
return m_rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix::matrix(matrix &&other) noexcept
|
Matrix::Matrix(Matrix &&other) noexcept
|
||||||
{
|
{
|
||||||
m_rows = other.m_rows;
|
m_rows = other.m_rows;
|
||||||
m_columns = other.m_columns;
|
m_columns = other.m_columns;
|
||||||
@@ -73,22 +73,22 @@ namespace omath
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t matrix::get_columns_count() const noexcept
|
size_t Matrix::get_columns_count() 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::get_size() const noexcept
|
||||||
{
|
{
|
||||||
return {get_rows_count(), get_columns_count()};
|
return {get_rows_count(), get_columns_count()};
|
||||||
}
|
}
|
||||||
|
|
||||||
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::get_sum()
|
||||||
{
|
{
|
||||||
float sum = 0;
|
float sum = 0;
|
||||||
|
|
||||||
@@ -99,17 +99,17 @@ namespace omath
|
|||||||
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_pData[iRow * m_columns + iCol];
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix matrix::operator*(const matrix &other) const
|
Matrix Matrix::operator*(const Matrix &other) const
|
||||||
{
|
{
|
||||||
if (m_columns != other.m_rows)
|
if (m_columns != other.m_rows)
|
||||||
throw std::runtime_error("n != m");
|
throw std::runtime_error("n != m");
|
||||||
|
|
||||||
auto outMat = matrix(m_rows, other.m_columns);
|
auto outMat = Matrix(m_rows, other.m_columns);
|
||||||
|
|
||||||
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)
|
||||||
@@ -120,7 +120,7 @@ namespace omath
|
|||||||
return outMat;
|
return outMat;
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix matrix::operator*(const float f) const
|
Matrix Matrix::operator*(const float f) const
|
||||||
{
|
{
|
||||||
auto out = *this;
|
auto out = *this;
|
||||||
for (size_t i = 0; i < m_rows; ++i)
|
for (size_t i = 0; i < m_rows; ++i)
|
||||||
@@ -130,7 +130,7 @@ namespace omath
|
|||||||
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 < get_rows_count(); i++)
|
||||||
for (size_t j = 0; j < get_columns_count(); j++)
|
for (size_t j = 0; j < get_columns_count(); j++)
|
||||||
@@ -138,14 +138,14 @@ namespace omath
|
|||||||
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;
|
||||||
@@ -156,7 +156,7 @@ namespace omath
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
matrix &matrix::operator=(const matrix &other)
|
Matrix &Matrix::operator=(const Matrix &other)
|
||||||
{
|
{
|
||||||
if (this == &other)
|
if (this == &other)
|
||||||
return *this;
|
return *this;
|
||||||
@@ -169,7 +169,7 @@ namespace omath
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix &matrix::operator=(matrix &&other) noexcept
|
Matrix &Matrix::operator=(Matrix &&other) noexcept
|
||||||
{
|
{
|
||||||
if (this == &other)
|
if (this == &other)
|
||||||
return *this;
|
return *this;
|
||||||
@@ -182,7 +182,7 @@ namespace omath
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix &matrix::operator/=(const float f)
|
Matrix &Matrix::operator/=(const float f)
|
||||||
{
|
{
|
||||||
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)
|
||||||
@@ -191,7 +191,7 @@ namespace omath
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix matrix::operator/(const float f) const
|
Matrix Matrix::operator/(const float f) const
|
||||||
{
|
{
|
||||||
auto out = *this;
|
auto out = *this;
|
||||||
for (size_t i = 0; i < m_rows; ++i)
|
for (size_t i = 0; i < m_rows; ++i)
|
||||||
@@ -201,7 +201,7 @@ namespace omath
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string matrix::to_string() const
|
std::string Matrix::to_string() const
|
||||||
{
|
{
|
||||||
std::string str;
|
std::string str;
|
||||||
|
|
||||||
@@ -220,7 +220,7 @@ namespace omath
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
float matrix::det() const
|
float Matrix::det() const
|
||||||
{
|
{
|
||||||
if (m_rows + m_columns == 2)
|
if (m_rows + m_columns == 2)
|
||||||
return at(0, 0);
|
return at(0, 0);
|
||||||
@@ -235,15 +235,15 @@ namespace omath
|
|||||||
return fDet;
|
return fDet;
|
||||||
}
|
}
|
||||||
|
|
||||||
float matrix::alg_complement(const size_t i, const size_t j) const
|
float Matrix::alg_complement(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)
|
||||||
@@ -252,18 +252,18 @@ namespace omath
|
|||||||
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;
|
||||||
|
|
||||||
for (size_t i = 0; i < m_rows; i++)
|
for (size_t i = 0; i < m_rows; i++)
|
||||||
@@ -287,14 +287,14 @@ 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).det();
|
||||||
}
|
}
|
||||||
|
|
||||||
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},
|
||||||
@@ -302,12 +302,12 @@ namespace omath
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const float * matrix::raw() const
|
const float * Matrix::raw() const
|
||||||
{
|
{
|
||||||
return m_pData.get();
|
return m_pData.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void matrix::set_from_raw(const float *pRawMatrix)
|
void Matrix::set_from_raw(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];
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
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.to_string();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user