10 #ifndef CTRL_UTILS_EIGEN_STRING_H_
11 #define CTRL_UTILS_EIGEN_STRING_H_
28 template<
typename Derived>
38 template<
typename Derived>
39 std::string
EncodeMatlab(
const Eigen::DenseBase<Derived>& matrix);
48 template<
typename Derived>
58 template<
typename Derived>
59 std::string
EncodeJson(
const Eigen::DenseBase<Derived>& matrix);
65 template<
typename Scalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
66 std::stringstream& operator>>(std::stringstream& ss,
67 Eigen::Matrix<Scalar, Rows, Cols, Options,
68 MaxRows, MaxCols>& matrix) {
69 using PlainMatrix = Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>;
70 matrix = ctrl_utils::DecodeMatlab<PlainMatrix>(ss.str());
74 template<
typename Derived>
75 std::stringstream&
operator<<(std::stringstream& ss,
const Eigen::DenseBase<Derived>& matrix) {
80 template<
typename Derived>
81 std::stringstream& operator>>(std::stringstream& ss, Eigen::QuaternionBase<Derived>& quat) {
86 template<
typename Derived>
87 std::stringstream&
operator<<(std::stringstream& ss,
const Eigen::QuaternionBase<Derived>& quat) {
96 template<
typename Derived>
98 std::string str_local = str;
102 size_t num_cols = matrix.cols();
103 size_t num_rows = matrix.rows();
104 typename Derived::Scalar eof;
107 size_t idx_col_end = 0;
108 if (num_cols == 0 || (num_cols == 1 && num_rows == 0)) {
111 idx_col_end = str.find_first_of(
';');
112 while (idx < idx_col_end) {
114 idx = str.find_first_not_of(
' ', idx);
115 if (idx >= idx_col_end)
break;
118 if (str[idx] ==
';')
break;
119 idx = str.find_first_of(
' ', idx + 1);
126 size_t idx = idx_col_end;
127 if (idx_col_end != 0) {
131 while (idx != std::string::npos) {
133 if (idx != 0) str_local[idx] =
' ';
136 idx = str.find_first_of(
';', idx + 1);
140 idx = str.find_last_not_of(
' ');
141 if (str[idx] ==
';') --num_rows;
144 for (
size_t idx = idx_col_end; idx < str.size(); idx++) {
145 if (str[idx] ==
';') str_local[idx] =
' ';
151 throw std::invalid_argument(
152 "DecodeMatlab(): Failed to decode Eigen::MatrixX" +
153 std::string(
typeid(eof).name()) +
"(" + std::to_string(num_rows) +
154 ", " + std::to_string(num_cols) +
") from: (" + str +
").");
160 if (matrix.rows() == 0 || matrix.cols() == 0) {
161 matrix.resize(num_rows, num_cols);
165 std::stringstream ss(str_local);
166 for (
size_t i = 0; i < num_rows; ++i) {
167 for (
size_t j = 0; j < num_cols; ++j) {
170 throw std::invalid_argument(
171 "DecodeMatlab(): Failed to decode Eigen::MatrixX" +
172 std::string(
typeid(eof).name()) +
"(" + std::to_string(num_rows) +
173 ", " + std::to_string(num_cols) +
") from: (" + str +
").");
181 throw std::invalid_argument(
182 "DecodeMatlab(): Failed to decode Eigen::MatrixX" +
183 std::string(
typeid(eof).name()) +
"(" + std::to_string(num_rows) +
184 ", " + std::to_string(num_cols) +
") from: (" + str +
").");
190 template<
typename Derived>
192 std::stringstream ss;
193 ss.precision(std::numeric_limits<typename Derived::Scalar>::digits10);
194 if (matrix.cols() == 1) {
196 for (
int i = 0; i < matrix.rows(); ++i) {
197 if (i > 0) ss <<
" ";
203 for (
int i = 0; i < matrix.rows(); ++i) {
204 if (i > 0) ss <<
"; ";
205 for (
int j = 0; j < matrix.cols(); ++j) {
206 if (j > 0) ss <<
" ";
214 template<
typename Derived>
215 std::string
EncodeJson(
const Eigen::DenseBase<Derived>& matrix) {
217 if (matrix.cols() == 1) {
219 for (
int i = 0; i < matrix.rows(); ++i) {
220 if (i > 0) s.append(
",");
221 s.append(std::to_string(matrix(i,0)));
226 for (
int i = 0; i < matrix.rows(); ++i) {
227 if (i > 0) s.append(
",");
229 if (matrix.rows() > 1) s.append(
"[");
230 for (
int j = 0; j < matrix.cols(); ++j) {
231 if (j > 0) s.append(
",");
232 s.append(std::to_string(matrix(i,j)));
235 if (matrix.rows() > 1) s.append(
"]");
242 template<
typename Derived>
245 size_t idx_row_end = str.find_last_of(
']');
246 if (idx_row_end != std::string::npos) {
247 size_t idx_temp = str.substr(0, idx_row_end).find_last_of(
']');
248 if (idx_temp != std::string::npos) idx_row_end = idx_temp;
254 size_t idx_col_end = str.find_first_of(
']');
255 while (idx < idx_col_end) {
257 idx = str.find_first_not_of(
' ', idx);
258 if (idx >= idx_col_end)
break;
261 idx = str.find_first_of(
',', idx + 1);
264 if (idx > idx_col_end) idx = idx_col_end;
268 while (idx < idx_row_end) {
270 idx = str.find_first_not_of(
']', idx);
271 if (idx >= idx_row_end)
break;
274 idx = str.find_first_of(
']', idx + 1);
280 throw std::runtime_error(
"RedisClient: Failed to decode Eigen Matrix from: " + str +
".");
288 Eigen::MatrixXd matrix(num_rows, num_cols);
289 std::string str_local(str);
290 for (
char delimiter :
",[]") {
291 std::replace(str_local.begin(), str_local.end(), delimiter,
' ');
293 std::stringstream ss(str_local);
294 for (
size_t i = 0; i < num_rows; ++i) {
295 for (
size_t j = 0; j < num_cols; ++j) {
299 matrix(i,j) = std::stod(val);
300 }
catch (
const std::exception& e) {
301 throw std::runtime_error(
"RedisClient: Failed to decode Eigen Matrix from: " + str +
".");
Definition: ctrl_utils.cc:18
std::string EncodeJson(const Eigen::DenseBase< Derived > &matrix)
Definition: eigen_string.h:215
Derived DecodeJson(const std::string &str)
Definition: eigen_string.h:243
std::ostream & operator<<(std::ostream &os, const Args &args)
Definition: argparse.h:544
std::string EncodeMatlab(const Eigen::DenseBase< Derived > &matrix)
Definition: eigen_string.h:191
Derived DecodeMatlab(const std::string &str)
Definition: eigen_string.h:97