-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_importer.cpp
118 lines (107 loc) · 3.01 KB
/
obj_importer.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
#include"obj_importer.h"
/*
* init all the number member
* */
obj_importer::obj_importer(string obj_filename): obj_filename(obj_filename){
m_vMax.x = m_vMax.y = m_vMax.z = numeric_limits<float>::max();
m_vMin.x = m_vMin.y = m_vMin.z = numeric_limits<float>::min();
// 查询点的个数,为存储预分配空间
fstream obj_file(obj_filename.c_str());
// assert(obj_file);
vertice_number = 0;
face_number = 0;
if(!obj_file){
cerr << "error: unable to open obj file:"
<< obj_filename << endl;
return;
}
string line;
while(getline(obj_file, line)){
// Vertice
if(line[0] == 'v' && line[1] == ' ')
vertice_number++;
// Face
else if(line[0] == 'f' && line[1] == ' ')
face_number++;
// Normal
else if(line[0] == 'v' && line[1] == 'n');
// Parameter space vertice
else if(line[0] == 'v' && line[1] == 'p');
}
// 此处可能的改进:将文件流设为一个类成员,每次用完将其定位到特定位置
obj_file.close();
//cout << vertice_number << endl << face_number << endl;
vertices.reserve(vertice_number * 3);
//faces.reverse(face_number * 3);
file_parser();
}
vector<string> obj_importer::line_parser(string line){
int space_pos = line.find(" ");
// 删除开头的字段类型标记
line.erase(0, space_pos + 1);
vector<string> result;
// 用空格将line分割开
while((space_pos = line.find(" ")) != -1){
result.push_back(line.substr(0, space_pos));
line.erase(0, space_pos + 1);
}
if(line.length())
result.push_back(line);
/*for(unsigned i = 0; i < result.size(); i++)
cout << result[i] << endl;*/
return result;
}
// 将obj文件中的各种信息存储到obj_importer类中
void obj_importer::file_parser(void){
fstream obj_file(obj_filename.c_str());
if(!obj_file){
cerr << "error: unable to open obj file:"
<< obj_filename << endl;
return;
}
string line;
vector<string> tmp;
vertice vtmp;
vector<int> ftmp;
while(getline(obj_file, line)){
// Vertice
if(line[0] == 'v' && line[1] == ' '){
tmp = line_parser(line);
vtmp.x = (float)atof(tmp[0].c_str());
vtmp.y = (float)atof(tmp[1].c_str());
vtmp.z = (float)atof(tmp[2].c_str());
if(vtmp.x > m_vMax.x)
m_vMax.x = vtmp.x;
if(vtmp.y > m_vMax.y)
m_vMax.y = vtmp.y;
if(vtmp.z > m_vMax.z)
m_vMax.z = vtmp.z;
if(vtmp.x < m_vMin.x)
m_vMin.x = vtmp.x;
if(vtmp.y < m_vMin.y)
m_vMin.y = vtmp.y;
if(vtmp.z < m_vMin.z)
m_vMin.z = vtmp.z;
vertices.push_back(vtmp);
}
// Face,暂时只支持三个index值
else if(line[0] == 'f' && line[1] == ' '){
ftmp.clear();
tmp = line_parser(line);
for (unsigned i = 0; i < tmp.size(); i++){
ftmp.push_back(atoi(tmp[i].c_str()));
}
faces.push_back(ftmp);
}
// Normal
else if(line[0] == 'v' && line[1] == 'n');
// Parameter space vertice
else if(line[0] == 'v' && line[1] == 'p');
}
obj_file.close();
}
//int main(){
// obj_importer o(string("C:\\Users\\Administrator\\Documents\\GitHub\\obj_importer\\data.obj"));
// o.line_parser(string("v 0.006636 -0.001313 2.447146"));
// return 0;
//}