mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
Merge pull request #57 from orange-cpp/feature/added_formatters
Adds std::format support for math types
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include "omath/angles.hpp"
|
#include "omath/angles.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
@@ -149,3 +150,17 @@ namespace omath
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace omath
|
} // namespace omath
|
||||||
|
template<class Type, Type min, Type max, omath::AngleFlags flags>
|
||||||
|
struct std::formatter<omath::Angle<Type, min, max, flags>> // NOLINT(*-dcl58-cpp)
|
||||||
|
{
|
||||||
|
[[nodiscard]]
|
||||||
|
static constexpr auto parse(std::format_parse_context& ctx)
|
||||||
|
{
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
static auto format(const omath::Angle<Type, min, max, flags>& deg, std::format_context& ctx)
|
||||||
|
{
|
||||||
|
return std::format_to(ctx.out(), "{}deg", deg.as_degrees());
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -167,3 +167,21 @@ namespace omath
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
} // namespace omath
|
} // namespace omath
|
||||||
|
template<>
|
||||||
|
struct std::formatter<omath::Color> // NOLINT(*-dcl58-cpp)
|
||||||
|
{
|
||||||
|
[[nodiscard]]
|
||||||
|
static constexpr auto parse(std::format_parse_context& ctx)
|
||||||
|
{
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
static auto format(const omath::Color& col, std::format_context& ctx)
|
||||||
|
{
|
||||||
|
return std::format_to(ctx.out(), "[r:{}, g:{}, b:{}, a:{}]",
|
||||||
|
static_cast<int>(col.x * 255.f),
|
||||||
|
static_cast<int>(col.y * 255.f),
|
||||||
|
static_cast<int>(col.z * 255.f),
|
||||||
|
static_cast<int>(col.w * 255.f));
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -480,3 +480,19 @@ namespace omath
|
|||||||
{0.f, 0.f, -1.f, 0.f}};
|
{0.f, 0.f, -1.f, 0.f}};
|
||||||
}
|
}
|
||||||
} // namespace omath
|
} // 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
|
#pragma once
|
||||||
|
#include "vector3.hpp"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <format>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
#ifdef OMATH_IMGUI_INTEGRATION
|
#ifdef OMATH_IMGUI_INTEGRATION
|
||||||
@@ -218,7 +220,6 @@ namespace omath
|
|||||||
{
|
{
|
||||||
return std::make_tuple(x, y);
|
return std::make_tuple(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef OMATH_IMGUI_INTEGRATION
|
#ifdef OMATH_IMGUI_INTEGRATION
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
ImVec2 to_im_vec2() const noexcept
|
ImVec2 to_im_vec2() const noexcept
|
||||||
@@ -233,3 +234,18 @@ namespace omath
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
} // namespace omath
|
} // 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();
|
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
|
#endif
|
||||||
|
|
||||||
@@ -279,21 +279,33 @@ namespace omath
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // 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.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||||
hash ^= hasher(vec.y) + 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.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace std
|
|
||||||
|
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
|
#endif
|
||||||
};
|
};
|
||||||
} // namespace omath
|
} // 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -125,7 +125,7 @@ TEST_F(UnitTestMat, Clear)
|
|||||||
|
|
||||||
TEST_F(UnitTestMat, ToString)
|
TEST_F(UnitTestMat, ToString)
|
||||||
{
|
{
|
||||||
const std::string str = m2.to_string();
|
const std::string str = std::format("{}", m2);
|
||||||
EXPECT_FALSE(str.empty());
|
EXPECT_FALSE(str.empty());
|
||||||
EXPECT_EQ(str, "[[ 1.000, 2.000]\n [ 3.000, 4.000]]");
|
EXPECT_EQ(str, "[[ 1.000, 2.000]\n [ 3.000, 4.000]]");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user