v2.0.0
Loading...
Searching...
No Matches
ioutils.h
Go to the documentation of this file.
1//=============================================================================================================
35
36#ifndef IOUTILS_H
37#define IOUTILS_H
38
39//=============================================================================================================
40// INCLUDES
41//=============================================================================================================
42
43#include "utils_global.h"
44#include <ios>
45#include <string>
46#include <sstream>
47#include <vector>
48#include <fstream>
49
50//=============================================================================================================
51// QT INCLUDES
52//=============================================================================================================
53
54#include <QSharedPointer>
55#include <QTextStream>
56#include <QFile>
57#include <QDebug>
58#include <QStringList>
59#include <QRegularExpression>
60
61//=============================================================================================================
62// QT INCLUDES
63//=============================================================================================================
64
65#include <Eigen/Core>
66
67//=============================================================================================================
68// DEFINE NAMESPACE UTILSLIB
69//=============================================================================================================
70
71namespace UTILSLIB
72{
73
74//=============================================================================================================
75// FORWARD DECLARATIONS
76//=============================================================================================================
77
78//=============================================================================================================
85{
86public:
87 typedef QSharedPointer<IOUtils> SPtr;
88 typedef QSharedPointer<const IOUtils> ConstSPtr;
89
90 //=========================================================================================================
95
96 //=========================================================================================================
106 static qint32 fread3(QDataStream &p_qStream);
107
108 //=========================================================================================================
118 static qint32 fread3(std::iostream& stream);
119
120 //=========================================================================================================
131 static Eigen::VectorXi fread3_many(QDataStream &p_qStream, qint32 count);
132
133 //=========================================================================================================
144 static Eigen::VectorXi fread3_many(std::iostream &stream, qint32 count);
145
146 //=========================================================================================================
154 static qint16 swap_short (qint16 source);
155
156 //=========================================================================================================
164 static qint32 swap_int (qint32 source);
165
166 //=========================================================================================================
172 static void swap_intp (qint32 *source);
173
174 //=========================================================================================================
182 static qint64 swap_long (qint64 source);
183
184 //=========================================================================================================
190 static void swap_longp (qint64 *source);
191
192 //=========================================================================================================
200 static float swap_float (float source);
201
202 //=========================================================================================================
208 static void swap_floatp (float *source);
209
210 //=========================================================================================================
216 static void swap_doublep(double *source);
217
218 //=========================================================================================================
225 template<typename T>
226 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
227 template<typename T>
228 static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
229 template<typename T>
230 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription = QString());
231
232 //=========================================================================================================
239 template<typename T>
240 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
241 template<typename T>
242 static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
243 template<typename T>
244 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription = std::string());
245
246 //=========================================================================================================
253 template<typename T>
254 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path);
255 template<typename T>
256 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path);
257 template<typename T>
258 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path);
259
260 //=========================================================================================================
267 template<typename T>
268 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path);
269 template<typename T>
270 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path);
271 template<typename T>
272 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path);
273
274 //=========================================================================================================
282 static QStringList get_new_chnames_conventions(const QStringList& chNames);
283
284 //=========================================================================================================
292 static std::vector<std::string> get_new_chnames_conventions(const std::vector<std::string>& chNames);
293
294 //=========================================================================================================
302 static QStringList get_old_chnames_conventions(const QStringList& chNames);
303
304 //=========================================================================================================
312 static std::vector<std::string> get_old_chnames_conventions(const std::vector<std::string>& chNames);
313
314 //=========================================================================================================
324 static bool check_matching_chnames_conventions(const QStringList& chNamesA, const QStringList& chNamesB, bool bCheckForNewNamingConvention = false);
325
326 //=========================================================================================================
336 static bool check_matching_chnames_conventions(const std::vector<std::string>& chNamesA, const std::vector<std::string>& chNamesB, bool bCheckForNewNamingConvention = false);
337};
338
339//=============================================================================================================
340// INLINE DEFINITIONS
341//=============================================================================================================
342
343template<typename T>
344bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
345{
346 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
347 matrixName.row(0)= in;
348 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
349}
350
351//=============================================================================================================
352
353template<typename T>
354bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription)
355{
356 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
357 matrixName.col(0)= in;
358 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
359}
360
361//=============================================================================================================
362
363template<typename T>
364bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
365{
366 QFile file(sPath);
367 if(file.open(QIODevice::WriteOnly|QIODevice::Truncate))
368 {
369 QTextStream stream(&file);
370 if(!sDescription.isEmpty()) {
371 stream<<"# Dimensions (rows x cols): "<<in.rows()<<" x "<<in.cols()<<"\n";
372 stream<<"# Description: "<<sDescription<<"\n";
373 }
374
375 for(int row = 0; row<in.rows(); row++) {
376 for(int col = 0; col<in.cols(); col++)
377 stream << in(row, col)<<" ";
378 stream<<"\n";
379 }
380 } else {
381 qWarning()<<"Could not write Eigen element to file! Path does not exist!";
382 return false;
383 }
384
385 file.close();
386
387 return true;
388}
389
390//=============================================================================================================
391
392template<typename T>
393bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
394{
395 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
396 matrixName.row(0)= in;
397 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
398}
399
400//=============================================================================================================
401
402template<typename T>
403bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription)
404{
405 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
406 matrixName.col(0)= in;
407 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
408}
409
410//=============================================================================================================
411
412template<typename T>
413bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
414{
415 std::ofstream outputFile(sPath);
416 if(outputFile.is_open())
417 {
418 if(!sDescription.empty()) {
419 outputFile<<"# Dimensions (rows x cols): "<<in.rows()<<" x "<<in.cols()<<"\n";
420 outputFile<<"# Description: "<<sDescription<<"\n";
421 }
422
423 for(int row = 0; row<in.rows(); row++) {
424 for(int col = 0; col<in.cols(); col++)
425 outputFile << in(row, col)<<" ";
426 outputFile<<"\n";
427 }
428 } else {
429 qWarning()<<"Could not write Eigen element to file! Path does not exist!";
430 return false;
431 }
432
433 return true;
434}
435
436//=============================================================================================================
437
438template<typename T>
439bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path)
440{
441 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
442 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
443
444 if(matrixName.rows() > 0)
445 {
446 out = matrixName.row(0);
447 }
448
449 return bStatus;
450}
451
452//=============================================================================================================
453
454template<typename T>
455bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path)
456{
457 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
458 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
459
460 if(matrixName.cols() > 0)
461 {
462 out = matrixName.col(0);
463 }
464
465 return bStatus;
466}
467
468//=============================================================================================================
469
470template<typename T>
471bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path)
472{
473 QFile file(path);
474
475 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
476 //Start reading from file
477 QTextStream in(&file);
478 QList<Eigen::VectorXd> help;
479
480 while(!in.atEnd())
481 {
482 QString line = in.readLine();
483 if(!line.contains("#")) {
484 QStringList fields = line.split(QRegularExpression("\\s+"));
485
486 //Delete last element if it is a blank character
487 if(fields.at(fields.size()-1) == "")
488 fields.removeLast();
489
490 Eigen::VectorXd x (fields.size());
491
492 for (int j = 0; j<fields.size(); j++) {
493 x(j) = fields.at(j).toDouble();
494 }
495
496 help.append(x);
497 }
498 }
499
500 int rows = help.size();
501 int cols = rows <= 0 ? 0 : help.at(0).rows();
502
503 out.resize(rows, cols);
504
505 for (int i=0; i < help.length(); i++) {
506 out.row(i) = help[i].transpose();
507 }
508 } else {
509 qWarning()<<"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
510 return false;
511 }
512
513 return true;
514}
515
516//=============================================================================================================
517
518template<typename T>
519bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path)
520{
521 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
522 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
523
524 if(matrixName.rows() > 0)
525 {
526 out = matrixName.row(0);
527 }
528
529 return bStatus;
530}
531
532//=============================================================================================================
533
534template<typename T>
535bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path)
536{
537 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
538 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
539
540 if(matrixName.cols() > 0)
541 {
542 out = matrixName.col(0);
543 }
544
545 return bStatus;
546}
547
548//=============================================================================================================
549
550template<typename T>
551bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path)
552{
553 std::ifstream inputFile(path);
554
555 if(inputFile.is_open()) {
556 //Start reading from file
557 std::vector<Eigen::VectorXd> help;
558
559 std::string line;
560
561 while(std::getline(inputFile, line)){
562 if(line.find('#') == std::string::npos){
563 std::vector<double> elements;
564 std::stringstream stream{line};
565 std::string element;
566
567 stream >> std::ws;
568 while(stream >> element){
569 elements.push_back(std::stod(element));
570 stream >> std::ws;
571 }
572
573 Eigen::VectorXd x (elements.size());
574
575 for(size_t i = 0; i < elements.size(); ++i){
576 x(i) = elements.at(i);
577 }
578
579 help.push_back(std::move(x));
580 }
581 }
582
583 int rows = help.size();
584 int cols = rows <= 0 ? 0 : help.at(0).rows();
585
586 out.resize(rows, cols);
587
588 for (size_t i = 0; i < help.size(); i++) {
589 out.row(i) = help[i].transpose();
590 }
591 } else {
592 qWarning()<<"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
593 return false;
594 }
595
596 return true;
597}
598} // NAMESPACE
599
600#endif // IOUTILS_H
601
utils library export/import macros.
#define UTILSSHARED_EXPORT
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Definition buildinfo.h:45
IO utilitie routines.
Definition ioutils.h:85
static bool write_eigen_matrix(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &in, const QString &sPath, const QString &sDescription=QString())
Definition ioutils.h:364
QSharedPointer< IOUtils > SPtr
Definition ioutils.h:87
QSharedPointer< const IOUtils > ConstSPtr
Definition ioutils.h:88
static bool read_eigen_matrix(Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &out, const QString &path)
Definition ioutils.h:471