-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestRead.cpp
More file actions
75 lines (67 loc) · 2.17 KB
/
testRead.cpp
File metadata and controls
75 lines (67 loc) · 2.17 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
#include <string.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<string> split(string s, char splitBy)
{
vector<string> splitted = {};
string buffer = "";
for (char c : s)
{
if (c == splitBy)
{
splitted.push_back(buffer);
buffer = "";
continue;
}
buffer = c;
}
if (buffer.length())
{
splitted.push_back(buffer);
}
return splitted;
}
int main(int argc, char const *argv[])
{
string fileName = "./mesh.obj";
// Create a text string, which is used to output the text file
string myText;
vector triangles = vector<vector<int>>({});
vector vertices = vector<vector<float>>({});
// Read from the text file
ifstream MyReadFile(fileName);
// Use a while loop together with the getline() function to read the file line by line
while (getline(MyReadFile, myText))
{
// Output the text from the file
// cout << myText << endl;
if (myText.length() > 0 && myText.at(0) == 'f')
{
size_t ind1 = myText.find('/');
int size = ind1 - 2;
int f1 = atoi(myText.substr(2, size).c_str());
size_t spaceInd = myText.find(' ', 2);
size_t slashInd = myText.find('/', spaceInd);
int f2 = atoi(myText.substr(spaceInd + 1, slashInd - spaceInd - 1).c_str());
size_t spaceInd2 = myText.find(' ', spaceInd + 1);
size_t slashInd2 = myText.find('/', spaceInd2);
int f3 = atoi(myText.substr(spaceInd2 + 1, slashInd2 - spaceInd2 - 1).c_str());
triangles.push_back(vector<int>({f1, f2, f3}));
}
else if (myText.length() > 0 && myText.at(0) == 'v' && myText.at(1) == ' ')
{
auto splitted = split(myText, ' ');
float p1 = atof(splitted[1].c_str());
float p2 = atof(splitted[2].c_str());
float p3 = atof(splitted[3].c_str());
vertices.push_back(vector<float>({p1, p2, p3}));
}
}
cout << triangles.size() << endl;
cout << vertices.size() << endl;
// Close the file
MyReadFile.close();
return 0;
}