-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathparse_stl.cpp
More file actions
66 lines (57 loc) · 1.7 KB
/
parse_stl.cpp
File metadata and controls
66 lines (57 loc) · 1.7 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
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <streambuf>
#include "parse_stl.h"
namespace stl {
std::ostream& operator<<(std::ostream& out, const point p) {
out << "(" << p.x << ", " << p.y << ", " << p.z << ")" << std::endl;
return out;
}
std::ostream& operator<<(std::ostream& out, const triangle& t) {
out << "---- TRIANGLE ----" << std::endl;
out << t.normal << std::endl;
out << t.v1 << std::endl;
out << t.v2 << std::endl;
out << t.v3 << std::endl;
return out;
}
float parse_float(std::ifstream& s) {
char f_buf[sizeof(float)];
s.read(f_buf, 4);
float* fptr = (float*) f_buf;
return *fptr;
}
point parse_point(std::ifstream& s) {
float x = parse_float(s);
float y = parse_float(s);
float z = parse_float(s);
return point(x, y, z);
}
stl_data parse_stl(const std::string& stl_path) {
std::ifstream stl_file(stl_path.c_str(), std::ios::in | std::ios::binary);
if (!stl_file) {
std::cout << "ERROR: COULD NOT READ FILE" << std::endl;
assert(false);
}
char header_info[80] = "";
char n_triangles[4];
stl_file.read(header_info, 80);
stl_file.read(n_triangles, 4);
std::string h(header_info);
stl_data info(h);
unsigned int* r = (unsigned int*) n_triangles;
unsigned int num_triangles = *r;
for (unsigned int i = 0; i < num_triangles; i++) {
auto normal = parse_point(stl_file);
auto v1 = parse_point(stl_file);
auto v2 = parse_point(stl_file);
auto v3 = parse_point(stl_file);
info.triangles.push_back(triangle(normal, v1, v2, v3));
char dummy[2];
stl_file.read(dummy, 2);
}
return info;
}
}