-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
53 lines (45 loc) · 1.62 KB
/
Copy pathmodel.cpp
File metadata and controls
53 lines (45 loc) · 1.62 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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "./model.h"
std::vector<std::string> splitString(const std::string& input, char delimiter) {
std::vector<std::string> content;
std::stringstream line(input);
std::string temp;
while (std::getline(line, temp, delimiter)) {
content.push_back(temp);
}
return content;
}
Model::Model(const std::string filename) {
std::ifstream file(filename);
if (!file) {
std::cout << "File cannot be opened!" << std::endl;
}
std::string line;
while (getline(file, line)) {
if (line.empty()) continue;
std::vector<std::string> content = splitString(line, ' ');
if (content.at(0) == "v") {
vec3 vertex = {std::stod(content.at(1)), std::stod(content.at(2)),
std::stod(content.at(3))};
verts.push_back(vertex);
} else if (content[0] == "f") {
std::vector<std::string> contentFacesA = splitString(content[1], '/');
std::vector<std::string> contentFacesB = splitString(content[2], '/');
std::vector<std::string> contentFacesC = splitString(content[3], '/');
// Decrement by ones because obj file indices start at 1
facet_vrt.push_back(stoi(contentFacesA[0]) - 1);
facet_vrt.push_back(stoi(contentFacesB[0]) - 1);
facet_vrt.push_back(stoi(contentFacesC[0]) - 1);
}
}
}
int Model::nverts() const { return verts.size(); }
int Model::nfaces() const { return facet_vrt.size() / 3; }
vec3 Model::vert(const int i) const { return verts[i]; }
vec3 Model::vert(const int iface, const int nthvert) const {
return verts[facet_vrt[iface * 3 + nthvert]];
}