-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkSTL.hpp
159 lines (129 loc) · 3.68 KB
/
kSTL.hpp
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
#pragma once
#include <QString>
#include <QDataStream>
#include <vector>
#include "Facet.hpp"
namespace kSTL {
/**
* @brief The Mesh class
*
* This class loads, transforms, merges, saves and stores STL files
* They can be ASCII, binary, and with Unicode names
* It saves STLs as binaries, which is way more efficient than ASCII
*/
class Mesh {
public:
/**
* @brief Convinience class to handle STL objects, with Unicode filenames
* @param path
*/
Mesh(QString path);
/**
* @brief Init by copy
* @param base
*/
Mesh(Mesh* base);
/**
* @brief There is no pointers involved, the default copy operator works just fine
*/
Mesh& operator=(const Mesh&) = default;
/**
* @brief Loads the stl file (binary or ascii). Unicode file names can be loaded
* @param path
*/
void load(QString path);
/**
* @brief Saves the mesh in binary stl format. Filename can be Unicode
* @param path: the path to the file
* @param header: the additional data you can add to the file (80 bytes max)
* @param len: the length of the passed data (80 max)
*/
void save(QString path, const char* header = nullptr, std::size_t len = 0);
/**
* Destructor
*/
~Mesh();
/**
* @brief Resets the mesh to how it was when load() was called
*/
void reset();
/**
* @brief Translates the mesh along x, y, z
* @param x
* @param y
* @param z
*/
void translate(float x, float y, float z);
/**
* @brief Rotates the mesh, with z->y->x order
* x rotates around the x axis, and so on
* @param x
* @param y
* @param z
*/
void rotate(float x, float y, float z);
/**
* @brief Rescale the mesh along x, y, z
* @param x
* @param y
* @param z
*/
void scale(float x, float y, float z);
/**
* @brief Adds the triangles of with to this.
* The header and history are kept from this, with's are discarded
* @param The mesh to merge with
*/
void merge(kSTL::Mesh* with);
/**
* @brief Returns the minimum x,y,z of the mesh
* @return std::array<float, 3>
*/
Facet::Point min() const;
/**
* @brief Returns the maximum x,y,z of the mesh
* @return std::array<float, 3>
*/
Facet::Point max() const;
/**
* @brief Returns the size of the mesh
* @return std::array<float, 3>
*/
Facet::Point size() const;
/**
* @brief TODO Fixes the mesh (holes, duplicates, normals, etc...)
*/
void fix();
/**
* @brief Get the number of triangles in the mesh
* @return
*/
std::size_t num_triangles() const;
/**
* @brief Returns the coordinates of the specified vertex
* @param ti: Triangle id (0 .. num_triangles - 1)
* @param ci: Corner id (0 .. 2)
* @return
*/
const Facet::Point* coords_vertex(const std::size_t ti, const std::size_t ci) const;
/**
* @brief Returns the coordinates of the specified normal
* @param ti: Triangle id (0 .. num_triangles - 1)
* @return
*/
const Facet::Point* coords_normal(const std::size_t ti) const;
private:
bool is_binary(QString path);
void load_binary(QString path);
void compute_stats();
void load_ascii(QString path);
void add_float_to_stl(QDataStream& out, float what);
void add_int_to_stl(QDataStream& out, int what);
std::vector<Facet> mFacets;
std::vector<Facet> mOldFacets;
QString mHeader;
Facet::Point mMin = { std::numeric_limits<float>::max() };
Facet::Point mMax = { std::numeric_limits<float>::min() };
Facet::Point mSize = { 0.0f, 0.0f, 0.0f };
};
}