From 583cbaccfff28125f3e166130db5fb17b1ae4fea Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Thu, 31 Oct 2024 19:02:29 +0200 Subject: [PATCH] YamlCppConverters update + tests --- .../Extensions/YamlCppConverters.hpp | 175 ++++++++++++++++++ tests/Extensions/YamlCppConverters.cpp | 167 +++++++++++++++++ 2 files changed, 342 insertions(+) create mode 100644 tests/Extensions/YamlCppConverters.cpp diff --git a/openVulkanoCpp/Extensions/YamlCppConverters.hpp b/openVulkanoCpp/Extensions/YamlCppConverters.hpp index ce9e52b..0286a12 100644 --- a/openVulkanoCpp/Extensions/YamlCppConverters.hpp +++ b/openVulkanoCpp/Extensions/YamlCppConverters.hpp @@ -71,4 +71,179 @@ namespace YAML return false; } }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Vector2& v) + { + return Node(fmt::format("({},{})", v.x, v.y)); + } + + static bool decode(const Node& node, OpenVulkano::Math::Vector2& v) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{})", v.x, v.y); + return ret != c4::csubstr::npos; + } + return false; + } + }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Vector3& v) + { + return Node(fmt::format("({},{},{})", v.x, v.y, v.z)); + } + + static bool decode(const Node& node, OpenVulkano::Math::Vector3& v) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{})", v.x, v.y, v.z); + return ret != c4::csubstr::npos; + } + return false; + } + }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Vector4& v) + { + return Node(fmt::format("({},{},{},{})", v.x, v.y, v.z, v.w)); + } + + static bool decode(const Node& node, OpenVulkano::Math::Vector4& v) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{},{})", v.x, v.y, v.z, v.w); + return ret != c4::csubstr::npos; + } + return false; + } + }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Vector2_SIMD& v) + { + return Node(fmt::format("({},{})", v.x, v.y)); + } + + static bool decode(const Node& node, OpenVulkano::Math::Vector2_SIMD& v) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{})", v.x, v.y); + return ret != c4::csubstr::npos; + } + return false; + } + }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Vector3_SIMD& v) + { + return Node(fmt::format("({},{},{})", v.x, v.y, v.z)); + } + + static bool decode(const Node& node, OpenVulkano::Math::Vector3_SIMD& v) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{})", v.x, v.y, v.z); + return ret != c4::csubstr::npos; + } + return false; + } + }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Matrix3& mat) + { + return Node(fmt::format("(({},{},{}),({},{},{}),({},{},{}))", + mat[0][0], mat[0][1], mat[0][2], + mat[1][0], mat[1][1], mat[1][2], + mat[2][0], mat[2][1], mat[2][2])); + } + + static bool decode(const Node& node, OpenVulkano::Math::Matrix3& mat) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), + "(({},{},{}),({},{},{}),({},{},{}))", + mat[0][0], mat[0][1], mat[0][2], + mat[1][0], mat[1][1], mat[1][2], + mat[2][0], mat[2][1], mat[2][2]); + return ret != c4::csubstr::npos; + } + return false; + } + }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Matrix3_SIMD& mat) + { + return Node(fmt::format("(({},{},{}),({},{},{}),({},{},{}))", + mat[0][0], mat[0][1], mat[0][2], + mat[1][0], mat[1][1], mat[1][2], + mat[2][0], mat[2][1], mat[2][2])); + } + + static bool decode(const Node& node, OpenVulkano::Math::Matrix3_SIMD& mat) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), + "(({},{},{}),({},{},{}),({},{},{}))", + mat[0][0], mat[0][1], mat[0][2], + mat[1][0], mat[1][1], mat[1][2], + mat[2][0], mat[2][1], mat[2][2]); + return ret != c4::csubstr::npos; + } + return false; + } + }; + + template + struct convert> + { + static Node encode(const OpenVulkano::Math::Matrix4& mat) + { + return Node(fmt::format("(({},{},{},{}),({},{},{},{}),({},{},{},{}),({},{},{},{}))", + mat[0][0], mat[0][1], mat[0][2], mat[0][3], + mat[1][0], mat[1][1], mat[1][2], mat[1][3], + mat[2][0], mat[2][1], mat[2][2], mat[2][3], + mat[3][0], mat[3][1], mat[3][2], mat[3][3])); + } + + static bool decode(const Node& node, OpenVulkano::Math::Matrix4& mat) + { + if (node.IsScalar()) + { + size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), + "(({},{},{},{}),({},{},{},{}),({},{},{},{}),({},{},{},{}))", + mat[0][0], mat[0][1], mat[0][2], mat[0][3], + mat[1][0], mat[1][1], mat[1][2], mat[1][3], + mat[2][0], mat[2][1], mat[2][2], mat[2][3], + mat[3][0], mat[3][1], mat[3][2], mat[3][3]); + return ret != c4::csubstr::npos; + } + return false; + } + }; } diff --git a/tests/Extensions/YamlCppConverters.cpp b/tests/Extensions/YamlCppConverters.cpp new file mode 100644 index 0000000..5d0b257 --- /dev/null +++ b/tests/Extensions/YamlCppConverters.cpp @@ -0,0 +1,167 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "Extensions/YamlCppConverters.hpp" +#include "Math/Math.hpp" + +using namespace OpenVulkano; +using namespace OpenVulkano::Math; + +TEST_CASE("UUID encoding and decoding", "[YamlCppConverters]") +{ + UUID uuid("123e4567-e89b-12d3-a456-426614174000"); + YAML::Node node = YAML::convert::encode(uuid); + REQUIRE(node.IsScalar()); + REQUIRE(node.as() == uuid.string()); + + UUID decoded_uuid; + REQUIRE(YAML::convert::decode(node, decoded_uuid)); + REQUIRE(decoded_uuid == uuid); +} + +TEST_CASE("AABB encoding and decoding", "[YamlCppConverters]") +{ + Math::AABB bbox { { 1.0f, 2.0f, 3.0f }, { 4.0f, 5.0f, 6.0f } }; + YAML::Node node = YAML::convert::encode(bbox); + REQUIRE(node.IsScalar()); + + Math::AABB decoded_bbox; + REQUIRE(YAML::convert::decode(node, decoded_bbox)); + REQUIRE(decoded_bbox.min == bbox.min); + REQUIRE(decoded_bbox.max == bbox.max); +} + +TEST_CASE("PoseF encoding and decoding", "[YamlCppConverters]") +{ + Math::PoseF pose; + pose.SetPosition({1.0f, 2.0f, 3.0f}); + pose.SetOrientation({1.0f, 2.0f, 3.0f, 1.0f}); + YAML::Node node = YAML::convert::encode(pose); + REQUIRE(node.IsScalar()); + + Math::PoseF decoded_pose; + REQUIRE(YAML::convert::decode(node, decoded_pose)); + REQUIRE(decoded_pose.GetPosition() == pose.GetPosition()); + REQUIRE(decoded_pose.GetOrientation() == pose.GetOrientation()); +} + +TEST_CASE("Vector2 encoding and decoding", "[YamlCppConverters]") +{ + Math::Vector2 vec { 1.0f, 2.0f }; + YAML::Node node = YAML::convert>::encode(vec); + REQUIRE(node.IsScalar()); + + Math::Vector2 decoded_vec; + REQUIRE(YAML::convert>::decode(node, decoded_vec)); + REQUIRE(decoded_vec == vec); +} + +TEST_CASE("Vector3 encoding and decoding", "[YamlCppConverters]") +{ + Math::Vector3 vec { 1.0f, 2.0f, 3.0f }; + YAML::Node node = YAML::convert>::encode(vec); + REQUIRE(node.IsScalar()); + + Math::Vector3 decoded_vec; + REQUIRE(YAML::convert>::decode(node, decoded_vec)); + REQUIRE(decoded_vec == vec); +} + +TEST_CASE("Vector4 encoding and decoding", "[YamlCppConverters]") +{ + Math::Vector4 vec { 1.0f, 2.0f, 3.0f, 4.0f }; + YAML::Node node = YAML::convert>::encode(vec); + REQUIRE(node.IsScalar()); + + Math::Vector4 decoded_vec; + REQUIRE(YAML::convert>::decode(node, decoded_vec)); + REQUIRE(decoded_vec == vec); +} + +TEST_CASE("Matrix3 encoding and decoding", "[YamlCppConverters]") +{ + Math::Matrix3 mat { { 1.0f, 2.0f, 3.0f }, { 4.0f, 5.0f, 6.0f }, { 7.0f, 8.0f, 9.0f } }; + YAML::Node node = YAML::convert>::encode(mat); + REQUIRE(node.IsScalar()); + + Math::Matrix3 decoded_mat; + REQUIRE(YAML::convert>::decode(node, decoded_mat)); + REQUIRE(decoded_mat == mat); +} + +TEST_CASE("Matrix4 encoding and decoding", "[YamlCppConverters]") +{ + Math::Matrix4 mat { { 1.0f, 2.0f, 3.0f, 4.0f }, + { 5.0f, 6.0f, 7.0f, 8.0f }, + { 9.0f, 10.0f, 11.0f, 12.0f }, + { 13.0f, 14.0f, 15.0f, 16.0f } }; + YAML::Node node = YAML::convert>::encode(mat); + REQUIRE(node.IsScalar()); + + Math::Matrix4 decoded_mat; + REQUIRE(YAML::convert>::decode(node, decoded_mat)); + REQUIRE(decoded_mat == mat); +} + +TEST_CASE("Vector2_SIMD encoding and decoding", "[YamlCppConverters]") +{ + Math::Vector2_SIMD vec { 1.0f, 2.0f }; + YAML::Node node = YAML::convert>::encode(vec); + REQUIRE(node.IsScalar()); + + Math::Vector2_SIMD decoded_vec; + REQUIRE(YAML::convert>::decode(node, decoded_vec)); + REQUIRE(decoded_vec == vec); +} + +TEST_CASE("Vector3_SIMD encoding and decoding", "[YamlCppConverters]") +{ + Math::Vector3_SIMD vec { 1.0f, 2.0f, 3.0f }; + YAML::Node node = YAML::convert>::encode(vec); + REQUIRE(node.IsScalar()); + + Math::Vector3_SIMD decoded_vec; + REQUIRE(YAML::convert>::decode(node, decoded_vec)); + REQUIRE(decoded_vec == vec); +} + +TEST_CASE("Vector4_SIMD encoding and decoding", "[YamlCppConverters]") +{ + Math::Vector4_SIMD vec { 1.0f, 2.0f, 3.0f, 4.0f }; + YAML::Node node = YAML::convert>::encode(vec); + REQUIRE(node.IsScalar()); + + Math::Vector4_SIMD decoded_vec; + REQUIRE(YAML::convert>::decode(node, decoded_vec)); + REQUIRE(decoded_vec == vec); +} + +TEST_CASE("Matrix3_SIMD encoding and decoding", "[YamlCppConverters]") +{ + Math::Matrix3_SIMD mat { { 1.0f, 2.0f, 3.0f }, { 4.0f, 5.0f, 6.0f }, { 7.0f, 8.0f, 9.0f } }; + YAML::Node node = YAML::convert>::encode(mat); + REQUIRE(node.IsScalar()); + + Math::Matrix3_SIMD decoded_mat; + REQUIRE(YAML::convert>::decode(node, decoded_mat)); + REQUIRE(decoded_mat == mat); +} + +TEST_CASE("Matrix4_SIMD encoding and decoding", "[YamlCppConverters]") +{ + Math::Matrix4_SIMD mat { { 1.0f, 2.0f, 3.0f, 4.0f }, + { 5.0f, 6.0f, 7.0f, 8.0f }, + { 9.0f, 10.0f, 11.0f, 12.0f }, + { 13.0f, 14.0f, 15.0f, 16.0f } }; + YAML::Node node = YAML::convert>::encode(mat); + REQUIRE(node.IsScalar()); + + Math::Matrix4_SIMD decoded_mat; + REQUIRE(YAML::convert>::decode(node, decoded_mat)); + REQUIRE(decoded_mat == mat); +}