From 92dab52d66df0c33ba1d2b9b1efe7fb0062167b2 Mon Sep 17 00:00:00 2001 From: Orange Date: Thu, 25 Sep 2025 21:43:33 +0300 Subject: [PATCH 1/4] improvement --- include/omath/color.hpp | 6 ++--- include/omath/linear_algebra/mat.hpp | 28 ++++++++++++++++++++++-- include/omath/linear_algebra/vector2.hpp | 13 +++++++++-- include/omath/linear_algebra/vector3.hpp | 13 +++++++++-- include/omath/linear_algebra/vector4.hpp | 12 ++++++++-- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/include/omath/color.hpp b/include/omath/color.hpp index b606231..7de2efe 100644 --- a/include/omath/color.hpp +++ b/include/omath/color.hpp @@ -179,20 +179,20 @@ struct std::formatter // NOLINT(*-dcl58-cpp) [[nodiscard]] static auto format(const omath::Color& col, FormatContext& ctx) { - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) return std::format_to(ctx.out(), "[r:{}, g:{}, b:{}, a:{}]", static_cast(col.x * 255.f), static_cast(col.y * 255.f), static_cast(col.z * 255.f), static_cast(col.w * 255.f)); - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) return std::format_to(ctx.out(), L"[r:{}, g:{}, b:{}, a:{}]", static_cast(col.x * 255.f), static_cast(col.y * 255.f), static_cast(col.z * 255.f), static_cast(col.w * 255.f)); - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) return std::format_to(ctx.out(), u8"[r:{}, g:{}, b:{}, a:{}]", static_cast(col.x * 255.f), static_cast(col.y * 255.f), diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index 3549db2..b926edd 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -328,6 +328,21 @@ namespace omath return oss.str(); } + [[nodiscard]] + std::wstring to_wstring() const noexcept + { + const auto ascii_string = to_string(); + return {ascii_string.cbegin(), ascii_string.cend()}; + } + + [[nodiscard]] + // ReSharper disable once CppInconsistentNaming + std::u8string to_u8string() const noexcept + { + const auto ascii_string = to_string(); + return {ascii_string.cbegin(), ascii_string.cend()}; + } + [[nodiscard]] bool operator==(const Mat& mat) const { @@ -706,9 +721,18 @@ struct std::formatter> // NOLINT(*-dc { return ctx.begin(); } + + template [[nodiscard]] - static auto format(const MatType& mat, std::format_context& ctx) + static auto format(const MatType& mat, FormatContext& ctx) { - return std::format_to(ctx.out(), "{}", mat.to_string()); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), "{}", mat.to_string()); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), L"{}", mat.to_wstring()); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), u8"{}", mat.to_u8string()); } }; \ No newline at end of file diff --git a/include/omath/linear_algebra/vector2.hpp b/include/omath/linear_algebra/vector2.hpp index 7e8ddc8..4dc96c7 100644 --- a/include/omath/linear_algebra/vector2.hpp +++ b/include/omath/linear_algebra/vector2.hpp @@ -242,9 +242,18 @@ struct std::formatter> // NOLINT(*-dcl58-cpp) { return ctx.begin(); } + + template [[nodiscard]] - static auto format(const omath::Vector2& vec, std::format_context& ctx) + static auto format(const omath::Vector2& vec, FormatContext& ctx) { - return std::format_to(ctx.out(), "[{}, {}]", vec.x, vec.y); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), "[{}, {}]", vec.x, vec.y); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), L"[{}, {}]", vec.x, vec.y); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), u8"[{}, {}]", vec.x, vec.y); } }; diff --git a/include/omath/linear_algebra/vector3.hpp b/include/omath/linear_algebra/vector3.hpp index e5f4fda..63e731e 100644 --- a/include/omath/linear_algebra/vector3.hpp +++ b/include/omath/linear_algebra/vector3.hpp @@ -303,9 +303,18 @@ struct std::formatter> // NOLINT(*-dcl58-cpp) { return ctx.begin(); } + + template [[nodiscard]] - static auto format(const omath::Vector3& vec, std::format_context& ctx) + static auto format(const omath::Vector3& vec, FormatContext& ctx) { - return std::format_to(ctx.out(), "[{}, {}, {}]", vec.x, vec.y, vec.z); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), "[{}, {}, {}]", vec.x, vec.y, vec.z); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), L"[{}, {}, {}]", vec.x, vec.y, vec.z); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), u8"[{}, {}, {}]", vec.x, vec.y, vec.z); } }; diff --git a/include/omath/linear_algebra/vector4.hpp b/include/omath/linear_algebra/vector4.hpp index 95c095f..d1e1377 100644 --- a/include/omath/linear_algebra/vector4.hpp +++ b/include/omath/linear_algebra/vector4.hpp @@ -210,9 +210,17 @@ struct std::formatter> // NOLINT(*-dcl58-cpp) { return ctx.begin(); } + template [[nodiscard]] - static auto format(const omath::Vector4& vec, std::format_context& ctx) + static auto format(const omath::Vector4& vec, FormatContext& ctx) { - return std::format_to(ctx.out(), "[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), "[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), L"[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w); + + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), u8"[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w); } }; \ No newline at end of file From c49c93d5428cd2ce8c6592c93e5e4127399d26f1 Mon Sep 17 00:00:00 2001 From: Orange Date: Thu, 25 Sep 2025 21:55:56 +0300 Subject: [PATCH 2/4] decomposed formatter --- include/omath/color.hpp | 47 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/include/omath/color.hpp b/include/omath/color.hpp index 7de2efe..7d72036 100644 --- a/include/omath/color.hpp +++ b/include/omath/color.hpp @@ -164,6 +164,26 @@ namespace omath return {to_im_vec4()}; } #endif + [[nodiscard]] std::string to_string() const noexcept + { + return std::format("[r:{}, g:{}, b:{}, a:{}]", + static_cast(x * 255.f), + static_cast(y * 255.f), + static_cast(z * 255.f), + static_cast(w * 255.f)); + } + [[nodiscard]] std::wstring to_wstring() const noexcept + { + const auto ascii_string = to_string(); + return {ascii_string.cbegin(), ascii_string.cend()}; + } + + // ReSharper disable once CppInconsistentNaming + [[nodiscard]] std::u8string to_u8string() const noexcept + { + const auto ascii_string = to_string(); + return {ascii_string.cbegin(), ascii_string.cend()}; + } }; } // namespace omath template<> @@ -179,25 +199,14 @@ struct std::formatter // NOLINT(*-dcl58-cpp) [[nodiscard]] static auto format(const omath::Color& col, FormatContext& ctx) { - if constexpr (std::is_same_v) - return std::format_to(ctx.out(), "[r:{}, g:{}, b:{}, a:{}]", - static_cast(col.x * 255.f), - static_cast(col.y * 255.f), - static_cast(col.z * 255.f), - static_cast(col.w * 255.f)); - if constexpr (std::is_same_v) - return std::format_to(ctx.out(), L"[r:{}, g:{}, b:{}, a:{}]", - static_cast(col.x * 255.f), - static_cast(col.y * 255.f), - static_cast(col.z * 255.f), - static_cast(col.w * 255.f)); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), "{}", col.to_string()); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), L"{}", col.to_wstring()); - if constexpr (std::is_same_v) - return std::format_to(ctx.out(), u8"[r:{}, g:{}, b:{}, a:{}]", - static_cast(col.x * 255.f), - static_cast(col.y * 255.f), - static_cast(col.z * 255.f), - static_cast(col.w * 255.f)); - std::unreachable(); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), u8"{}", col.to_u8string()); + + return std::unreachable(); } }; \ No newline at end of file From 83ba339eb52c6ebdb2991f7139e764217ebd0ae2 Mon Sep 17 00:00:00 2001 From: Orange Date: Thu, 25 Sep 2025 21:57:05 +0300 Subject: [PATCH 3/4] fix --- include/omath/angle.hpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/omath/angle.hpp b/include/omath/angle.hpp index 1f9d820..993ddae 100644 --- a/include/omath/angle.hpp +++ b/include/omath/angle.hpp @@ -162,6 +162,7 @@ struct std::formatter, char> // NOLINT(*-dcl58-cp } template + [[nodiscard]] auto format(const AngleT& a, FormatContext& ctx) const { static_assert(std::is_same_v); @@ -181,9 +182,30 @@ struct std::formatter, wchar_t> // NOLINT(*-dcl58 } template + [[nodiscard]] auto format(const AngleT& a, FormatContext& ctx) const { static_assert(std::is_same_v); return std::format_to(ctx.out(), L"{}deg", a.as_degrees()); } }; + +// wchar_t formatter +template +struct std::formatter, char8_t> // NOLINT(*-dcl58-cpp) +{ + using AngleT = omath::Angle; + + static constexpr auto parse(std::wformat_parse_context& ctx) + { + return ctx.begin(); + } + + template + [[nodiscard]] + auto format(const AngleT& a, FormatContext& ctx) const + { + static_assert(std::is_same_v); + return std::format_to(ctx.out(), u8"{}deg", a.as_degrees()); + } +}; From 64095683341fc05bf48fdda2f0176c6008c1154e Mon Sep 17 00:00:00 2001 From: Orange Date: Thu, 25 Sep 2025 21:59:59 +0300 Subject: [PATCH 4/4] added plane header --- include/omath/omath.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/omath/omath.hpp b/include/omath/omath.hpp index f96bfe7..474f927 100644 --- a/include/omath/omath.hpp +++ b/include/omath/omath.hpp @@ -26,6 +26,7 @@ // 3D primitives #include "omath/3d_primitives/box.hpp" +#include "omath/3d_primitives/plane.hpp" // Collision detection #include "omath/collision/line_tracer.hpp"