Skip to content

Commit 55c51cb

Browse files
Rohan Budhirajaolivier-stasse
authored andcommitted
[eigen] add ostream and istream operators for Eigen/Geoemetry classes.
modify dg::Vector and dg::Matrix istream operators.
1 parent bfafccb commit 55c51cb

File tree

3 files changed

+106
-67
lines changed

3 files changed

+106
-67
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ CHANGELOG
33

44
[Current]
55

6+
[v3.0.0]
7+
* Replace jrl-mal with eigen
8+
* Add ostream and isteram operators for Eigen Classes
9+
10+
611
[v2.5.5]
712
* Improve Travis support
813

include/dynamic-graph/eigen-io.h

Lines changed: 100 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2010 CNRS
2+
// Copyright 2016 CNRS
33
//
44
// Author: Rohan Budhiraja
55
//
@@ -34,92 +34,126 @@ using dynamicgraph::ExceptionSignal;
3434

3535
/* \brief Eigen Vector input from istream
3636
*
37-
* Input Vector format: val1 val2 val3 ... valN
38-
* e.g. 1 23 32.2 12.12 32
37+
* Input Vector format: [N](val1,val2,val3,...,valN)
38+
* e.g. [5](1,23,32.2,12.12,32)
3939
*/
4040
namespace Eigen {
4141
typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE eigen_index;
4242
inline std::istringstream& operator >> (std::istringstream &iss,
43-
dynamicgraph::Vector &inst)
44-
{
45-
std::vector<double> _stdvec;
46-
double _dbl_val;
47-
48-
boost::format fmt ("Failed to enter %s as vector. Reenter as [val1 val2 ... valN]");
49-
fmt %iss.str();
50-
51-
while(iss >> _dbl_val && !iss.fail()) {
52-
_stdvec.push_back(_dbl_val);
53-
}
54-
try {
55-
inst = Eigen::VectorXd::Map (_stdvec.data(),
56-
boost::numeric_cast<eigen_index> (_stdvec.size()) );
43+
dynamicgraph::Vector &inst) {
44+
unsigned int _size;
45+
double _dbl_val;
46+
char _ch;
47+
boost::format fmt ("Failed to enter %s as vector. Reenter as [N](val1,val2,val3,...,valN)");
48+
fmt %iss.str();
49+
if(iss>> _ch && _ch != '['){
50+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
51+
}
52+
else {
53+
if(iss >> _size && !iss.fail()){
54+
inst.resize(_size);
5755
}
58-
catch (boost::bad_numeric_cast&) {
59-
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str() );
56+
else
57+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
58+
if(iss >> _ch && _ch != ']')
59+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
60+
else {
61+
if(iss>> _ch && _ch != '(')
62+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
63+
else {
64+
for (unsigned int i=0;i<_size; i++){
65+
iss >> _dbl_val;
66+
if (iss.peek() == ',' || iss.peek() == ' ')
67+
iss.ignore();
68+
inst(i) = _dbl_val;
69+
}
70+
if(iss >> _ch && _ch != ')')
71+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
72+
}
6073
}
61-
62-
return iss;
6374
}
75+
return iss;
76+
}
6477

6578
/* \brief Eigen Matrix input from istream
6679
*
67-
* Matrix format: [[val11 val12 val13 ... val1N] ... [valM1 valM2 ... valMN]]
68-
* e.g. [[1 23 32.2 12.12 32][2 32 23 92.01 19.2]]
80+
* Matrix format: [M,N]((val11,val12,val13,...,val1N),...,(valM1,valM2,...,valMN))
81+
* e.g. [2,5]((1 23 32.2 12.12 32),(2 32 23 92.01 19.2))
6982
*/
7083

7184
template<typename Derived>
7285
inline std::istringstream& operator >> (std::istringstream &iss,
73-
DenseBase<Derived> &inst)
74-
{
75-
std::vector<dynamicgraph::Vector> _stdmat;
76-
char _ch;
77-
int _vec_size;
78-
bool _vec_size_set = false;
79-
80-
boost::format fmt
81-
("Failed to enter %s as matrix. Reenter as [[val11 val12 ... val1N]...[valM1 valM2 ... valMN]]. Check that vector sizes are consistent.");
82-
fmt %iss.str();
83-
84-
if(iss>> _ch && _ch != '['){
85-
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
86-
}
87-
else{
88-
dynamicgraph::Vector _eigvec;
89-
while(iss >> _eigvec && !iss.fail()){
90-
if (!_vec_size_set) {
91-
try {
92-
_vec_size = boost::numeric_cast <int> (_eigvec.size());
93-
}
94-
catch (boost::bad_numeric_cast&) {
95-
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
96-
}
97-
_vec_size_set = true;
98-
}
99-
else {
100-
if (_eigvec.size() != _vec_size) {
101-
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
102-
}
103-
}
104-
_stdmat.push_back(_eigvec);
105-
}
106-
if(iss>> _ch && _ch != ']'){
86+
DenseBase<Derived> &inst) {
87+
unsigned int _colsize;
88+
unsigned int _rowsize;
89+
double _dbl_val;
90+
char _ch;
91+
boost::format fmt ("Failed to enter %s as vector. Reenter as [N](val1,val2,val3,...,valN)");
92+
fmt %iss.str();
93+
if(iss>> _ch && _ch != '['){
94+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
95+
}
96+
else {
97+
iss >>_rowsize;
98+
if (iss.peek() == ',' || iss.peek() == ' ')
99+
iss.ignore();
100+
iss >> _colsize;
101+
if (iss.fail())
102+
throw ExceptionSignal(ExceptionSignal::GENERIC,fmt.str());
103+
else {
104+
inst.resize(_rowsize,_colsize);
105+
if(iss >> _ch && _ch != ']')
107106
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
108-
}
109107
else {
110-
try {
111-
inst.resize(boost::numeric_cast<eigen_index> (_stdmat.size()), _vec_size);
112-
}
113-
catch (boost::bad_numeric_cast&) {
108+
if(iss>> _ch && _ch != '(')
114109
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
115-
}
116-
for (unsigned int i =0;i<_stdmat.size(); i++) {
117-
inst.row(i) = _stdmat[i];
110+
else {
111+
for (unsigned int j=0;j<_rowsize; j++){
112+
if(iss>> _ch && _ch != '(')
113+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
114+
for (unsigned int i=0;i<_colsize; i++){
115+
iss >> _dbl_val;
116+
if (iss.peek() == ',' || iss.peek() == ' ')
117+
iss.ignore();
118+
inst(j,i) = _dbl_val;
119+
}
120+
if(iss >> _ch && _ch != ')')
121+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
122+
if (iss.peek() == ',' || iss.peek() == ' ')
123+
iss.ignore();
124+
}
125+
if(iss >> _ch && _ch != ')')
126+
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
118127
}
119128
}
120129
}
121-
return iss;
122130
}
131+
return iss;
132+
}
133+
134+
135+
inline std::istringstream& operator >> (std::istringstream &iss,
136+
Transform<double,3,Affine> &inst) {
137+
Matrix4d M; iss >> M; inst = M; return iss; }
138+
139+
140+
141+
inline std::ostream& operator << (std::ostream &os,
142+
Transform<double,3,Affine> MH) {
143+
os << MH.matrix(); return os; }
144+
145+
146+
inline std::ostream& operator << (std::ostream &os,
147+
AngleAxisd quat) {
148+
Vector4d v; v(0) = quat.angle(); v.tail<3>() = quat.axis();
149+
os << v; return os; }
150+
151+
inline std::istringstream& operator >> (std::istringstream &iss,
152+
AngleAxisd &inst) {
153+
Vector4d v; iss >>v;
154+
inst.angle() = v(0); inst.axis() = v.tail<3>();
155+
return iss; }
156+
123157
}
124158

125159

tests/signal-cast-registerer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE (custom_vector_registerer)
149149
Vector v = Vector::Unit(5,i) ;
150150
std::ostringstream os;
151151
os << v;
152-
std::istringstream ss (os.str ());
152+
std::istringstream ss ("[5]("+os.str ()+")");
153153

154154
// Set signal value.
155155
myVectorSignal.set (ss);

0 commit comments

Comments
 (0)