-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParser.cpp
More file actions
executable file
·92 lines (80 loc) · 2.46 KB
/
Parser.cpp
File metadata and controls
executable file
·92 lines (80 loc) · 2.46 KB
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
/*
* Parser.cpp
*
* Created by Jacopo Volpin on 12/04/10.
*
* classe per le lettura del file *.obj e relativo immagazzinamento dei dati. Essendo puntatori a strutture dati
* presenti in ObjReader, vi è condivisione di memoria e non replicazione dati.
* Per l'oggetto Vertex è stato utilizzato un vector in quanto sempre presenti nel file *.obj.
* Per il resto è stato optato l'utilizzo di una struttura dati simile.
*/
#include "Parser.h"
Parser::Parser(char *s){fp = fopen(s, "r");};
/* metodo di avvio del parser; apre il file specificato e riga per riga aggiunge nelle strutture dati i corrispondenti elementi */
void Parser::start_parser(vector<Vertex> *v, Container<Triangles> *tr, Container<Quads> *qs, Container<Normal> *vn){
bool boot_f = true, boot_vn = true;
float f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12;
if(!fp){
cout << "ERROR : file not found or wrong declaration"<<endl;
exit(0);
}
while (!feof(fp)) {
memset(str, 0, 256);
fgets(str, 256, fp);
if( strncmp("vn ",str,3) == 0 )
{
if(boot_vn){
vn->initiateData();
boot_vn=false;
}
sscanf((str+2),"%f %f %f",&f1,&f2,&f3);
Normal n = Normal(f1,f2,f3);
vn->addData(n);
}
else if(strncmp("v ",str,2) == 0 )
{
sscanf((str+1),"%f %f %f",&f1,&f2,&f3);
Vertex tmp = Vertex(f1,f2,f3);
v->push_back(tmp);
}
else if(strncmp("f ",str,2) == 0 )
{
if (boot_f) {
//attivare struttura dati
tr->initiateData();
qs->initiateData();
boot_f=false;
}
int match = sscanf((str+1),"%f %f %f %f",&f1,&f2,&f3,&f4); //triangolo o quadrato
Triangles t;
Quads q;
if(match==3){
t = Triangles(f1,f2,f3);
tr->addData(t);
}
if(match==4){
q = Quads(f1,f2,f3,f4);
qs->addData(q);
}
int matchN = sscanf((str+1),"%f//%f %f//%f %f//%f %f//%f",&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8); // triangolo/quadrato + normale
if(matchN==6){
t = Triangles(f1,f3,f5,f2,f4,f6);
tr->addData(t);
}
if(matchN==8){
q = Quads(f1,f3,f5,f7,f2,f4,f6,f8);
qs->addData(q);
}
int matchNT = sscanf((str+1),"%f/%f/%f %f/%f/%f %f/%f/%f %f/%f/%f",&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&f11,&f12); // triangolo/quadrato + texture + normale
if(matchNT==9){
t = Triangles(f1,f4,f7,f3,f6,f9);
tr->addData(t);
}
if(matchNT==12){
q = Quads(f1,f4,f7,f10,f3,f6,f9,f12);
qs->addData(q);
}
}
}
fclose(fp);
};