mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
fixed some stuff added constexpr
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<float, float, float> AsTuple() const
|
||||
{
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace omath::projection
|
||||
|
||||
[[nodiscard]] Mat<4, 4> GetViewMatrix() const;
|
||||
|
||||
[[nodiscard]] std::expected<Vector2, Error> WorldToScreen(Vector3 worldPosition) const;
|
||||
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition) const;
|
||||
|
||||
ViewPort m_viewPort{};
|
||||
float m_fieldOfView;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ namespace omath::projection
|
||||
return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up);
|
||||
}
|
||||
|
||||
std::expected<Vector2, Error> Camera::WorldToScreen(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}});
|
||||
|
||||
@@ -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)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user