47 #ifndef _MIRA_EIGENFORMAT_H_ 48 #define _MIRA_EIGENFORMAT_H_ 51 #include <boost/algorithm/string/trim.hpp> 56 #include <Eigen/Eigen> 57 #include <Eigen/StdVector> 98 template <
typename MatrixType>
99 struct EigenFormatSizeHelper
101 static bool checkOrResize(MatrixType& matrix,
int rows,
int cols)
104 return (matrix.rows() == rows && matrix.cols() == cols);
109 template<
typename Scalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
110 struct EigenFormatSizeHelper<
Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
114 static bool checkOrResize(MatrixType& matrix,
int rows,
int cols)
116 Derived& m =
static_cast<Derived&
>(matrix);
117 if (Derived::RowsAtCompileTime == Eigen::Dynamic ||
118 Derived::ColsAtCompileTime == Eigen::Dynamic )
119 m.resize(rows, cols);
120 return (matrix.rows() == rows && matrix.cols() == cols);
133 template <
typename Derived>
159 static_assert(
sizeof(Derived)==0,
"operator>> is not supported for read-only matrices");
165 void print(std::ostream& os)
const 172 const typename Derived::Nested m =
mMatrix;
173 typedef typename Derived::Scalar Scalar;
174 typedef typename Derived::Index Index;
186 bool alignCols = !(
mFormat.
flags & Eigen::DontAlignCols);
189 for (Index j = 1; j < m.cols(); ++j)
190 for (Index i = 0; i < m.rows(); ++i) {
191 std::stringstream sstr;
192 NumericalOstream nos(sstr);
195 nos << m.coeff(i, j);
196 width = std::max<Index>(width, Index(sstr.str().length()));
200 NumericalOstream nos(os);
201 std::streamsize oldprecision = 0;
205 for(Index i = 0; i < m.rows(); ++i) {
211 nos << m.coeff(i, 0);
212 for(Index j = 1; j < m.cols(); ++j) {
216 nos << m.coeff(i, j);
224 os.precision(oldprecision);
241 template <
typename Derived>
264 void parse(std::istream& is)
266 typedef typename Eigen::internal::traits< Derived >::Scalar Scalar;
271 std::vector<Scalar> values;
289 if(rowSuffix.empty() && coeffSeparator.empty()) {
290 std::swap(rowSuffix, rowSeparator);
293 assert(rowSuffix != coeffSeparator);
300 match(is, matPrefix);
308 if(
match(is, rowPrefix, matSuffix)==2 || is.eof())
316 values.push_back(val);
322 if(
match(is, coeffSeparator, rowSuffix)==2 || is.eof()) {
331 MIRA_THROW(XIO,
"Invalid number of columns, expected " 332 << cols <<
" but got " << col);
338 if(
match(is, rowSeparator, matSuffix)==2 || is.eof())
356 if(!EigenFormatSizeHelper<Derived>::checkOrResize(this->
mMatrix,rows,cols))
357 MIRA_THROW(XIO,
"The size of the given matix (" 359 "does not match the size of the read matrix (" 360 << rows <<
"x" << cols <<
")");
364 for(
int i=0; i<rows; ++i)
365 for(
int j=0; j<cols; ++j, ++idx)
366 this->
mMatrix(i,j) = values[idx];
373 static std::string
getToken(std::istream& is, std::size_t maxlength=255,
374 char keepwhitespace=0)
384 if(!isspace(ch) || ch==keepwhitespace)
394 token.push_back(is.get());
397 for(std::size_t i=1; i<maxlength && !is.eof() && !isspace(is.peek()); ++i)
398 token.push_back(is.get());
403 static void putback(std::istream& is,
int num)
405 for(
int i=0; i<num; ++i)
411 static int match(std::istream& is,
const std::string str1,
const std::string str2)
413 std::size_t len = std::max(str1.size(), str2.size());
421 if(str1.size()==1 && isspace(str1[0]))
425 if(str2.size()==1 && isspace(str2[0]))
429 assert(whitespace1==0 || whitespace2==0);
434 whitespace=whitespace1;
435 else if(whitespace2!=0)
436 whitespace=whitespace2;
438 std::string token =
getToken(is, len, whitespace);
440 bool matches1st = (token.substr(0,str1.size()) == str1);
441 bool matches2nd = (token.substr(0,str2.size()) == str2);
444 if(matches1st && matches2nd) {
445 if(str1.size()>=str2.size())
451 if(matches1st && !matches2nd) {
454 if(str1.size() < token.size())
455 putback(is, token.size()-str1.size());
459 if(matches2nd && !matches1st) {
461 if(str2.size() < token.size())
462 putback(is, token.size()-str2.size());
467 MIRA_THROW(XIO,
"Expected either '" << str1 <<
"' or '" << str2
468 <<
"' in the input stream");
473 static void match(std::istream& is,
const std::string str)
479 if(str.size()==1 && isspace(str[0]))
482 std::string token =
getToken(is, str.size(), whitespace);
488 MIRA_THROW(XIO,
"Expected '" << str <<
"' in the input stream");
521 template <
typename Derived>
531 template <
typename Derived>
TEigenFormat< Derived > format(Eigen::MatrixBase< Derived > &matrix, Eigen::IOFormat format=EigenFormat::matlab())
Function for formatting an Eigen matrix using a special format.
Definition: EigenFormat.h:522
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Definition: YawPitchRoll.h:684
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
Commonly used exception classes.
Numerical stream adapter that can be assigned to any input stream and allows streaming of numerical v...
Definition: NumericalStream.h:186
PropertyHint precision(int p)
Sets the attribute "precision".
Definition: PropertyHint.h:285