-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjElement.cpp
250 lines (206 loc) · 6.57 KB
/
ObjElement.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
#include "ObjElement.hpp"
#include <sstream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
#include <iostream>
#include <cassert>
#include <boost/regex.hpp>
#include <math.h>
#include "VertexNormal.hpp"
Vertex::Vertex(VertMap& vmap, float x, float y, float z)
: _vid (_next_vid()),
_pt(x,y,z),
_avg_normal(NULL)
{
vmap[_vid] = this;
}
VertexNormal* Vertex::avg_normal (VertexNormal::VertNormalMap& vnmap)
{
if (_avg_normal == NULL)
{
Point3 avger(0,0,0);
for (std::vector<Face*>::iterator iter = _face_vec.begin();
iter != _face_vec.end(); ++iter)
{
Face* face = *iter;
VertexNormal* vn = face->face_normal();
avger = avger + vn->pt();
}
if (_face_vec.size() == 0)
assert(false);
avger.normalize();
_avg_normal = new VertexNormal(vnmap,avger.x,avger.y,avger.z);
}
return _avg_normal;
}
Vertex::~Vertex()
{
if (_avg_normal != NULL)
{
delete _avg_normal;
_avg_normal = NULL;
}
}
bool Vertex::construct_from_line(VertMap& vmap,std::string line)
{
remove_comment(line);
trim(line);
// using 2 here because, we must check at least second index to ensure that
// not a texture coordinate or normal
if (line.size() < 2)
return false;
if ((line[0] == 'v') &&
// ensure that not a texture or normal coordinate, both of which also
// begin with 'v'. FIXME: this is sort of gross to hard-code. Maybe
// figure out a better approach later.
(line[1] != 't') &&
(line[1] != 'n'))
{
// start from 1 to remove command character, 'v'.
std::string to_tokenize = line.substr(1);
std::istringstream tokenizer(to_tokenize);
float x,y,z;
tokenizer >> x;
tokenizer >> y;
tokenizer >> z;
new Vertex(vmap,x,y,z);
return true;
}
return false;
}
TextureCoordinate::TextureCoordinate(
TextCoordinateMap& tcmap,GLfloat _u, GLfloat _v, GLfloat _w)
: _tcid( _next_tcid()),
_pt(_u,_v,_w)
{
tcmap[_tcid] = this;
}
TextureCoordinate::~TextureCoordinate()
{}
bool TextureCoordinate::construct_from_line(
TextureCoordinate::TextCoordinateMap& tcmap,std::string line)
{
remove_comment(line);
trim(line);
// using 2 here because, we must check at least second index to ensure that
// not a texture coordinate or normal
if (line.size() < 2)
return false;
if ((line[0] == 'v') && (line[1] == 't'))
{
// start from 1 to remove command characters, 'vt'.
std::string to_tokenize = line.substr(2);
std::istringstream tokenizer(to_tokenize);
float u,v,w;
w = 0.0;
tokenizer >> u;
tokenizer >> v;
if (! tokenizer.eof())
tokenizer >> w;
new TextureCoordinate(tcmap,(GLfloat)u, (GLfloat)v, (GLfloat)w);
return true;
}
return false;
}
/************* FACE *************/
Face::~Face()
{
for (FaceVertDataVecIter fvdv_iter = _vert_data_vec.begin();
fvdv_iter != _vert_data_vec.end(); ++fvdv_iter)
{
delete *fvdv_iter;
}
}
Face::Face(FaceMap& fmap, VertexNormal::VertNormalMap& vnmap,
const FaceVertDataVec& fvdv)
: _fid(_next_fid()),
_vert_data_vec(fvdv)
{
Point3 veca = _vert_data_vec[0]->vert->pt() - _vert_data_vec[1]->vert->pt();
Point3 vecb = _vert_data_vec[2]->vert->pt() - _vert_data_vec[1]->vert->pt();
Point3 normal_vec = veca % vecb;
normal_vec.normalize();
_face_normal = new VertexNormal(
vnmap,-normal_vec.x,-normal_vec.y,-normal_vec.z);
fmap[_fid] = this;
for( FaceVertDataVecIter fvdv_iter = _vert_data_vec.begin();
fvdv_iter != _vert_data_vec.end(); ++fvdv_iter)
{
(*fvdv_iter) -> vert -> add_face(this);
}
}
/**
When vertices do not have their normals specified, we calculate their normals
as the average of all surrounding face normals
*/
void Face::calc_normals(VertexNormal::VertNormalMap& vnmap)
{
for (FaceVertDataVecIter iter = vert_iter_begin();
iter != vert_iter_end(); ++iter)
{
FaceVertexData* fvd = *iter;
Vertex* vert = fvd->vert;
VertexNormal* vert_normal = fvd->vert_normal;
if (vert_normal == NULL)
fvd->vert_normal = vert-> avg_normal(vnmap);
}
}
bool Face::construct_from_line(
Vertex::VertMap& vmap,TextureCoordinate::TextCoordinateMap& tcmap,
VertexNormal::VertNormalMap& vnmap,Face::FaceMap& fmap, std::string line)
{
remove_comment(line);
trim(line);
// using 2 here because, we must check at least second index to ensure that
// not a texture coordinate or normal
if (line.size() < 1)
return false;
FaceVertDataVec face_vert_data_vec;
if (line[0] == 'f')
{
// start from 1 to remove command character, 'f'.
std::string to_tokenize = line.substr(1);
std::istringstream tokenizer(to_tokenize);
if (to_tokenize.find("/") == std::string::npos)
{
while (! tokenizer.eof())
{
Vertex::VertId vid;
tokenizer >> vid;
FaceVertexData* fvd = new FaceVertexData(
vmap[vid], NULL,NULL);
face_vert_data_vec.push_back(fvd);
}
}
else
{
// Face has / format for each grouping:
// a/b/c d/e/f g/h/i ...
boost::regex regex("(\\d+)/(\\d+)/(\\d+)");
std::string::const_iterator to_search_from, end;
to_search_from = to_tokenize.begin();
end = to_tokenize.end();
boost::match_results<std::string::const_iterator> matches;
while(boost::regex_search(to_search_from,end,matches,regex))
{
Vertex::VertId vid;
TextureCoordinate::TextCoordId tcid;
VertexNormal::VertNormalId vnid;
vid = atoi(
std::string(matches[1].first,matches[1].second).c_str());
tcid = atoi(
std::string(matches[2].first,matches[2].second).c_str());
vnid = atoi(
std::string(matches[3].first,matches[3].second).c_str());
FaceVertexData* fvd = new FaceVertexData(
vmap[vid],vnmap[vnid],tcmap[tcid]);
face_vert_data_vec.push_back(fvd);
to_search_from = matches[3].second;
}
}
new Face(fmap,vnmap,face_vert_data_vec);
return true;
}
return false;
}