-
Notifications
You must be signed in to change notification settings - Fork 3
/
loader.cpp
249 lines (218 loc) · 6.39 KB
/
loader.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
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
#include <future>
#include "loader.h"
#include "vertex.h"
Loader::Loader(QObject* parent, const QString& filename, bool is_reload)
: QThread(parent), filename(filename), is_reload(is_reload)
{
// Nothing to do here
}
void Loader::run()
{
Mesh* mesh = load_stl();
if (mesh)
{
if (mesh->empty())
{
emit error_empty_mesh();
delete mesh;
}
else
{
emit got_mesh(mesh, is_reload);
emit loaded_file(filename);
}
}
}
////////////////////////////////////////////////////////////////////////////////
void parallel_sort(Vertex* begin, Vertex* end, int threads)
{
if (threads < 2 || end - begin < 2)
{
std::sort(begin, end);
}
else
{
const auto mid = begin + (end - begin) / 2;
if (threads == 2)
{
auto future = std::async(parallel_sort, begin, mid, threads / 2);
std::sort(mid, end);
future.wait();
}
else
{
auto a = std::async(std::launch::async, parallel_sort, begin, mid, threads / 2);
auto b = std::async(std::launch::async, parallel_sort, mid, end, threads / 2);
a.wait();
b.wait();
}
std::inplace_merge(begin, mid, end);
}
}
Mesh* Loader::empty_mesh(){
std::vector<GLuint> indices(0);
std::vector<GLfloat> verts(0);
return new Mesh(std::move(verts), std::move(indices));
}
Mesh* mesh_from_verts(uint32_t tri_count, QVector<Vertex>& verts)
{
// Save indicies as the second element in the array
// (so that we can reconstruct triangle order after sorting)
for (size_t i=0; i < tri_count*3; ++i)
{
verts[i].i = i;
}
// Check how many threads the hardware can safely support. This may return
// 0 if the property can't be read so we shoud check for that too.
auto threads = std::thread::hardware_concurrency();
if (threads == 0)
{
threads = 8;
}
// Sort the set of vertices (to deduplicate)
parallel_sort(verts.begin(), verts.end(), threads);
// This vector will store triangles as sets of 3 indices
std::vector<GLuint> indices(tri_count*3);
// Go through the sorted vertex list, deduplicating and creating
// an indexed geometry representation for the triangles.
// Unique vertices are moved so that they occupy the first vertex_count
// positions in the verts array.
size_t vertex_count = 0;
for (auto v : verts)
{
if (!vertex_count || v != verts[vertex_count-1])
{
verts[vertex_count++] = v;
}
indices[v.i] = vertex_count - 1;
}
verts.resize(vertex_count);
std::vector<GLfloat> flat_verts;
flat_verts.reserve(vertex_count*3);
for (auto v : verts)
{
flat_verts.push_back(v.x);
flat_verts.push_back(v.y);
flat_verts.push_back(v.z);
}
return new Mesh(std::move(flat_verts), std::move(indices));
}
////////////////////////////////////////////////////////////////////////////////
Mesh* Loader::load_stl()
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
{
emit error_missing_file();
return NULL;
}
// First, try to read the stl as an ASCII file
if (file.read(5) == "solid")
{
file.readLine(); // skip solid name
const auto line = file.readLine().trimmed();
if (line.startsWith("facet") ||
line.startsWith("endsolid"))
{
file.seek(0);
return read_stl_ascii(file);
}
confusing_stl = true;
}
else
{
confusing_stl = false;
}
// Otherwise, skip the rest of the header material and read as binary
file.seek(0);
return read_stl_binary(file);
}
Mesh* Loader::read_stl_binary(QFile& file)
{
QDataStream data(&file);
data.setByteOrder(QDataStream::LittleEndian);
data.setFloatingPointPrecision(QDataStream::SinglePrecision);
// Load the triangle count from the .stl file
file.seek(80);
uint32_t tri_count;
data >> tri_count;
// Verify that the file is the right size
if (file.size() != 84 + tri_count*50)
{
emit error_bad_stl();
return NULL;
}
// Extract vertices into an array of xyz, unsigned pairs
QVector<Vertex> verts(tri_count*3);
// Dummy array, because readRawData is faster than skipRawData
std::unique_ptr<uint8_t> buffer(new uint8_t[tri_count * 50]);
data.readRawData((char*)buffer.get(), tri_count * 50);
// Store vertices in the array, processing one triangle at a time.
auto b = buffer.get() + 3 * sizeof(float);
for (auto v=verts.begin(); v != verts.end(); v += 3)
{
// Load vertex data from .stl file into vertices
for (unsigned i=0; i < 3; ++i)
{
memcpy(&v[i], b, 3*sizeof(float));
b += 3 * sizeof(float);
}
// Skip face attribute and next face's normal vector
b += 3 * sizeof(float) + sizeof(uint16_t);
}
if (confusing_stl)
{
emit warning_confusing_stl();
}
return mesh_from_verts(tri_count, verts);
}
Mesh* Loader::read_stl_ascii(QFile& file)
{
file.readLine();
uint32_t tri_count = 0;
QVector<Vertex> verts(tri_count*3);
bool okay = true;
while (!file.atEnd() && okay)
{
const auto line = file.readLine().simplified();
if (line.startsWith("endsolid"))
{
break;
}
else if (!line.startsWith("facet normal") ||
!file.readLine().simplified().startsWith("outer loop"))
{
okay = false;
break;
}
for (int i=0; i < 3; ++i)
{
auto line = file.readLine().simplified().split(' ');
if (line[0] != "vertex")
{
okay = false;
break;
}
const float x = line[1].toFloat(&okay);
const float y = line[2].toFloat(&okay);
const float z = line[3].toFloat(&okay);
verts.push_back(Vertex(x, y, z));
}
if (!file.readLine().trimmed().startsWith("endloop") ||
!file.readLine().trimmed().startsWith("endfacet"))
{
okay = false;
break;
}
tri_count++;
}
if (okay)
{
return mesh_from_verts(tri_count, verts);
}
else
{
emit error_bad_stl();
return NULL;
}
}