mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
changed code style
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
#include "omath/matrix.hpp"
|
||||
#include "omath/angles.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
|
||||
#include <complex>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
Matrix::Matrix(const size_t rows, const size_t columns)
|
||||
@@ -21,7 +18,7 @@ namespace omath
|
||||
|
||||
m_data = std::make_unique<float[]>(m_rows * m_columns);
|
||||
|
||||
Set(0.f);
|
||||
set(0.f);
|
||||
}
|
||||
|
||||
Matrix::Matrix(const std::initializer_list<std::initializer_list<float>>& rows)
|
||||
@@ -29,7 +26,6 @@ namespace omath
|
||||
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.");
|
||||
@@ -41,7 +37,7 @@ namespace omath
|
||||
{
|
||||
size_t j = 0;
|
||||
for (const auto& value: row)
|
||||
At(i, j++) = value;
|
||||
at(i, j++) = value;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
@@ -55,29 +51,28 @@ namespace omath
|
||||
|
||||
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);
|
||||
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* raw_data)
|
||||
{
|
||||
m_rows = rows;
|
||||
m_columns = columns;
|
||||
|
||||
|
||||
m_data = std::make_unique<float[]>(m_rows * m_columns);
|
||||
|
||||
for (size_t i = 0; i < rows * columns; ++i)
|
||||
At(i / rows, i % columns) = pRaw[i];
|
||||
at(i / rows, i % columns) = raw_data[i];
|
||||
}
|
||||
|
||||
size_t Matrix::RowCount() const noexcept
|
||||
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);
|
||||
return at(row, column);
|
||||
}
|
||||
|
||||
Matrix::Matrix(Matrix&& other) noexcept
|
||||
@@ -92,35 +87,35 @@ namespace omath
|
||||
other.m_data = nullptr;
|
||||
}
|
||||
|
||||
size_t Matrix::ColumnsCount() const noexcept
|
||||
size_t Matrix::columns_count() const noexcept
|
||||
{
|
||||
return m_columns;
|
||||
}
|
||||
|
||||
std::pair<size_t, size_t> Matrix::Size() const noexcept
|
||||
std::pair<size_t, size_t> Matrix::size() const noexcept
|
||||
{
|
||||
return {RowCount(), ColumnsCount()};
|
||||
return {row_count(), columns_count()};
|
||||
}
|
||||
|
||||
float& Matrix::At(const size_t iRow, const size_t iCol)
|
||||
float& Matrix::at(const size_t row, const size_t col)
|
||||
{
|
||||
return const_cast<float&>(std::as_const(*this).At(iRow, iCol));
|
||||
return const_cast<float&>(std::as_const(*this).at(row, col));
|
||||
}
|
||||
|
||||
float Matrix::Sum()
|
||||
float Matrix::sum()
|
||||
{
|
||||
float sum = 0;
|
||||
|
||||
for (size_t i = 0; i < RowCount(); i++)
|
||||
for (size_t j = 0; j < ColumnsCount(); j++)
|
||||
sum += At(i, j);
|
||||
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 iRow, const size_t iCol) const
|
||||
const float& Matrix::at(const size_t row, const size_t col) const
|
||||
{
|
||||
return m_data[iRow * m_columns + iCol];
|
||||
return m_data[row * m_columns + col];
|
||||
}
|
||||
|
||||
Matrix Matrix::operator*(const Matrix& other) const
|
||||
@@ -128,15 +123,14 @@ namespace omath
|
||||
if (m_columns != other.m_rows)
|
||||
throw std::runtime_error("n != m");
|
||||
|
||||
auto outMat = Matrix(m_rows, other.m_columns);
|
||||
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)
|
||||
outMat.At(d, i) += At(d, j) * other.At(j, i);
|
||||
out_mat.at(d, i) += at(d, j) * other.at(j, i);
|
||||
|
||||
|
||||
return outMat;
|
||||
return out_mat;
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator*=(const Matrix& other)
|
||||
@@ -150,22 +144,22 @@ namespace omath
|
||||
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;
|
||||
out.at(i, j) *= f;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator*=(const float f)
|
||||
{
|
||||
for (size_t i = 0; i < RowCount(); i++)
|
||||
for (size_t j = 0; j < ColumnsCount(); j++)
|
||||
At(i, j) *= 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()
|
||||
void Matrix::clear()
|
||||
{
|
||||
Set(0.f);
|
||||
set(0.f);
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator=(const Matrix& other)
|
||||
@@ -175,7 +169,7 @@ namespace omath
|
||||
|
||||
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);
|
||||
at(i, j) = other.at(i, j);
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -199,7 +193,7 @@ namespace omath
|
||||
{
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
At(i, j) /= f;
|
||||
at(i, j) /= f;
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -209,12 +203,12 @@ namespace omath
|
||||
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;
|
||||
out.at(i, j) /= f;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string Matrix::ToString() const
|
||||
std::string Matrix::to_string() const
|
||||
{
|
||||
std::string str;
|
||||
|
||||
@@ -222,7 +216,7 @@ namespace omath
|
||||
{
|
||||
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)
|
||||
str += '\n';
|
||||
@@ -233,89 +227,89 @@ namespace omath
|
||||
return str;
|
||||
}
|
||||
|
||||
float Matrix::Determinant() const
|
||||
float Matrix::determinant() const // NOLINT(*-no-recursion)
|
||||
{
|
||||
if (m_rows + m_columns == 2)
|
||||
return At(0, 0);
|
||||
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);
|
||||
return at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0);
|
||||
|
||||
float fDet = 0;
|
||||
float det = 0;
|
||||
for (size_t i = 0; i < m_columns; i++)
|
||||
fDet += AlgComplement(0, i) * At(0, i);
|
||||
det += alg_complement(0, i) * at(0, i);
|
||||
|
||||
return fDet;
|
||||
return det;
|
||||
}
|
||||
|
||||
float Matrix::AlgComplement(const size_t i, const size_t j) const
|
||||
float Matrix::alg_complement(const size_t i, const size_t j) const // NOLINT(*-no-recursion)
|
||||
{
|
||||
const auto tmp = Minor(i, j);
|
||||
const auto tmp = minor(i, j);
|
||||
return ((i + j + 2) % 2 == 0) ? tmp : -tmp;
|
||||
}
|
||||
|
||||
Matrix Matrix::Transpose() const
|
||||
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);
|
||||
transposed.at(j, i) = at(i, j);
|
||||
|
||||
return transposed;
|
||||
}
|
||||
|
||||
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 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};
|
||||
size_t iStripRowIndex = 0;
|
||||
size_t strip_row_index = 0;
|
||||
|
||||
for (size_t i = 0; i < m_rows; i++)
|
||||
{
|
||||
if (i == row)
|
||||
continue;
|
||||
|
||||
size_t iStripColumnIndex = 0;
|
||||
size_t strip_column_index = 0;
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
{
|
||||
if (j == column)
|
||||
continue;
|
||||
|
||||
stripped.At(iStripRowIndex, iStripColumnIndex) = At(i, j);
|
||||
iStripColumnIndex++;
|
||||
stripped.at(strip_row_index, strip_column_index) = at(i, j);
|
||||
strip_column_index++;
|
||||
}
|
||||
|
||||
iStripRowIndex++;
|
||||
strip_row_index++;
|
||||
}
|
||||
|
||||
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 // NOLINT(*-no-recursion)
|
||||
{
|
||||
return Strip(i, j).Determinant();
|
||||
return strip(i, j).determinant();
|
||||
}
|
||||
|
||||
Matrix Matrix::ToScreenMatrix(const float screenWidth, const float screenHeight)
|
||||
Matrix Matrix::to_screen_matrix(const float screen_width, const float screen_height)
|
||||
{
|
||||
return {
|
||||
{screenWidth / 2.f, 0.f, 0.f, 0.f},
|
||||
{0.f, -screenHeight / 2.f, 0.f, 0.f},
|
||||
{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},
|
||||
{screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f},
|
||||
{screen_width / 2.f, screen_height / 2.f, 0.f, 1.f},
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::TranslationMatrix(const Vector3<float>& diff)
|
||||
Matrix Matrix::translation_matrix(const Vector3<float>& diff)
|
||||
{
|
||||
return {
|
||||
{1.f, 0.f, 0.f, 0.f},
|
||||
@@ -325,7 +319,8 @@ namespace omath
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up)
|
||||
Matrix Matrix::orientation_matrix(const Vector3<float>& forward, const Vector3<float>& right,
|
||||
const Vector3<float>& up)
|
||||
{
|
||||
return {
|
||||
{right.x, up.x, forward.x, 0.f},
|
||||
@@ -335,25 +330,26 @@ namespace omath
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::ProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, const float far)
|
||||
Matrix Matrix::projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
|
||||
const float far)
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
|
||||
|
||||
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fovHalfTan, 0.f, 0.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
|
||||
const float* Matrix::raw() const
|
||||
{
|
||||
return m_data.get();
|
||||
}
|
||||
|
||||
void Matrix::SetDataFromRaw(const float* pRawMatrix)
|
||||
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) = pRawMatrix[i];
|
||||
at(i / m_rows, i % m_columns) = raw_matrix[i];
|
||||
}
|
||||
|
||||
Matrix::Matrix()
|
||||
|
||||
Reference in New Issue
Block a user