5 #ifndef DYNAMIC_GRAPH_SIGNAL_CASTER_HH
6 #define DYNAMIC_GRAPH_SIGNAL_CASTER_HH
7 #include <dynamic-graph/dynamic-graph-api.h>
8 #include <dynamic-graph/eigen-io.h>
9 #include <dynamic-graph/linear-algebra.h>
11 #include <boost/format.hpp>
12 #include <boost/lexical_cast.hpp>
16 #include "dynamic-graph/exception-signal.h"
25 inline static void disp(
const T &value, std::ostream &os) { os << value; }
27 inline static T
cast(std::istringstream &is) {
32 "failed to serialize " + is.str());
37 inline static void trace(
const T &value, std::ostream &os) { os << value; }
43 inline static void disp(
const T &, std::ostream &) {
44 throw std::logic_error(
"this disp is not implemented.");
46 inline static T cast(std::istringstream &) {
47 throw std::logic_error(
"this cast is not implemented.");
49 inline static void trace(
const T &, std::ostream &) {
50 throw std::logic_error(
"this trace is not implemented.");
59 template <
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
62 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>>
64 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>> {
65 typedef Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
68 inline static void disp(
const matrix_type &value, std::ostream &os) {
69 static const Eigen::IOFormat row_format(
70 Eigen::StreamPrecision, Eigen::DontAlignCols,
" ",
" ",
"",
"",
"",
"");
71 os << value.format(row_format);
74 inline static void trace(
const matrix_type &value, std::ostream &os) {
75 static const Eigen::IOFormat row_format(Eigen::StreamPrecision,
76 Eigen::DontAlignCols,
"\t",
"\t",
78 os << value.format(row_format);
83 template <
typename _Scalar,
int _Options>
84 struct signal_io<Eigen::Quaternion<_Scalar, _Options>>
86 typedef Eigen::Quaternion<_Scalar, _Options> quat_type;
87 typedef Eigen::Matrix<_Scalar, 4, 1, _Options> matrix_type;
89 inline static void disp(
const quat_type &value, std::ostream &os) {
93 inline static quat_type
cast(std::istringstream &is) {
97 inline static void trace(
const quat_type &value, std::ostream &os) {
106 inline static std::string
cast(std::istringstream &iss) {
return iss.str(); }
123 inline static double cast(std::istringstream &iss) {
124 std::string tmp(iss.str());
127 return std::numeric_limits<double>::quiet_NaN();
128 else if (tmp ==
"inf" || tmp ==
"+inf")
129 return std::numeric_limits<double>::infinity();
130 else if (tmp ==
"-inf")
131 return -1. * std::numeric_limits<double>::infinity();
134 return boost::lexical_cast<double>(tmp);
135 }
catch (boost::bad_lexical_cast &) {
136 boost::format fmt(
"failed to serialize %s (to double)");