-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobj_input.h
More file actions
71 lines (70 loc) · 2.64 KB
/
obj_input.h
File metadata and controls
71 lines (70 loc) · 2.64 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
//
// Created by rainbowwing on 2023/8/25.
//
#ifndef THICKEN2_OBJ_INPUT_H
#define THICKEN2_OBJ_INPUT_H
double default_move = -1;
int thread_num = 12;
double max_distance_limit = 1.30;
double min_distance_limit = 1.0;
MeshKernel::SurfaceMesh ReadObjFile(const std::string &_InputFile) {
//std::ifstream inputfile(_InputFile, std::ios::in);
std::vector<MeshKernel::iGameVertex> vertices;
std::vector<std::vector<MeshKernel::iGameVertexHandle> > faces;
std::vector<double> move_dist;
std::vector<std::vector<double>> normals;
std::vector<std::vector<double>> uvs;
std::unordered_map<int, int> V2N;// vertex to normal
std::unordered_map<int, int> V2T;// vertex to uv
//std::cout << "Reading " << _InputFile << " File" << std::endl;
FILE *inputfile = fopen(_InputFile.c_str(), "r");
char inputs[100];
while (fscanf(inputfile, "%[^\n]\n", inputs) != EOF) {
string line(inputs);
if (line[0] == '#') {
continue;
}
std::stringstream linestream;
linestream.str(line);
std::string flag;
linestream >> flag;
if (flag == "v") {
double x, y, z;
linestream >> x >> y >> z;
vertices.push_back(MeshKernel::iGameVertex(x, y, z));
} else if (flag == "f") {
std::vector<std::string> vex;
std::string tmp;
while (linestream >> tmp) vex.push_back(tmp);
std::vector<MeshKernel::iGameVertexHandle> face(3);
for (size_t i = 0; i < 3; i++) {
size_t idx = 0;
while (idx < vex[i].length() && std::isdigit(vex[i][idx])) idx++;
int vh = std::stoi(vex[i].substr(0, idx)) - 1;// obj start v from 1
face[i] = (MeshKernel::iGameVertexHandle)(vh);
if (vh >= vertices.size()) {
std::cerr << vh << " " << vertices.size() << std::endl;
}
}
if (vex.size() >= 4)
move_dist.push_back(std::stod(vex[3]));
else
move_dist.push_back(default_move);
faces.push_back(face);
}
}
if (!normals.empty()) {
int ncnt = normals.size();
for (int i = 0; i < vertices.size(); ++i) {
int nidx = V2N[i];
//if (nidx < 0 || nidx >= ncnt) printf("error: nidx = %d\n", nidx);// debug 用
assert(nidx >= 0 && nidx < ncnt);
vertices[i].setNormal(normals[nidx]);
}
}
auto mesh = MeshKernel::SurfaceMesh(vertices, faces, move_dist);
fclose(inputfile);
return mesh;
}
shared_ptr <MeshKernel::SurfaceMesh> mesh;
#endif //THICKEN2_OBJ_INPUT_H