Repository navigation
Expand file tree
/
Copy pathParser.cpp
More file actions
156 lines (136 loc) · 3.74 KB
/
Copy pathParser.cpp
File metadata and controls
156 lines (136 loc) · 3.74 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Vertex{
public:
float x, y, z;
void setVertexLoc(float data, int axis){
switch (axis){
case 0:
x = data;
break;
case 1:
y = data;
break;
case 2:
z = data;
break;
default:
break;
}
}
void checkLocation(){
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;
}
};
class Face{
public:
int nVer = 0;
vector <Vertex> ver;
void checkVertices(){
for(int i = 0; i < nVer; i++){
cout << "V" << i+1 << ":\n";
ver[i].checkLocation();
}
}
};
class Object{
public:
vector<Vertex> vertices;
vector<Face> faces;
};
void readVertexCoords(string line, Vertex &v){
int begin = 2, j;
string buff;
for(int i = 0; i < 3; i++){
for(j = begin; (line[j] != ' ' && j < line.size()); j++){
buff+=line[j];
}
v.setVertexLoc(stof(buff), i);
buff.clear();
begin = j + 1;
}
}
void readFaceVertices(string line, vector<int> &index, int offset){
int begin = 2, j;
string buff;
while(begin < line.size()){
for(j = begin; (line[j] != ' ' && j < line.size()); j++){
buff+=line[j];
}
index.push_back(stoi(buff)-1-offset);
buff.clear();
begin = j + 1;
}
}
void pairFaceVertices(vector<Vertex> ver, vector<int> index, Face &f){
f.nVer = index.size();
for(int i = 0; i < index.size(); i++){
f.ver.push_back(ver[index[i]]);
}
}
// void printRealIndex(vector<int> index){
// for(int i = 0; i < index.size(); i++){
// cout << index[i] << " ";
// }
// cout << endl;
// }
int main(){
fstream myFile;
myFile.open("Golf.obj", ios::in);
Object objAux;
vector <Object> obj;
bool notFirst = false; //Dismiss the first 'o' from the file
int offset = 0, cont = 0; //Offset of multiple obj vertices in files
obj.clear();
if(myFile.is_open()){
string s;
while(getline(myFile, s)){
if(s[0] == 'v'){
Vertex tempV;
readVertexCoords(s, tempV);
objAux.vertices.push_back(tempV);
cont++;
}
if(s[0] == 'f'){
vector <int> indices;
Face tempF;
readFaceVertices(s, indices, offset);
//printRealIndex(indices);
pairFaceVertices(objAux.vertices, indices, tempF);
objAux.faces.push_back(tempF);
}
if(s[0] == 'o'){
if(notFirst){
//Push the Nth object into the structure
obj.push_back(objAux);
//Clear our vectors for next object
objAux.faces.clear();
objAux.vertices.clear();
} else
notFirst = true;
offset = cont;
}
}
obj.push_back(objAux); //Save last object in the file
myFile.close();
}
for(int i = 0; i < obj.size(); i++){
cout << "Object " << i+1 << endl;
for(int j = 0; j < obj[i].faces.size(); j++){
cout << "Face " << j+1 << endl;
obj[i].faces[j].checkVertices();
cout << "----------------\n";
}
cout << "*******************\n";
}
// for(int i = 0; i < facesAux.size(); i++){
// cout << "Face " << i << "\n";
// facesAux[i].checkVertices();
// cout << "----------------\n";
// }
}