From e814c01c108b30b6cba330dafc4bd731fa282186 Mon Sep 17 00:00:00 2001 From: = <tilman.daab@kit.edu> Date: Wed, 18 Jan 2023 17:33:12 +0100 Subject: [PATCH] resolve resolution problems of templates --- SimoxUtility/json/eigen_conversion.cpp | 6 +-- SimoxUtility/json/eigen_conversion.h | 70 +++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/SimoxUtility/json/eigen_conversion.cpp b/SimoxUtility/json/eigen_conversion.cpp index ed158b365..8018e507f 100644 --- a/SimoxUtility/json/eigen_conversion.cpp +++ b/SimoxUtility/json/eigen_conversion.cpp @@ -7,8 +7,7 @@ namespace Eigen { - template <> - void from_json<Eigen::Vector3f>(const ::simox::json::json& j, Eigen::MatrixBase<Eigen::Vector3f>& vector) + void from_json(const ::simox::json::json& j, Eigen::Vector3f& vector) { if (j.is_object()) { @@ -23,8 +22,7 @@ namespace Eigen } - template <> - void from_json<Eigen::Matrix4f>(const ::simox::json::json& j, Eigen::MatrixBase<Eigen::Matrix4f>& matrix) + void from_json(const ::simox::json::json& j, Eigen::Matrix4f& matrix) { if (j.is_object()) { diff --git a/SimoxUtility/json/eigen_conversion.h b/SimoxUtility/json/eigen_conversion.h index d0a816297..1989a72b8 100644 --- a/SimoxUtility/json/eigen_conversion.h +++ b/SimoxUtility/json/eigen_conversion.h @@ -27,19 +27,23 @@ namespace Eigen // Eigen::MatrixBase (non-specialized). /// Writes the matrix as list of rows. + template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void to_json(simox::json::json& j, const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix); + template <typename Derived> void to_json(simox::json::json& j, const Eigen::MatrixBase<Derived>& matrix); /// Reads the matrix from list of rows. + template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void from_json(const simox::json::json& j, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix); + template <typename Derived> void from_json(const simox::json::json& j, Eigen::MatrixBase<Derived>& matrix); - // Specialization for Eigen::Vector3f (implemented in .cpp) /// If `j` is an object, reads vector from `x, y, z` keys. Otherwise, reads it as matrix. - template <> - void from_json<Eigen::Vector3f>(const simox::json::json& j, Eigen::MatrixBase<Eigen::Vector3f>& vector); + void from_json(const simox::json::json& j, Eigen::Vector3f& vector); // Specialization for Eigen::Matrix4f as transformation matrix (implemented in .cpp). @@ -50,8 +54,7 @@ namespace Eigen * If `j` is an object, reads `matrix` as transformation matrix from `pos` and `ori` keys. * Otherweise, reads it from list of rows. */ - template <> - void from_json<Eigen::Matrix4f>(const simox::json::json& j, Eigen::MatrixBase<Eigen::Matrix4f>& matrix); + void from_json(const simox::json::json& j, Eigen::Matrix4f& matrix); // Eigen::Quaternion @@ -88,6 +91,27 @@ namespace jsonbase // by specializations in the .cpp) /// Writes the matrix as list of rows. + template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void to_json(simox::json::json& j, const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix) + { + for (int row = 0; row < matrix.rows(); ++row) + { + if (matrix.cols() > 1) + { + simox::json::json jrow = simox::json::json::array(); + for (int col = 0; col < matrix.cols(); ++col) + { + jrow.push_back(matrix(row, col)); + } + j.push_back(jrow); + } + else + { + j.push_back(matrix(row, 0)); + } + } + } + template <typename Derived> void to_json(simox::json::json& j, const Eigen::MatrixBase<Derived>& matrix) { @@ -109,7 +133,31 @@ namespace jsonbase } } + /// Reads the matrix from list of rows. + template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void from_json(const simox::json::json& j, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix) + { + using Index = typename Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Index; + + for (std::size_t row = 0; row < j.size(); ++row) + { + const auto& jrow = j.at(row); + if (jrow.is_array()) + { + for (std::size_t col = 0; col < jrow.size(); ++col) + { + const auto& value = jrow.at(col); + matrix(static_cast<Index>(row), static_cast<Index>(col)) = value.get<_Scalar>(); + } + } + else + { + matrix(static_cast<Index>(row), 0) = jrow.get<_Scalar>(); + } + } + } + template <typename Derived> void from_json(const simox::json::json& j, Eigen::MatrixBase<Derived>& matrix) { @@ -135,6 +183,11 @@ namespace jsonbase } } + template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void to_json(simox::json::json& j, const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix) + { + jsonbase::to_json(j, matrix); + } template <typename Derived> void to_json(simox::json::json& j, const Eigen::MatrixBase<Derived>& matrix) @@ -142,13 +195,18 @@ namespace jsonbase jsonbase::to_json(j, matrix); } + template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void from_json(const simox::json::json& j, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix) + { + jsonbase::from_json(j, matrix); + } + template <typename Derived> void from_json(const simox::json::json& j, Eigen::MatrixBase<Derived>& matrix) { jsonbase::from_json(j, matrix); } - template <typename Derived> void to_json(simox::json::json& j, const Eigen::QuaternionBase<Derived>& quat) { -- GitLab