-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
260 lines (205 loc) · 5.03 KB
/
parser.cpp
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
#include <parser.h>
using namespace std;
Parser* Parser::_instance = nullptr;
Parser::Parser(void)
{
_positions = *(new vector<Vector3f>());
_scales = *(new vector<float>());
_rotations = *(new vector<Quaternionf>());
_c_points = *(new vector<vector<Vector3f>>());
_section_num = 0;
_cp_num_per_section = 0;
_spline_type = "";
Parser::_instance = this;
}
Parser::Parser(char *filename)
{
_positions = *(new vector<Vector3f>());
_scales = *(new vector<float>());
_rotations = *(new vector<Quaternionf>());
_c_points = *(new vector<vector<Vector3f>>());
_parsed = parseDatafile(filename);
//cout << "Parser called!" << endl;
Parser::_instance = this;
}
bool Parser::_parseSection(ifstream& file, int cpnum)
{
try
{
string curr_line;
// parsing control points in this section
int i = 0;
while(i < cpnum)
{
getline(file, curr_line);
float x, z;
int result = sscanf(curr_line.c_str(), "%f %f %*[^\n]", &x, &z);
if(result < 2) continue;
_c_points[i++].push_back(*(new Vector3f(x, 0.0, z)));
}
// parsing scale factor
float scale;
while(true)
{
getline(file, curr_line);
int result = sscanf(curr_line.c_str(), "%f %*[^\n]", &scale);
if(result < 1) continue;
_scales.push_back(scale);
break;
}
// parsing rotation
float rot_w, rot_x, rot_y, rot_z;
while(true)
{
getline(file, curr_line);
int result = sscanf(curr_line.c_str(), "%f %f %f %f %*[^\n]",
&rot_w, &rot_x, &rot_y, &rot_z);
// cout << "parsing " << curr_line << " : " << result << endl;
if(result < 4) continue;
Vector3f axis(rot_x, rot_y, rot_z);
if(axis.norm() < 0.00001) axis[0] = 1.0;
axis.normalize();
AngleAxisf aa(rot_w, axis);
Quaternionf new_rot(aa);
float _nrm;
_nrm = new_rot.norm();
if(_nrm != _nrm)
{
cout << "NaN Alert!! (in parser)" << endl;
cout << "parsed data : ( " << rot_w << ", " << axis[0] << ", " << axis[1] << ", " << axis[2] << " )" << endl;
cout << "section num = " << _rotations.size() << endl;
}
_rotations.push_back(new_rot);
break;
}
// parsing position
float pos_x, pos_y, pos_z;
while(true)
{
getline(file, curr_line);
int result = sscanf(curr_line.c_str(), "%f %f %f %*[^\n]",
&pos_x, &pos_y, &pos_z);
if(result < 3) continue;
Vector3f pos(pos_x, pos_y, pos_z);
_positions.push_back(pos);
break;
}
}
catch(...)
{
return false;
}
return true;
}
bool Parser::parseDatafile(char *filename)
{
ifstream file(filename);
string curr_line;
char buf[15];
_positions.clear();
_scales.clear();
_rotations.clear();
_c_points.clear();
_section_num = 0;
_cp_num_per_section = 0;
_spline_type = "";
_parsed = false;
// spline type parsing
while(file.good())
{
getline(file, curr_line);
sscanf(curr_line.c_str(), "%s %*[^\n]", buf);
_spline_type = *(new string(buf));
if(_spline_type == "BSPLINE" || _spline_type == "CATMULL_ROM")
break;
}
if(!file.good()) return false;
// section number parsing
while(file.good())
{
getline(file, curr_line);
int result = sscanf(curr_line.c_str(), "%d %*[^\n]", &_section_num);
if(result >= 1) break;
}
if(!file.good()) return false;
// control point number parsing
while(file.good())
{
getline(file, curr_line);
int result = sscanf(curr_line.c_str(), "%d %*[^\n]", &_cp_num_per_section);
if(result >= 1) break;
}
if(!file.good()) return false;
// initializing _c_points
for(int i = 0; i < _cp_num_per_section; i++)
{
_c_points.push_back(*(new vector<Vector3f>()));
}
for(int i = 0; i < _section_num; i++)
{
bool res = _parseSection(file, _cp_num_per_section);
if(!res) return false;
}
return true;
}
Parser* Parser::Get(char *filename)
{
if(_instance == nullptr)
{
Parser* new_parser;
if(filename != nullptr) new_parser = new Parser(filename);
else new_parser = new Parser();
return new_parser;
}
}
bool Parser:: isParsed(void)
{
return _parsed;
}
vector<Vector3f>& Parser::getPositions(void)
{
return _positions;
}
vector<float>& Parser::getScales(void)
{
return _scales;
}
vector<Quaternionf>& Parser::getRotations(void)
{
return _rotations;
}
vector<vector<Vector3f>>& Parser::getCPoints(void)
{
return _c_points;
}
vector<Vector3f>& Parser::getCPointsByCol(int col)
{
return _c_points[col];
}
string Parser::getSplineType(void)
{
return _spline_type;
}
int Parser::getSectionNum(void)
{
return _section_num;
}
int Parser::getCPointNum(void)
{
return _cp_num_per_section;
}
Parser::~Parser(void)
{
for(int i = 0; i < _section_num; i++)
{
delete &(_positions[i]);
delete &(_rotations[i]);
for(int j = 0; j < _cp_num_per_section; j++)
delete &(_c_points[i][j]);
delete &(_c_points[i]);
}
delete &(_positions);
delete &(_scales);
delete &(_rotations);
delete &(_c_points);
}