mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
Adds std::format support for math types
Adds `std::formatter` specializations for `Mat`, `Vector2`, `Vector3`, and `Vector4` types, enabling the use of `std::format` for these types. This simplifies formatting and printing of these mathematical objects, improving code readability and consistency. Also adds a hash function specialization for Vector3.
This commit is contained in:
@@ -480,3 +480,19 @@ namespace omath
|
||||
{0.f, 0.f, -1.f, 0.f}};
|
||||
}
|
||||
} // namespace omath
|
||||
|
||||
template<size_t Rows, size_t Columns, class Type, omath::MatStoreType StoreType>
|
||||
struct std::formatter<omath::Mat<Rows, Columns, Type, StoreType>> // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
using MatType = omath::Mat<Rows, Columns, Type, StoreType>;
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
[[nodiscard]]
|
||||
static auto format(const MatType& mat, std::format_context& ctx)
|
||||
{
|
||||
return std::format_to(ctx.out(), "{}", mat.to_string());
|
||||
}
|
||||
};
|
||||
@@ -3,7 +3,9 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "vector3.hpp"
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
#include <tuple>
|
||||
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
@@ -218,7 +220,6 @@ namespace omath
|
||||
{
|
||||
return std::make_tuple(x, y);
|
||||
}
|
||||
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
[[nodiscard]]
|
||||
ImVec2 to_im_vec2() const noexcept
|
||||
@@ -233,3 +234,18 @@ namespace omath
|
||||
#endif
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Vector2<Type>> // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
[[nodiscard]]
|
||||
static auto format(const omath::Vector2<Type>& vec, std::format_context& ctx)
|
||||
{
|
||||
return std::format_to(ctx.out(), "[{}, {}]", vec.x, vec.y);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -159,9 +159,9 @@ namespace omath
|
||||
return Vector2<Type>::length();
|
||||
}
|
||||
|
||||
[[nodiscard]] Type distance_to(const Vector3& vOther) const noexcept
|
||||
[[nodiscard]] Type distance_to(const Vector3& v_other) const noexcept
|
||||
{
|
||||
return (*this - vOther).length();
|
||||
return (*this - v_other).length();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -279,21 +279,33 @@ namespace omath
|
||||
}
|
||||
};
|
||||
} // namespace omath
|
||||
// ReSharper disable once CppRedundantNamespaceDefinition
|
||||
namespace std
|
||||
|
||||
template<> struct std::hash<omath::Vector3<float>>
|
||||
{
|
||||
template<> struct hash<omath::Vector3<float>>
|
||||
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
|
||||
{
|
||||
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Vector3<Type>> // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
[[nodiscard]]
|
||||
static auto format(const omath::Vector3<Type>& vec, std::format_context& ctx)
|
||||
{
|
||||
return std::format_to(ctx.out(), "[{}, {}, {}]", vec.x, vec.y, vec.z);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -201,3 +201,18 @@ namespace omath
|
||||
#endif
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Vector4<Type>> // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
[[nodiscard]]
|
||||
static auto format(const omath::Vector4<Type>& vec, std::format_context& ctx)
|
||||
{
|
||||
return std::format_to(ctx.out(), "[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user