diff --git a/include/omath/Vector2.h b/include/omath/Vector2.h index f04da2d..0bb73e9 100644 --- a/include/omath/Vector2.h +++ b/include/omath/Vector2.h @@ -114,7 +114,10 @@ namespace omath return x * vOther.x + y * vOther.y; } - [[nodiscard]] float Length() const; + [[nodiscard]] constexpr float Length() const + { + return std::hypot(x, y); + } [[nodiscard]] constexpr float LengthSqr() const { @@ -167,7 +170,11 @@ namespace omath } // Normalize the vector - [[nodiscard]] Vector2 Normalized() const; + [[nodiscard]] constexpr Vector2 Normalized() const + { + const float len = Length(); + return len > 0.f ? *this / len : *this; + } // Sum of elements [[nodiscard]] constexpr float Sum() const diff --git a/include/omath/Vector3.h b/include/omath/Vector3.h index 18e2ffd..1b75d39 100644 --- a/include/omath/Vector3.h +++ b/include/omath/Vector3.h @@ -92,8 +92,10 @@ namespace omath return *this; } - [[nodiscard]] - float DistTo(const Vector3& vOther) const; + [[nodiscard]] constexpr float DistTo(const Vector3& vOther) const + { + return (*this - vOther).Length(); + } constexpr Vector3& Abs() { @@ -112,14 +114,21 @@ namespace omath { return Vector2::Dot(vOther) + z * vOther.z; } - [[nodiscard]] float Length() const; + + [[nodiscard]] constexpr float Length() const + { + return std::hypot(x, y, z); + } [[nodiscard]] constexpr float LengthSqr() const { return Vector2::LengthSqr() + z * z; } - [[nodiscard]] float Length2D() const; + [[nodiscard]] constexpr float Length2D() const + { + return Vector2::Length(); + } [[nodiscard]] constexpr Vector3 operator-() const { @@ -182,8 +191,12 @@ namespace omath [[nodiscard]] static Vector3 UpVector(float pitch, float yaw, float roll); - [[nodiscard]] - Vector3 Normalized() const; + [[nodiscard]] constexpr Vector3 Normalized() const + { + const float length = this->Length(); + + return length != 0 ? *this / length : *this; + } [[nodiscard]] std::tuple AsTuple() const { diff --git a/include/omath/projection/Camera.h b/include/omath/projection/Camera.h index 23067dd..f7020c8 100644 --- a/include/omath/projection/Camera.h +++ b/include/omath/projection/Camera.h @@ -31,7 +31,7 @@ namespace omath::projection [[nodiscard]] Mat<4, 4> GetViewMatrix() const; - [[nodiscard]] std::expected WorldToScreen(Vector3 worldPosition) const; + [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const; ViewPort m_viewPort{}; float m_fieldOfView; diff --git a/source/Vector2.cpp b/source/Vector2.cpp index 66dc4f0..56d8044 100644 --- a/source/Vector2.cpp +++ b/source/Vector2.cpp @@ -8,18 +8,4 @@ namespace omath { - Vector2 Vector2::Normalized() const - { - const float len = Length(); - - if (len > 0.f) - return {x / len, y / len}; - - return {0.f, 0.f}; - } - - float Vector2::Length() const - { - return std::sqrt(x * x + y * y); - } } \ No newline at end of file diff --git a/source/Vector3.cpp b/source/Vector3.cpp index 4d5d756..8da7c68 100644 --- a/source/Vector3.cpp +++ b/source/Vector3.cpp @@ -8,23 +8,6 @@ namespace omath { - - float Vector3::DistTo(const Vector3 &vOther) const - { - return (*this - vOther).Length(); - } - - float Vector3::Length() const - { - return std::sqrt(Vector2::LengthSqr() + z * z); - } - - - float Vector3::Length2D() const - { - return Vector2::Length(); - } - Vector3 Vector3::ViewAngleTo(const Vector3 &other) const { const float distance = DistTo(other); @@ -83,12 +66,4 @@ namespace omath { return RightVector(pitch, yaw, roll).Cross(ForwardVector(pitch, yaw)); } - - - Vector3 Vector3::Normalized() const - { - const float length = this->Length(); - - return length != 0 ? *this / length : *this; - } } \ No newline at end of file diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index a741139..e5b206e 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -32,7 +32,7 @@ namespace omath::projection return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up); } - std::expected Camera::WorldToScreen(Vector3 worldPosition) const + std::expected Camera::WorldToScreen(const Vector3& worldPosition) const { const auto posVecAsMatrix = Mat<1, 4>({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); @@ -54,6 +54,6 @@ namespace omath::projection projected *= Mat<4, 4>::ToScreenMat(m_viewPort.m_width, m_viewPort.m_height); - return Vector2{projected.At(0, 0), projected.At(0, 1)}; + return Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)}; } } diff --git a/tests/UnitTestMat.cpp b/tests/UnitTestMat.cpp index f76566a..931c72f 100644 --- a/tests/UnitTestMat.cpp +++ b/tests/UnitTestMat.cpp @@ -184,24 +184,6 @@ TEST_F(UnitTestMat, StaticMethod_OrientationMat) EXPECT_FLOAT_EQ(orientMat.At(2, 2), forward.z); } -// Test static method: ProjectionMat -TEST_F(UnitTestMat, StaticMethod_ProjectionMat) -{ - constexpr float fieldOfView = 45.0f; - constexpr float aspectRatio = 1.33f; - constexpr float near = 0.1f; - constexpr float far = 100.0f; - const Mat<4, 4> projMat = Mat<4, 4>::ProjectionMat(fieldOfView, aspectRatio, near, far, 1.335f); - - const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f); - - EXPECT_FLOAT_EQ(projMat.At(0, 0), 1.f / (aspectRatio * fovHalfTan)); - EXPECT_FLOAT_EQ(projMat.At(1, 1), 1.f / fovHalfTan); - EXPECT_FLOAT_EQ(projMat.At(2, 2), (far + near) / (far - near)); - EXPECT_FLOAT_EQ(projMat.At(2, 3), (2.f * near * far) / (far - near)); - EXPECT_FLOAT_EQ(projMat.At(3, 2), -1.f); -} - // Test exception handling in At() method TEST_F(UnitTestMat, Method_At_OutOfRange) { diff --git a/tests/UnitTestProjection.cpp b/tests/UnitTestProjection.cpp index a4199d1..2bb50f9 100644 --- a/tests/UnitTestProjection.cpp +++ b/tests/UnitTestProjection.cpp @@ -9,7 +9,11 @@ TEST(UnitTestProjection, Projection) { - const omath::projection::Camera camera({0.f, 0.f, 0.f}, {0, 0.f, 0.f} , {1920.f, 1080.f}, 110.f, 0.375f, 5000.f); + const omath::projection::Camera camera({0.f, 0.f, 0.f}, {0, 0.f, 0.f} , {1920.f, 1080.f}, 110.f, 0.375f, 5000.f, 1.335f); - camera.WorldToScreen({5000, 0, 0}); + const auto projected = camera.WorldToScreen({5000, 0, 0}); + + + EXPECT_TRUE(projected.has_value()); + EXPECT_EQ(projected->z, 1.f); } \ No newline at end of file