Mat support multiple integral types

This commit is contained in:
2024-10-20 09:01:18 -07:00
parent 6c85515dd0
commit 50c322dbfc

View File

@@ -13,18 +13,17 @@
namespace omath namespace omath
{ {
template <size_t Rows, size_t Columns> template<size_t Rows, size_t Columns, class Type = float>
class Mat final class Mat final
{ {
public: public:
constexpr Mat() constexpr Mat()
{ {
Clear(); Clear();
} }
constexpr Mat(const std::initializer_list<std::initializer_list<float>>& rows) constexpr Mat(const std::initializer_list<std::initializer_list<Type> > &rows)
{ {
if (rows.size() != Rows) if (rows.size() != Rows)
throw std::invalid_argument("Initializer list rows size does not match template parameter Rows"); throw std::invalid_argument("Initializer list rows size does not match template parameter Rows");
@@ -33,7 +32,8 @@ namespace omath
for (size_t i = 0; i < Rows; ++i, ++rowIt) for (size_t i = 0; i < Rows; ++i, ++rowIt)
{ {
if (rowIt->size() != Columns) if (rowIt->size() != Columns)
throw std::invalid_argument("All rows must have the same number of columns as template parameter Columns"); throw std::invalid_argument(
"All rows must have the same number of columns as template parameter Columns");
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)
@@ -44,13 +44,13 @@ namespace omath
} }
constexpr Mat(const Mat& other) 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);
} }
@@ -62,24 +62,26 @@ namespace omath
static consteval size_t ColumnsCount() noexcept { return Columns; } static consteval size_t ColumnsCount() noexcept { return Columns; }
[[nodiscard]] [[nodiscard]]
static consteval std::pair<size_t, size_t> Size() noexcept { return { Rows, Columns }; } static consteval std::pair<size_t, size_t> Size() noexcept { return {Rows, Columns}; }
[[nodiscard]] constexpr const float& 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");
return m_data[rowIndex * Columns + columnIndex]; return m_data[rowIndex * Columns + columnIndex];
} }
[[nodiscard]] constexpr float& At(const size_t rowIndex, const size_t columnIndex)
[[nodiscard]] constexpr Type &At(const size_t rowIndex, const size_t columnIndex)
{ {
return const_cast<float&>(std::as_const(*this).At(rowIndex, columnIndex)); return const_cast<Type &>(std::as_const(*this).At(rowIndex, columnIndex));
} }
[[nodiscard]] [[nodiscard]]
constexpr float Sum() const constexpr Type Sum() const noexcept
{ {
float sum = 0.f; Type sum = 0;
for (size_t i = 0; i < Rows; ++i) for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < Columns; ++j) for (size_t j = 0; j < Columns; ++j)
sum += At(i, j); sum += At(i, j);
@@ -87,25 +89,26 @@ namespace omath
return sum; return sum;
} }
constexpr void Clear() constexpr void Clear() noexcept
{ {
Set(0.f); Set(0);
} }
constexpr void Set(const float value) constexpr void Set(const Type &value) noexcept
{ {
std::ranges::fill(m_data, value); std::ranges::fill(m_data, value);
} }
// Operator overloading for multiplication with another Mat // Operator overloading for multiplication with another Mat
template <size_t OtherColumns> template<size_t OtherColumns>
constexpr Mat<Rows, OtherColumns> operator*(const Mat<Columns, OtherColumns>& other) const constexpr Mat<Rows, OtherColumns> operator*(const Mat<Columns, OtherColumns> &other) const
{ {
Mat<Rows, OtherColumns> result; Mat<Rows, OtherColumns> result;
for (size_t i = 0; i < Rows; ++i) for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < OtherColumns; ++j) for (size_t j = 0; j < OtherColumns; ++j)
{ {
float sum = 0.f; Type sum = 0;
for (size_t k = 0; k < Columns; ++k) for (size_t k = 0; k < Columns; ++k)
sum += At(i, k) * other.At(k, j); sum += At(i, k) * other.At(k, j);
result.At(i, j) = sum; result.At(i, j) = sum;
@@ -113,7 +116,7 @@ namespace omath
return result; return result;
} }
constexpr Mat& operator*=(float f) constexpr Mat &operator*=(Type f) noexcept
{ {
for (size_t i = 0; i < Rows; ++i) for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < Columns; ++j) for (size_t j = 0; j < Columns; ++j)
@@ -121,20 +124,20 @@ namespace omath
return *this; return *this;
} }
template <size_t OtherColumns> template<size_t OtherColumns>
constexpr Mat<Rows, OtherColumns> operator*=(const Mat<Columns, OtherColumns>& other) constexpr Mat<Rows, OtherColumns> operator*=(const Mat<Columns, OtherColumns> &other)
{ {
return *this = *this * other; return *this = *this * other;
} }
constexpr Mat operator*(float f) const constexpr Mat operator*(const Type &f) const noexcept
{ {
Mat result(*this); Mat result(*this);
result *= f; result *= f;
return result; return result;
} }
constexpr Mat& operator/=(float f) constexpr Mat &operator/=(const Type &f) noexcept
{ {
for (size_t i = 0; i < Rows; ++i) for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < Columns; ++j) for (size_t j = 0; j < Columns; ++j)
@@ -142,14 +145,14 @@ namespace omath
return *this; return *this;
} }
constexpr Mat operator/(float f) const constexpr Mat operator/(const Type &f) const noexcept
{ {
Mat result(*this); Mat result(*this);
result /= f; result /= f;
return result; return result;
} }
constexpr Mat& operator=(const Mat& other) constexpr Mat &operator=(const Mat &other) noexcept
{ {
if (this == &other) if (this == &other)
return *this; return *this;
@@ -159,7 +162,7 @@ namespace omath
return *this; return *this;
} }
constexpr Mat& operator=(Mat&& other) noexcept constexpr Mat &operator=(Mat &&other) noexcept
{ {
if (this == &other) if (this == &other)
return *this; return *this;
@@ -172,7 +175,7 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr Mat<Columns, Rows> Transpose() const constexpr Mat<Columns, Rows> Transpose() const noexcept
{ {
Mat<Columns, Rows> transposed; Mat<Columns, Rows> transposed;
for (size_t i = 0; i < Rows; ++i) for (size_t i = 0; i < Rows; ++i)
@@ -183,7 +186,7 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr float Determinant() const constexpr Type Determinant() const
{ {
static_assert(Rows == Columns, "Determinant is only defined for square matrices."); static_assert(Rows == Columns, "Determinant is only defined for square matrices.");
@@ -194,10 +197,10 @@ namespace omath
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);
else else
{ {
float det = 0.f; Type det = 0;
for (size_t i = 0; i < Columns; ++i) for (size_t i = 0; i < Columns; ++i)
{ {
const float cofactor = (i % 2 == 0 ? 1.f : -1.f) * At(0, i) * Minor(0, i).Determinant(); const Type cofactor = (i % 2 == 0 ? 1 : -1) * At(0, i) * Minor(0, i).Determinant();
det += cofactor; det += cofactor;
} }
return det; return det;
@@ -225,7 +228,7 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
std::string ToString() const std::string ToString() const noexcept
{ {
std::ostringstream oss; std::ostringstream oss;
for (size_t i = 0; i < Rows; ++i) for (size_t i = 0; i < Rows; ++i)
@@ -243,58 +246,59 @@ namespace omath
// Static methods that return fixed-size matrices // Static methods that return fixed-size matrices
[[nodiscard]] [[nodiscard]]
constexpr static Mat<4, 4> ToScreenMat(const float screenWidth, const float screenHeight) constexpr static Mat<4, 4> ToScreenMat(const Type &screenWidth, const Type &screenHeight) noexcept
{ {
return return
{ {
{screenWidth / 2.f, 0.f, 0.f, 0.f}, {screenWidth / 2, 0, 0, 0},
{0.f, -screenHeight / 2.f, 0.f, 0.f}, {0, -screenHeight / 2, 0, 0},
{0.f, 0.f, 1.f, 0.f}, {0, 0, 1, 0},
{screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f}, {screenWidth / 2, screenHeight / 2, 0, 1},
}; };
} }
[[nodiscard]] [[nodiscard]]
constexpr static Mat<4, 4> TranslationMat(const Vector3& diff) constexpr static Mat<4, 4> TranslationMat(const Vector3 &diff) noexcept
{ {
return return
{ {
{1.f, 0.f, 0.f, 0.f}, {1, 0, 0, 0},
{0.f, 1.f, 0.f, 0.f}, {0, 1, 0, 0},
{0.f, 0.f, 1.f, 0.f}, {0, 0, 1, 0},
{diff.x, diff.y, diff.z, 1.f}, {diff.x, diff.y, diff.z, 1},
}; };
} }
[[nodiscard]] [[nodiscard]]
constexpr static Mat<4, 4> OrientationMat(const Vector3& forward, const Vector3& right, const Vector3& up) constexpr static Mat<4, 4> OrientationMat(const Vector3 &forward, const Vector3 &right,
const Vector3 &up) noexcept
{ {
return return
{ {
{right.x, up.x, forward.x, 0.f}, {right.x, up.x, forward.x, 0},
{right.y, up.y, forward.y, 0.f}, {right.y, up.y, forward.y, 0},
{right.z, up.z, forward.z, 0.f}, {right.z, up.z, forward.z, 0},
{0.f, 0.f, 0.f, 1.f}, {0, 0, 0, 1},
}; };
} }
[[nodiscard]] [[nodiscard]]
constexpr static Mat<4, 4> ProjectionMat(const float fieldOfView, const float aspectRatio, constexpr static Mat<4, 4> ProjectionMat(const Type &fieldOfView, const Type &aspectRatio,
const float near, const float far, const float lensZoom) const Type &near, const Type &far, const Type &lensZoom) noexcept
{ {
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f); const Type &fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2);
const float frustumHeight = far - near; const Type &frustumHeight = far - near;
return return
{ {
{-1.f / (aspectRatio * fovHalfTan) * lensZoom, 0.f, 0.f, 0.f}, {-1 / (aspectRatio * fovHalfTan) * lensZoom, 0, 0, 0},
{0.f, -1.f / fovHalfTan * lensZoom, 0.f, 0.f}, {0, -1 / fovHalfTan * lensZoom, 0, 0},
{0.f, 0.f, -far / frustumHeight, -1}, {0, 0, -far / frustumHeight, -1},
{0.f, 0.f, near * far / frustumHeight, 0.f} {0, 0, near * far / frustumHeight, 0}
}; };
} }
private: private:
std::array<float, Rows*Columns> m_data; std::array<Type, Rows * Columns> m_data;
}; };
} }