-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
134 lines (123 loc) · 5.18 KB
/
Copy pathParser.cpp
File metadata and controls
134 lines (123 loc) · 5.18 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
// Copyright 2022, University of Freiburg
// Author: Joel Stanciu
#include <vector>
#include <string>
#include "./Parser.h"
// ____________________________________________________________________________
Parser::Parser() {
objectCount_ = 0;
}
// ____________________________________________________________________________
Parser::~Parser() {
objectCount_ = 0;
}
// ____________________________________________________________________________
std::vector<float> Parser::parseLineVertex(std::string& input) {
size_t pos1 = input.find(' ');
size_t pos2 = input.find(' ', pos1 + 1);
size_t pos3 = input.find(' ', pos2 + 1);
float point1 = std::atof(input.data() + pos1);
float point2 = std::atof(input.data() + pos2);
float point3 = std::atof(input.data() + pos3);
std::vector<float> points = { point1, point2, point3, 1 };
return points;
}
// ____________________________________________________________________________
std::vector<float> Parser::parseLineTexture(std::string& input) {
size_t pos1 = input.find(' ');
size_t pos2 = input.find(' ', pos1 + 1);
float point1 = std::atof(input.data() + pos1);
float point2 = std::atof(input.data() + pos2);
std::vector<float> points = { point1, point2 };
return points;
}
// ____________________________________________________________________________
std::vector<float> Parser::parseLineNormal(std::string& input) {
size_t pos1 = input.find(' ');
size_t pos2 = input.find(' ', pos1 + 1);
size_t pos3 = input.find(' ', pos2 + 1);
float point1 = std::atof(input.data() + pos1);
float point2 = std::atof(input.data() + pos2);
float point3 = std::atof(input.data() + pos3);
std::vector<float> points = { point1, point2, point3 };
return points;
}
// ____________________________________________________________________________
void Parser::parseLineFace(std::string& input,
std::vector<std::vector<float>>& points,
std::vector<std::vector<float>>& textures,
std::vector<std::vector<float>>& normals) {
size_t pos11 = input.find(' ');
size_t pos12 = input.find('/', pos11 + 1);
size_t pos13 = input.find('/', pos12 + 1);
size_t pos21 = input.find(' ', pos13 + 1);
size_t pos22 = input.find('/', pos21 + 1);
size_t pos23 = input.find('/', pos22 + 1);
size_t pos31 = input.find(' ', pos23 + 1);
size_t pos32 = input.find('/', pos31 + 1);
size_t pos33 = input.find('/', pos32 + 1);
size_t index00 = (size_t)std::atof(input.data() + pos11);
size_t index01 = (size_t)std::atof(input.data() + pos12 + 1);
size_t index02 = (size_t)std::atof(input.data() + pos13 + 1);
size_t index10 = (size_t)std::atof(input.data() + pos21);
size_t index11 = (size_t)std::atof(input.data() + pos22 + 1);
size_t index12 = (size_t)std::atof(input.data() + pos23 + 1);
size_t index20 = (size_t)std::atof(input.data() + pos31);
size_t index21 = (size_t)std::atof(input.data() + pos32 + 1);
size_t index22 = (size_t)std::atof(input.data() + pos33 + 1);
std::vector<std::vector<float>> component0 = {
points[index00-1],
textures[index01-1],
normals[index02-1]
};
std::vector<std::vector<float>> component1 = {
points[index10-1],
textures[index11-1],
normals[index12-1]
};
std::vector<std::vector<float>> component2 = {
points[index20-1],
textures[index21-1],
normals[index22-1]
};
std::vector<std::vector<std::vector<float>>> polygon = {
component0,
component1,
component2
};
object_.push_back(polygon);
objectCount_++;
}
// ____________________________________________________________________________
void Parser::parsefile(std::string& filename) {
std::ifstream pointsFile(filename);
std::string line;
std::string vt = "vt";
std::string vn = "vn";
size_t found;
std::vector<std::vector<float>> points;
std::vector<std::vector<float>> textures;
std::vector<std::vector<float>> normals;
while (std::getline(pointsFile, line)) {
found = line.find('t');
if (found != std::string::npos) {
textures.push_back(parseLineTexture(line));
continue;
}
found = line.find('n');
if (found != std::string::npos) {
normals.push_back(parseLineNormal(line));
continue;
}
found = line.find('f');
if (found != std::string::npos) {
parseLineFace(line, points, textures, normals);
continue;
}
found = line.find('v');
if (found != std::string::npos) {
points.push_back(parseLineVertex(line));
continue;
}
}
}