-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathvtkReader.h
291 lines (236 loc) · 6.66 KB
/
vtkReader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* \file vtkReader.h
* \brief Contains class definition of vtkReader
* \author Christian Baumgartner ([email protected]) based on code by Stefan Lienhard
*/
#ifndef VTKREADER_H_
#define VTKREADER_H_
#include <string>
#include <vector>
#include <map>
#include "linalg.h"
// HACK #include "CompareFiber.h"
#include "fiber.h"
#include <fstream>
#include <assert.h>
/**
* \class vtkReader
* \brief Class that allows to read a .vtk file.
*/
template <class TFiberType = Fiber>
class vtkReader
{
public:
/** Constructor */
vtkReader() : _sInputPath(""), _fibers(NULL)
{
}
/** Destructor */
virtual ~vtkReader()
{
}
/** Start the reading of the file with the specified values */
bool Run()
{
std::ifstream input(_sInputPath.c_str() );
if( !input.is_open() )
{
std::cout << "Failed to open " << _sInputPath << "." << std::endl;
return 1;
}
if( ReadLines(input) )
{
std::cout << "Couldn't read LINES\n";
return 1;
}
if( ReadRest(input) )
{
std::cout << "Error parsing the vtk file.\n";
return 1;
}
input.close();
return 0;
}
/** Set the path of the vtk file to be read */
void SetInputPath(const std::string & path)
{
_sInputPath = path;
}
/** Set the pointer to the fiber vector to store the data from the vtk file */
void SetOutputFibers(std::vector<TFiberType> & fibers)
{
_fibers = &fibers;
}
/** Set whether the additional fields should be read or not */
void SetReadFieldData(const bool option)
{
_bReadFieldData = option;
}
/** Set wheater the program should outputput execution infos on the commandline */
void SetVerbose(bool b)
{
_bVerbose = b;
}
protected:
private:
/** Pointer to the input path of the vtk File */
std::string _sInputPath;
/** Pointer to the fiber vector where the input is stored */
std::vector<TFiberType> * _fibers;
/** Array to temporalily store the lines field of the vtk */
std::vector<std::vector<unsigned int> > _lines;
/** Total number of points in the vtk */
int _nNumOfPoints;
/** Total number of fibers in the vtk */
int _nNumOfFibers;
/** Total number of scalar fields in the vtk */
int _nNumOfFields;
/** Wheater to read the field data */
bool _bReadFieldData;
/** Wheater to output execution infos on the command line */
bool _bVerbose;
/** Read the Line information of the vtk, needs to be done before reading points. */
bool ReadLines(std::ifstream & input)
{
input.seekg(std::ios::beg);
std::string sCurr;
int nCurr;
while( !input.eof() )
{
// skip forward to lines
while( sCurr != "LINES" )
{
input >> sCurr;
}
input >> nCurr;
_nNumOfFibers = nCurr;
_lines.resize(_nNumOfFibers);
_fibers->resize(_nNumOfFibers);
input >> nCurr;
_nNumOfPoints = nCurr - _nNumOfFibers;
int nLineLength;
for( int i = 0; i < _nNumOfFibers; ++i )
{
// first element of line is fiber length
input >> nLineLength;
_lines[i].resize(nLineLength);
// the remaining elements are the point indices
for( int j = 0; j < nLineLength; ++j )
{
input >> _lines[i][j];
}
}
return 0;
}
return 1;
}
/** Read the points data, and field data */
bool ReadRest(std::ifstream & input)
{
// TODO: actually use _lines data because vtk fibers might be unordered
input.seekg(std::ios::beg);
std::string sCurr;
int nCurr;
int num_points = 0;
while( !input.eof() )
{
while( input.peek() == '#' )
{
getline(input, sCurr);
}
input >> sCurr;
// std::cout << sCurr << " ";
if( !sCurr.compare("DATASET") )
{
input >> sCurr;
assert(!sCurr.compare("POLYDATA") );
input >> sCurr;
assert(!sCurr.compare("POINTS") );
if( _bVerbose )
{
std::cout << "-Found Point data...\n";
}
input >> num_points;
assert(num_points == _nNumOfPoints);
input >> sCurr;
assert(!sCurr.compare("float") );
float x, y, z;
for( int i = 0; i < _nNumOfFibers; ++i )
{
int nLineLength = _lines[i].size();
(*_fibers)[i].Points.resize(nLineLength);
for( int j = 0; j < nLineLength; ++j )
{
input >> x;
input >> y;
input >> z;
(*_fibers)[i].Points[j] << x, y, z;
}
}
}
else if( _bReadFieldData && !sCurr.compare("FIELD") )
{
input >> sCurr;
assert(!sCurr.compare("FieldData") );
if( _bVerbose )
{
std::cout << "-Found Field data...\n";
}
input >> nCurr;
_nNumOfFields = nCurr;
assert(_nNumOfFields > 0);
for( int nFieldCounter = 0; nFieldCounter < _nNumOfFields; ++nFieldCounter )
{
std::string name;
input >> name;
input >> nCurr;
assert(nCurr == 1); // this version doesn't support multi dimensional fields
input >> nCurr;
assert(nCurr == _nNumOfPoints);
input >> sCurr;
assert(!sCurr.compare("float") );
for( int i = 0; i < _nNumOfFibers; ++i )
{
int nLineLength = _lines[i].size();
(*_fibers)[i].Fields[name].resize(nLineLength);
for( int j = 0; j < nLineLength; ++j )
{
input >> (*_fibers)[i].Fields[name][j];
}
}
}
}
else if( _bReadFieldData && !sCurr.compare("TENSORS") )
{
std::string name;
input >> name;
input >> sCurr;
assert(!sCurr.compare("float") );
if( _bVerbose )
{
std::cout << "-Found Tensor Data: " << name << std::endl;
}
for( int i = 0; i < _nNumOfFibers; ++i )
{
int nLineLength = _lines[i].size();
(*_fibers)[i].Tensors[name].resize(nLineLength);
for( int j = 0; j < nLineLength; ++j )
{
ukfPrecisionType t11, t12, t13;
ukfPrecisionType t21, t22, t23;
ukfPrecisionType t31, t32, t33;
input >> t11; input >> t12; input >> t13;
input >> t21; input >> t22; input >> t23;
input >> t31; input >> t32; input >> t33;
(*_fibers)[i].Tensors[name][j] <<
t11, t12, t13,
t21, t22, t23,
t31, t31, t33;
}
}
}
}
return 0; // should never reach eof
}
};
#endif // VTKREADER_H_