DMP_BBO library
EigenFileIO.tpp
Go to the documentation of this file.
1 
32 template<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
33 inline bool loadMatrix(std::string filename, Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime>& m)
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 }
74 
82 template<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
83 inline bool saveMatrix(std::string directory, std::string filename, Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> matrix, bool overwrite)
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 }
104 
111 template<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
112 inline bool saveMatrix(std::string filename, Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> matrix, bool overwrite)
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 }
140 
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 loadMatrix(std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > &m)
Load an Eigen matrix from an ASCII file.
Definition: EigenFileIO.tpp:33