-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl.cpp
More file actions
354 lines (250 loc) · 7.68 KB
/
stl.cpp
File metadata and controls
354 lines (250 loc) · 7.68 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// -*- compile-command: "c++ -std=c++11 stl.cpp -o stl -lstdc++" -*-
#include <stdint.h>
#include <iostream>
#include <vector>
#include <fstream>
#include "parse.hpp"
namespace stl {
union uint32 {
char bytes[4];
std::uint32_t value;
};
std::istream& operator>>(std::istream& in, uint32& self) {
return in.read(self.bytes, sizeof(self.bytes));
}
union real32 {
char bytes[4];
float value;
static_assert(sizeof(float) == 4, "size error");
};
std::istream& operator>>(std::istream& in, real32& self) {
// TODO deal with endianess here(yummy)
return in.read(self.bytes, sizeof(self.bytes));
}
struct vec3 {
float x, y, z;
};
struct normal : vec3 {
using vec3::vec3;
normal(const vec3& v) : vec3(v) { }
};
static std::ostream& operator<<(std::ostream& out, const normal& self) {
return out << "normal " << self.x << ' ' << self.y << ' ' << self.z;
}
struct vertex : vec3 {
using vec3::vec3;
vertex(const vec3& v) : vec3(v) { }
};
static std::ostream& operator<<(std::ostream& out, const vertex& self) {
return out << "vertex " << self.x << ' ' << self.y << ' ' << self.z;
}
std::istream& operator>>(std::istream& in, vec3& self) {
real32 tmp;
for(float* ptr = &self.x; ptr < &self.x + 3; ++ptr) {
in >> tmp;
*ptr = tmp.value;
}
return in;
}
struct outer_loop {
vertex v0, v1, v2;
};
std::istream& operator>>(std::istream& in, outer_loop& self) {
return in >> self.v0 >> self.v1 >> self.v2;
}
static std::ostream& operator<<(std::ostream& out, const outer_loop& self) {
return out << "outer loop" << '\n'
<< " " << self.v0 << '\n'
<< " " << self.v1 << '\n'
<< " " << self.v2 << '\n'
<< "endloop";
}
union attribute_count {
char bytes[2];
std::uint16_t value;
};
std::istream& operator>>(std::istream& in, attribute_count& self) {
return in.read(self.bytes, sizeof(self.bytes));
}
struct triangle {
normal n;
outer_loop loop;
std::uint16_t attr;
};
static std::ostream& operator<<(std::ostream& out, const triangle& self) {
return out << "facet " << self.n << '\n'
<< self.loop << '\n'
<< "endfacet";
}
std::istream& operator>>(std::istream& in, triangle& self) {
attribute_count attr;
in >> self.n >> self.loop >> attr;
self.attr = attr.value;
return in;
}
struct file {
std::string info;
std::vector<triangle> triangles;
};
struct binary : file { };
std::istream& operator>>(std::istream& in, binary& self) {
self = binary();
char header[80];
in.read(header, sizeof(header));
self.info = header;
uint32 size;
in >> size;
triangle t;
attribute_count attr;
for(std::uint32_t i = 0; i < size.value; ++i) {
in >> t >> attr;
self.triangles.emplace_back( std::move(t) );
}
return in;
}
struct stream_pos {
std::istream& in;
const std::istream::pos_type pos;
stream_pos(std::istream& in)
: in(in),
pos(in.tellg()) {
}
~stream_pos() {
in.clear();
in.seekg(pos);
}
};
static bool is_binary(std::istream& in) {
const stream_pos backup(in);
in.seekg(0, std::ios::end);
const std::size_t file_size = in.tellg();
in.seekg(0, std::ios::beg);
char header[80];
if(!in.read(header, sizeof(header)) ) {
return false;
}
uint32 triangles;
if(!in.read(triangles.bytes, sizeof(triangles.bytes))) {
return false;
}
static const std::size_t float_size = 4;
static const std::size_t vec3_size = 3 * float_size;
static const std::size_t triangle_size = 4 * vec3_size + 2;
static const std::size_t header_size = 80 + 4;
const std::size_t expected_size = header_size + triangles.value * triangle_size;
return file_size == expected_size;
}
struct ascii : file { };
static std::ostream& operator<<(std::ostream& out, const ascii& self) {
out << "solid " << self.info << '\n';
for(const triangle& t : self.triangles) {
out << t << '\n';
}
return out << "endsolid " << self.info << '\n';
}
std::istream& operator>>(std::istream& in, ascii& self) {
using namespace parse;
in >> std::noskipws;
auto space = chr<' '>();
auto newline = chr<'\n'>();
auto eol = token(newline, space);
// keywords
auto solid = token(str("solid"), space);
auto endsolid = token(str("endsolid"), space);
auto facet = token(str("facet"), space);
auto endfacet = token(str("endfacet"), space);
auto normal = token(str("normal"), space);
auto vertex = token(str("vertex"), space);
auto outer = token(str("outer"), space);
auto loop = token(str("loop"), space);
auto endloop = token(str("endloop"), space);
// floating point numbers
using real_type = float;
auto num = token(lit<real_type>(), space);
// vec3
auto vec3 = num >> [&](real_type&& x) {
return num >> [&](real_type&& y) {
return num >> [&](real_type&& z) {
return pure( stl::vec3{x, y, z} );
};
};
};
// normal/vertex
auto normal_def = (normal, space, vec3) >> [](stl::vec3&& v) {
return pure( stl::normal(v) );
} >> drop(eol);
auto vertex_def = (vertex, space, vec3) >> [](stl::vec3&& v) {
return pure( stl::vertex(v) );
} >> drop(eol);
// loops
auto outer_loop = (outer, space, loop);
auto loop_def = (outer_loop, eol, vertex_def) >> [&](stl::vertex&& v0) {
return vertex_def >> [&](stl::vertex&& v1) {
return vertex_def >> [&](stl::vertex&& v2) {
stl::outer_loop res = {v0, v1, v2};
return pure(std::move(res));
};
};
} >> drop(endloop) >> drop(eol);
// facets
auto facet_def = (facet, space, normal_def) >> [&](stl::vec3&& n) {
return loop_def >> [&](stl::outer_loop&& loop) {
stl::triangle res;
res.loop = loop;
res.n = n;
return pure(std::move(res));
};
}
>> drop(endfacet) >> drop(eol);
// solids
auto name = +!newline;
auto solid_name = (solid, space, name) >> drop(eol);
auto endsolid_name = (endsolid, space, name) >> drop(eol);
auto solid_def = solid_name >> [&](std::vector<char>&& sname) {
return +facet_def >> [&](std::vector<triangle>&& triangles) {
return endsolid_name >> [&](std::vector<char>&& ename) {
if(ename != sname) {
throw std::runtime_error("bad endsolid name");
}
stl::ascii res;
res.info = std::string(sname.begin(), sname.end());
res.triangles = std::move(triangles);
return pure( std::move(res) );
};
};
};
auto parser = solid_def;
const auto res = parser(in);
if(!res) {
throw std::runtime_error("parse error");
} else {
std::clog << "success" << std::endl;
}
// self = std::move(res.get());
return in;
}
static std::istream& operator>>(std::istream& in, file& self) {
if( is_binary(in) ) {
binary b;
in >> b;
self = b;
} else {
ascii a;
in >> a;
self = a;
}
return in;
};
}
int main(int argc, char** argv) {
if(argc < 2) {
std::cerr << "usage: " << argv[0] << " input.stl" << std::endl;
return 1;
}
std::ifstream input(argv[1]);
stl::file file;
input >> file;
// print first normal
//std::cout << file.info << " " << n.x << " " << n.y << " " << n.z << std::endl;
return 0;
}