/* * 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/. */ #pragma once #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 "Data/Containers/String.hpp" #include "Base/Version.hpp" #include #include #include namespace YAML { template<> struct convert { static Node encode(const OpenVulkano::String& string) { return Node(static_cast(string)); } static bool decode(const Node& node, OpenVulkano::String& string) { if (node.IsScalar()) { string = node.as(); return true; } return false; } }; template<> struct convert { static Node encode(const OpenVulkano::UUID& uuid) { return Node(uuid.string()); } static bool decode(const Node& node, OpenVulkano::UUID& uuid) { if (node.IsScalar()) { uuid.assign(node.Scalar()); return true; } return false; } }; template<> struct convert { static Node encode(const OpenVulkano::Version& version) { return Node(static_cast(version)); } static bool decode(const Node& node, OpenVulkano::Version& version) { if (node.IsScalar()) { version = OpenVulkano::Version(node.Scalar()); return true; } return false; } }; template<> struct convert { static Node encode(const OpenVulkano::Math::AABB& bbox) { return Node(fmt::format("({},{},{}),({},{},{})", bbox.min.x, bbox.min.y, bbox.min.z, bbox.max.x, bbox.max.y, bbox.max.z)); } static bool decode(const Node& node, OpenVulkano::Math::AABB& bbox) { if (node.IsScalar()) { size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{}),({},{},{})", bbox.min.x, bbox.min.y, bbox.min.z, bbox.max.x, bbox.max.y, bbox.max.z); return ret != c4::csubstr::npos; } return false; } }; template<> struct convert { static Node encode(const OpenVulkano::Math::PoseF& pose) { return Node(fmt::format("({},{},{}),({},{},{},{})", pose.GetPosition().x, pose.GetPosition().y, pose.GetPosition().z, pose.GetOrientation().x, pose.GetOrientation().y, pose.GetOrientation().z, pose.GetOrientation().w)); } static bool decode(const Node& node, OpenVulkano::Math::PoseF& pose) { if (node.IsScalar()) { size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{}),({},{},{},{})", pose.GetPosition().x, pose.GetPosition().y, pose.GetPosition().z, pose.GetOrientation().x, pose.GetOrientation().y, pose.GetOrientation().z, pose.GetOrientation().w); return ret != c4::csubstr::npos; } return false; } }; template bool ReadSequence(const Node& node, const int count, T* array) { if (!node.IsSequence()) return false; int readCount = 0; YAML::const_iterator iter = node.begin(); for(int i = 0; i < count && iter != node.end(); i++, iter++) { array[i] = iter->as(); readCount++; } return readCount == count; } 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 ReadSequence(node, 2, &v.x); } }; 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 ReadSequence(node, 3, &v.x); } }; 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 ReadSequence(node, 4, &v.x); } }; 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 ReadSequence(node, 2, &v.x); } }; 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 ReadSequence(node, 3, &v.x); } }; template bool ReadSequenceMat(const Node& node, const int size, MAT& data) { if (!node.IsSequence()) return false; int readCount = 0; MAT tmp; YAML::const_iterator iter = node.begin(); for(int i = 0; i < size && iter != node.end(); i++, iter++) { if (!iter->IsSequence()) return false; if (ReadSequence(*iter, size, &tmp[i][0])) readCount++; } data = glm::transpose(tmp); return readCount == size; } 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 ReadSequenceMat(node, 3, mat); } }; 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 ReadSequenceMat(node, 3, mat); } }; 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 ReadSequenceMat(node, 4, mat); } }; template struct convert> { static Node encode(const OpenVulkano::Math::DenseVector3i& v) { return Node(fmt::format("({},{},{})", v.X(), v.Y(), v.Z())); } static bool decode(const Node& node, OpenVulkano::Math::DenseVector3i& 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(x, y, z); return ret != c4::csubstr::npos; } return false; } }; template<> struct convert { static Node encode(const OpenVulkano::int24& v) { return Node(fmt::format("{}", static_cast(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 struct convert> { static Node encode(const OpenVulkano::Math::Range& r) { return Node(fmt::format("[{},{}]", r.GetMin(), r.GetMax())); } static bool decode(const Node& node, OpenVulkano::Math::Range& 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(minVal, maxVal); return ret != c4::csubstr::npos; } return false; } }; template struct convert> { static Node encode(const OpenVulkano::Math::RGB10A2& c) { return Node(fmt::format("[{},{},{},{}]", c.r, c.g, c.b, c.a)); } static bool decode(const Node& node, OpenVulkano::Math::RGB10A2& 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 { 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 { 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 { 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; } }; }