-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjReader.h
201 lines (165 loc) · 4.55 KB
/
objReader.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
//------------------------------------------------------------------------------
// Copyright 2007-2014 by Jyh-Ming Lien and George Mason University
// See the file "LICENSE" for more information
//------------------------------------------------------------------------------
#ifndef _OBJ_READER_H_
#define _OBJ_READER_H_
#include <string>
#include <fstream>
#include <iostream>
#include <list>
#include <vector>
#include <ctype.h>
#include <math.h>
using namespace std;
#include <Point.h>
#include <Quaternion.h>
#include <Vector.h>
using namespace mathtool;
class Vpt
{
public:
Vpt(){ x=y=z=nx=ny=nz=0; }
void normalize()
{
double norm=(double)sqrt(nx*nx+ny*ny+nz*nz);
nx/=norm;
ny/=norm;
nz/=norm;
}
double x, y, z, nx, ny, nz;
};
class V
{
public:
V(){ x=y=z=0; }
double x, y, z;
};
class polygon
{
public:
list<int> pts;
list<int> normals;
};
class objModel
{
public:
objModel() { }
void compute_v_normal()
{
//check if normal information is valid from the obj file
if(normals.empty()){ //compute normal
for(list<polygon>::iterator i=polys.begin();i!=polys.end();i++)
{
//get 3 points, compute normal and assign to all vertices
list<int>::iterator pi=i->pts.begin();
vector<Point3d> v3;
for( ;pi!=i->pts.end();pi++){
Vpt& pt=pts[*pi];
Point3d pos(pt.x,pt.y,pt.z);
v3.push_back(pos);
if(v3.size()==3) break; //we've collected 3 points
}
//compute normal
Vector3d n=((v3[1]-v3[0])%(v3[2]-v3[0])).normalize();
//copy normals
pi=i->pts.begin();
for( ;pi!=i->pts.end();pi++){
Vpt& pt=pts[*pi];
pt.nx+=n[0];
pt.ny+=n[1];
pt.nz+=n[2];
}//end copying normals
}//end looping polygons
}
else{ // use the information provided
for(list<polygon>::iterator i=polys.begin();i!=polys.end();i++)
{
list<int>::iterator ni=i->normals.begin();
list<int>::iterator pi=i->pts.begin();
for( ;pi!=i->pts.end();pi++,ni++){
V& n=normals[*ni];
Vpt& pt=pts[*pi];
pt.nx+=n.x;
pt.ny+=n.y;
pt.nz+=n.z;
}//end copying normals
}//end looping polygons
}
//normalize
for(vector<Vpt>::iterator i=pts.begin();i!=pts.end();i++)
{
i->normalize();
}
}
vector<Vpt> pts;
vector<V> normals;
list<polygon> polys;
};
class objReader
{
public:
objReader(const string& name){ m_filename=name; }
bool Read(){
ifstream in(m_filename.c_str());
if( !in.good() ){
cerr<<"Error: Can't open file "<<m_filename<<endl;
return false;
}
bool r=Read(in);
in.close();
return r;
}
const objModel& getModel() const { return data; }
objModel& getModel() { return data; }
private:
bool Read(istream& in){
string tmp;
//read pts
while(true){
in>>tmp;
if( tmp=="f" ) break;
if( tmp=="v" ){
Vpt pt;
in>>pt.x>>pt.y>>pt.z;
data.pts.push_back(pt);
}
else if( tmp=="vn" ){
V pt;
in>>pt.x>>pt.y>>pt.z;
data.normals.push_back(pt);
}
getline(in,tmp);
}
//read faces
polygon poly;
while (true)
{
//string tmp;
if( !(in >> tmp) ) break;
if( isdigit(tmp[0]) ){ //this defines a vetex
int pos1=tmp.find('/');
int pos2=tmp.rfind('/');
int id_v=atoi(tmp.substr(0,pos1).c_str())-1;
int id_n=atoi(tmp.substr(pos2+1).c_str())-1;
poly.pts.push_back(id_v);
poly.normals.push_back(id_n);
}
else if( tmp=="f" )
{
data.polys.push_back(poly);
poly.pts.clear();
poly.normals.clear();
}
else {
getline(in, tmp);
}
}
data.polys.push_back(poly);
data.compute_v_normal();
return true;
}
string m_filename;
objModel data;
};
#endif //_OBJ_READER_H_