10 #ifndef CTRL_UTILS_YAML_H_
11 #define CTRL_UTILS_YAML_H_
13 #include <yaml-cpp/yaml.h>
15 #include <Eigen/Eigen>
17 #include "ctrl_utils/string.h"
22 inline YAML::Node
FromString(
const std::string& str) {
23 return YAML::Load(str);
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) {
34 if (rhs.cols() == 1) {
35 for (
int i = 0; i < rhs.size(); i++) {
36 node.push_back(rhs(i));
41 for (
int i = 0; i < rhs.rows(); i++) {
43 for (
int j = 0; j < rhs.cols(); j++) {
44 row.push_back(rhs(i, j));
51 static bool decode(
const Node& node, Eigen::Matrix<Scalar, Rows, Cols>& rhs) {
52 if (!node.IsSequence() || node.size() == 0)
return false;
54 if (rhs.size() == 0) {
55 if (!node[0].IsSequence()) {
56 rhs.resize(node.size(), 1);
58 if ((rhs.rows() != 0 && rhs.rows() !=
static_cast<int>(node.size())) ||
59 (rhs.cols() != 0 && rhs.cols() !=
static_cast<int>(node[0].size())))
61 rhs.resize(node.size(), node[0].size());
65 if (rhs.cols() == 1) {
66 for (
size_t i = 0; i < node.size(); i++) {
67 rhs(i) = node[i].as<Scalar>();
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>();
81 template <
typename Scalar>
82 struct convert<Eigen::Quaternion<Scalar>> {
83 static Node encode(
const Eigen::Quaternion<Scalar>& rhs) {
92 static bool decode(
const Node& node, Eigen::Quaternion<Scalar>& rhs) {
93 if (!node.IsMap() || !node[
"w"] || !node[
"x"] || !node[
"y"] || !node[
"z"])
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>();
105 struct convert<Eigen::Isometry3d> {
106 static Node encode(
const Eigen::Isometry3d& rhs) {
108 node[
"pos"] = Eigen::Vector3d(rhs.translation());
109 node[
"ori"] = Eigen::Quaterniond(rhs.linear());
113 static bool decode(
const Node& node, Eigen::Isometry3d& rhs) {
114 if (!node.IsMap() || !node[
"pos"] || !node[
"ori"])
return false;
116 Eigen::Vector3d pos = node[
"pos"].as<Eigen::Vector3d>();
117 Eigen::Quaterniond ori = node[
"ori"].as<Eigen::Quaterniond>();
118 rhs = Eigen::Translation3d(pos) * ori;
Definition: ctrl_utils.cc:18
nlohmann::json FromString(const std::string &str)
Definition: json.h:27