mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
improved matrix class
This commit is contained in:
@@ -17,7 +17,15 @@ namespace omath
|
|||||||
{
|
{
|
||||||
size_t rows, columns;
|
size_t rows, columns;
|
||||||
};
|
};
|
||||||
template<size_t Rows = 0, size_t Columns = 0, class Type = float>
|
|
||||||
|
enum class MatStoreType : uint8_t
|
||||||
|
{
|
||||||
|
ROW_MAJOR = 0,
|
||||||
|
COLUMN_MAJOR
|
||||||
|
};
|
||||||
|
|
||||||
|
template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR>
|
||||||
|
requires (std::is_floating_point_v<Type> || std::is_integral_v<Type>)
|
||||||
class Mat final
|
class Mat final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -26,7 +34,6 @@ namespace omath
|
|||||||
Clear();
|
Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
constexpr Mat(const std::initializer_list<std::initializer_list<Type>>& rows)
|
constexpr Mat(const std::initializer_list<std::initializer_list<Type>>& rows)
|
||||||
{
|
{
|
||||||
if (rows.size() != Rows)
|
if (rows.size() != Rows)
|
||||||
@@ -42,18 +49,21 @@ namespace omath
|
|||||||
auto colIt = rowIt->begin();
|
auto colIt = rowIt->begin();
|
||||||
for (size_t j = 0; j < Columns; ++j, ++colIt)
|
for (size_t j = 0; j < Columns; ++j, ++colIt)
|
||||||
{
|
{
|
||||||
At(i, j) = *colIt;
|
At(i, j) = std::move(*colIt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr explicit Mat(const Type* rawData)
|
||||||
|
{
|
||||||
|
std::copy_n(rawData, Rows * Columns, m_data.begin());
|
||||||
|
}
|
||||||
|
|
||||||
constexpr Mat(const Mat &other) noexcept
|
constexpr Mat(const Mat &other) noexcept
|
||||||
{
|
{
|
||||||
m_data = other.m_data;
|
m_data = other.m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
constexpr Mat(Mat &&other) noexcept
|
constexpr Mat(Mat &&other) noexcept
|
||||||
{
|
{
|
||||||
m_data = std::move(other.m_data);
|
m_data = std::move(other.m_data);
|
||||||
@@ -77,13 +87,22 @@ namespace omath
|
|||||||
return {Rows, Columns};
|
return {Rows, Columns};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr const Type &At(const size_t rowIndex, const size_t columnIndex) const
|
[[nodiscard]] constexpr const Type &At(const size_t rowIndex, const size_t columnIndex) const
|
||||||
{
|
{
|
||||||
if (rowIndex >= Rows || columnIndex >= Columns)
|
if (rowIndex >= Rows || columnIndex >= Columns)
|
||||||
throw std::out_of_range("Index out of range");
|
throw std::out_of_range("Index out of range");
|
||||||
|
|
||||||
|
if constexpr (StoreType == MatStoreType::ROW_MAJOR)
|
||||||
return m_data[rowIndex * Columns + columnIndex];
|
return m_data[rowIndex * Columns + columnIndex];
|
||||||
|
|
||||||
|
else if constexpr (StoreType == MatStoreType::COLUMN_MAJOR)
|
||||||
|
return m_data[rowIndex + columnIndex * Rows];
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static_assert(false, "Invalid matrix access convention");
|
||||||
|
std::unreachable();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr Type &At(const size_t rowIndex, const size_t columnIndex)
|
[[nodiscard]] constexpr Type &At(const size_t rowIndex, const size_t columnIndex)
|
||||||
@@ -240,6 +259,18 @@ namespace omath
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr const std::array<Type, Rows*Columns>& RawArray() const
|
||||||
|
{
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr std::array<Type, Rows*Columns>& RawArray()
|
||||||
|
{
|
||||||
|
return const_cast<std::array<Type, Rows*Columns>>(std::as_const(*this).RawArray());
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::string ToString() const noexcept
|
std::string ToString() const noexcept
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user