v2.0.0
Loading...
Searching...
No Matches
ioutils.h
Go to the documentation of this file.
1//=============================================================================================================
34
35#ifndef IOUTILS_H
36#define IOUTILS_H
37
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "utils_global.h"
43#include <string>
44#include <sstream>
45#include <vector>
46#include <fstream>
47
48//=============================================================================================================
49// QT INCLUDES
50//=============================================================================================================
51
52#include <QSharedPointer>
53#include <QTextStream>
54#include <QFile>
55#include <QDebug>
56#include <QStringList>
57#include <QRegularExpression>
58
59//=============================================================================================================
60// EIGEN INCLUDES
61//=============================================================================================================
62
63#include <Eigen/Core>
64
65//=============================================================================================================
66// DEFINE NAMESPACE UTILSLIB
67//=============================================================================================================
68
69namespace UTILSLIB
70{
71
72//=============================================================================================================
79{
80public:
81 typedef QSharedPointer<IOUtils> SPtr;
82 typedef QSharedPointer<const IOUtils> ConstSPtr;
83
85
86 //=========================================================================================================
90 template<typename T>
91 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
92 template<typename T>
93 static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
94 template<typename T>
95 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription = QString());
96
97 //=========================================================================================================
101 template<typename T>
102 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
103 template<typename T>
104 static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
105 template<typename T>
106 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription = std::string());
107
108 //=========================================================================================================
112 template<typename T>
113 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path);
114 template<typename T>
115 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path);
116 template<typename T>
117 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path);
118
119 //=========================================================================================================
123 template<typename T>
124 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path);
125 template<typename T>
126 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path);
127 template<typename T>
128 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path);
129};
130
131//=============================================================================================================
132// INLINE DEFINITIONS
133//=============================================================================================================
134
135template<typename T>
136bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
137{
138 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
139 matrixName.row(0)= in;
140 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
141}
142
143//=============================================================================================================
144
145template<typename T>
146bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription)
147{
148 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
149 matrixName.col(0)= in;
150 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
151}
152
153//=============================================================================================================
154
155template<typename T>
156bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
157{
158 QFile file(sPath);
159 if(file.open(QIODevice::WriteOnly|QIODevice::Truncate))
160 {
161 QTextStream stream(&file);
162 if(!sDescription.isEmpty()) {
163 stream<<"# Dimensions (rows x cols): "<<in.rows()<<" x "<<in.cols()<<"\n";
164 stream<<"# Description: "<<sDescription<<"\n";
165 }
166
167 for(int row = 0; row<in.rows(); row++) {
168 for(int col = 0; col<in.cols(); col++)
169 stream << in(row, col)<<" ";
170 stream<<"\n";
171 }
172 } else {
173 qWarning()<<"Could not write Eigen element to file! Path does not exist!";
174 return false;
175 }
176
177 file.close();
178
179 return true;
180}
181
182//=============================================================================================================
183
184template<typename T>
185bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
186{
187 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
188 matrixName.row(0)= in;
189 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
190}
191
192//=============================================================================================================
193
194template<typename T>
195bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription)
196{
197 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
198 matrixName.col(0)= in;
199 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
200}
201
202//=============================================================================================================
203
204template<typename T>
205bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
206{
207 std::ofstream outputFile(sPath);
208 if(outputFile.is_open())
209 {
210 if(!sDescription.empty()) {
211 outputFile<<"# Dimensions (rows x cols): "<<in.rows()<<" x "<<in.cols()<<"\n";
212 outputFile<<"# Description: "<<sDescription<<"\n";
213 }
214
215 for(int row = 0; row<in.rows(); row++) {
216 for(int col = 0; col<in.cols(); col++)
217 outputFile << in(row, col)<<" ";
218 outputFile<<"\n";
219 }
220 } else {
221 qWarning()<<"Could not write Eigen element to file! Path does not exist!";
222 return false;
223 }
224
225 return true;
226}
227
228//=============================================================================================================
229
230template<typename T>
231bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path)
232{
233 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
234 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
235
236 if(matrixName.rows() > 0)
237 {
238 out = matrixName.row(0);
239 }
240
241 return bStatus;
242}
243
244//=============================================================================================================
245
246template<typename T>
247bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path)
248{
249 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
250 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
251
252 if(matrixName.cols() > 0)
253 {
254 out = matrixName.col(0);
255 }
256
257 return bStatus;
258}
259
260//=============================================================================================================
261
262template<typename T>
263bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path)
264{
265 QFile file(path);
266
267 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
268 //Start reading from file
269 QTextStream in(&file);
270 QList<Eigen::VectorXd> help;
271
272 while(!in.atEnd())
273 {
274 QString line = in.readLine();
275 if(!line.contains("#")) {
276 QStringList fields = line.split(QRegularExpression("\\s+"));
277
278 //Delete last element if it is a blank character
279 if(fields.at(fields.size()-1) == "")
280 fields.removeLast();
281
282 Eigen::VectorXd x (fields.size());
283
284 for (int j = 0; j<fields.size(); j++) {
285 x(j) = fields.at(j).toDouble();
286 }
287
288 help.append(x);
289 }
290 }
291
292 int rows = help.size();
293 int cols = rows <= 0 ? 0 : help.at(0).rows();
294
295 out.resize(rows, cols);
296
297 for (int i=0; i < help.length(); i++) {
298 out.row(i) = help[i].transpose();
299 }
300 } else {
301 qWarning()<<"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
302 return false;
303 }
304
305 return true;
306}
307
308//=============================================================================================================
309
310template<typename T>
311bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path)
312{
313 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
314 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
315
316 if(matrixName.rows() > 0)
317 {
318 out = matrixName.row(0);
319 }
320
321 return bStatus;
322}
323
324//=============================================================================================================
325
326template<typename T>
327bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path)
328{
329 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
330 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
331
332 if(matrixName.cols() > 0)
333 {
334 out = matrixName.col(0);
335 }
336
337 return bStatus;
338}
339
340//=============================================================================================================
341
342template<typename T>
343bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path)
344{
345 std::ifstream inputFile(path);
346
347 if(inputFile.is_open()) {
348 //Start reading from file
349 std::vector<Eigen::VectorXd> help;
350
351 std::string line;
352
353 while(std::getline(inputFile, line)){
354 if(line.find('#') == std::string::npos){
355 std::vector<double> elements;
356 std::stringstream stream{line};
357 std::string element;
358
359 stream >> std::ws;
360 while(stream >> element){
361 elements.push_back(std::stod(element));
362 stream >> std::ws;
363 }
364
365 Eigen::VectorXd x (elements.size());
366
367 for(size_t i = 0; i < elements.size(); ++i){
368 x(i) = elements.at(i);
369 }
370
371 help.push_back(std::move(x));
372 }
373 }
374
375 int rows = help.size();
376 int cols = rows <= 0 ? 0 : help.at(0).rows();
377
378 out.resize(rows, cols);
379
380 for (size_t i = 0; i < help.size(); i++) {
381 out.row(i) = help[i].transpose();
382 }
383 } else {
384 qWarning()<<"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
385 return false;
386 }
387
388 return true;
389}
390} // NAMESPACE
391
392#endif // IOUTILS_H
393
Public umbrella header for UTILSLIB exposing the UTILSSHARED_EXPORT macro and build-info accessors.
#define UTILSSHARED_EXPORT
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Eigen matrix I/O utilities.
Definition ioutils.h:79
static bool write_eigen_matrix(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &in, const QString &sPath, const QString &sDescription=QString())
Definition ioutils.h:156
QSharedPointer< IOUtils > SPtr
Definition ioutils.h:81
QSharedPointer< const IOUtils > ConstSPtr
Definition ioutils.h:82
static bool read_eigen_matrix(Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &out, const QString &path)
Definition ioutils.h:263