-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTL.cpp
86 lines (69 loc) · 2.42 KB
/
STL.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
#include "STL.h"
#include "R3Graph.h"
#include <fstream>
#include <iomanip>
void WriteSTLBinary(const Triangulation& triangulation, std::string& filename)
{
std::ofstream out;
// Check path after merging
//filename = "C:\\Users\\owchi\\source\\repos\\Triangulation\\bin64\\" + filename;
out.open(filename, std::ios::out | std::ios::binary);
if (out.is_open())
{
char header[80];
std::memset(header, 0, 80);
out.write(header, 80);
auto numTriang = triangulation.triangles.size();
out.write((const char*)(&numTriang), 4);
for (int i = 0; i < triangulation.triangles.size(); ++i)
{
const Triangulation::Triangle& t = triangulation.triangles.at(i);
R3Graph::R3Point p[4];
p[0] = t.Normal;
p[1] = triangulation.vertices.at(t.indices[0]).point;
p[2] = triangulation.vertices.at(t.indices[1]).point;
p[3] = triangulation.vertices.at(t.indices[2]).point;
for (int i = 0; i < 4; ++i)
{
float vec[3];
vec[0] = (float)p[i].x;
vec[1] = (float)p[i].y;
vec[2] = (float)p[i].z;
out.write((const char*)&vec[0], 4);
out.write((const char*)&vec[1], 4);
out.write((const char*)&vec[2], 4);
}
unsigned short dummy = 0;
out.write((const char*)&dummy, 2);
}
}
out.close();
}
void WriteStlASCII(const Triangulation& triangulation, std::string& filename)
{
std::ofstream out;
out.open(filename);
if (out.is_open())
{
out << "solid iso" << std::endl;
for (int i = 0; i < triangulation.triangles.size(); ++i)
{
out << "\t" << "facet normal ";
Triangulation::Triangle t = triangulation.triangles.at(i);
R3Graph::R3Point p0 = triangulation.vertices.at(t.indices[0]).point;
R3Graph::R3Point p1 = triangulation.vertices.at(t.indices[1]).point;
R3Graph::R3Point p2 = triangulation.vertices.at(t.indices[2]).point;
//out << std::setprecision(6);
out << std::setprecision(9) << t.Normal.x << " " << t.Normal.y << " " << t.Normal.z << std::endl;
out << "\t" << "outer loop" << std::endl;
//out << std::setprecision(6);
out << "\t\t" << "vertex " << std::setprecision(9) << p0.x << " " << p0.y << " " << p0.z << std::endl;
out << "\t\t" << "vertex " << std::setprecision(9) << p1.x << " " << p1.y << " " << p1.z << std::endl;
out << "\t\t" << "vertex " << std::setprecision(9) << p2.x << " " << p2.y << " " << p2.z << std::endl;
out << "\t" << "endloop" << std::endl;
out << "\t" << "endfacet" << std::endl;
}
out << "endsolid iso";
}
out.close();
}