54 #include <QSharedPointer>
55 #include <QTextStream>
58 #include <QStringList>
59 #include <QRegularExpression>
97 typedef QSharedPointer<IOUtils>
SPtr;
116 static qint32 fread3(QDataStream &p_qStream);
128 static qint32 fread3(std::iostream& stream);
141 static Eigen::VectorXi fread3_many(QDataStream &p_qStream, qint32 count);
154 static Eigen::VectorXi fread3_many(std::iostream &stream, qint32 count);
164 static qint16 swap_short (qint16 source);
174 static qint32 swap_int (qint32 source);
182 static void swap_intp (qint32 *source);
192 static qint64 swap_long (qint64 source);
200 static void swap_longp (qint64 *source);
210 static float swap_float (
float source);
218 static void swap_floatp (
float *source);
226 static void swap_doublep(
double *source);
236 static bool write_eigen_matrix(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in,
const QString& sPath,
const QString& sDescription = QString());
238 static bool write_eigen_matrix(
const Eigen::Matrix<T, 1, Eigen::Dynamic>& in,
const QString& sPath,
const QString& sDescription = QString());
240 static bool write_eigen_matrix(
const Eigen::Matrix<T, Eigen::Dynamic, 1>& in,
const QString& sPath,
const QString& sDescription = QString());
250 static bool write_eigen_matrix(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in,
const std::string& sPath,
const std::string& sDescription = std::string());
252 static bool write_eigen_matrix(
const Eigen::Matrix<T, 1, Eigen::Dynamic>& in,
const std::string& sPath,
const std::string& sDescription = std::string());
254 static bool write_eigen_matrix(
const Eigen::Matrix<T, Eigen::Dynamic, 1>& in,
const std::string& sPath,
const std::string& sDescription = std::string());
264 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out,
const QString& path);
266 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out,
const QString& path);
268 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out,
const QString& path);
278 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out,
const std::string& path);
280 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out,
const std::string& path);
282 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out,
const std::string& path);
292 static QStringList get_new_chnames_conventions(
const QStringList& chNames);
302 static std::vector<std::string> get_new_chnames_conventions(
const std::vector<std::string>& chNames);
312 static QStringList get_old_chnames_conventions(
const QStringList& chNames);
322 static std::vector<std::string> get_old_chnames_conventions(
const std::vector<std::string>& chNames);
334 static bool check_matching_chnames_conventions(
const QStringList& chNamesA,
const QStringList& chNamesB,
bool bCheckForNewNamingConvention =
false);
346 static bool check_matching_chnames_conventions(
const std::vector<std::string>& chNamesA,
const std::vector<std::string>& chNamesB,
bool bCheckForNewNamingConvention =
false);
356 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
357 matrixName.row(0)= in;
366 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
367 matrixName.col(0)= in;
377 if(file.open(QIODevice::WriteOnly|QIODevice::Truncate))
379 QTextStream stream(&file);
380 if(!sDescription.isEmpty()) {
381 stream<<
"# Dimensions (rows x cols): "<<in.rows()<<
" x "<<in.cols()<<
"\n";
382 stream<<
"# Description: "<<sDescription<<
"\n";
385 for(
int row = 0; row<in.rows(); row++) {
386 for(
int col = 0; col<in.cols(); col++)
387 stream << in(row, col)<<
" ";
391 qWarning()<<
"Could not write Eigen element to file! Path does not exist!";
403 bool IOUtils::write_eigen_matrix(
const Eigen::Matrix<T, 1, Eigen::Dynamic>& in,
const std::string& sPath,
const std::string& sDescription)
405 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
406 matrixName.row(0)= in;
413 bool IOUtils::write_eigen_matrix(
const Eigen::Matrix<T, Eigen::Dynamic, 1>& in,
const std::string& sPath,
const std::string& sDescription)
415 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
416 matrixName.col(0)= in;
423 bool IOUtils::write_eigen_matrix(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in,
const std::string& sPath,
const std::string& sDescription)
425 std::ofstream outputFile(sPath);
426 if(outputFile.is_open())
428 if(!sDescription.empty()) {
429 outputFile<<
"# Dimensions (rows x cols): "<<in.rows()<<
" x "<<in.cols()<<
"\n";
430 outputFile<<
"# Description: "<<sDescription<<
"\n";
433 for(
int row = 0; row<in.rows(); row++) {
434 for(
int col = 0; col<in.cols(); col++)
435 outputFile << in(row, col)<<
" ";
439 qWarning()<<
"Could not write Eigen element to file! Path does not exist!";
451 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
454 if(matrixName.rows() > 0)
456 out = matrixName.row(0);
467 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
470 if(matrixName.cols() > 0)
472 out = matrixName.col(0);
485 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
487 QTextStream in(&file);
488 QList<Eigen::VectorXd> help;
492 QString line = in.readLine();
493 if(!line.contains(
"#")) {
494 QStringList fields = line.split(QRegularExpression(
"\\s+"));
497 if(fields.at(fields.size()-1) ==
"")
500 Eigen::VectorXd x (fields.size());
502 for (
int j = 0; j<fields.size(); j++) {
503 x(j) = fields.at(j).toDouble();
510 int rows = help.size();
511 int cols = rows <= 0 ? 0 : help.at(0).rows();
513 out.resize(rows, cols);
515 for (
int i=0; i < help.length(); i++) {
516 out.row(i) = help[i].transpose();
519 qWarning()<<
"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
531 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
534 if(matrixName.rows() > 0)
536 out = matrixName.row(0);
547 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
550 if(matrixName.cols() > 0)
552 out = matrixName.col(0);
563 std::ifstream inputFile(path);
565 if(inputFile.is_open()) {
567 std::vector<Eigen::VectorXd> help;
571 while(std::getline(inputFile, line)){
572 if(line.find(
'#') == std::string::npos){
573 std::vector<double> elements;
574 std::stringstream stream{line};
578 while(stream >> element){
579 elements.push_back(std::stod(element));
583 Eigen::VectorXd x (elements.size());
585 for(
size_t i = 0; i < elements.size(); ++i){
586 x(i) = elements.at(i);
589 help.push_back(std::move(x));
593 int rows = help.size();
594 int cols = rows <= 0 ? 0 : help.at(0).rows();
596 out.resize(rows, cols);
598 for (
size_t i = 0; i < help.size(); i++) {
599 out.row(i) = help[i].transpose();
602 qWarning()<<
"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";