RymlConverters for various pieces of math + tests

This commit is contained in:
Vladyslav Baranovskyi
2024-11-04 17:03:46 +02:00
parent 583cbaccff
commit b81a3088c5
3 changed files with 410 additions and 3 deletions

View File

@@ -10,6 +10,13 @@
#include "Math/Math.hpp"
#include "Math/AABB.hpp"
#include "Math/Pose.hpp"
#include "Math/DenseVector3i.hpp"
#include "Math/Int24.hpp"
#include "Math/Range.hpp"
#include "Math/RGB10A2.hpp"
#include "Math/RGB565.hpp"
#include "Math/RGBA5551.hpp"
#include "Math/Timestamp.hpp"
#include <ryml.hpp>
#include <ryml_std.hpp>
#include <c4/format.hpp>
@@ -142,4 +149,134 @@ namespace c4
uuid->assign(buf.str);
return true;
}
template<typename T, bool REDUCE_Y_RESOLUTION, bool ASSERT_INPUT_VALUES, int BITS_PER_COMPONENT>
inline size_t to_chars(
ryml::substr buf,
const OpenVulkano::Math::DenseVector3i<T, REDUCE_Y_RESOLUTION, ASSERT_INPUT_VALUES, BITS_PER_COMPONENT>& vec)
{
return ryml::format(buf, "({},{},{})", vec.X(), vec.Y(), vec.Z());
}
template<typename T, bool REDUCE_Y_RESOLUTION, bool ASSERT_INPUT_VALUES, int BITS_PER_COMPONENT>
inline bool
from_chars(ryml::csubstr buf,
OpenVulkano::Math::DenseVector3i<T, REDUCE_Y_RESOLUTION, ASSERT_INPUT_VALUES, BITS_PER_COMPONENT>* vec)
{
int x, y, z;
size_t ret = ryml::unformat(buf, "({},{},{})", x, y, z);
if (ret != ryml::yml::npos)
{
*vec = DenseVector3i<T, REDUCE_Y_RESOLUTION, ASSERT_INPUT_VALUES, BITS_PER_COMPONENT>(x, y, z);
return true;
}
return false;
}
size_t to_chars(c4::substr buf, const OpenVulkano::int24& value)
{
int intValue = static_cast<int>(value);
return ryml::format(buf, "{}", intValue);
}
bool from_chars(c4::csubstr buf, OpenVulkano::int24* value)
{
int intValue;
size_t ret = ryml::unformat(buf, "{}", intValue);
if (ret != ryml::yml::npos)
{
*value = OpenVulkano::int24(intValue);
return true;
}
return false;
}
template<typename T> size_t to_chars(c4::substr buf, const OpenVulkano::Math::Range<T>& range)
{
return ryml::format(buf, "[{},{}]", range.GetMin(), range.GetMax());
}
template<typename T> bool from_chars(c4::csubstr buf, OpenVulkano::Math::Range<T>* range)
{
T minVal, maxVal;
size_t ret = ryml::unformat(buf, "[{},{}]", minVal, maxVal);
if (ret != ryml::yml::npos)
{
*range = Range<T>(minVal, maxVal);
return true;
}
return false;
}
template<typename T> size_t to_chars(c4::substr buf, const OpenVulkano::Math::RGB10A2<T>& color)
{
return ryml::format(buf, "[{},{},{},{}]", color.r, color.g, color.b, color.a);
}
template<typename T> bool from_chars(c4::csubstr buf, OpenVulkano::Math::RGB10A2<T>* color)
{
T r, g, b, a;
size_t ret = ryml::unformat(buf, "[{},{},{},{}]", r, g, b, a);
if (ret != ryml::yml::npos)
{
color->r = r;
color->g = g;
color->b = b;
color->a = a;
return true;
}
return false;
}
size_t to_chars(ryml::substr buf, const OpenVulkano::Math::RGB565& rgb)
{
return ryml::format(buf, "[{},{},{}]", rgb.r, rgb.g, rgb.b);
}
bool from_chars(ryml::csubstr buf, OpenVulkano::Math::RGB565* rgb)
{
int r, g, b;
size_t ret = ryml::unformat (buf, "[{},{},{}]", r, g, b);
rgb->r = r;
rgb->g = g;
rgb->b = b;
return ret != ryml::yml::npos;
}
size_t to_chars(c4::substr buf, const OpenVulkano::Math::RGBA5551& color)
{
return ryml::format(buf, "[{},{},{},{}]", color.r, color.g, color.b, color.a);
}
bool from_chars(c4::csubstr buf, OpenVulkano::Math::RGBA5551* color)
{
int r, g, b, a;
size_t ret = ryml::unformat(buf, "[{},{},{},{}]", r, g, b, a);
if (ret != ryml::yml::npos)
{
color->r = r;
color->g = g;
color->b = b;
color->a = a;
return true;
}
return false;
}
size_t to_chars(c4::substr buf, const OpenVulkano::Math::Timestamp& ts)
{
return ryml::format(buf, "{}", ts.GetNanos());
}
bool from_chars(c4::csubstr buf, OpenVulkano::Math::Timestamp* ts)
{
uint64_t nanos;
size_t ret = ryml::unformat(buf, "{}", nanos);
if (ret != ryml::yml::npos)
{
*ts = OpenVulkano::Math::Timestamp(nanos);
return true;
}
return false;
}
}