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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#include "obj_parser.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstddef>
#include <array>
#include <string_view>
#include <optional>
#include "math/vector.hpp"
#include "math/utils.hpp"
#include "o3d/mesh.hpp"
namespace engine {
using math::Vector2, math::Vector3;
template<typename CallbackFn>
static void split(const std::string_view& s, char sep, CallbackFn fn) {
size_t n = 0;
std::string_view::size_type last_ind = 0;
for (std::string_view::size_type ind = 0; ind < s.length(); ind++) {
if (s[ind] == sep) {
if (!fn(n, s.substr(last_ind, ind - last_ind))) return;
n++;
last_ind = ind + 1;
}
}
fn(n, s.substr(last_ind));
}
template<typename NumType>
static std::optional<NumType> parse_num(const std::string_view& s);
template<>
std::optional<float> parse_num<float>(const std::string_view& s) {
std::string::size_type ind = 0;
bool positive = true;
while (ind < s.length() && (s[ind] == '+' || s[ind] == '-'))
if (s[ind++] == '-')
positive = !positive;
if (ind == s.length())
return {};
int n = 0;
while (ind < s.length() && s[ind] >= '0' && s[ind] <= '9')
n = n * 10 + static_cast<int>(s[ind++]) - static_cast<int>('0');
if (ind == s.length())
return (positive ? 1.f : -1.f) * static_cast<float>(n);
if (s[ind] != '.')
return {};
ind++;
float decimal_part = 0.f;
float decimal_fac = .1f;
while (ind < s.length() && s[ind] >= '0' && s[ind] <= '9') {
decimal_part += decimal_fac * static_cast<float>(static_cast<int>(s[ind++]) - static_cast<int>('0'));
decimal_fac *= .1f;
}
if (ind != s.length())
return {};
return (positive ? 1.f : -1.f) * (static_cast<float>(n) + decimal_part);
}
template<>
std::optional<int> parse_num<int>(const std::string_view& s) {
std::string::size_type ind = 0;
bool positive = true;
while (ind < s.length() && (s[ind] == '+' || s[ind] == '-'))
if (s[ind++] == '-')
positive = !positive;
if (ind == s.length())
return {};
int n = 0;
while (ind < s.length() && s[ind] >= '0' && s[ind] <= '9')
n = n * 10 + static_cast<int>(s[ind++]) - static_cast<int>('0');
if (ind != s.length())
return {};
return (positive ? 1.f : -1.f) * n;
}
template<typename VectorType>
static constexpr std::optional<VectorType> split_to_vec(const std::string_view& s, char sep) {
std::array<float, VectorType::size> coords;
bool err = false;
split(s, sep, [&](auto n, const auto& coord_s) {
auto coord = parse_num<float>(coord_s);
if (!coord) {
err = true;
return false;
}
coords[n] = *coord;
return true;
});
if (err) return {};
return engine::math::utils::array_to_vec(coords);
}
// TODO: improve this. This is a workaround to have a unwrap-like function, but we should check more
// precisely the error
template<typename Ret, size_t line_num>
[[noreturn]]
static std::optional<Ret> err_exit() {
std::cerr << "error: failed to parse .obj file (line number " << line_num << ")" << std::endl;
exit(EXIT_FAILURE);
}
o3d::Mesh parse_object(const std::string& obj_path) {
o3d::Mesh mesh;
std::ifstream obj_file(obj_path);
if (!obj_file.is_open()) {
std::cerr << "file `" << obj_path << "' not found" << std::endl; // TODO: improve
exit(EXIT_FAILURE);
}
std::string line;
while (std::getline(obj_file, line)) {
std::string_view line_view { line };
if (line_view.length() == 0 || line_view[0] == '#')
continue;
auto data_type_idx = line_view.find(" ");
if (data_type_idx == std::string_view::npos) {
std::cerr << "error: failed to parse .obj file: no argument given to data type '" << line_view << "`" << std::endl;
exit(EXIT_FAILURE);
}
auto data_type = line_view.substr(0, data_type_idx);
auto data_arg = line_view.substr(data_type_idx + 1);
if (data_type == "o") {
// TODO: implement
} else if (data_type == "v") {
mesh.vertices.push_back(*split_to_vec<Vector3>(data_arg, ' ').or_else(err_exit<Vector3, __LINE__>));
} else if (data_type == "vn") {
mesh.normals.push_back(*split_to_vec<Vector3>(data_arg, ' ').or_else(err_exit<Vector3, __LINE__>));
} else if (data_type == "vt") {
auto uv = *split_to_vec<Vector2>(data_arg, ' ').or_else(err_exit<Vector2, __LINE__>);
uv.y = 1.f - uv.y;
mesh.uvs.push_back(uv);
} else if (data_type == "s") {
// TODO: implement
} else if (data_type == "f") {
std::array<std::array<std::size_t, 3>, 3> indices;
bool err = false;
split(data_arg, ' ', [&](auto m, const auto& vertex_indices_s) {
split(vertex_indices_s, '/', [&](auto n, const auto& idx_s) {
// indices of texture coordinates and normals are reversed in .obj relative to the
// engine
auto idx_opt = parse_num<int>(idx_s);
if (!idx_opt) {
err = true;
return false;
}
indices[m][(n > 0 ? 3 - n : n)] = *idx_opt - 1;
return true;
});
return !err;
});
if (err) err_exit<int, __LINE__>();
mesh.indices.push_back(indices);
} else if (data_type == "mtllib") {
// TODO: implement
} else if (data_type == "usemtl") {
// TODO: implement
} else {
std::cerr << "error while parsing .obj file: unknown data type: " << line_view << std::endl;
exit(EXIT_FAILURE);
}
}
return mesh;
}
}
|