-
Notifications
You must be signed in to change notification settings - Fork 0
/
objParser.cpp
559 lines (445 loc) · 12.3 KB
/
objParser.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "objParser.h"
/**
* function to create new vertex
*
* @param x, y, z coordinates
* @return vertex with specified x,y,z coordinates
*
*/
vertex *newVertex(float x, float y, float z)
{
vertex *temp = new vertex;
temp->x = x;
temp->y = y;
temp->z = z;
temp->edge = NULL;
temp->displayFlag = 0;
temp->quadMat = NULL;
return temp;
}
/**
* Function to create new edge
* @params
* *start = pointer to start vertex
* *end = pointer to end vertex
* @return
* new edge with specified start and end vertex
*/
h_edge *newHEdge(vertex *start, vertex *end)
{
h_edge* edge = new h_edge;
edge->start = start;
edge->end = end;
edge->left = NULL;
edge->right = NULL;
edge->left_prev = NULL;
edge->left_next = NULL;
edge->right_prev = NULL;
edge->right_next = NULL;
edge->decimationCost = 0;
return edge;
}
//function to create new hEdge with face structure passed as argument
h_edge *newHEdge(vertex *start, vertex *end, face* left)
{
h_edge* edge = newHEdge(start, end);
edge->left = left;
return edge;
}
//function to create new face object
face *newFace(h_edge *edge)
{
face* temp = new face;
temp->edge = edge;
temp->displayFlag = 0;
temp->quadMat = NULL;
return temp;
}
//function to create new glvector
GLVector* newGLVector(float x, float y, float z)
{
GLVector* temp = new GLVector;
temp->x = x;
temp->y = y;
temp->z = z;
return temp;
}
//Checks if equal, with tolerance
/**
* checking face normal direction (with tolerance)
* @return
* true : if almost same direction
* false : otherwise
*/
bool faceNormalDirectionValidity(GLVector *v1, GLVector *v2)
{
//using cross product
//float x = ((v1->y)*(v2->z)) - ((v1->z)*(v2->y));
//float y = ((v2->x)*(v1->z)) - ((v1->x)*(v2->z));
//float z = ((v1->x)*(v2->y)) - ((v1->y)*(v2->x));
//if(x==0 && y==0 && z==0)
//return true;
//using dot product
float modV1 = sqrt((v1->x * v1->x) + (v1->y * v1->y) + (v1->z * v1->z));
float modV2 = sqrt((v2->x * v2->x) + (v2->y * v2->y) + (v2->z * v2->z));
float cosTheta = ((v1->x * v2->x) + (v1->y * v2->y) + (v1->z * v2->z))/(modV1 * modV2);
if(cosTheta > 0.966)
return true;
return false;
}
// DFS for planar proxies
void DFSUtil(face* f, map<face*, bool> &visited, int proxy)
{
// Mark the current node as visited and print it
visited[f] = true;
f->proxy = proxy;
h_edge * faceEdgeArray[3];
h_edge *edge = f->edge;
if (edge->left != f) edge = edge->right_next->left_prev;
faceEdgeArray[0] = edge;
faceEdgeArray[1] = edge->left_next;
faceEdgeArray[2] = edge->left_prev;
for(int k=0; k<3; k++)
{
if(faceEdgeArray[k]->left == f)
{
face* rightface = faceEdgeArray[k]->right;
if(!visited[rightface])
{
GLVector *normal1 = faceNormalMap[f];
GLVector *normal2 = faceNormalMap[rightface];
if(faceNormalDirectionValidity(normal1, normal2))
{
DFSUtil(rightface, visited, proxy);
}
}
}
else
{
face* leftface = faceEdgeArray[k]->left;
if(!visited[leftface])
{
GLVector *normal1 = faceNormalMap[f];
GLVector *normal2 = faceNormalMap[leftface];
if(faceNormalDirectionValidity(normal1, normal2))
{
DFSUtil(leftface, visited, proxy);
}
}
}
}
}
/**
* Depth first search implementation for finding nearby proxy plane.
*/
void DFS()
{
// Mark all the vertices as not visited
map<face*, bool> visited;
bool allVisited = false;
face* fCurrent;
for (auto f : faceList)
visited[f] = false;
fCurrent = *faceList.begin();
visited[fCurrent] = true;
int proxy = 1;
// Call the recursive helper function to print DFS traversal
while(!allVisited)
{
DFSUtil(fCurrent, visited, proxy);
int noIfound = 1;
for (auto i : faceList)
{
if(visited[i] == false)
{
fCurrent = i;
noIfound = 0;
}
}
if(noIfound == 1)
allVisited = true;
proxy++;
}
cout << "Proxy: " << proxy-1 << endl;
}
/**
* For saving .OBJ file.
*
* @param filename
* @return void
*/
void saveMesh()
{
ofstream outFile("output.obj", ofstream::out);
if (!outFile.is_open()) {
cout << "Failed" << endl;
return;
}
int numVertices, numFaces;
int vertexAdjust = 0;
int faceAdjust = 0;
vector<int> prefix;
for (auto vertex1 : vertexList)
{
if (vertex1->displayFlag == 1)
vertexAdjust++;
prefix.push_back(vertexAdjust);
}
for (auto f : faceList)
if (f->displayFlag == 1) faceAdjust++;
numVertices = vertexList.size() - vertexAdjust;
numFaces = faceList.size() - faceAdjust;
outFile << "# " << numVertices << " " << numFaces << endl;
for (auto v1 : vertexList)
{
if (v1->displayFlag == 1)
continue;
outFile << "v " << v1->x << " " << v1->y << " " << v1->z << endl;
}
for (auto f : faceList)
{
if (f->displayFlag == 1)
continue;
h_edge *edg = f->edge;
h_edge *edg1 = edg;
outFile << "f";
do {
outFile << " " << indexMap[edg1->start] + 1 - prefix[indexMap[edg1->start]]; // The faces data
if (edg1->right == f)
edg1 = edg1->right_next;
else
edg1 = edg1->left_next;
} while (edg1 != edg);
outFile << endl;
}
outFile.close();
cout << "File saved." << endl;
}
/*
* populating proxy plane of each face
*/
void calculateProxy()
{
proxyCount.clear();
DFS();
for(auto f : faceList) {
if (proxyCount.find(f->proxy) != proxyCount.end())
proxyCount[f->proxy] += 1;
else
proxyCount[f->proxy] = 1;
//cout << f->proxy << " " << proxyCount[f->proxy] << endl;
}
}
// calculating face normal in unnormalized
GLVector *faceNormalFromVertexUnnormalized(face* f, Vertex *v1, Vertex *v2, Vertex *v3)
{
float x = ((v2->y - v1->y)*(v3->z - v1->z)) - ((v2->z - v1->z)*(v3->y - v1->y));
float y = ((v3->x - v1->x)*(v2->z - v1->z)) - ((v3->z - v1->z)*(v2->x - v1->x));
float z = ((v2->x - v1->x)*(v3->y - v1->y)) - ((v2->y - v1->y)*(v3->x - v1->x));
return newGLVector(x, y, z);
}
// calculating face normals
void calculateFaceNormal()
{
std::map<face*, GLVector*>::iterator itr4;
for(itr4 = faceNormalMap.begin(); itr4 != faceNormalMap.end(); itr4++)
{
// found it - delete it
delete itr4->second;
faceNormalMap.erase(itr4);
}
for(face* itr : faceList)
{
face* f = itr;
if (f->displayFlag == 1) continue;
h_edge *edge = f->edge;
if (edge->left != f) edge = edge->right_next->left_prev; // Get the three points in the plane
Vertex *vertex1 = edge->start;
Vertex *vertex2 = edge->end;
Vertex *vertex3 = edge->left_next->end;
/*
*create face normals
*/
GLVector *vector1;
vector1 = faceNormalFromVertexUnnormalized(f, vertex1, vertex2, vertex3);
faceNormalMap.insert(std::make_pair(f, vector1));
}
}
// updating the vertexNormal in vertexNormalMap
void updateVertexNormal(face* f, Vertex* v1, Vertex* v2, Vertex* v3)
{
GLVector* vertexNormal;
GLVector* faceNormal;
if(vertexNormalMap.find(v1) != vertexNormalMap.end())
{
vertexNormal = vertexNormalMap[v1];
}
else
{
vertexNormal = newGLVector(0.0, 0.0, 0.0);
vertexNormalMap.insert(std::make_pair(v1, vertexNormal));
}
if(faceNormalMap.find(f) != faceNormalMap.end())
{
faceNormal = faceNormalMap[f];
float normalLength = sqrtf((faceNormal->x * faceNormal->x) + (faceNormal->y * faceNormal->y) + (faceNormal->z * faceNormal->z));
float vector1X = v2->x - v1->x;
float vector1Y = v2->y - v1->y;
float vector1Z = v2->z - v1->z;
float vector2X = v3->x - v1->x;
float vector2Y = v3->y - v1->y;
float vector2Z = v3->z - v1->z;
float vector1Length = sqrtf((vector1X * vector1X) + (vector1Y * vector1Y) + (vector1Z * vector1Z));
float vector2Length = sqrtf((vector2X * vector2X) + (vector2Y * vector2Y) + (vector2Z * vector2Z));
float sin_alpha = normalLength/(vector1Length*vector2Length);
float normalX = faceNormal->x/normalLength;
float normalY = faceNormal->y/normalLength;
float normalZ = faceNormal->z/normalLength;
normalX = normalX * asin(sin_alpha);
normalY = normalY * asin(sin_alpha);
normalZ = normalZ * asin(sin_alpha);
//updating the entry in vertexNormal
vertexNormal->x = vertexNormal->x + normalX;
vertexNormal->y = vertexNormal->y + normalY;
vertexNormal->z = vertexNormal->z + normalZ;
}
else
{
cout << "error in updateVertexNormal()";
}
}
// function calculating vertex normal for all vertices
void calculateVertexNormal()
{
//clear old vertexNormalData
std::map<Vertex*, GLVector*>::iterator itr;
for(itr = vertexNormalMap.begin(); itr != vertexNormalMap.end(); )
{
delete itr->second;
itr = vertexNormalMap.erase(itr);
}
for(face* itr : faceList)
{
face* f = itr;
if (f->displayFlag == 1) continue;
h_edge *edge = f->edge;
if (edge->left != f) edge = edge->right_next->left_prev; // Get the three points in the plane
Vertex *vertex1 = edge->start;
Vertex *vertex2 = edge->end;
Vertex *vertex3 = edge->left_next->end;
updateVertexNormal(f, vertex1, vertex2, vertex3);
updateVertexNormal(f, vertex2, vertex3, vertex1);
updateVertexNormal(f, vertex3, vertex1, vertex2);
}
}
// function returning vertex normal of a vertex
GLVector* findVertexNormal(Vertex *v)
{
if(vertexNormalMap.find(v) != vertexNormalMap.end())
return vertexNormalMap[v];
else
cout << "error finding vertex normal\n";
return NULL;
}
/**
* Utility method
*/
void processStringForInt(string &s, char delim, vector<int> &vec)
{
stringstream ss(s);
string str;
while (getline(ss, str, delim)) {
if (str.compare("") != 0)
vec.push_back((int)stof(str));
}
}
/**
* clear all tables on load
*/
void clearData()
{
vertexList.clear();
faceList.clear();
edgeList.clear();
}
/**
* for reading .obj file.
*
* @param name of file
* @return void
*/
void loadMesh(string fileName)
{
int count = 0;
string inputType;
int numVertices, numFaces;
ifstream fileInput(fileName.c_str());
if (!fileInput.is_open()) {
cout << "File open issue." << endl;
return;
}
clearData();
while (true) {
float x, y, z;
fileInput >> inputType;
if (inputType.compare("v") != 0)
break;
fileInput >> x >> y >> z;
vertex *vertex1 = newVertex(x, y, z); // Add new vertex
vertexList.push_back(vertex1);
indexMap[vertex1] = (vertexList.size() - 1);
count++;
}
count = 0;
while (true) {
//vertices indexes
int v1, v2, v3;
vector<int> verticesIndex;
fileInput >> v1 >> v2 >> v3;
verticesIndex.push_back(v1);
verticesIndex.push_back(v2);
verticesIndex.push_back(v3);
face *faceNew = newFace(NULL);
faceList.push_back(faceNew);
for (int i = 0; i < 3; i++) {
Vertex *startV = vertexList[verticesIndex[i] - 1];
Vertex *endV = vertexList[verticesIndex[(i + 1) % 3] - 1];
h_edge *edgeNew = newHEdge(startV, endV);
pair<int, int> p(verticesIndex[i], verticesIndex[(i + 1) % 3]);
edgeList[p] = edgeNew;
faceNew->edge = edgeNew;
edgeNew->left = faceNew;
startV->edge = edgeNew;
}
for (int i = 0; i < 3; i++) {
pair<int, int> E(verticesIndex[i], verticesIndex[(i + 1) % 3]);
pair<int, int> Eprev(verticesIndex[(i - 1 + 3) % 3], verticesIndex[i]);
pair<int, int> Enext(verticesIndex[(i + 1) % 3], verticesIndex[(i + 2) % 3]);
pair<int, int> Ereverse(verticesIndex[(i + 1) % 3], verticesIndex[i]);
h_edge *edge = edgeList.find(E)->second;
h_edge *edge_prev = edgeList.find(Eprev)->second;
h_edge *edge_next = edgeList.find(Enext)->second;
edge->left_next = edge_next;
edge->left_prev = edge_prev;
if (edgeList.find(Ereverse) != edgeList.end()) {
h_edge *edge2 = edgeList.find(Ereverse)->second;
edge->right = edge2->left;
edge->right_next = edge2->left_next;
edge->right_prev = edge2->left_prev;
edge2->right = edge->left;
edge2->right_next = edge->left_next;
edge2->right_prev = edge->left_prev;
}
}
fileInput >> inputType;
if (fileInput.eof())
break;
count++;
}
fileInput.close();
calculateFaceNormal();
calculateVertexNormal();
calculateProxy();
cout << "Import Summary: Edge count - " << edgeList.size() / 2
<< " Face count - " << faceList.size() << endl;
}