minor improvement

This commit is contained in:
2024-11-22 20:53:43 +03:00
parent 046981bc86
commit 5454c43b18
7 changed files with 66 additions and 34 deletions

View File

@@ -13,7 +13,11 @@
namespace omath namespace omath
{ {
template<size_t Rows, size_t Columns, class Type = float> struct MatSize
{
size_t rows, columns;
};
template<size_t Rows = 0, size_t Columns = 0, class Type = float>
class Mat final class Mat final
{ {
public: public:
@@ -56,13 +60,22 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
static consteval size_t RowCount() noexcept { return Rows; } static consteval size_t RowCount() noexcept
{
return Rows;
}
[[nodiscard]] [[nodiscard]]
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 MatSize Size() noexcept
{
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
@@ -116,7 +129,7 @@ namespace omath
return result; return result;
} }
constexpr Mat &operator*=(const Type& f) noexcept 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)
@@ -175,7 +188,7 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr Mat<Columns, Rows> Transpose() const noexcept constexpr Mat<Columns, Rows> Transposed() 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)
@@ -298,6 +311,26 @@ namespace omath
}; };
} }
[[nodiscard]]
constexpr static Mat<4, 1> MatRowFromVector(const Vector3 &vector) noexcept
{
return {{vector.x, vector.y, vector.z, 1}};
}
[[nodiscard]]
constexpr static Mat<1, 4> MatColumnFromVector(const Vector3 &vector) noexcept
{
return
{
{
{vector.x},
{vector.y},
{vector.z},
{1}
}
};
}
private: private:
std::array<Type, Rows * Columns> m_data; std::array<Type, Rows * Columns> m_data;
}; };

View File

@@ -1,5 +1,4 @@
#pragma once #pragma once
#include <vector>
#include <memory> #include <memory>
#include <string> #include <string>
#include <initializer_list> #include <initializer_list>

View File

@@ -16,7 +16,7 @@ namespace omath
float y = 0.f; float y = 0.f;
// Constructors // Constructors
constexpr Vector2() : x(0.f), y(0.f) {} constexpr Vector2() = default;
constexpr Vector2(const float x, const float y) : x(x), y(y) {} constexpr Vector2(const float x, const float y) : x(x), y(y) {}
@@ -66,7 +66,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator*=(float fl) constexpr Vector2& operator*=(const float fl)
{ {
x *= fl; x *= fl;
y *= fl; y *= fl;
@@ -74,7 +74,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator/=(float fl) constexpr Vector2& operator/=(const float fl)
{ {
x /= fl; x /= fl;
y /= fl; y /= fl;
@@ -82,7 +82,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator+=(float fl) constexpr Vector2& operator+=(const float fl)
{ {
x += fl; x += fl;
y += fl; y += fl;
@@ -90,7 +90,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator-=(float fl) constexpr Vector2& operator-=(const float fl)
{ {
x -= fl; x -= fl;
y -= fl; y -= fl;
@@ -177,12 +177,12 @@ namespace omath
return {x - v.x, y - v.y}; return {x - v.x, y - v.y};
} }
[[nodiscard]] constexpr Vector2 operator*(float fl) const [[nodiscard]] constexpr Vector2 operator*(const float fl) const
{ {
return {x * fl, y * fl}; return {x * fl, y * fl};
} }
[[nodiscard]] constexpr Vector2 operator/(float fl) const [[nodiscard]] constexpr Vector2 operator/(const float fl) const
{ {
return {x / fl, y / fl}; return {x / fl, y / fl};
} }

View File

@@ -15,8 +15,8 @@ namespace omath
{ {
public: public:
float z = 0.f; float z = 0.f;
constexpr Vector3(float x, float y, float z) : Vector2(x, y), z(z) { } constexpr Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z) { }
constexpr Vector3() : Vector2(), z(0.f) {}; constexpr Vector3() : Vector2() {};
[[nodiscard]] constexpr bool operator==(const Vector3& src) const [[nodiscard]] constexpr bool operator==(const Vector3& src) const
{ {
@@ -44,7 +44,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator*=(float fl) constexpr Vector3& operator*=(const float fl)
{ {
Vector2::operator*=(fl); Vector2::operator*=(fl);
z *= fl; z *= fl;
@@ -68,7 +68,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator+=(float fl) constexpr Vector3& operator+=(const float fl)
{ {
Vector2::operator+=(fl); Vector2::operator+=(fl);
z += fl; z += fl;
@@ -76,7 +76,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator/=(float fl) constexpr Vector3& operator/=(const float fl)
{ {
Vector2::operator/=(fl); Vector2::operator/=(fl);
z /= fl; z /= fl;
@@ -84,7 +84,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator-=(float fl) constexpr Vector3& operator-=(const float fl)
{ {
Vector2::operator-=(fl); Vector2::operator-=(fl);
z -= fl; z -= fl;
@@ -175,7 +175,7 @@ namespace omath
return {x - v.x, y - v.y, z - v.z}; return {x - v.x, y - v.y, z - v.z};
} }
[[nodiscard]] constexpr Vector3 operator*(float fl) const [[nodiscard]] constexpr Vector3 operator*(const float fl) const
{ {
return {x * fl, y * fl, z * fl}; return {x * fl, y * fl, z * fl};
} }
@@ -206,7 +206,7 @@ namespace omath
} }
[[nodiscard]] constexpr float Sum() const [[nodiscard]] constexpr float Sum() const
{ {
return Vector3::Sum2D() + z; return Sum2D() + z;
} }
[[nodiscard]] constexpr float Sum2D() const [[nodiscard]] constexpr float Sum2D() const

View File

@@ -14,7 +14,7 @@ namespace omath
public: public:
float w; float w;
constexpr Vector4(float x, float y, float z, float w) : Vector3(x, y, z), w(w) {} constexpr Vector4(const float x, const float y, const float z, const float w) : Vector3(x, y, z), w(w) {}
constexpr Vector4() : Vector3(), w(0.f) {}; constexpr Vector4() : Vector3(), w(0.f) {};
[[nodiscard]] [[nodiscard]]
@@ -45,7 +45,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& operator*=(float scalar) constexpr Vector4& operator*=(const float scalar)
{ {
Vector3::operator*=(scalar); Vector3::operator*=(scalar);
w *= scalar; w *= scalar;
@@ -61,7 +61,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& operator/=(float scalar) constexpr Vector4& operator/=(const float scalar)
{ {
Vector3::operator/=(scalar); Vector3::operator/=(scalar);
w /= scalar; w /= scalar;
@@ -95,7 +95,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& Clamp(float min, float max) constexpr Vector4& Clamp(const float min, const float max)
{ {
x = std::clamp(x, min, max); x = std::clamp(x, min, max);
y = std::clamp(y, min, max); y = std::clamp(y, min, max);
@@ -123,7 +123,7 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator*(float scalar) const constexpr Vector4 operator*(const float scalar) const
{ {
return {x * scalar, y * scalar, z * scalar, w * scalar}; return {x * scalar, y * scalar, z * scalar, w * scalar};
} }
@@ -135,7 +135,7 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator/(float scalar) const constexpr Vector4 operator/(const float scalar) const
{ {
return {x / scalar, y / scalar, z / scalar, w / scalar}; return {x / scalar, y / scalar, z / scalar, w / scalar};
} }

View File

@@ -28,16 +28,16 @@ namespace omath::projection
const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up); return Mat<>::TranslationMat(-m_origin) * Mat<>::OrientationMat(forward, right, up);
} }
std::expected<Vector3, Error> Camera::WorldToScreen(const Vector3& worldPosition) const std::expected<Vector3, Error> Camera::WorldToScreen(const Vector3& worldPosition) const
{ {
const auto posVecAsMatrix = Mat<1, 4>({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); const auto posVecAsMatrix = Mat<>::MatColumnFromVector(worldPosition);
const auto projectionMatrix = Mat<4, 4>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(), const auto projectionMatrix = Mat<>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(),
m_nearPlaneDistance, m_farPlaneDistance, 1.335f); m_nearPlaneDistance, m_farPlaneDistance, m_lensZoom);
Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);

View File

@@ -88,7 +88,7 @@ TEST_F(UnitTestMat, Operator_Division_Scalar)
// Test matrix functions // Test matrix functions
TEST_F(UnitTestMat, Transpose) TEST_F(UnitTestMat, Transpose)
{ {
Mat<2, 2> m3 = m2.Transpose(); Mat<2, 2> m3 = m2.Transposed();
EXPECT_FLOAT_EQ(m3.At(0, 0), m2.At(0, 0)); EXPECT_FLOAT_EQ(m3.At(0, 0), m2.At(0, 0));
EXPECT_FLOAT_EQ(m3.At(0, 1), m2.At(1, 0)); EXPECT_FLOAT_EQ(m3.At(0, 1), m2.At(1, 0));
EXPECT_FLOAT_EQ(m3.At(1, 0), m2.At(0, 1)); EXPECT_FLOAT_EQ(m3.At(1, 0), m2.At(0, 1));
@@ -215,7 +215,7 @@ TEST(UnitTestMatStandalone, Minor_3x3)
TEST(UnitTestMatStandalone, Transpose_NonSquare) TEST(UnitTestMatStandalone, Transpose_NonSquare)
{ {
constexpr Mat<2, 3> m{{1.0f, 2.0f, 3.0f}, {4.0f, 5.0f, 6.0f}}; constexpr Mat<2, 3> m{{1.0f, 2.0f, 3.0f}, {4.0f, 5.0f, 6.0f}};
auto transposed = m.Transpose(); auto transposed = m.Transposed();
EXPECT_EQ(transposed.RowCount(), 3); EXPECT_EQ(transposed.RowCount(), 3);
EXPECT_EQ(transposed.ColumnsCount(), 2); EXPECT_EQ(transposed.ColumnsCount(), 2);
EXPECT_FLOAT_EQ(transposed.At(0, 0), 1.0f); EXPECT_FLOAT_EQ(transposed.At(0, 0), 1.0f);