From 08d2ccc03aa827ae5ce5f115b9590b03c4ac84fd Mon Sep 17 00:00:00 2001 From: Orange Date: Wed, 6 Aug 2025 06:06:42 +0300 Subject: [PATCH] Refactors Vector operations for type safety Ensures type safety in Vector2, Vector3, and Vector4 operations by using static_cast(0) instead of relying on implicit conversions. This prevents potential issues with different numeric types. Adds from_im_vec2 and from_im_vec4 methods for creating vectors from ImVec2/ImVec4 types. --- include/omath/vector2.hpp | 11 ++++++++--- include/omath/vector3.hpp | 6 +++--- include/omath/vector4.hpp | 10 ++++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/omath/vector2.hpp b/include/omath/vector2.hpp index 6287131..2c59b06 100644 --- a/include/omath/vector2.hpp +++ b/include/omath/vector2.hpp @@ -142,7 +142,7 @@ namespace omath [[nodiscard]] Vector2 normalized() const noexcept { const Type len = length(); - return len > 0.f ? *this / len : *this; + return len > static_cast(0) ? *this / len : *this; } #endif [[nodiscard]] constexpr Type length_sqr() const noexcept @@ -153,8 +153,8 @@ namespace omath constexpr Vector2& abs() noexcept { // FIXME: Replace with std::abs, if it will become constexprable - x = x < 0 ? -x : x; - y = y < 0 ? -y : y; + x = x < static_cast(0) ? -x : x; + y = y < static_cast(0) ? -y : y; return *this; } @@ -202,6 +202,11 @@ namespace omath { return {static_cast(this->x), static_cast(this->y)}; } + [[nodiscard]] + static Vector3 from_im_vec2(const ImVec2& other) noexcept + { + return {static_cast(other.x), static_cast(other.y)}; + } #endif }; } // namespace omath diff --git a/include/omath/vector3.hpp b/include/omath/vector3.hpp index a4654c0..662ff80 100644 --- a/include/omath/vector3.hpp +++ b/include/omath/vector3.hpp @@ -151,7 +151,7 @@ namespace omath { const Type len = this->length(); - return len != 0 ? *this / len : *this; + return len != static_cast(0) ? *this / len : *this; } [[nodiscard]] Type length_2d() const noexcept @@ -221,7 +221,7 @@ namespace omath { const auto bottom = length() * other.length(); - if (bottom == 0.f) + if (bottom == static_cast(0)) return std::unexpected(Vector3Error::IMPOSSIBLE_BETWEEN_ANGLE); return Angle::from_radians(std::acos(dot(other) / bottom)); @@ -230,7 +230,7 @@ namespace omath [[nodiscard]] bool is_perpendicular(const Vector3& other) const noexcept { if (const auto angle = angle_between(other)) - return angle->as_degrees() == 90.f; + return angle->as_degrees() == static_cast(90); return false; } diff --git a/include/omath/vector4.hpp b/include/omath/vector4.hpp index 9e54fbb..24d3d59 100644 --- a/include/omath/vector4.hpp +++ b/include/omath/vector4.hpp @@ -18,7 +18,7 @@ namespace omath constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w): Vector3(x, y, z), w(w) { } - constexpr Vector4() noexcept : Vector3(), w(0) {}; + constexpr Vector4() noexcept: Vector3(), w(static_cast(0)) {}; [[nodiscard]] constexpr bool operator==(const Vector4& other) const noexcept @@ -169,6 +169,12 @@ namespace omath static_cast(w), }; } + [[nodiscard]] + static Vector4 from_im_vec4(const ImVec4& other) noexcept + { + return {static_cast(other.x), static_cast(other.y), static_cast(other.z)}; + } + } #endif - }; +}; } // namespace omath