-
Notifications
You must be signed in to change notification settings - Fork 1
/
defaulttextreader.cpp
221 lines (211 loc) · 5.33 KB
/
defaulttextreader.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
//
// THU-Render
//
// Copyright 2007-2008 CG & CAD Institute, Tsinghua University.
// All rights reserved.
//
#include "defaulttextreader.h"
// C++ headers
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <fstream>
DefaultTextReader::DefaultTextReader(void)
: vertices(NULL), normals(NULL), tex(NULL), vertexIndices(NULL),
vertexCount(0), faceCount(0), inversed(false)
{
}
DefaultTextReader::~DefaultTextReader(void)
{
}
bool DefaultTextReader::readFile(const char* filename)
{
std::ifstream ff;
ff.open(filename);
if (!ff.is_open())
return false;
f = &ff;
// Token:
// 0 - initial
// 1 - vertex count
// 2 - face count
// 3 - vertices
// 4 - vertex indices
// 5 - normals
// 6 - texture coordinates
// 7 - end of file
int state = 0;
vertexCount = 0;
faceCount = 0;
bool facesRead = false;
bool verticesRead = false;
while (state != 7)
{
switch (state)
{
case 0: // initial
{
std::string token = readToken();
if (token == "vertexcount")
{
state = 1;
}
else if (token == "facecount")
{
state = 2;
}
else if (token == "vertices")
{
state = 3;
}
else if (token == "vertexindices")
{
state = 4;
}
else if (token == "normals")
{
normals = new double[vertexCount * 3];
state = 5;
}
else if (token == "texturecoords")
{
tex = new double[vertexCount * 2];
state = 6;
}
else if (token == "inversed")
{
inversed = true;
}
else if (token == "")
{
state = 7;
}
else
{
return false;
}
}
break;
case 1: // vertex count
{
std::string number = readToken();
if (number.empty())
return false;
vertexCount = atoi(number.c_str());
if (vertexCount == 0)
return false;
vertices = new double[vertexCount * 3];
state = 0;
}
break;
case 2: // face count
{
std::string number = readToken();
if (number.empty())
return false;
faceCount = atoi(number.c_str());
if (faceCount == 0)
return false;
vertexIndices = new size_t[faceCount * 3];
state = 0;
}
state = 0;
break;
case 3: // vertices
if (!readDoubles(vertices, vertexCount * 3))
return false;
verticesRead = true;
state = 0;
break;
case 4: // vertex indices
if (!readIntegers(vertexIndices, faceCount * 3))
return false;
facesRead = true;
state = 0;
break;
case 5: // normals
if (!readDoubles(normals, vertexCount * 3))
return false;
state = 0;
break;
case 6: // texture coordinates
if (!readDoubles(tex, vertexCount * 2))
return false;
state = 0;
break;
case 7: // end of file
break;
default:
assert(false);
}
}
return verticesRead && facesRead;
}
std::string DefaultTextReader::readToken(void)
{
std::string result;
int data = f->peek();
bool isSpace = true;
while (data >= 0 && data != '>')
{
if (isSpace)
{
switch (data)
{
case ' ':
case '\r':
case '\n':
case '\t':
case ':':
case ',':
break;
default:
isSpace = false;
result += static_cast<char>(data);
}
}
else
{
switch (data)
{
case ' ':
case '\r':
case '\n':
case '\t':
case ':':
case ',':
return result;
break;
default:
isSpace = false;
result += static_cast<char>(data);
}
}
char cc;
f->read(&cc, 1);
data = f->peek();
}
return result;
}
bool DefaultTextReader::readIntegers(size_t* data, size_t count)
{
for (size_t i = 0; i < count; ++i)
{
std::string token = readToken();
if (token.empty())
return false;
data[i] = atoi(token.c_str());
}
return true;
}
bool DefaultTextReader::readDoubles(double* data, size_t count)
{
for (size_t i = 0; i < count; ++i)
{
std::string token = readToken();
if (token.empty())
return false;
data[i] = atof(token.c_str());
}
return true;
}