5 #ifndef DYNAMIC_GRAPH_SIGNAL_CASTER_HH
6 #define DYNAMIC_GRAPH_SIGNAL_CASTER_HH
10 #include <boost/format.hpp>
11 #include <boost/lexical_cast.hpp>
13 #include "dynamic-graph/exception-signal.h"
14 #include <dynamic-graph/dynamic-graph-api.h>
15 #include <dynamic-graph/eigen-io.h>
16 #include <dynamic-graph/linear-algebra.h>
24 inline static void disp(
const T &value, std::ostream &os) { os << value; }
26 inline static T
cast(std::istringstream &is) {
31 "failed to serialize " + is.str());
36 inline static void trace(
const T &value, std::ostream &os) { os << value; }
41 inline static void disp(
const T &, std::ostream &) {
42 throw std::logic_error(
"this disp is not implemented.");
44 inline static T cast(std::istringstream &) {
45 throw std::logic_error(
"this cast is not implemented.");
47 inline static void trace(
const T &, std::ostream &) {
48 throw std::logic_error(
"this trace is not implemented.");
56 template <
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
59 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>>
61 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>> {
62 typedef Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
65 inline static void disp(
const matrix_type &value, std::ostream &os) {
66 static const Eigen::IOFormat row_format(
67 Eigen::StreamPrecision, Eigen::DontAlignCols,
" ",
" ",
"",
"",
"",
"");
68 os << value.format(row_format);
71 inline static void trace(
const matrix_type &value, std::ostream &os) {
72 static const Eigen::IOFormat row_format(Eigen::StreamPrecision,
73 Eigen::DontAlignCols,
"\t",
"\t",
75 os << value.format(row_format);
80 template <
typename _Scalar,
int _Options>
81 struct signal_io<Eigen::Quaternion<_Scalar, _Options>>
83 typedef Eigen::Quaternion<_Scalar, _Options> quat_type;
84 typedef Eigen::Matrix<_Scalar, 4, 1, _Options> matrix_type;
86 inline static void disp(
const quat_type &value, std::ostream &os) {
90 inline static quat_type
cast(std::istringstream &is) {
94 inline static void trace(
const quat_type &value, std::ostream &os) {
102 inline static std::string
cast(std::istringstream &iss) {
return iss.str(); }
118 inline static double cast(std::istringstream &iss) {
119 std::string tmp(iss.str());
122 return std::numeric_limits<double>::quiet_NaN();
123 else if (tmp ==
"inf" || tmp ==
"+inf")
124 return std::numeric_limits<double>::infinity();
125 else if (tmp ==
"-inf")
126 return -1. * std::numeric_limits<double>::infinity();
129 return boost::lexical_cast<double>(tmp);
130 }
catch (boost::bad_lexical_cast &) {
131 boost::format fmt(
"failed to serialize %s (to double)");