Merge pull request #54 from orange-cpp/feature/vec_fix_improvement

Adds comparison operators to Vector types
This commit is contained in:
2025-08-12 08:57:31 +03:00
committed by GitHub
6 changed files with 135 additions and 1 deletions

View File

@@ -190,6 +190,29 @@ namespace omath
return x + y; return x + y;
} }
[[nodiscard]]
bool operator<(const Vector2& other) const noexcept
{
return length() < other.length();
}
[[nodiscard]]
bool operator>(const Vector2& other) const noexcept
{
return length() > other.length();
}
[[nodiscard]]
bool operator<=(const Vector2& other) const noexcept
{
return length() <= other.length();
}
[[nodiscard]]
bool operator>=(const Vector2& other) const noexcept
{
return length() >= other.length();
}
[[nodiscard]] [[nodiscard]]
constexpr std::tuple<Type, Type> as_tuple() const noexcept constexpr std::tuple<Type, Type> as_tuple() const noexcept
{ {

View File

@@ -253,6 +253,30 @@ namespace omath
return {angles::radians_to_degrees(std::asin(delta.z / distance)), return {angles::radians_to_degrees(std::asin(delta.z / distance)),
angles::radians_to_degrees(std::atan2(delta.y, delta.x)), 0}; angles::radians_to_degrees(std::atan2(delta.y, delta.x)), 0};
} }
[[nodiscard]]
bool operator<(const Vector3& other) const noexcept
{
return length() < other.length();
}
[[nodiscard]]
bool operator>(const Vector3& other) const noexcept
{
return length() > other.length();
}
[[nodiscard]]
bool operator<=(const Vector3& other) const noexcept
{
return length() <= other.length();
}
[[nodiscard]]
bool operator>=(const Vector3& other) const noexcept
{
return length() >= other.length();
}
}; };
} // namespace omath } // namespace omath
// ReSharper disable once CppRedundantNamespaceDefinition // ReSharper disable once CppRedundantNamespaceDefinition

View File

@@ -89,7 +89,7 @@ namespace omath
return Vector3<Type>::dot(other) + w * other.w; return Vector3<Type>::dot(other) + w * other.w;
} }
[[nodiscard]] Vector3<Type> length() const noexcept [[nodiscard]] Type length() const noexcept
{ {
return std::sqrt(length_sqr()); return std::sqrt(length_sqr());
} }
@@ -158,6 +158,30 @@ namespace omath
return Vector3<Type>::sum() + w; return Vector3<Type>::sum() + w;
} }
[[nodiscard]]
bool operator<(const Vector4& other) const noexcept
{
return length() < other.length();
}
[[nodiscard]]
bool operator>(const Vector4& other) const noexcept
{
return length() > other.length();
}
[[nodiscard]]
bool operator<=(const Vector4& other) const noexcept
{
return length() <= other.length();
}
[[nodiscard]]
bool operator>=(const Vector4& other) const noexcept
{
return length() >= other.length();
}
#ifdef OMATH_IMGUI_INTEGRATION #ifdef OMATH_IMGUI_INTEGRATION
[[nodiscard]] [[nodiscard]]
ImVec4 to_im_vec4() const noexcept ImVec4 to_im_vec4() const noexcept

View File

@@ -346,6 +346,28 @@ TEST_F(UnitTestVector2, NegationOperator_ZeroVector)
EXPECT_FLOAT_EQ(result.y, -0.0f); EXPECT_FLOAT_EQ(result.y, -0.0f);
} }
TEST_F(UnitTestVector2, LessOperator)
{
EXPECT_TRUE(v1 < v2);
}
TEST_F(UnitTestVector2, GreaterOperator)
{
EXPECT_TRUE(v2 > v1);
}
TEST_F(UnitTestVector2, LessEqualOperator)
{
EXPECT_TRUE(omath::Vector2<float>{} <= omath::Vector2<float>{});
EXPECT_TRUE(omath::Vector2<float>{} <= omath::Vector2(1.f, 1.f));
}
TEST_F(UnitTestVector2, GreaterEqualOperator)
{
EXPECT_TRUE(omath::Vector2<float>{} >= omath::Vector2<float>{});
EXPECT_TRUE(omath::Vector2(1.f, 1.f) >= omath::Vector2<float>{});
}
// Static assertions (compile-time checks) // Static assertions (compile-time checks)
static_assert(Vector2(1.0f, 2.0f).length_sqr() == 5.0f, "LengthSqr should be 5"); static_assert(Vector2(1.0f, 2.0f).length_sqr() == 5.0f, "LengthSqr should be 5");
static_assert(Vector2(1.0f, 2.0f).dot(Vector2(4.0f, 5.0f)) == 14.0f, "Dot product should be 14"); static_assert(Vector2(1.0f, 2.0f).dot(Vector2(4.0f, 5.0f)) == 14.0f, "Dot product should be 14");

View File

@@ -402,6 +402,27 @@ TEST_F(UnitTestVector3, IsPerpendicular)
EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).is_perpendicular({0.0f, 0.0f, 1.0f})); EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).is_perpendicular({0.0f, 0.0f, 1.0f}));
} }
TEST_F(UnitTestVector3, LessOperator)
{
EXPECT_TRUE(v1 < v2);
}
TEST_F(UnitTestVector3, GreaterOperator)
{
EXPECT_TRUE(v2 > v1);
}
TEST_F(UnitTestVector3, LessEqualOperator)
{
EXPECT_TRUE(omath::Vector3<float>{} <= omath::Vector3<float>{});
EXPECT_TRUE(omath::Vector3<float>{} <= omath::Vector3(1.f, 1.f, 1.f));
}
TEST_F(UnitTestVector3, GreaterEqualOperator)
{
EXPECT_TRUE(omath::Vector3<float>{} >= omath::Vector3<float>{});
EXPECT_TRUE(omath::Vector3(1.f, 1.f, 1.f) >= omath::Vector3<float>{});
}
// Static assertions (compile-time checks) // Static assertions (compile-time checks)
static_assert(Vector3(1.0f, 2.0f, 3.0f).length_sqr() == 14.0f, "LengthSqr should be 14"); static_assert(Vector3(1.0f, 2.0f, 3.0f).length_sqr() == 14.0f, "LengthSqr should be 14");
static_assert(Vector3(1.0f, 2.0f, 3.0f).dot(Vector3(4.0f, 5.0f, 6.0f)) == 32.0f, "Dot product should be 32"); static_assert(Vector3(1.0f, 2.0f, 3.0f).dot(Vector3(4.0f, 5.0f, 6.0f)) == 32.0f, "Dot product should be 32");

View File

@@ -215,3 +215,23 @@ TEST_F(UnitTestVector4, Clamp)
EXPECT_FLOAT_EQ(v3.z, 2.5f); EXPECT_FLOAT_EQ(v3.z, 2.5f);
EXPECT_FLOAT_EQ(v3.w, 4.0f); // w is not clamped in this method EXPECT_FLOAT_EQ(v3.w, 4.0f); // w is not clamped in this method
} }
TEST_F(UnitTestVector4, LessOperator)
{
EXPECT_TRUE(v1 < v2);
}
TEST_F(UnitTestVector4, GreaterOperator)
{
EXPECT_TRUE(v2 > v1);
}
TEST_F(UnitTestVector4, LessEqualOperator)
{
EXPECT_TRUE(omath::Vector4<float>{} <= omath::Vector4<float>{});
EXPECT_TRUE(omath::Vector4<float>{} <= omath::Vector4(1.f, 1.f, 1.f, 1.f));
}
TEST_F(UnitTestVector4, GreaterEqualOperator)
{
EXPECT_TRUE(omath::Vector4<float>{} >= omath::Vector4<float>{});
EXPECT_TRUE(omath::Vector4(1.f, 1.f, 1.f, 1.f) >= omath::Vector4<float>{});
}