diff --git a/include/omath/Mat.hpp b/include/omath/Mat.hpp index c68dc95..cf6c497 100644 --- a/include/omath/Mat.hpp +++ b/include/omath/Mat.hpp @@ -13,7 +13,11 @@ namespace omath { - template + struct MatSize + { + size_t rows, columns; + }; + template class Mat final { public: @@ -56,13 +60,22 @@ namespace omath } [[nodiscard]] - static consteval size_t RowCount() noexcept { return Rows; } + static consteval size_t RowCount() noexcept + { + return Rows; + } [[nodiscard]] - static consteval size_t ColumnsCount() noexcept { return Columns; } + static consteval size_t ColumnsCount() noexcept + { + return Columns; + } [[nodiscard]] - static consteval std::pair 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 @@ -116,7 +129,7 @@ namespace omath 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 j = 0; j < Columns; ++j) @@ -175,7 +188,7 @@ namespace omath } [[nodiscard]] - constexpr Mat Transpose() const noexcept + constexpr Mat Transposed() const noexcept { Mat transposed; 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: std::array m_data; }; diff --git a/include/omath/Matrix.hpp b/include/omath/Matrix.hpp index 2488815..81e7b4f 100644 --- a/include/omath/Matrix.hpp +++ b/include/omath/Matrix.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include diff --git a/include/omath/Vector2.hpp b/include/omath/Vector2.hpp index ba5017e..46bc467 100644 --- a/include/omath/Vector2.hpp +++ b/include/omath/Vector2.hpp @@ -16,7 +16,7 @@ namespace omath float y = 0.f; // Constructors - constexpr Vector2() : x(0.f), y(0.f) {} + constexpr Vector2() = default; constexpr Vector2(const float x, const float y) : x(x), y(y) {} @@ -66,7 +66,7 @@ namespace omath return *this; } - constexpr Vector2& operator*=(float fl) + constexpr Vector2& operator*=(const float fl) { x *= fl; y *= fl; @@ -74,7 +74,7 @@ namespace omath return *this; } - constexpr Vector2& operator/=(float fl) + constexpr Vector2& operator/=(const float fl) { x /= fl; y /= fl; @@ -82,7 +82,7 @@ namespace omath return *this; } - constexpr Vector2& operator+=(float fl) + constexpr Vector2& operator+=(const float fl) { x += fl; y += fl; @@ -90,7 +90,7 @@ namespace omath return *this; } - constexpr Vector2& operator-=(float fl) + constexpr Vector2& operator-=(const float fl) { x -= fl; y -= fl; @@ -177,12 +177,12 @@ namespace omath 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}; } - [[nodiscard]] constexpr Vector2 operator/(float fl) const + [[nodiscard]] constexpr Vector2 operator/(const float fl) const { return {x / fl, y / fl}; } diff --git a/include/omath/Vector3.hpp b/include/omath/Vector3.hpp index ec10848..a9a687e 100644 --- a/include/omath/Vector3.hpp +++ b/include/omath/Vector3.hpp @@ -15,8 +15,8 @@ namespace omath { public: float z = 0.f; - constexpr Vector3(float x, float y, float z) : Vector2(x, y), z(z) { } - constexpr Vector3() : Vector2(), z(0.f) {}; + constexpr Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z) { } + constexpr Vector3() : Vector2() {}; [[nodiscard]] constexpr bool operator==(const Vector3& src) const { @@ -44,7 +44,7 @@ namespace omath return *this; } - constexpr Vector3& operator*=(float fl) + constexpr Vector3& operator*=(const float fl) { Vector2::operator*=(fl); z *= fl; @@ -68,7 +68,7 @@ namespace omath return *this; } - constexpr Vector3& operator+=(float fl) + constexpr Vector3& operator+=(const float fl) { Vector2::operator+=(fl); z += fl; @@ -76,7 +76,7 @@ namespace omath return *this; } - constexpr Vector3& operator/=(float fl) + constexpr Vector3& operator/=(const float fl) { Vector2::operator/=(fl); z /= fl; @@ -84,7 +84,7 @@ namespace omath return *this; } - constexpr Vector3& operator-=(float fl) + constexpr Vector3& operator-=(const float fl) { Vector2::operator-=(fl); z -= fl; @@ -175,7 +175,7 @@ namespace omath 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}; } @@ -206,7 +206,7 @@ namespace omath } [[nodiscard]] constexpr float Sum() const { - return Vector3::Sum2D() + z; + return Sum2D() + z; } [[nodiscard]] constexpr float Sum2D() const diff --git a/include/omath/Vector4.hpp b/include/omath/Vector4.hpp index 55c364c..4b43501 100644 --- a/include/omath/Vector4.hpp +++ b/include/omath/Vector4.hpp @@ -14,7 +14,7 @@ namespace omath public: 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) {}; [[nodiscard]] @@ -45,7 +45,7 @@ namespace omath return *this; } - constexpr Vector4& operator*=(float scalar) + constexpr Vector4& operator*=(const float scalar) { Vector3::operator*=(scalar); w *= scalar; @@ -61,7 +61,7 @@ namespace omath return *this; } - constexpr Vector4& operator/=(float scalar) + constexpr Vector4& operator/=(const float scalar) { Vector3::operator/=(scalar); w /= scalar; @@ -95,7 +95,7 @@ namespace omath return *this; } - constexpr Vector4& Clamp(float min, float max) + constexpr Vector4& Clamp(const float min, const float max) { x = std::clamp(x, min, max); y = std::clamp(y, min, max); @@ -123,7 +123,7 @@ namespace omath } [[nodiscard]] - constexpr Vector4 operator*(float scalar) const + constexpr Vector4 operator*(const float scalar) const { return {x * scalar, y * scalar, z * scalar, w * scalar}; } @@ -135,7 +135,7 @@ namespace omath } [[nodiscard]] - constexpr Vector4 operator/(float scalar) const + constexpr Vector4 operator/(const float scalar) const { return {x / scalar, y / scalar, z / scalar, w / scalar}; } diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index be552fa..c26f75a 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -28,16 +28,16 @@ namespace omath::projection 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); - 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 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(), - m_nearPlaneDistance, m_farPlaneDistance, 1.335f); + const auto projectionMatrix = Mat<>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(), + m_nearPlaneDistance, m_farPlaneDistance, m_lensZoom); Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); diff --git a/tests/UnitTestMat.cpp b/tests/UnitTestMat.cpp index 613325c..94d451a 100644 --- a/tests/UnitTestMat.cpp +++ b/tests/UnitTestMat.cpp @@ -88,7 +88,7 @@ TEST_F(UnitTestMat, Operator_Division_Scalar) // Test matrix functions 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, 1), m2.At(1, 0)); EXPECT_FLOAT_EQ(m3.At(1, 0), m2.At(0, 1)); @@ -215,7 +215,7 @@ TEST(UnitTestMatStandalone, Minor_3x3) TEST(UnitTestMatStandalone, Transpose_NonSquare) { 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.ColumnsCount(), 2); EXPECT_FLOAT_EQ(transposed.At(0, 0), 1.0f);