2 #ifndef LVR2_IO_YAML_MATRIX_IO_HPP
3 #define LVR2_IO_YAML_MATRIX_IO_HPP
6 #include <yaml-cpp/yaml.h>
12 template <
class Scalar_,
int A_,
int B_,
int C_,
int D_,
int E_>
13 struct convert<Eigen::Matrix<Scalar_, A_, B_, C_, D_, E_> >
19 template <
class Scalar,
int A,
int B,
int C,
int D,
int E>
20 static Node
encode(
const Eigen::Matrix<Scalar, A, B, C, D, E>& M)
23 IndexType rows = M.rows();
24 IndexType cols = M.cols();
30 node[
"data"] = Load(
"[]");
32 for (IndexType i = 0; i < rows; ++i) {
33 for (IndexType j = 0; j < cols; ++j) {
34 node[
"data"].push_back(M.coeff(i, j));
40 template <
class Scalar,
int A,
int B,
int C,
int D,
int E>
41 static bool decode(
const Node& node, Eigen::Matrix<Scalar, A, B, C, D, E>& M)
45 IndexType rows = node[
"rows"].as<IndexType>();
46 IndexType cols = node[
"cols"].as<IndexType>();
48 size_t expected_size = M.rows() * M.cols();
49 if (!node[
"data"].IsSequence() || node[
"data"].size() != expected_size) {
53 YAML::const_iterator it = node[
"data"].begin();
54 YAML::const_iterator it_end = node[
"data"].end();
55 if (rows > 0 && cols > 0) {
56 for (IndexType i = 0; i < rows; ++i) {
57 for (IndexType j = 0; j < cols; ++j) {
58 M.coeffRef(i, j) = it->as<Scalar>();
66 template <
class Scalar,
int B,
int C,
int D,
int E>
67 static bool decode(
const Node& node, Eigen::Matrix<Scalar, Eigen::Dynamic, B, C, D, E>& M)
71 IndexType rows = node[
"rows"].as<IndexType>();
72 IndexType cols = node[
"cols"].as<IndexType>();
74 M.resize(rows, Eigen::NoChange);
76 size_t expected_size = M.rows() * M.cols();
77 if (!node[
"data"].IsSequence() || node[
"data"].size() != expected_size) {
81 YAML::const_iterator it = node[
"data"].begin();
82 YAML::const_iterator it_end = node[
"data"].end();
83 if (rows > 0 && cols > 0) {
84 for (IndexType i = 0; i < rows; ++i) {
85 for (IndexType j = 0; j < cols; ++j) {
86 M.coeffRef(i, j) = it->as<Scalar>();
97 template <
class Scalar,
int A,
int C,
int D,
int E>
98 static bool decode(
const Node& node, Eigen::Matrix<Scalar, A, Eigen::Dynamic, C, D, E>& M)
101 IndexType rows = node[
"rows"].as<IndexType>();
102 IndexType cols = node[
"cols"].as<IndexType>();
104 M.resize(Eigen::NoChange, cols);
106 size_t expected_size = M.rows() * M.cols();
107 if (!node[
"data"].IsSequence() || node[
"data"].size() != expected_size) {
111 YAML::const_iterator it = node[
"data"].begin();
112 YAML::const_iterator it_end = node[
"data"].end();
113 if (rows > 0 && cols > 0) {
114 for (IndexType i = 0; i < rows; ++i) {
115 for (IndexType j = 0; j < cols; ++j) {
116 M.coeffRef(i, j) = it->as<Scalar>();
127 template <
class Scalar,
int C,
int D,
int E>
128 static bool decode(
const Node& node, Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, C, D, E>& M)
131 IndexType rows = node[
"rows"].as<IndexType>();
132 IndexType cols = node[
"cols"].as<IndexType>();
134 M.resize(rows, cols);
136 size_t expected_size = M.rows() * M.cols();
137 if (!node[
"data"].IsSequence() || node[
"data"].size() != expected_size) {
141 YAML::const_iterator it = node[
"data"].begin();
142 YAML::const_iterator it_end = node[
"data"].end();
143 if (rows > 0 && cols > 0) {
144 for (IndexType i = 0; i < rows; ++i) {
145 for (IndexType j = 0; j < cols; ++j) {
146 M.coeffRef(i, j) = it->as<Scalar>();
156 #endif // LVR2_IO_YAML_MATRIX_IO_HPP