dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
eigen-io.h
1 //
2 // Copyright 2016 CNRS
3 //
4 // Author: Rohan Budhiraja
5 //
6 
7 #ifndef DYNAMIC_GRAPH_EIGEN_IO_H
8 #define DYNAMIC_GRAPH_EIGEN_IO_H
9 
10 #include <boost/format.hpp>
11 #include <boost/numeric/conversion/cast.hpp>
12 #pragma GCC diagnostic push
13 #pragma GCC system_header
14 #include <Eigen/Geometry>
15 #pragma GCC diagnostic pop
16 #include <dynamic-graph/exception-signal.h>
17 #include <dynamic-graph/linear-algebra.h>
18 
20 
21 // TODO: Eigen 3.3 onwards has a global Eigen::Index definition.
22 // If Eigen version is updated, use Eigen::Index instead of this macro.
23 
24 /* \brief Eigen Vector input from istream
25  *
26  * Input Vector format: [N](val1,val2,val3,...,valN)
27  * e.g. [5](1,23,32.2,12.12,32)
28  */
29 namespace Eigen {
30 typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE eigen_index;
31 
32 inline std::istringstream &operator>>(std::istringstream &iss,
33  dynamicgraph::Vector &inst) {
34  unsigned int _size;
35  double _dbl_val;
36  char _ch;
37  boost::format fmt("Failed to enter %s as vector."
38  " Reenter as [N](val1,val2,val3,...,valN)");
39  fmt % iss.str();
40  if (iss >> _ch && _ch != '[') {
41  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
42  } else {
43  if (iss >> _size && !iss.fail()) {
44  inst.resize(_size);
45  } else
46  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
47  if (iss >> _ch && _ch != ']')
48  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
49  else {
50  if (iss >> _ch && _ch != '(')
51  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
52  else {
53  for (unsigned int i = 0; i < _size; i++) {
54  iss >> _dbl_val;
55  if (iss.peek() == ',' || iss.peek() == ' ')
56  iss.ignore();
57  inst(i) = _dbl_val;
58  }
59  if (iss >> _ch && _ch != ')')
60  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
61  }
62  }
63  }
64  return iss;
65 }
66 
67 /* \brief Eigen Matrix input from istream
68  *
69  * Matrix format: [M,N]((val11,val12,val13,...,val1N),...,
70  * (valM1,valM2,...,valMN))
71  * e.g. [2,5]((1 23 32.2 12.12 32),(2 32 23 92.01 19.2))
72  */
73 
74 template <typename Derived>
75 inline std::istringstream &operator>>(std::istringstream &iss,
76  DenseBase<Derived> &inst) {
77  unsigned int _colsize;
78  unsigned int _rowsize;
79  double _dbl_val;
80  char _ch;
81  boost::format fmt("Failed to enter %s as matrix. Reenter as "
82  "((val11,val12,val13,...,val1N),"
83  "...,(valM1,valM2,...,valMN))");
84  MatrixXd _tmp_matrix;
85  fmt % iss.str();
86  if (iss >> _ch && _ch != '[') {
87  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
88  } else {
89  iss >> _rowsize;
90  if (iss.peek() == ',' || iss.peek() == ' ')
91  iss.ignore();
92  iss >> _colsize;
93  if (iss.fail())
94  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
95  else {
96  _tmp_matrix.resize(_rowsize, _colsize);
97  if (iss >> _ch && _ch != ']')
98  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
99  else {
100  if (iss >> _ch && _ch != '(')
101  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
102  else {
103  for (unsigned int j = 0; j < _rowsize; j++) {
104  if (iss >> _ch && _ch != '(')
105  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
106  for (unsigned int i = 0; i < _colsize; i++) {
107  iss >> _dbl_val;
108  if (iss.peek() == ',' || iss.peek() == ' ')
109  iss.ignore();
110  _tmp_matrix(j, i) = _dbl_val;
111  }
112  if (iss >> _ch && _ch != ')')
113  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
114  if (iss.peek() == ',' || iss.peek() == ' ')
115  iss.ignore();
116  }
117  if (iss >> _ch && _ch != ')')
118  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
119  }
120  }
121  }
122  }
123  inst = _tmp_matrix;
124  return iss;
125 }
126 
127 inline std::istringstream &operator>>(std::istringstream &iss,
128  Transform<double, 3, Affine> &inst) {
129  MatrixXd M;
130  iss >> M;
131  inst.matrix() = M;
132  return iss;
133 }
134 
135 /* \brief Eigen Homogeneous Matrix output
136  *
137  * Matrix format: [M,N]((val11,val12,val13,...,val1N),...,
138  * (valM1,valM2,...,valMN))
139  * e.g. [2,5]((1 23 32.2 12.12 32),(2 32 23 92.01 19.2))
140  */
141 
142 inline std::ostream &operator<<(std::ostream &os,
143  Transform<double, 3, Affine> MH) {
144  IOFormat boostFmt(StreamPrecision, DontAlignCols, ",", ",", "(", ")", "(",
145  ")");
146 
147  os << "[4,4]" << MH.matrix().format(boostFmt);
148  return os;
149 }
150 
151 inline std::ostream &operator<<(std::ostream &os, AngleAxisd quat) {
152  VectorXd v(4);
153  v(0) = quat.angle();
154  v.tail<3>() = quat.axis();
155  os << v;
156  return os;
157 }
158 
159 inline std::istringstream &operator>>(std::istringstream &iss,
160  AngleAxisd &inst) {
161  VectorXd v(4);
162  iss >> v;
163  inst.angle() = v(0);
164  inst.axis() = v.tail<3>();
165  return iss;
166 }
167 
168 } // namespace Eigen
169 
170 #endif // DYNAMIC_GRAPH_EIGEN_IO_H
Definition: eigen-io.h:29
Exceptions raised when an error related to signals happen.