-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl_utils.cpp
More file actions
107 lines (81 loc) · 2.57 KB
/
Copy pathstl_utils.cpp
File metadata and controls
107 lines (81 loc) · 2.57 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
#include <cassert>
#include "stl_utils.h"
inline bool startsWith(const std::string& s, const std::string& patt) {
return s.find(patt) == 0;
}
std::string trim(const std::string& s) {
const char *blank = "\n \t";
return s.substr(s.find_first_not_of(blank), s.find_last_not_of(blank));
}
std::vector<std::string> split(const std::string& s) {
const char *blank = "\n \t";
std::vector<std::string> ss;
for(size_t start = 0; start != std::string::npos; ) {
auto fst = s.find_first_not_of(blank, start);
auto snd = s.find_first_of(blank, fst);
ss.push_back(s.substr(fst, snd));
start = snd;
}
return ss;
}
STLModel::STLModel(const std::string& fname) {
std::ifstream ifs(fname);
std::string line;
std::getline(ifs, line);
ifs >> line;
ifs.close();
if(line == "facet")
load_ascii_stl(fname);
else
load_binary_stl(fname);
}
void STLModel::load_ascii_stl(const std::string& fname) {
std::ifstream ifs(fname);
std::string header;
std::getline(ifs, header);
assert(startsWith(header, "solid"));
std::string token;
while(!ifs.eof()) {
float n1, n2, n3, v1, v2, v3;
ifs >> token;
if(token != "facet") {break;}
ifs >> token; assert(token == "normal");
ifs >> n1 >> n2 >> n3;
ifs >> token; assert(token == "outer");
ifs >> token; assert(token == "loop");
for(int i = 0; i < 3; ++i) {
ifs >> token;
assert(token == "vertex");
ifs >> v1 >> v2 >> v3;
vertices.push_back({v1, v2, v3});
}
ifs >> token; assert(token == "endloop");
ifs >> token; assert(token == "endfacet");
}
}
void STLModel::load_binary_stl(const std::string& fname) {
std::ifstream ifs(fname, std::fstream::binary);
char buff[50] = {'\0'};
ifs.read(buff, 5);
//assert(std::string(buff) == "solid");
ifs.seekg(80);
uint32_t num_tri;
ifs.read(reinterpret_cast<char*>(&num_tri), 4);
vertices.reserve(num_tri);
auto cpy_vert = [](char* buff, Vec3f &v) {
v[0] = *reinterpret_cast<float*>(buff);
v[1] = *reinterpret_cast<float*>(buff+4);
v[2] = *reinterpret_cast<float*>(buff+4*2);
};
Vec3f v;
for(int i = 0; i < num_tri; ++i) {
ifs.read(buff, 50);
int offset = 3*4; //skip normal vector
cpy_vert(buff + offset, v);
vertices.push_back(v);
cpy_vert(buff + offset * 2, v);
vertices.push_back(v);
cpy_vert(buff + offset * 3, v);
vertices.push_back(v);
}
}