eigenDiskImport.h 2.57 KB
Newer Older
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
1
2
3
4
5
6
7
8
9
10
/*
 * eigenDiskImport.h
 *
 *  Created on: 18.04.2017
 *      Author: Yaro
 */

#ifndef EIGENDISKIMPORT_H_
#define EIGENDISKIMPORT_H_

11
#include <pinkIndexer/BadInputException.h>
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
12
13
#include <Eigen/Dense>
#include <algorithm>
14
15
#include <fstream>
#include <iterator>
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
16
#include <string.h>
17
#include <vector>
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
18

19
namespace pinkIndexer
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
20
{
21
22
23
24
25
26
27
28
29
30
31
    // matrix must be whitespace separated
    template <typename T>
    void loadEigenMatrixFromDisk(Eigen::DenseBase<T>& matrix, std::string path)
    {
        std::ifstream file(path);
        if (!file.is_open())
        {
            std::stringstream errStream;
            errStream << "File " << path << " not found.";
            throw BadInputException(errStream.str());
        }
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
32

33
34
        std::istream_iterator<typename T::RealScalar> startFile(file), end;
        std::vector<typename T::RealScalar> numbers(startFile, end);
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
35

36
37
38
39
40
41
        file.clear();
        file.seekg(std::ios::beg);
        std::string firstLine;
        getline(file, firstLine);
        std::istringstream iss(firstLine);
        std::vector<typename T::RealScalar> firstLineNumbers(std::istream_iterator<typename T::RealScalar>(iss), end);
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
42

43
44
        int cols = (int)firstLineNumbers.size();
        int rows = (int)numbers.size() / cols;
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
45

46
47
        bool constexpr checkDynamicRows = T::RowsAtCompileTime != Eigen::Dynamic;
        bool constexpr checkDynamicCols = T::ColsAtCompileTime != Eigen::Dynamic;
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        if (checkDynamicRows)
        {
            if (T::RowsAtCompileTime != rows)
            {
                std::stringstream errStream;
                errStream << "Matrix in file " << path << " contains wrong number of rows";
                throw BadInputException(errStream.str());
            }
        }
        if (checkDynamicCols)
        {
            if (T::ColsAtCompileTime != cols)
            {
                std::stringstream errStream;
                errStream << "Matrix in file " << path << " contains wrong number of columns";
                throw BadInputException(errStream.str());
            }
        }
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
67

68
69
70
71
72
73
        if ((int)numbers.size() != rows * cols)
        {
            std::stringstream errStream;
            errStream << "Matrix in file " << path << " contains a non rectangular matrix";
            throw BadInputException(errStream.str());
        }
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
74

75
76
77
78
79
        Eigen::Array<typename T::RealScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> tmp =
            Eigen::Map<Eigen::Array<typename T::RealScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(numbers.data(), rows, cols);
        matrix = tmp;
    }
} // namespace pinkIndexer
Yaroslav Gevorkov's avatar
Yaroslav Gevorkov committed
80
81

#endif /* EIGENDISKIMPORT_H_ */