From dc0bca14c50a1b6adb72d9be5f1494d5cb85d4d9 Mon Sep 17 00:00:00 2001 From: Orange Date: Thu, 2 Jan 2025 12:52:34 +0300 Subject: [PATCH] added method + tests --- include/omath/Vector3.hpp | 20 ++++++++++++++++++++ tests/general/UnitTestVector3.cpp | 9 +++++++++ 2 files changed, 29 insertions(+) diff --git a/include/omath/Vector3.hpp b/include/omath/Vector3.hpp index b24ad660..7d3ff50d 100644 --- a/include/omath/Vector3.hpp +++ b/include/omath/Vector3.hpp @@ -7,9 +7,18 @@ #include #include #include "omath/Vector2.hpp" +#include "omath/Angle.hpp" +#include + namespace omath { + + enum class Vector3Error + { + IMPOSSIBLE_BETWEEN_ANGLE, + }; + class Vector3 : public Vector2 { public: @@ -208,6 +217,17 @@ namespace omath return Sum2D() + z; } + [[nodiscard]] std::expected, Vector3Error> + AngleBetween(const Vector3& other) const + { + const auto bottom = Length() * other.Length(); + + if (bottom == 0.f) + return std::unexpected(Vector3Error::IMPOSSIBLE_BETWEEN_ANGLE); + + return Angle::FromRadians(std::acos(Dot(other) / bottom)); + } + [[nodiscard]] constexpr float Sum2D() const { return Vector2::Sum(); diff --git a/tests/general/UnitTestVector3.cpp b/tests/general/UnitTestVector3.cpp index f08a35f8..14002d34 100644 --- a/tests/general/UnitTestVector3.cpp +++ b/tests/general/UnitTestVector3.cpp @@ -387,6 +387,15 @@ TEST_F(UnitTestVector3, AsTuple) EXPECT_FLOAT_EQ(std::get<2>(tuple), v1.z); } +// Test AsTuple method +TEST_F(UnitTestVector3, AngleBeatween) +{ + EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).AngleBetween({1, 0 ,0}).value().AsDegrees(), 90.0f); + EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).AngleBetween({0.0f, 0.0f, 1.0f}).value().AsDegrees(), 0.0f); + EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).AngleBetween({0.0f, 0.0f, 1.0f}).has_value()); +} + + // Static assertions (compile-time checks) static_assert(Vector3(1.0f, 2.0f, 3.0f).LengthSqr() == 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");