From 6dd72d2448d568e87b40943985a01cba09af1d86 Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 17 Mar 2025 03:56:09 +0300 Subject: [PATCH 1/3] added convertors --- CMakeLists.txt | 13 +++++++++---- include/omath/Vector2.hpp | 34 ++++++++++++++++++++++++++++------ include/omath/Vector3.hpp | 9 ++++----- include/omath/Vector4.hpp | 18 ++++++++++++++++++ 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8229e3..3c94fc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ option(OMATH_BUILD_TESTS "Build unit tests" OFF) option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON) option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF) option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON) - +option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" ON) if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/Vector3.cpp) @@ -17,6 +17,14 @@ else() add_library(omath STATIC source/Matrix.cpp) endif() +if (OMATH_IMGUI_INTEGRATION) + target_compile_definitions(omath PUBLIC OMATH_IMGUI_INTEGRATION) +endif() + +if (OMATH_USE_AVX2) + target_compile_definitions(omath PUBLIC OMATH_USE_AVX2) +endif() + set_target_properties(omath PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" @@ -31,9 +39,6 @@ endif() target_compile_features(omath PUBLIC cxx_std_23) -if (OMATH_USE_AVX2) - target_compile_definitions(omath PUBLIC OMATH_USE_AVX2) -endif() add_subdirectory(source) diff --git a/include/omath/Vector2.hpp b/include/omath/Vector2.hpp index 60d2ed5..5873eb7 100644 --- a/include/omath/Vector2.hpp +++ b/include/omath/Vector2.hpp @@ -3,13 +3,19 @@ // #pragma once -#include #include +#include + +#ifdef OMATH_IMGUI_INTEGRATION + class ImVec2; +#endif + namespace omath { - template requires std::is_arithmetic_v + template + requires std::is_arithmetic_v class Vector2 { public: @@ -19,7 +25,9 @@ namespace omath // Constructors constexpr Vector2() = default; - constexpr Vector2(const Type& x, const Type& y) : x(x), y(y) {} + constexpr Vector2(const Type& x, const Type& y) : x(x), y(y) + { + } // Equality operators [[nodiscard]] @@ -145,7 +153,7 @@ namespace omath constexpr Vector2& Abs() { - //FIXME: Replace with std::abs, if it will become constexprable + // FIXME: Replace with std::abs, if it will become constexprable x = x < 0 ? -x : x; y = y < 0 ? -y : y; return *this; @@ -188,7 +196,7 @@ namespace omath return {x / fl, y / fl}; } - // Sum of elements + // Sum of elements [[nodiscard]] constexpr Type Sum() const { return x + y; @@ -199,5 +207,19 @@ namespace omath { return std::make_tuple(x, y); } + +#ifdef OMATH_IMGUI_INTEGRATION + [[nodiscard]] + const ImVec2& ToImVec2() const + { + return *reinterpret_cast(this); + } + + [[nodiscard]] + ImVec2& ToImVec2() + { + return *reinterpret_cast(this); + } +#endif }; -} \ No newline at end of file +} // namespace omath diff --git a/include/omath/Vector3.hpp b/include/omath/Vector3.hpp index 588c608..c3ffcd0 100644 --- a/include/omath/Vector3.hpp +++ b/include/omath/Vector3.hpp @@ -4,13 +4,12 @@ #pragma once +#include "Vector2.hpp" #include -#include -#include "omath/Vector2.hpp" -#include "omath/Angle.hpp" #include -#include - +#include +#include "omath/Angle.hpp" +#include "omath/Vector2.hpp" namespace omath { diff --git a/include/omath/Vector4.hpp b/include/omath/Vector4.hpp index f4fcadd..f927445 100644 --- a/include/omath/Vector4.hpp +++ b/include/omath/Vector4.hpp @@ -6,6 +6,10 @@ #include #include +#ifdef OMATH_IMGUI_INTEGRATION + class ImVec4; +#endif + namespace omath { template @@ -154,5 +158,19 @@ namespace omath { return Vector3::Sum() + w; } + +#ifdef OMATH_IMGUI_INTEGRATION + [[nodiscard]] + constexpr ImVec4& ToImVec4() const + { + return *reinterpret_cast(this); + } + + [[nodiscard]] + constexpr ImVec2& ToImVec2() + { + return *reinterpret_cast(this); + } +#endif }; } From 8fc107ec0fb4abfcbad025b1a48a813c5e4c64af Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 17 Mar 2025 04:07:21 +0300 Subject: [PATCH 2/3] fix --- include/omath/Vector2.hpp | 23 +++-------------------- include/omath/Vector4.hpp | 19 ++++++++----------- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/include/omath/Vector2.hpp b/include/omath/Vector2.hpp index 5873eb7..f2ae0c3 100644 --- a/include/omath/Vector2.hpp +++ b/include/omath/Vector2.hpp @@ -7,7 +7,7 @@ #include #ifdef OMATH_IMGUI_INTEGRATION - class ImVec2; +#include #endif @@ -159,17 +159,6 @@ namespace omath return *this; } - template - [[nodiscard]] constexpr const type& As() const - { - return *reinterpret_cast(this); - } - template - [[nodiscard]] constexpr type& As() - { - return *reinterpret_cast(this); - } - [[nodiscard]] constexpr Vector2 operator-() const { return {-x, -y}; @@ -210,15 +199,9 @@ namespace omath #ifdef OMATH_IMGUI_INTEGRATION [[nodiscard]] - const ImVec2& ToImVec2() const + ImVec2 ToImVec2() const { - return *reinterpret_cast(this); - } - - [[nodiscard]] - ImVec2& ToImVec2() - { - return *reinterpret_cast(this); + return {static_cast(this->x), static_cast(this->y)}; } #endif }; diff --git a/include/omath/Vector4.hpp b/include/omath/Vector4.hpp index f927445..258cf73 100644 --- a/include/omath/Vector4.hpp +++ b/include/omath/Vector4.hpp @@ -6,9 +6,6 @@ #include #include -#ifdef OMATH_IMGUI_INTEGRATION - class ImVec4; -#endif namespace omath { @@ -161,15 +158,15 @@ namespace omath #ifdef OMATH_IMGUI_INTEGRATION [[nodiscard]] - constexpr ImVec4& ToImVec4() const + ImVec4 ToImVec4() const { - return *reinterpret_cast(this); - } - - [[nodiscard]] - constexpr ImVec2& ToImVec2() - { - return *reinterpret_cast(this); + return + { + static_cast(this->x), + static_cast(this->y), + static_cast(this->z), + static_cast(w), + }; } #endif }; From e5d0adf24765e399c5dadfbaab763317f84735f3 Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 17 Mar 2025 04:11:49 +0300 Subject: [PATCH 3/3] changed default option --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c94fc3..b2bf452 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ option(OMATH_BUILD_TESTS "Build unit tests" OFF) option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON) option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF) option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON) -option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" ON) +option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" OFF) if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/Vector3.cpp)