DMP_BBO library
Functions
EigenFileIO.tpp File Reference

Source file for input/output of Eigen matrices to ASCII files. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename Scalar , int RowsAtCompileTime, int ColsAtCompileTime>
bool loadMatrix (std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > &m)
 Load an Eigen matrix from an ASCII file. More...
 
template<typename Scalar , int RowsAtCompileTime, int ColsAtCompileTime>
bool saveMatrix (std::string directory, std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > matrix, bool overwrite)
 Save an Eigen matrix to an ASCII file. More...
 
template<typename Scalar , int RowsAtCompileTime, int ColsAtCompileTime>
bool saveMatrix (std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > matrix, bool overwrite)
 Save an Eigen matrix to an ASCII file. More...
 

Detailed Description

Source file for input/output of Eigen matrices to ASCII files.

Author
Freek Stulp

Implementations are in the tpp file

This file is part of DmpBbo, a set of libraries and programs for the black-box optimization of dynamical movement primitives. Copyright (C) 2014 Freek Stulp, ENSTA-ParisTech

DmpBbo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

DmpBbo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with DmpBbo. If not, see http://www.gnu.org/licenses/.

Definition in file EigenFileIO.tpp.

Function Documentation

bool loadMatrix ( std::string  filename,
Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > &  m 
)
inline

Load an Eigen matrix from an ASCII file.

Parameters
[in]filenameName of the file from which to read the matrix
[out]mThe matrix that was read from file
Returns
true if loading was successful, false otherwise

Definition at line 33 of file EigenFileIO.tpp.

34 {
35 
36  // General structure
37  // 1. Read file contents into vector<double> and count number of lines
38  // 2. Initialize matrix
39  // 3. Put data in vector<double> into matrix
40 
41  std::ifstream input(filename.c_str());
42  if (input.fail())
43  {
44  std::cerr << "ERROR. Cannot find file '" << filename << "'." << std::endl;
45  m = Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime>(0,0);
46  return false;
47  }
48  std::string line;
49  Scalar d;
50 
51  std::vector<Scalar> v;
52  int n_rows = 0;
53  while (getline(input, line))
54  {
55  ++n_rows;
56  std::stringstream input_line(line);
57  while (!input_line.eof())
58  {
59  input_line >> d;
60  v.push_back(d);
61  }
62  }
63  input.close();
64 
65  int n_cols = v.size()/n_rows;
66  m = Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime>(n_rows,n_cols);
67 
68  for (int i=0; i<n_rows; i++)
69  for (int j=0; j<n_cols; j++)
70  m(i,j) = v[i*n_cols + j];
71 
72  return true;
73 }
bool saveMatrix ( std::string  directory,
std::string  filename,
Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime >  matrix,
bool  overwrite 
)
inline

Save an Eigen matrix to an ASCII file.

Parameters
[in]directoryName of the directory to which to save the matrix
[in]filenameName of the file to which to save the matrix
[in]matrixThe matrix to save to file
[in]overwriteWhether to overwrite any existing files
Returns
true if saving was successful, false otherwise

Definition at line 83 of file EigenFileIO.tpp.

84 {
85  if (directory.empty())
86  return false;
87 
88 
89  if (!boost::filesystem::exists(directory))
90  {
91  // Directory doesn't exist. Try to create it.
92  if (!boost::filesystem::create_directories(directory))
93  {
94  std::cerr << "Couldn't make directory '" << directory << "'. Not saving data." << std::endl;
95  return false;
96  }
97  }
98 
99  filename = directory+"/"+filename;
100  saveMatrix(filename,matrix,overwrite);
101 
102  return true;
103 }
bool saveMatrix(std::string directory, std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > matrix, bool overwrite)
Save an Eigen matrix to an ASCII file.
Definition: EigenFileIO.tpp:83
bool saveMatrix ( std::string  filename,
Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime >  matrix,
bool  overwrite 
)
inline

Save an Eigen matrix to an ASCII file.

Parameters
[in]filenameName of the file to which to save the matrix
[in]matrixThe matrix to save to file
[in]overwriteWhether to overwrite any existing files
Returns
true if saving was successful, false otherwise

Definition at line 112 of file EigenFileIO.tpp.

113 {
114  if (boost::filesystem::exists(filename))
115  {
116  if (!overwrite)
117  {
118  // File exists, but overwriting is not allowed. Abort.
119  std::cerr << "File '" << filename << "' already exists. Not saving data." << std::endl;
120  return false;
121  }
122  }
123 
124 
125  std::ofstream file;
126  file.open(filename.c_str());
127  if (!file.is_open())
128  {
129  std::cerr << "Couldn't open file '" << filename << "' for writing." << std::endl;
130  return false;
131  }
132 
133  file << std::fixed;
134  file << matrix;
135  file.close();
136 
137  return true;
138 
139 }