ctrl-utils
yaml.h
1 
10 #ifndef CTRL_UTILS_YAML_H_
11 #define CTRL_UTILS_YAML_H_
12 
13 #include <yaml-cpp/yaml.h>
14 
15 #include <Eigen/Eigen>
16 
17 #include "ctrl_utils/string.h"
18 
19 namespace ctrl_utils {
20 
21 template <>
22 inline YAML::Node FromString(const std::string& str) {
23  return YAML::Load(str);
24 }
25 
26 } // namespace ctrl_utils
27 
28 namespace YAML {
29 
30 template <typename Scalar, int Rows, int Cols>
31 struct convert<Eigen::Matrix<Scalar, Rows, Cols>> {
32  static Node encode(const Eigen::Matrix<Scalar, Rows, Cols>& rhs) {
33  Node node;
34  if (rhs.cols() == 1) {
35  for (int i = 0; i < rhs.size(); i++) {
36  node.push_back(rhs(i));
37  }
38  return node;
39  }
40 
41  for (int i = 0; i < rhs.rows(); i++) {
42  Node row;
43  for (int j = 0; j < rhs.cols(); j++) {
44  row.push_back(rhs(i, j));
45  }
46  node.push_back(row);
47  }
48  return node;
49  }
50 
51  static bool decode(const Node& node, Eigen::Matrix<Scalar, Rows, Cols>& rhs) {
52  if (!node.IsSequence() || node.size() == 0) return false;
53 
54  if (rhs.size() == 0) {
55  if (!node[0].IsSequence()) {
56  rhs.resize(node.size(), 1);
57  } else {
58  if ((rhs.rows() != 0 && rhs.rows() != static_cast<int>(node.size())) ||
59  (rhs.cols() != 0 && rhs.cols() != static_cast<int>(node[0].size())))
60  return false;
61  rhs.resize(node.size(), node[0].size());
62  }
63  }
64 
65  if (rhs.cols() == 1) {
66  for (size_t i = 0; i < node.size(); i++) {
67  rhs(i) = node[i].as<Scalar>();
68  }
69  return true;
70  }
71 
72  for (size_t i = 0; i < node.size(); i++) {
73  for (size_t j = 0; j < node[i].size(); j++) {
74  rhs(i, j) = node[i][j].as<Scalar>();
75  }
76  }
77  return true;
78  }
79 };
80 
81 template <typename Scalar>
82 struct convert<Eigen::Quaternion<Scalar>> {
83  static Node encode(const Eigen::Quaternion<Scalar>& rhs) {
84  Node node;
85  node["w"] = rhs.w();
86  node["x"] = rhs.x();
87  node["y"] = rhs.y();
88  node["z"] = rhs.z();
89  return node;
90  }
91 
92  static bool decode(const Node& node, Eigen::Quaternion<Scalar>& rhs) {
93  if (!node.IsMap() || !node["w"] || !node["x"] || !node["y"] || !node["z"])
94  return false;
95  rhs.w() = node["w"].as<Scalar>();
96  rhs.x() = node["x"].as<Scalar>();
97  rhs.y() = node["y"].as<Scalar>();
98  rhs.z() = node["z"].as<Scalar>();
99  rhs.normalize();
100  return true;
101  }
102 };
103 
104 template <>
105 struct convert<Eigen::Isometry3d> {
106  static Node encode(const Eigen::Isometry3d& rhs) {
107  Node node;
108  node["pos"] = Eigen::Vector3d(rhs.translation());
109  node["ori"] = Eigen::Quaterniond(rhs.linear());
110  return node;
111  }
112 
113  static bool decode(const Node& node, Eigen::Isometry3d& rhs) {
114  if (!node.IsMap() || !node["pos"] || !node["ori"]) return false;
115 
116  Eigen::Vector3d pos = node["pos"].as<Eigen::Vector3d>();
117  Eigen::Quaterniond ori = node["ori"].as<Eigen::Quaterniond>();
118  rhs = Eigen::Translation3d(pos) * ori;
119  return true;
120  }
121 };
122 
123 } // namespace YAML
124 
125 #endif // CTRL_UTILS_YAML_H_
Definition: ctrl_utils.cc:18
nlohmann::json FromString(const std::string &str)
Definition: json.h:27