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//=============================================================================================================
81{
82public:
83 typedef QSharedPointer<IOUtils> SPtr;
84 typedef QSharedPointer<const IOUtils> ConstSPtr;
85
87
88 //=========================================================================================================
98 template<typename T>
99 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
100 template<typename T>
101 static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
102 template<typename T>
103 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription = QString());
104
105 //=========================================================================================================
115 template<typename T>
116 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
117 template<typename T>
118 static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
119 template<typename T>
120 static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription = std::string());
121
122 //=========================================================================================================
131 template<typename T>
132 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path);
133 template<typename T>
134 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path);
135 template<typename T>
136 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path);
137
138 //=========================================================================================================
147 template<typename T>
148 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path);
149 template<typename T>
150 static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path);
151 template<typename T>
152 static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path);
153};
154
155//=============================================================================================================
156// INLINE DEFINITIONS
157//=============================================================================================================
158
159template<typename T>
160bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
161{
162 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1, in.cols());
163 matrixName.row(0) = in;
164 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
165}
166
167//=============================================================================================================
168
169template<typename T>
170bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription)
171{
172 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(), 1);
173 matrixName.col(0) = in;
174 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
175}
176
177//=============================================================================================================
178
179template<typename T>
180bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
181{
182 QFile file(sPath);
183 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
184 QTextStream stream(&file);
185 if (!sDescription.isEmpty()) {
186 stream << "# Dimensions (rows x cols): " << in.rows() << " x " << in.cols() << "\n";
187 stream << "# Description: " << sDescription << "\n";
188 }
189
190 for (int row = 0; row < in.rows(); row++) {
191 for (int col = 0; col < in.cols(); col++)
192 stream << in(row, col) << " ";
193 stream << "\n";
194 }
195 } else {
196 qWarning() << "Could not write Eigen element to file! Path does not exist!";
197 return false;
198 }
199
200 file.close();
201
202 return true;
203}
204
205//=============================================================================================================
206
207template<typename T>
208bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
209{
210 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1, in.cols());
211 matrixName.row(0) = in;
212 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
213}
214
215//=============================================================================================================
216
217template<typename T>
218bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription)
219{
220 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(), 1);
221 matrixName.col(0) = in;
222 return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
223}
224
225//=============================================================================================================
226
227template<typename T>
228bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
229{
230 std::ofstream outputFile(sPath);
231 if (outputFile.is_open()) {
232 if (!sDescription.empty()) {
233 outputFile << "# Dimensions (rows x cols): " << in.rows() << " x " << in.cols() << "\n";
234 outputFile << "# Description: " << sDescription << "\n";
235 }
236
237 for (int row = 0; row < in.rows(); row++) {
238 for (int col = 0; col < in.cols(); col++)
239 outputFile << in(row, col) << " ";
240 outputFile << "\n";
241 }
242 } else {
243 qWarning() << "Could not write Eigen element to file! Path does not exist!";
244 return false;
245 }
246
247 return true;
248}
249
250//=============================================================================================================
251
252template<typename T>
253bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path)
254{
255 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
256 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
257
258 if (matrixName.rows() > 0) {
259 out = matrixName.row(0);
260 }
261
262 return bStatus;
263}
264
265//=============================================================================================================
266
267template<typename T>
268bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path)
269{
270 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
271 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
272
273 if (matrixName.cols() > 0) {
274 out = matrixName.col(0);
275 }
276
277 return bStatus;
278}
279
280//=============================================================================================================
281
282template<typename T>
283bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path)
284{
285 QFile file(path);
286
287 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
288 //Start reading from file
289 QTextStream in(&file);
290 QList<Eigen::VectorXd> help;
291
292 while (!in.atEnd()) {
293 QString line = in.readLine();
294 if (!line.contains("#")) {
295 QStringList fields = line.split(QRegularExpression("\\s+"));
296
297 //Delete last element if it is a blank character
298 if (fields.at(fields.size() - 1) == "")
299 fields.removeLast();
300
301 Eigen::VectorXd x(fields.size());
302
303 for (int j = 0; j < fields.size(); j++) {
304 x(j) = fields.at(j).toDouble();
305 }
306
307 help.append(x);
308 }
309 }
310
311 int rows = static_cast<int>(help.size());
312 int cols = rows <= 0 ? 0 : static_cast<int>(help.at(0).rows());
313
314 out.resize(rows, cols);
315
316 for (int i = 0; i < help.length(); i++) {
317 out.row(i) = help[i].transpose();
318 }
319 } else {
320 qWarning() << "IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
321 return false;
322 }
323
324 return true;
325}
326
327//=============================================================================================================
328
329template<typename T>
330bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path)
331{
332 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
333 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
334
335 if (matrixName.rows() > 0) {
336 out = matrixName.row(0);
337 }
338
339 return bStatus;
340}
341
342//=============================================================================================================
343
344template<typename T>
345bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path)
346{
347 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
348 bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
349
350 if (matrixName.cols() > 0) {
351 out = matrixName.col(0);
352 }
353
354 return bStatus;
355}
356
357//=============================================================================================================
358
359template<typename T>
360bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path)
361{
362 std::ifstream inputFile(path);
363
364 if (inputFile.is_open()) {
365 //Start reading from file
366 std::vector<Eigen::VectorXd> help;
367
368 std::string line;
369
370 while (std::getline(inputFile, line)) {
371 if (line.find('#') == std::string::npos) {
372 std::vector<double> elements;
373 std::stringstream stream{line};
374 std::string element;
375
376 stream >> std::ws;
377 while (stream >> element) {
378 elements.push_back(std::stod(element));
379 stream >> std::ws;
380 }
381
382 Eigen::VectorXd x(elements.size());
383
384 for (size_t i = 0; i < elements.size(); ++i) {
385 x(i) = elements.at(i);
386 }
387
388 help.push_back(std::move(x));
389 }
390 }
391
392 int rows = static_cast<int>(help.size());
393 int cols = rows <= 0 ? 0 : static_cast<int>(help.at(0).rows());
394
395 out.resize(rows, cols);
396
397 for (size_t i = 0; i < help.size(); i++) {
398 out.row(static_cast<Eigen::Index>(i)) = help[i].transpose();
399 }
400 } else {
401 qWarning() << "IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
402 return false;
403 }
404
405 return true;
406}
407} // NAMESPACE
408
409#endif // IOUTILS_H
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:81
static bool write_eigen_matrix(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &in, const QString &sPath, const QString &sDescription=QString())
Definition ioutils.h:180
QSharedPointer< IOUtils > SPtr
Definition ioutils.h:83
QSharedPointer< const IOUtils > ConstSPtr
Definition ioutils.h:84
static bool read_eigen_matrix(Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &out, const QString &path)
Definition ioutils.h:283