YamlCppConverters for various pieces of math + tests

This commit is contained in:
Vladyslav Baranovskyi
2024-11-04 17:04:19 +02:00
parent b81a3088c5
commit 596de3cd2e
2 changed files with 287 additions and 42 deletions

View File

@@ -9,6 +9,13 @@
#include "Base/UUID.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 <yaml-cpp/yaml.h>
#include <fmt/format.h>
#include <c4/format.hpp>
@@ -246,4 +253,159 @@ namespace YAML
return false;
}
};
template<typename T, bool REDUCE_Y_RESOLUTION, bool ASSERT_INPUT_VALUES, int BITS_PER_COMPONENT>
struct convert<OpenVulkano::Math::DenseVector3i<T, REDUCE_Y_RESOLUTION, ASSERT_INPUT_VALUES, BITS_PER_COMPONENT>>
{
static Node encode(const OpenVulkano::Math::DenseVector3i<T, REDUCE_Y_RESOLUTION, ASSERT_INPUT_VALUES, BITS_PER_COMPONENT>& v)
{
return Node(fmt::format("({},{},{})", v.X(), v.Y(), v.Z()));
}
static bool decode(const Node& node, OpenVulkano::Math::DenseVector3i<T, REDUCE_Y_RESOLUTION, ASSERT_INPUT_VALUES, BITS_PER_COMPONENT>& v)
{
if (node.IsScalar())
{
int x, y, z;
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{})", x, y, z);
v = OpenVulkano::Math::DenseVector3i<T, REDUCE_Y_RESOLUTION, ASSERT_INPUT_VALUES, BITS_PER_COMPONENT>(x, y, z);
return ret != c4::csubstr::npos;
}
return false;
}
};
template<>
struct convert<OpenVulkano::int24>
{
static Node encode(const OpenVulkano::int24& v)
{
return Node(fmt::format("{}", static_cast<int>(v)));
}
static bool decode(const Node& node, OpenVulkano::int24& v)
{
if (node.IsScalar())
{
int value;
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "{}", value);
v = value;
return ret != c4::csubstr::npos;
}
return false;
}
};
template<typename T>
struct convert<OpenVulkano::Math::Range<T>>
{
static Node encode(const OpenVulkano::Math::Range<T>& r)
{
return Node(fmt::format("[{},{}]", r.GetMin(), r.GetMax()));
}
static bool decode(const Node& node, OpenVulkano::Math::Range<T>& r)
{
if (node.IsScalar())
{
T minVal, maxVal;
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "[{},{}]", minVal, maxVal);
r = OpenVulkano::Math::Range<T>(minVal, maxVal);
return ret != c4::csubstr::npos;
}
return false;
}
};
template<typename T>
struct convert<OpenVulkano::Math::RGB10A2<T>>
{
static Node encode(const OpenVulkano::Math::RGB10A2<T>& c)
{
return Node(fmt::format("[{},{},{},{}]", c.r, c.g, c.b, c.a));
}
static bool decode(const Node& node, OpenVulkano::Math::RGB10A2<T>& c)
{
if (node.IsScalar())
{
T r, g, b, a;
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "[{},{},{},{}]", r, g, b, a);
c.r = r;
c.g = g;
c.b = b;
c.a = a;
return ret != c4::csubstr::npos;
}
return false;
}
};
template<>
struct convert<OpenVulkano::Math::RGB565>
{
static Node encode(const OpenVulkano::Math::RGB565& c)
{
return Node(fmt::format("[{},{},{}]", c.r, c.g, c.b));
}
static bool decode(const Node& node, OpenVulkano::Math::RGB565& c)
{
if (node.IsScalar())
{
int r, g, b;
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "[{},{},{}]", r, g, b);
c.r = r;
c.g = g;
c.b = b;
return ret != c4::csubstr::npos;
}
return false;
}
};
template<>
struct convert<OpenVulkano::Math::RGBA5551>
{
static Node encode(const OpenVulkano::Math::RGBA5551& c)
{
return Node(fmt::format("[{},{},{},{}]", c.r, c.g, c.b, c.a));
}
static bool decode(const Node& node, OpenVulkano::Math::RGBA5551& c)
{
if (node.IsScalar())
{
int r, g, b, a;
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "[{},{},{},{}]", r, g, b, a);
c.r = r;
c.g = g;
c.b = b;
c.a = a;
return ret != c4::csubstr::npos;
}
return false;
}
};
template<>
struct convert<OpenVulkano::Math::Timestamp>
{
static Node encode(const OpenVulkano::Math::Timestamp& ts)
{
return Node(fmt::format("{}", ts.GetNanos()));
}
static bool decode(const Node& node, OpenVulkano::Math::Timestamp& ts)
{
if (node.IsScalar())
{
uint64_t nanos;
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "{}", nanos);
ts = OpenVulkano::Math::Timestamp(nanos);
return ret != c4::csubstr::npos;
}
return false;
}
};
}

View File

@@ -19,9 +19,9 @@ TEST_CASE("UUID encoding and decoding", "[YamlCppConverters]")
REQUIRE(node.IsScalar());
REQUIRE(node.as<std::string>() == uuid.string());
UUID decoded_uuid;
REQUIRE(YAML::convert<UUID>::decode(node, decoded_uuid));
REQUIRE(decoded_uuid == uuid);
UUID decodedUuid;
REQUIRE(YAML::convert<UUID>::decode(node, decodedUuid));
REQUIRE(decodedUuid == uuid);
}
TEST_CASE("AABB encoding and decoding", "[YamlCppConverters]")
@@ -30,10 +30,10 @@ TEST_CASE("AABB encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::AABB>::encode(bbox);
REQUIRE(node.IsScalar());
Math::AABB decoded_bbox;
REQUIRE(YAML::convert<Math::AABB>::decode(node, decoded_bbox));
REQUIRE(decoded_bbox.min == bbox.min);
REQUIRE(decoded_bbox.max == bbox.max);
Math::AABB decodedBbox;
REQUIRE(YAML::convert<Math::AABB>::decode(node, decodedBbox));
REQUIRE(decodedBbox.min == bbox.min);
REQUIRE(decodedBbox.max == bbox.max);
}
TEST_CASE("PoseF encoding and decoding", "[YamlCppConverters]")
@@ -44,10 +44,10 @@ TEST_CASE("PoseF encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::PoseF>::encode(pose);
REQUIRE(node.IsScalar());
Math::PoseF decoded_pose;
REQUIRE(YAML::convert<Math::PoseF>::decode(node, decoded_pose));
REQUIRE(decoded_pose.GetPosition() == pose.GetPosition());
REQUIRE(decoded_pose.GetOrientation() == pose.GetOrientation());
Math::PoseF decodedPose;
REQUIRE(YAML::convert<Math::PoseF>::decode(node, decodedPose));
REQUIRE(decodedPose.GetPosition() == pose.GetPosition());
REQUIRE(decodedPose.GetOrientation() == pose.GetOrientation());
}
TEST_CASE("Vector2 encoding and decoding", "[YamlCppConverters]")
@@ -56,9 +56,9 @@ TEST_CASE("Vector2 encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Vector2<float>>::encode(vec);
REQUIRE(node.IsScalar());
Math::Vector2<float> decoded_vec;
REQUIRE(YAML::convert<Math::Vector2<float>>::decode(node, decoded_vec));
REQUIRE(decoded_vec == vec);
Math::Vector2<float> decodedVec;
REQUIRE(YAML::convert<Math::Vector2<float>>::decode(node, decodedVec));
REQUIRE(decodedVec == vec);
}
TEST_CASE("Vector3 encoding and decoding", "[YamlCppConverters]")
@@ -67,9 +67,9 @@ TEST_CASE("Vector3 encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Vector3<float>>::encode(vec);
REQUIRE(node.IsScalar());
Math::Vector3<float> decoded_vec;
REQUIRE(YAML::convert<Math::Vector3<float>>::decode(node, decoded_vec));
REQUIRE(decoded_vec == vec);
Math::Vector3<float> decodedVec;
REQUIRE(YAML::convert<Math::Vector3<float>>::decode(node, decodedVec));
REQUIRE(decodedVec == vec);
}
TEST_CASE("Vector4 encoding and decoding", "[YamlCppConverters]")
@@ -78,9 +78,9 @@ TEST_CASE("Vector4 encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Vector4<float>>::encode(vec);
REQUIRE(node.IsScalar());
Math::Vector4<float> decoded_vec;
REQUIRE(YAML::convert<Math::Vector4<float>>::decode(node, decoded_vec));
REQUIRE(decoded_vec == vec);
Math::Vector4<float> decodedVec;
REQUIRE(YAML::convert<Math::Vector4<float>>::decode(node, decodedVec));
REQUIRE(decodedVec == vec);
}
TEST_CASE("Matrix3 encoding and decoding", "[YamlCppConverters]")
@@ -89,9 +89,9 @@ TEST_CASE("Matrix3 encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Matrix3<float>>::encode(mat);
REQUIRE(node.IsScalar());
Math::Matrix3<float> decoded_mat;
REQUIRE(YAML::convert<Math::Matrix3<float>>::decode(node, decoded_mat));
REQUIRE(decoded_mat == mat);
Math::Matrix3<float> decodedMat;
REQUIRE(YAML::convert<Math::Matrix3<float>>::decode(node, decodedMat));
REQUIRE(decodedMat == mat);
}
TEST_CASE("Matrix4 encoding and decoding", "[YamlCppConverters]")
@@ -103,9 +103,9 @@ TEST_CASE("Matrix4 encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Matrix4<float>>::encode(mat);
REQUIRE(node.IsScalar());
Math::Matrix4<float> decoded_mat;
REQUIRE(YAML::convert<Math::Matrix4<float>>::decode(node, decoded_mat));
REQUIRE(decoded_mat == mat);
Math::Matrix4<float> decodedMat;
REQUIRE(YAML::convert<Math::Matrix4<float>>::decode(node, decodedMat));
REQUIRE(decodedMat == mat);
}
TEST_CASE("Vector2_SIMD encoding and decoding", "[YamlCppConverters]")
@@ -114,9 +114,9 @@ TEST_CASE("Vector2_SIMD encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Vector2_SIMD<float>>::encode(vec);
REQUIRE(node.IsScalar());
Math::Vector2_SIMD<float> decoded_vec;
REQUIRE(YAML::convert<Math::Vector2_SIMD<float>>::decode(node, decoded_vec));
REQUIRE(decoded_vec == vec);
Math::Vector2_SIMD<float> decodedVec;
REQUIRE(YAML::convert<Math::Vector2_SIMD<float>>::decode(node, decodedVec));
REQUIRE(decodedVec == vec);
}
TEST_CASE("Vector3_SIMD encoding and decoding", "[YamlCppConverters]")
@@ -125,9 +125,9 @@ TEST_CASE("Vector3_SIMD encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Vector3_SIMD<float>>::encode(vec);
REQUIRE(node.IsScalar());
Math::Vector3_SIMD<float> decoded_vec;
REQUIRE(YAML::convert<Math::Vector3_SIMD<float>>::decode(node, decoded_vec));
REQUIRE(decoded_vec == vec);
Math::Vector3_SIMD<float> decodedVec;
REQUIRE(YAML::convert<Math::Vector3_SIMD<float>>::decode(node, decodedVec));
REQUIRE(decodedVec == vec);
}
TEST_CASE("Vector4_SIMD encoding and decoding", "[YamlCppConverters]")
@@ -136,9 +136,9 @@ TEST_CASE("Vector4_SIMD encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Vector4_SIMD<float>>::encode(vec);
REQUIRE(node.IsScalar());
Math::Vector4_SIMD<float> decoded_vec;
REQUIRE(YAML::convert<Math::Vector4_SIMD<float>>::decode(node, decoded_vec));
REQUIRE(decoded_vec == vec);
Math::Vector4_SIMD<float> decodedVec;
REQUIRE(YAML::convert<Math::Vector4_SIMD<float>>::decode(node, decodedVec));
REQUIRE(decodedVec == vec);
}
TEST_CASE("Matrix3_SIMD encoding and decoding", "[YamlCppConverters]")
@@ -147,9 +147,9 @@ TEST_CASE("Matrix3_SIMD encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Matrix3_SIMD<float>>::encode(mat);
REQUIRE(node.IsScalar());
Math::Matrix3_SIMD<float> decoded_mat;
REQUIRE(YAML::convert<Math::Matrix3_SIMD<float>>::decode(node, decoded_mat));
REQUIRE(decoded_mat == mat);
Math::Matrix3_SIMD<float> decodedMat;
REQUIRE(YAML::convert<Math::Matrix3_SIMD<float>>::decode(node, decodedMat));
REQUIRE(decodedMat == mat);
}
TEST_CASE("Matrix4_SIMD encoding and decoding", "[YamlCppConverters]")
@@ -161,7 +161,90 @@ TEST_CASE("Matrix4_SIMD encoding and decoding", "[YamlCppConverters]")
YAML::Node node = YAML::convert<Math::Matrix4_SIMD<float>>::encode(mat);
REQUIRE(node.IsScalar());
Math::Matrix4_SIMD<float> decoded_mat;
REQUIRE(YAML::convert<Math::Matrix4_SIMD<float>>::decode(node, decoded_mat));
REQUIRE(decoded_mat == mat);
Math::Matrix4_SIMD<float> decodedMat;
REQUIRE(YAML::convert<Math::Matrix4_SIMD<float>>::decode(node, decodedMat));
REQUIRE(decodedMat == mat);
}
TEST_CASE("DenseVector3i encoding and decoding", "[YamlCppConverters]")
{
OpenVulkano::Math::DenseVector3i<int, true, true, 8> vec { 1, 2, 3 };
YAML::Node node = YAML::convert<OpenVulkano::Math::DenseVector3i<int, true, true, 8>>::encode(vec);
REQUIRE(node.IsScalar());
OpenVulkano::Math::DenseVector3i<int, true, true, 8> decodedVec(0, 0, 0);
REQUIRE(YAML::convert<OpenVulkano::Math::DenseVector3i<int, true, true, 8>>::decode(node, decodedVec));
REQUIRE(decodedVec == vec);
}
TEST_CASE("int24 encoding and decoding", "[YamlCppConverters]")
{
OpenVulkano::int24 value(42);
YAML::Node node = YAML::convert<OpenVulkano::int24>::encode(value);
REQUIRE(node.IsScalar());
OpenVulkano::int24 decodedValue;
REQUIRE(YAML::convert<OpenVulkano::int24>::decode(node, decodedValue));
REQUIRE(decodedValue == value);
}
TEST_CASE("Range encoding and decoding", "[YamlCppConverters]")
{
OpenVulkano::Math::Range<int> range(10, 20);
YAML::Node node = YAML::convert<OpenVulkano::Math::Range<int>>::encode(range);
REQUIRE(node.IsScalar());
OpenVulkano::Math::Range<int> decodedRange;
REQUIRE(YAML::convert<OpenVulkano::Math::Range<int>>::decode(node, decodedRange));
REQUIRE(decodedRange.GetMin() == range.GetMin());
REQUIRE(decodedRange.GetMax() == range.GetMax());
}
TEST_CASE("RGB10A2 encoding and decoding", "[YamlCppConverters]")
{
OpenVulkano::Math::RGB10A2<int> color;
color.Set(OpenVulkano::Math::Vector4i(1023, 512, 256, 3));
YAML::Node node = YAML::convert<OpenVulkano::Math::RGB10A2<int>>::encode(color);
REQUIRE(node.IsScalar());
OpenVulkano::Math::RGB10A2<int> decodedColor;
REQUIRE(YAML::convert<OpenVulkano::Math::RGB10A2<int>>::decode(node, decodedColor));
REQUIRE(decodedColor == color);
}
TEST_CASE("RGB565 encoding and decoding", "[YamlCppConverters]")
{
OpenVulkano::Math::RGB565 color;
color.Set(OpenVulkano::Math::Vector3i(31, 63, 31));
YAML::Node node = YAML::convert<OpenVulkano::Math::RGB565>::encode(color);
REQUIRE(node.IsScalar());
OpenVulkano::Math::RGB565 decodedColor;
REQUIRE(YAML::convert<OpenVulkano::Math::RGB565>::decode(node, decodedColor));
REQUIRE(decodedColor == color);
}
TEST_CASE("RGBA5551 encoding and decoding", "[YamlCppConverters]")
{
OpenVulkano::Math::RGBA5551 color(OpenVulkano::Math::Vector4i(31, 31, 31, 1));
YAML::Node node = YAML::convert<OpenVulkano::Math::RGBA5551>::encode(color);
REQUIRE(node.IsScalar());
OpenVulkano::Math::RGBA5551 decodedColor;
REQUIRE(YAML::convert<OpenVulkano::Math::RGBA5551>::decode(node, decodedColor));
REQUIRE(decodedColor == color);
}
TEST_CASE("Timestamp encoding and decoding", "[YamlCppConverters]")
{
OpenVulkano::Math::Timestamp timestamp(uint64_t(1234567890));
YAML::Node node = YAML::convert<OpenVulkano::Math::Timestamp>::encode(timestamp);
REQUIRE(node.IsScalar());
OpenVulkano::Math::Timestamp decodedTimestamp;
REQUIRE(YAML::convert<OpenVulkano::Math::Timestamp>::decode(node, decodedTimestamp));
REQUIRE(decodedTimestamp == timestamp);
}