Support yaml for ArFrameMetadata
This commit is contained in:
@@ -5,8 +5,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ArFrameMetadata.hpp"
|
#include "ArFrameMetadata.hpp"
|
||||||
|
#include "Extensions/RymlConverters.hpp"
|
||||||
|
#include <fmt/format.h>
|
||||||
#include <pugixml.hpp>
|
#include <pugixml.hpp>
|
||||||
#include <yaml-cpp/yaml.h>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
namespace OpenVulkano::AR
|
namespace OpenVulkano::AR
|
||||||
@@ -45,18 +46,21 @@ namespace OpenVulkano::AR
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<glm::length_t SIZE, typename T, glm::qualifier Q>
|
template<typename T, int S>
|
||||||
void MatToYaml(const glm::mat<SIZE, SIZE, T, Q>& mat, std::ostream& stream, const std::string& nl = "\n")
|
void ReadMat(const ryml::NodeRef& node, T& mat)
|
||||||
{
|
{
|
||||||
for(int r = 0 ; r < SIZE; r++)
|
if (!node.is_seq()) return;
|
||||||
|
int row = 0;
|
||||||
|
for(auto iter = node.begin(); iter != node.end() && row < S; ++iter)
|
||||||
{
|
{
|
||||||
stream << nl << "- [";
|
const auto rowNode = *iter;
|
||||||
for (int c = 0; c < SIZE; c++)
|
int col = 0;
|
||||||
|
for(auto colIter = rowNode.begin(); colIter != rowNode.end() && col < S; ++colIter)
|
||||||
{
|
{
|
||||||
if (c) stream << ',';
|
(*colIter) >> mat[col][row];
|
||||||
stream << ' ' << mat[c][r];
|
col++;
|
||||||
}
|
}
|
||||||
stream << " ]";
|
row++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,32 +93,82 @@ namespace OpenVulkano::AR
|
|||||||
return frameData;
|
return frameData;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArFrameMetadata ArFrameMetadata::FromYaml(const char* xml, size_t length)
|
ArFrameMetadata ArFrameMetadata::FromYaml(const char* yaml, size_t length)
|
||||||
{
|
{
|
||||||
ArFrameMetadata frameData;
|
ArFrameMetadata frameData;
|
||||||
//TODO
|
ryml::Tree tree;
|
||||||
|
ryml::parse_in_arena(c4::csubstr(yaml, length), &tree);
|
||||||
|
ryml::NodeRef root = tree.rootref();
|
||||||
|
ryml::NodeRef camNode = root["Camera"];
|
||||||
|
ReadMat<Math::Matrix4f, 4>(camNode["Transform"], frameData.transformation);
|
||||||
|
ReadMat<Math::Matrix4f, 4>(camNode["Projection"], frameData.projection);
|
||||||
|
Math::Matrix3f intrinsic;
|
||||||
|
Math::Vector2i res;
|
||||||
|
camNode["Resolution"] >> res;
|
||||||
|
ReadMat<Math::Matrix3f, 3>(camNode["Intrinsics"], intrinsic);
|
||||||
|
frameData.intrinsic = {intrinsic, res};
|
||||||
|
camNode["ExposureDuration"] >> frameData.exposureTime;
|
||||||
|
camNode["ExposureOffset"] >> frameData.exposureOffset;
|
||||||
|
uint64_t nanos;
|
||||||
|
root["Timestamp"] >> nanos;
|
||||||
|
frameData.timestamp = nanos;
|
||||||
|
root["TimestampDepth"] >> nanos;
|
||||||
|
frameData.timestampDepth = nanos;
|
||||||
|
std::string tracking;
|
||||||
|
ryml::NodeRef trackingNode = root["TrackingState"];
|
||||||
|
trackingNode["Camera"] >> tracking;
|
||||||
|
frameData.trackingState = ArTrackingState::GetFromName(tracking);
|
||||||
|
ryml::NodeRef lightNode = root["Light"];
|
||||||
|
lightNode["Intensity"] >> frameData.lightIntensity;
|
||||||
|
lightNode["ColorTemp"] >> frameData.lightColorTemp;
|
||||||
return frameData;
|
return frameData;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ArFrameMetadata::ToYaml() const
|
std::string ArFrameMetadata::ToYaml() const
|
||||||
{
|
{
|
||||||
std::stringstream meta;
|
const auto& camMat = intrinsic.GetMatrix();
|
||||||
meta << std::setprecision(std::numeric_limits<double>::digits10);
|
std::string meta = fmt::format(R"(Camera:
|
||||||
|
Transform:
|
||||||
meta << "camera:\n transform: ";
|
- [ {}, {}, {}, {} ]
|
||||||
MatToYaml(transformation, meta, "\n ");
|
- [ {}, {}, {}, {} ]
|
||||||
meta << "\n projection";
|
- [ {}, {}, {}, {} ]
|
||||||
MatToYaml(projection, meta, "\n ");
|
- [ {}, {}, {}, {} ]
|
||||||
meta << "\n resolution:\n width: " << intrinsic.GetResolution().x << "\n height: " << intrinsic.GetResolution().y;
|
Projection:
|
||||||
meta << "\n intrinsics: ";
|
- [ {}, {}, {}, {} ]
|
||||||
MatToYaml(intrinsic.cameraMatrix, meta, "\n ");
|
- [ {}, {}, {}, {} ]
|
||||||
meta << "\n exposureDuration: " << exposureTime << "\n exposureOffset: " << exposureOffset;
|
- [ {}, {}, {}, {} ]
|
||||||
meta << "\ntimestamp: " << timestamp.GetSeconds();
|
- [ {}, {}, {}, {} ]
|
||||||
meta << "\ntimestampDepth: " << timestampDepth.GetNanos();
|
Resolution: '({},{})'
|
||||||
meta << "\ntrackingState: \n camera: " << trackingState.GetName();
|
Intrinsics:
|
||||||
meta << "\nlight:\n intensity: " << lightIntensity << "\n colorTemp: " << lightColorTemp;
|
- [ {}, {}, {} ]
|
||||||
|
- [ {}, {}, {} ]
|
||||||
return meta.str();
|
- [ {}, {}, {} ]
|
||||||
|
ExposureDuration: {}
|
||||||
|
ExposureOffset: {}
|
||||||
|
Timestamp: {}
|
||||||
|
TimestampDepth: {}
|
||||||
|
TrackingState:
|
||||||
|
Camera: {}
|
||||||
|
Light:
|
||||||
|
Intensity: {}
|
||||||
|
ColorTemp: {}
|
||||||
|
)",
|
||||||
|
transformation[0][0], transformation[1][0], transformation[2][0], transformation[3][0],
|
||||||
|
transformation[0][1], transformation[1][1], transformation[2][1], transformation[3][1],
|
||||||
|
transformation[0][2], transformation[1][2], transformation[2][2], transformation[3][2],
|
||||||
|
transformation[0][3], transformation[1][3], transformation[2][3], transformation[3][3],
|
||||||
|
projection[0][0], projection[1][0], projection[2][0], projection[3][0],
|
||||||
|
projection[0][1], projection[1][1], projection[2][1], projection[3][1],
|
||||||
|
projection[0][2], projection[1][2], projection[2][2], projection[3][2],
|
||||||
|
projection[0][3], projection[1][3], projection[2][3], projection[3][3],
|
||||||
|
intrinsic.GetResolution().x, intrinsic.GetResolution().y,
|
||||||
|
camMat[0][0], camMat[1][0], camMat[2][0],
|
||||||
|
camMat[0][1], camMat[1][1], camMat[2][1],
|
||||||
|
camMat[0][2], camMat[1][2], camMat[2][2],
|
||||||
|
exposureTime, exposureOffset, timestamp.GetNanos(), timestampDepth.GetNanos(),
|
||||||
|
trackingState.GetName(), lightIntensity, lightColorTemp
|
||||||
|
);
|
||||||
|
return meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ArFrameMetadata::ToXML() const
|
std::string ArFrameMetadata::ToXML() const
|
||||||
@@ -128,7 +182,7 @@ namespace OpenVulkano::AR
|
|||||||
MatToXML(projection, meta, "\n\t\t\t");
|
MatToXML(projection, meta, "\n\t\t\t");
|
||||||
meta << "\n\t\t</projection>\n\t\t<resolution>\n\t\t\t<width>" << intrinsic.GetResolution().x << "</width>\n\t\t\t";
|
meta << "\n\t\t</projection>\n\t\t<resolution>\n\t\t\t<width>" << intrinsic.GetResolution().x << "</width>\n\t\t\t";
|
||||||
meta << "<height>" << intrinsic.GetResolution().y << "</height>\n\t\t</resolution>\n\t\t<intrinsics>";
|
meta << "<height>" << intrinsic.GetResolution().y << "</height>\n\t\t</resolution>\n\t\t<intrinsics>";
|
||||||
MatToXML(intrinsic.cameraMatrix, meta, "\n\t\t\t");
|
MatToXML(intrinsic.GetMatrix(), meta, "\n\t\t\t");
|
||||||
meta << "</intrinsics>\n\t\t<exposureDuration>" << exposureTime << "</exposureDuration>\n\t\t<exposureOffset>" << exposureOffset;
|
meta << "</intrinsics>\n\t\t<exposureDuration>" << exposureTime << "</exposureDuration>\n\t\t<exposureOffset>" << exposureOffset;
|
||||||
meta << "</exposureOffset>\n\t</camera>\n\t";
|
meta << "</exposureOffset>\n\t</camera>\n\t";
|
||||||
meta << "<timestamp>" << timestamp.GetSeconds() << "</timestamp>\n\t";
|
meta << "<timestamp>" << timestamp.GetSeconds() << "</timestamp>\n\t";
|
||||||
|
|||||||
@@ -79,6 +79,20 @@ namespace YAML
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
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<T>();
|
||||||
|
readCount++;
|
||||||
|
}
|
||||||
|
return readCount == count;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct convert<OpenVulkano::Math::Vector2<T>>
|
struct convert<OpenVulkano::Math::Vector2<T>>
|
||||||
{
|
{
|
||||||
@@ -94,7 +108,7 @@ namespace YAML
|
|||||||
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{})", v.x, v.y);
|
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{})", v.x, v.y);
|
||||||
return ret != c4::csubstr::npos;
|
return ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequence(node, 2, &v.x);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -113,7 +127,7 @@ namespace YAML
|
|||||||
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{})", v.x, v.y, v.z);
|
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{})", v.x, v.y, v.z);
|
||||||
return ret != c4::csubstr::npos;
|
return ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequence(node, 3, &v.x);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -132,7 +146,7 @@ namespace YAML
|
|||||||
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{},{})", v.x, v.y, v.z, v.w);
|
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 ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequence(node, 4, &v.x);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -151,7 +165,7 @@ namespace YAML
|
|||||||
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{})", v.x, v.y);
|
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{})", v.x, v.y);
|
||||||
return ret != c4::csubstr::npos;
|
return ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequence(node, 2, &v.x);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -170,10 +184,26 @@ namespace YAML
|
|||||||
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{})", v.x, v.y, v.z);
|
size_t ret = c4::unformat(c4::to_csubstr(node.Scalar().c_str()), "({},{},{})", v.x, v.y, v.z);
|
||||||
return ret != c4::csubstr::npos;
|
return ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequence(node, 3, &v.x);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename MAT>
|
||||||
|
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<typename T>
|
template<typename T>
|
||||||
struct convert<OpenVulkano::Math::Matrix3<T>>
|
struct convert<OpenVulkano::Math::Matrix3<T>>
|
||||||
{
|
{
|
||||||
@@ -196,7 +226,7 @@ namespace YAML
|
|||||||
mat[2][0], mat[2][1], mat[2][2]);
|
mat[2][0], mat[2][1], mat[2][2]);
|
||||||
return ret != c4::csubstr::npos;
|
return ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequenceMat(node, 3, mat);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -222,7 +252,7 @@ namespace YAML
|
|||||||
mat[2][0], mat[2][1], mat[2][2]);
|
mat[2][0], mat[2][1], mat[2][2]);
|
||||||
return ret != c4::csubstr::npos;
|
return ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequenceMat(node, 3, mat);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -250,7 +280,7 @@ namespace YAML
|
|||||||
mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
|
mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
|
||||||
return ret != c4::csubstr::npos;
|
return ret != c4::csubstr::npos;
|
||||||
}
|
}
|
||||||
return false;
|
return ReadSequenceMat(node, 4, mat);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,8 @@ namespace OpenVulkano::Math
|
|||||||
{
|
{
|
||||||
class CameraIntrinsic
|
class CameraIntrinsic
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
Math::Matrix3f_SIMD cameraMatrix;
|
Math::Matrix3f_SIMD cameraMatrix;
|
||||||
|
public:
|
||||||
CameraIntrinsic() : CameraIntrinsic(Math::Matrix3f(0)) {}
|
CameraIntrinsic() : CameraIntrinsic(Math::Matrix3f(0)) {}
|
||||||
|
|
||||||
CameraIntrinsic(const Math::Matrix3f& camMat) : cameraMatrix(camMat)
|
CameraIntrinsic(const Math::Matrix3f& camMat) : cameraMatrix(camMat)
|
||||||
@@ -103,15 +102,17 @@ namespace OpenVulkano::Math
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraIntrinsic operator * (const float scale) const
|
[[nodiscard]] CameraIntrinsic operator * (const float scale) const
|
||||||
{
|
{
|
||||||
return Scale(scale);
|
return Scale(scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraIntrinsic operator * (const Math::Vector2f& scale) const
|
[[nodiscard]] CameraIntrinsic operator * (const Math::Vector2f& scale) const
|
||||||
{
|
{
|
||||||
return Scale(scale);
|
return Scale(scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const Matrix3f_SIMD& GetMatrix() const { return cameraMatrix; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class CameraIntrinsicWithResolution : public CameraIntrinsic
|
class CameraIntrinsicWithResolution : public CameraIntrinsic
|
||||||
|
|||||||
Reference in New Issue
Block a user