Skip to content

Commit 3550076

Browse files
committed
RawFileIO.cpp added
1 parent 60db1ae commit 3550076

2 files changed

Lines changed: 391 additions & 0 deletions

File tree

src/utils/RawFileIO.cpp

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
#include "RawFileIO.h"
2+
3+
4+
void RawFileIO::write_image( string fname, const cv::Mat& img)
5+
{
6+
__RawFileIO__write_image_debug_dm( std::cout << "write_image: "<< fname << endl );
7+
cv::imwrite( (fname).c_str(), img );
8+
}
9+
10+
11+
void RawFileIO::write_string( string fname, const string& my_string)
12+
{
13+
__RawFileIO__write_image_debug_dm( std::cout << "write_string: "<< fname << endl );
14+
std::ofstream outfile( fname );
15+
outfile << my_string << endl;
16+
outfile.close();
17+
}
18+
19+
// templated static function canot only exist in header files.
20+
// so this was moved to the header file
21+
// template <typename Derived>
22+
// void RawFileIO::write_EigenMatrix(const string& filename, const MatrixBase<Derived>& a)
23+
// {
24+
// // string base = string("/home/mpkuse/Desktop/bundle_adj/dump/datamgr_mateigen_");
25+
// std::ofstream file(filename);
26+
// if( file.is_open() )
27+
// {
28+
// // file << a.format(CSVFormat) << endl;
29+
// file << a << endl;
30+
// write_image_debug_dm(std::cout << "\033[1;32m" <<"Written to file: "<< filename << "\033[0m\n" );
31+
// }
32+
// else
33+
// {
34+
// cout << "\033[1;31m" << "FAIL TO OPEN FILE for writing: "<< filename << "\033[0m\n";
35+
//
36+
// }
37+
// }
38+
39+
40+
void RawFileIO::write_Matrix2d( const string& filename, const double * D, int nRows, int nCols )
41+
{
42+
// string base = string("/home/mpkuse/Desktop/bundle_adj/dump/datamgr_mat2d_");
43+
std::ofstream file(filename);
44+
if( file.is_open() )
45+
{
46+
int c = 0 ;
47+
for( int i=0; i<nRows ; i++ )
48+
{
49+
file << D[c];
50+
c++;
51+
for( int j=1 ; j<nCols ; j++ )
52+
{
53+
file << ", " << D[c] ;
54+
c++;
55+
}
56+
file << "\n";
57+
}
58+
__RawFileIO__write_image_debug_dm( std::cout << "\033[1;32m" <<"write_Matrix2d to file: "<< filename << "\033[0m\n" );
59+
}
60+
else
61+
{
62+
std::cout << "\033[1;31m" << "FAIL TO OPEN FILE for writing: "<< filename << "\033[0m\n";
63+
64+
}
65+
66+
}
67+
68+
void RawFileIO::write_Matrix1d( const string& filename, const double * D, int n )
69+
{
70+
std::ofstream file(filename);
71+
if( file.is_open() )
72+
{
73+
file << D[0];
74+
for( int i=1 ; i<n ; i++ )
75+
file << ", " << D[i] ;
76+
file << "\n";
77+
__RawFileIO__write_image_debug_dm(std::cout << "\033[1;32m" <<"write_Matrix1d: "<< filename << "\033[0m\n");
78+
}
79+
else
80+
{
81+
std::cout << "\033[1;31m" << "FAIL TO OPEN FILE for writing: "<< filename << "\033[0m\n";
82+
83+
}
84+
85+
}
86+
87+
88+
89+
90+
bool RawFileIO::read_eigen_matrix( string filename, MatrixXd& result )
91+
{
92+
int cols = 0, rows = 0;
93+
const int MAXBUFSIZE = ((int) 1e6);
94+
double buff[MAXBUFSIZE];
95+
96+
// Read numbers from file into buffer.
97+
__RawFileIO__write_image_debug_dm( std::cout << "\033[1;32m" << "read_eigen_matrix: "<< filename << "\033[0m" );
98+
ifstream infile;
99+
infile.open(filename);
100+
if( !infile ) {
101+
cout << "\n\033[1;31m" << "failed to open file " << filename << "\033[0m" << endl;
102+
return false;
103+
}
104+
while (! infile.eof())
105+
{
106+
string line;
107+
getline(infile, line);
108+
109+
int temp_cols = 0;
110+
stringstream stream(line);
111+
while(! stream.eof())
112+
stream >> buff[cols*rows+temp_cols++];
113+
114+
if (temp_cols == 0)
115+
continue;
116+
117+
if (cols == 0)
118+
cols = temp_cols;
119+
120+
rows++;
121+
}
122+
123+
infile.close();
124+
125+
rows--;
126+
127+
// Populate matrix with numbers.
128+
result = MatrixXd::Zero(rows,cols);
129+
for (int i = 0; i < rows; i++)
130+
for (int j = 0; j < cols; j++)
131+
result(i,j) = buff[ cols*i+j ];
132+
133+
// return result;
134+
__RawFileIO__write_image_debug_dm( cout << "\tshape=" << result.rows() << "x" << result.cols() << endl; )
135+
136+
return true;
137+
};
138+
139+
140+
bool RawFileIO::read_eigen_matrix( string filename, Matrix4d& result )
141+
{
142+
int cols = 0, rows = 0;
143+
const int MAXBUFSIZE = ((int) 2000);
144+
double buff[MAXBUFSIZE];
145+
146+
// Read numbers from file into buffer.
147+
__RawFileIO__write_image_debug_dm( std::cout << "\033[1;32m" << "read_eigen_matrix: "<< filename << "\033[0m" );
148+
ifstream infile;
149+
infile.open(filename);
150+
if( !infile ) {
151+
cout << "\n\033[1;31m" << "failed to open file " << filename << "\033[0m" << endl;
152+
return false;
153+
}
154+
while (! infile.eof())
155+
{
156+
string line;
157+
getline(infile, line);
158+
159+
int temp_cols = 0;
160+
stringstream stream(line);
161+
while(! stream.eof())
162+
stream >> buff[cols*rows+temp_cols++];
163+
164+
if (temp_cols == 0)
165+
continue;
166+
167+
if (cols == 0)
168+
cols = temp_cols;
169+
170+
rows++;
171+
}
172+
173+
infile.close();
174+
175+
rows--;
176+
177+
assert( rows==4 && cols==4 && "\n[RawFileIO::read_eigen_matrix( string filename, Matrix4d& result )] The input file is not 4x4" );
178+
179+
// Populate matrix with numbers.
180+
result = Matrix4d::Zero();
181+
for (int i = 0; i < rows; i++)
182+
for (int j = 0; j < cols; j++)
183+
result(i,j) = buff[ cols*i+j ];
184+
185+
// return result;
186+
__RawFileIO__write_image_debug_dm( cout << "\tshape=" << result.rows() << "x" << result.cols() << endl; )
187+
return true;
188+
};
189+
190+
191+
192+
bool RawFileIO::read_eigen_matrix( string filename, Matrix3d& result )
193+
{
194+
int cols = 0, rows = 0;
195+
const int MAXBUFSIZE = ((int) 2000);
196+
double buff[MAXBUFSIZE];
197+
198+
// Read numbers from file into buffer.
199+
__RawFileIO__write_image_debug_dm( std::cout << "\033[1;32m" << "read_eigen_matrix: "<< filename << "\033[0m" );
200+
ifstream infile;
201+
infile.open(filename);
202+
if( !infile ) {
203+
cout << "\n\033[1;31m" << "failed to open file " << filename << "\033[0m" << endl;
204+
return false;
205+
}
206+
while (! infile.eof())
207+
{
208+
string line;
209+
getline(infile, line);
210+
211+
int temp_cols = 0;
212+
stringstream stream(line);
213+
while(! stream.eof())
214+
stream >> buff[cols*rows+temp_cols++];
215+
216+
if (temp_cols == 0)
217+
continue;
218+
219+
if (cols == 0)
220+
cols = temp_cols;
221+
222+
rows++;
223+
}
224+
225+
infile.close();
226+
227+
rows--;
228+
229+
assert( rows==3 && cols==3 && "\n[RawFileIO::read_eigen_matrix( string filename, Matrix3d& result )] The input file is not 3x3" );
230+
231+
// Populate matrix with numbers.
232+
result = Matrix3d::Zero();
233+
for (int i = 0; i < rows; i++)
234+
for (int j = 0; j < cols; j++)
235+
result(i,j) = buff[ cols*i+j ];
236+
237+
// return result;
238+
__RawFileIO__write_image_debug_dm( cout << "\tshape=" << result.rows() << "x" << result.cols() << endl; )
239+
return true;
240+
}
241+
242+
243+
bool RawFileIO::read_eigen_matrix( string filename, VectorXi& result )
244+
{
245+
int cols = 0, rows = 0;
246+
const int MAXBUFSIZE = ((int) 2000);
247+
double buff[MAXBUFSIZE];
248+
249+
// Read numbers from file into buffer.
250+
__RawFileIO__write_image_debug_dm( std::cout << "\033[1;32m" << "read_eigen_matrix: "<< filename << "\033[0m" );
251+
ifstream infile;
252+
infile.open(filename);
253+
if( !infile ) {
254+
cout << "\n\033[1;31m" << "failed to open file " << filename << "\033[0m" << endl;
255+
return false;
256+
}
257+
while (! infile.eof())
258+
{
259+
string line;
260+
getline(infile, line);
261+
262+
int temp_cols = 0;
263+
stringstream stream(line);
264+
while(! stream.eof())
265+
stream >> buff[cols*rows+temp_cols++];
266+
267+
if (temp_cols == 0)
268+
continue;
269+
270+
if (cols == 0)
271+
cols = temp_cols;
272+
273+
rows++;
274+
}
275+
276+
infile.close();
277+
278+
rows--;
279+
280+
assert( cols==1 && "\n[RawFileIO::read_eigen_matrix( string filename, VectorXi& result )] The input file is not row-vector" );
281+
282+
// Populate matrix with numbers.
283+
result = VectorXi::Zero(rows);
284+
for (int i = 0; i < rows; i++)
285+
for (int j = 0; j < cols; j++)
286+
result(i,j) = (int) buff[ cols*i+j ];
287+
288+
// return result;
289+
__RawFileIO__write_image_debug_dm( cout << "\tshape=" << result.rows() << "x" << result.cols() << endl; )
290+
return true;
291+
};
292+
293+
294+
bool RawFileIO::read_eigen_matrix( const std::vector<double>& ary, Matrix4d& result )
295+
{
296+
assert( ary.size() == result.rows() * result.cols() && "[RawFileIO::read_eigen_matrix] size of vector<double> need to be equal to the size of Eigen::Matrix");
297+
298+
for( int i=0 ; i<ary.size() ; i++ ) {
299+
result(i/4, i%4) = ary[i];
300+
}
301+
}
302+
303+
304+
bool RawFileIO::if_file_exist( char * fname )
305+
{
306+
ifstream f(fname);
307+
return f.good();
308+
}
309+
310+
bool RawFileIO::if_file_exist( string fname ) { return if_file_exist( fname.c_str() ); }
311+
312+
313+
314+
int RawFileIO::exec_cmd( const string& system_cmd ) //< Executes a unix command.
315+
{
316+
cout <<"\033[1;33m" << system_cmd << "\033[0m" << endl;
317+
const int _err_code = system( system_cmd.c_str() );
318+
return _err_code;
319+
}

src/utils/RawFileIO.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma once
2+
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <vector>
7+
#include <fstream>
8+
#include <queue>
9+
#include <ostream>
10+
#include <cstdlib>
11+
12+
//opencv
13+
#include <opencv2/core/core.hpp>
14+
#include <opencv2/highgui/highgui.hpp>
15+
#include <opencv2/calib3d.hpp>
16+
#include <opencv2/imgproc/imgproc.hpp>
17+
18+
19+
#include <Eigen/Core>
20+
#include <Eigen/Dense>
21+
#include <Eigen/Geometry>
22+
using namespace Eigen;
23+
#include <opencv2/core/eigen.hpp>
24+
25+
using namespace std;
26+
27+
#define __RawFileIO__write_image_debug_dm( msg ) msg;
28+
29+
class RawFileIO
30+
{
31+
public:
32+
static void write_image( string fname, const cv::Mat& img);
33+
static void write_string( string fname, const string& my_string);
34+
35+
// templated static function canot only exist in header files.
36+
template <typename Derived>
37+
static void write_EigenMatrix(const string& filename, const MatrixBase<Derived>& a)
38+
{
39+
// string base = string("/home/mpkuse/Desktop/bundle_adj/dump/datamgr_mateigen_");
40+
std::ofstream file(filename);
41+
if( file.is_open() )
42+
{
43+
// file << a.format(CSVFormat) << endl;
44+
file << a << endl;
45+
__RawFileIO__write_image_debug_dm(std::cout << "\033[1;32m" <<"write_EigenMatrix: "<< filename << " size=" << a.rows() << "x" << a.cols() << "\033[0m\n";)
46+
}
47+
else
48+
{
49+
cout << "\033[1;31m" << "FAIL TO OPEN FILE for writing: "<< filename << "\033[0m\n";
50+
51+
}
52+
}
53+
54+
55+
static void write_Matrix2d( const string& filename, const double * D, int nRows, int nCols );
56+
static void write_Matrix1d( const string& filename, const double * D, int n );
57+
58+
static bool read_eigen_matrix( string filename, MatrixXd& result );
59+
static bool read_eigen_matrix( string filename, Matrix4d& result );
60+
static bool read_eigen_matrix( string filename, Matrix3d& result );
61+
static bool read_eigen_matrix( string filename, VectorXi& result );
62+
63+
///< read the flat vector ary as a rowmajor matrix.
64+
/// [ 1, 2, 3, 4,5,6...,16 ] ==> [ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ]
65+
/// TODO: Have a flag to read interpret the 1d array as a colmajor.
66+
static bool read_eigen_matrix( const std::vector<double>& ary, Matrix4d& result );
67+
68+
static bool if_file_exist( char * fname );
69+
static bool if_file_exist( string fname );
70+
static int exec_cmd( const string& cmd ); //< Executes a unix command.
71+
72+
};

0 commit comments

Comments
 (0)