-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstl2gltf.cpp
220 lines (173 loc) · 5.97 KB
/
stl2gltf.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <string>
#include <fstream>
#include <array>
#include <vector>
#include <cstring>
#include <vertex.h>
#include <algorithm>
#include <iostream>
// https://stackoverflow.com/a/6500499
inline std::string trim(std::string& str)
{
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ')+1); //surfixing spaces
return str;
}
inline Vertex get_vector(std::string& str)
{
// "vertex float float float"
auto space0 = str.find_first_of(' ');
str.erase(0, space0); // remove "vertex"
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
auto space1 = str.find_first_of(' ');
float x = std::stof(str.substr(0, space1));
str.erase(0, space1+1); // remove x
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
auto space2 = str.find_last_of(' ');
Vertex v(x,
std::stof(str.substr(0, space2)),
std::stof(str.substr(space2, str.size()))
);
return v;
}
std::vector<Vertex> load_binary(const std::string filepath) {
printf("loading binary\n");
std::fstream fbin;
fbin.open(filepath.c_str(), std::ios::in | std::ios::binary);
fbin.seekg(80);
std::uint32_t num_faces;
fbin.read(reinterpret_cast<char *>(&num_faces), 4);
const unsigned int num_indices = num_faces*3;
size_t len = num_faces*50;
char *ret = new char[len];
fbin.read(ret, len);
std::vector<Vertex> all_vertices(num_indices);
for (int i=0;i<num_faces;i+=1) {
for (int j=0;j<3;j++) {
const int index = i*3+j;
std::memcpy(&all_vertices[index], &ret[12 + i*50 + j*12], 12);
}
}
fbin.close();
return all_vertices;
}
std::vector<Vertex> load_ascii(const std::string filepath) {
printf("loading ascii\n");
std::vector<Vertex> all_vertices;
std::ifstream file;
file.open(filepath.c_str());
std::string line;
while (!file.eof()) {
std::getline(file, line);
line = trim(line);
if (line.rfind("vertex", 0) == 0) {
all_vertices.push_back(get_vector(line));
}
}
file.close();
return all_vertices;
}
// thanks to https://github.com/mkeeter/fstl/blob/master/src/loader.cpp
std::vector<Vertex> load_stl(const std::string filepath) {
std::ifstream file(filepath);
std::string line;
std::getline(file, line);
if (line.rfind("solid ", 0) == 0) {
std::getline(file, line);
line = trim(line);
if (line.rfind("facet", 0) == 0)
{
file.close();
return load_ascii(filepath);
}
}
file.close();
return load_binary(filepath);
}
extern "C" {
void make_bin(const std::string filepath) {
auto all_vertices = load_stl(filepath.c_str());
const uint32_t num_indices = all_vertices.size();
for (int c=0;c<all_vertices.size();c++)
all_vertices[c].i = c;
std::uint32_t *indices;
indices = (std::uint32_t *) malloc(num_indices * sizeof(std::uint32_t));
std::sort(all_vertices.begin(), all_vertices.end());
float minx = 999999;
float miny = 999999;
float minz = 999999;
float maxx = -999999;
float maxy = -999999;
float maxz = -999999;
unsigned int num_vertices = 0;
for (auto v : all_vertices)
{
if (!num_vertices || v != all_vertices[num_vertices-1])
{
all_vertices[num_vertices++] = v;
if (v.x < minx) minx = v.x;
if (v.x > maxx) maxx = v.x;
if (v.y < miny) miny = v.y;
if (v.y > maxy) maxy = v.y;
if (v.z < minz) minz = v.z;
if (v.z > maxz) maxz = v.z;
}
indices[v.i] = num_vertices - 1;
}
all_vertices.resize(num_vertices);
// auto twrite = std::chrono::high_resolution_clock::now();
////////////////////// calculation done start writing ////////////////////
// total_blength, indices_blength, vertices_boffset, vertices_blength,
// number_indices, number_vertices, minx, miny, minz, maxx, maxy, maxz
const uint32_t indices_bytelength = num_indices*4;
const uint32_t vertices_bytelength = num_vertices*3*4;
std::fstream out_bin;
out_bin.open("out.bin", std::ios::out | std::ios::binary);
for (int i=0; i < num_indices; i++) {
out_bin.write((char*)&indices[i], 4);
}
// TODO: make sure padding is not necessary
// since indices bytelength are multiple of u32int
//const int padding_bytelength = (num_indices*4 + 3) & ~3 - num_indices*4;
//for (int i=0; i < padding_bytelength; i++) {
//out_bin.write(" ", 1); // this looks like problem
//}
for (int i=0; i < num_vertices; i++) {
out_bin.write((char*)&all_vertices[i], 12);
}
out_bin.close();
free(indices);
// auto twrite_end = std::chrono::high_resolution_clock::now();
// printf("write total took %lld milliseconds\n", std::chrono::duration_cast<std::chrono::milliseconds>(twrite_end-twrite).count());
printf("num indices %d\n", num_indices);
printf("num vertices %d\n", num_vertices);
// total_blength, indices_blength, vertices_boffset, vertices_blength,
// number_indices, number_vertices, minx, miny, minz, maxx, maxy, maxz
std::fstream gltf_data;
gltf_data.open("data.txt", std::ios::out);
gltf_data << num_indices << " ";
gltf_data << num_vertices << " ";
gltf_data << indices_bytelength << " ";
gltf_data << vertices_bytelength << " ";
gltf_data << indices_bytelength + vertices_bytelength << " ";
gltf_data << minx << " ";
gltf_data << miny << " ";
gltf_data << minz << " ";
gltf_data << maxx << " ";
gltf_data << maxy << " ";
gltf_data << maxz;
gltf_data.close();
printf("boundary %f %f %f\n", minx, miny, minz);
printf("boundary %f %f %f\n", maxx, maxy, maxz);
}
}
int main( int argc, char *argv[] )
{
std::string filepath = "meshes/perfect.stl";
if (argc < 2) {
printf("not stl file given use default \n");
} else {
filepath = argv[1];
}
make_bin(filepath);
}