Skip to content

Commit 61c9304

Browse files
committed
Move load_stl from Mesh to Loader
1 parent 9cc3bd8 commit 61c9304

File tree

4 files changed

+115
-119
lines changed

4 files changed

+115
-119
lines changed

src/loader.cpp

+111-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "loader.h"
2-
#include "mesh.h"
32

43
Loader::Loader(QObject* parent, const QString& filename)
54
: QThread(parent), filename(filename)
@@ -9,29 +8,122 @@ Loader::Loader(QObject* parent, const QString& filename)
98

109
void Loader::run()
1110
{
12-
{ // Verify that this isn't an ascii stl file
13-
QFile file(filename);
14-
file.open(QIODevice::ReadOnly);
15-
if (file.read(5) == "solid")
16-
{
17-
emit error_ascii_stl();
18-
return;
19-
}
11+
Mesh* mesh = load_stl();
12+
if (mesh)
13+
{
14+
emit got_mesh(mesh);
15+
emit loaded_file(filename);
16+
}
17+
}
18+
19+
20+
////////////////////////////////////////////////////////////////////////////////
21+
22+
struct Vec3
23+
{
24+
GLfloat x, y, z;
25+
bool operator!=(const Vec3& rhs) const
26+
{
27+
return x != rhs.x || y != rhs.y || z != rhs.z;
28+
}
29+
bool operator<(const Vec3& rhs) const
30+
{
31+
if (x != rhs.x) return x < rhs.x;
32+
else if (y != rhs.y) return y < rhs.y;
33+
else if (z != rhs.z) return z < rhs.z;
34+
else return false;
35+
}
36+
};
37+
38+
typedef std::pair<Vec3, GLuint> Vec3i;
39+
40+
////////////////////////////////////////////////////////////////////////////////
41+
42+
Mesh* Loader::load_stl()
43+
{
44+
QFile file(filename);
45+
file.open(QIODevice::ReadOnly);
46+
if (file.read(5) == "solid")
47+
{
48+
emit error_ascii_stl();
49+
return NULL;
50+
}
51+
// Skip the rest of the header material
52+
file.read(75);
2053

21-
// Skip the rest of the buffer
22-
file.read(75);
54+
QDataStream data(&file);
55+
data.setByteOrder(QDataStream::LittleEndian);
56+
data.setFloatingPointPrecision(QDataStream::SinglePrecision);
2357

24-
// Assume we're on a little-endian system for simplicity
25-
uint32_t tri_count;
26-
file.read(reinterpret_cast<char*>(&tri_count), sizeof(tri_count));
58+
// Load the triangle count from the .stl file
59+
uint32_t tri_count;
60+
data >> tri_count;
2761

28-
if (file.size() != 84 + tri_count*50)
62+
// Verify that the file is the right size
63+
if (file.size() != 84 + tri_count*50)
64+
{
65+
emit error_bad_stl();
66+
return NULL;
67+
}
68+
69+
// Extract vertices into an array of xyz, unsigned pairs
70+
QVector<Vec3i> verts(tri_count*3);
71+
72+
// Dummy array, because readRawData is faster than skipRawData
73+
char buffer[sizeof(float)*3];
74+
75+
// Store vertices in the array, processing one triangle at a time.
76+
for (auto v=verts.begin(); v != verts.end(); v += 3)
77+
{
78+
// Skip face's normal vector
79+
data.readRawData(buffer, 3*sizeof(float));
80+
81+
// Load vertex data from .stl file into vertices
82+
data >> v[0].first.x >> v[0].first.y >> v[0].first.z;
83+
data >> v[1].first.x >> v[1].first.y >> v[1].first.z;
84+
data >> v[2].first.x >> v[2].first.y >> v[2].first.z;
85+
86+
// Skip face attribute
87+
data.readRawData(buffer, sizeof(uint16_t));
88+
}
89+
90+
// Save indicies as the second element in the array
91+
// (so that we can reconstruct triangle order after sorting)
92+
for (size_t i=0; i < tri_count*3; ++i)
93+
{
94+
verts[i].second = i;
95+
}
96+
97+
// Sort the set of vertices (to deduplicate)
98+
std::sort(verts.begin(), verts.end());
99+
100+
// This vector will store triangles as sets of 3 indices
101+
std::vector<GLuint> indices(tri_count*3);
102+
103+
// Go through the sorted vertex list, deduplicating and creating
104+
// an indexed geometry representation for the triangles.
105+
// Unique vertices are moved so that they occupy the first vertex_count
106+
// positions in the verts array.
107+
size_t vertex_count = 0;
108+
for (auto v : verts)
109+
{
110+
if (!vertex_count || v.first != verts[vertex_count-1].first)
29111
{
30-
emit error_bad_stl();
31-
return;
112+
verts[vertex_count++] = v;
32113
}
114+
indices[v.second] = vertex_count - 1;
33115
}
116+
verts.resize(vertex_count);
34117

35-
emit got_mesh(Mesh::load_stl(filename));
36-
emit loaded_file(filename);
118+
std::vector<GLfloat> flat_verts;
119+
flat_verts.reserve(vertex_count*3);
120+
for (auto v : verts)
121+
{
122+
flat_verts.push_back(v.first.x);
123+
flat_verts.push_back(v.first.y);
124+
flat_verts.push_back(v.first.z);
125+
}
126+
127+
return new Mesh(flat_verts, indices);
37128
}
129+

src/loader.h

+4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ class Loader : public QThread
1212
explicit Loader(QObject* parent, const QString& filename);
1313
void run();
1414

15+
protected:
16+
Mesh* load_stl();
17+
1518
signals:
1619
void loaded_file(QString filename);
1720
void got_mesh(Mesh* m);
21+
1822
void error_ascii_stl();
1923
void error_bad_stl();
2024

src/mesh.cpp

-99
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <QDataStream>
33
#include <QVector3D>
44

5-
#include <algorithm>
65
#include <cmath>
76

87
#include "mesh.h"
@@ -34,101 +33,3 @@ float Mesh::max(size_t start) const
3433
}
3534
return v;
3635
}
37-
////////////////////////////////////////////////////////////////////////////////
38-
39-
struct Vec3
40-
{
41-
GLfloat x, y, z;
42-
bool operator!=(const Vec3& rhs) const
43-
{
44-
return x != rhs.x || y != rhs.y || z != rhs.z;
45-
}
46-
bool operator<(const Vec3& rhs) const
47-
{
48-
if (x != rhs.x) return x < rhs.x;
49-
else if (y != rhs.y) return y < rhs.y;
50-
else if (z != rhs.z) return z < rhs.z;
51-
else return false;
52-
}
53-
};
54-
55-
typedef std::pair<Vec3, GLuint> Vec3i;
56-
57-
////////////////////////////////////////////////////////////////////////////////
58-
59-
Mesh* Mesh::load_stl(const QString& filename)
60-
{
61-
QFile file(filename);
62-
file.open(QIODevice::ReadOnly);
63-
64-
QDataStream data(&file);
65-
data.setByteOrder(QDataStream::LittleEndian);
66-
data.setFloatingPointPrecision(QDataStream::SinglePrecision);
67-
68-
// Skip .stl file header
69-
data.skipRawData(80);
70-
71-
// Load the triangle count from the .stl file
72-
uint32_t tri_count;
73-
data >> tri_count;
74-
75-
// Extract vertices into an array of xyz, unsigned pairs
76-
QVector<Vec3i> verts(tri_count*3);
77-
78-
// Dummy array, because readRawData is faster than skipRawData
79-
char buffer[sizeof(float)*3];
80-
81-
// Store vertices in the array, processing one triangle at a time.
82-
for (auto v=verts.begin(); v != verts.end(); v += 3)
83-
{
84-
// Skip face's normal vector
85-
data.readRawData(buffer, 3*sizeof(float));
86-
87-
// Load vertex data from .stl file into vertices
88-
data >> v[0].first.x >> v[0].first.y >> v[0].first.z;
89-
data >> v[1].first.x >> v[1].first.y >> v[1].first.z;
90-
data >> v[2].first.x >> v[2].first.y >> v[2].first.z;
91-
92-
// Skip face attribute
93-
data.readRawData(buffer, sizeof(uint16_t));
94-
}
95-
96-
// Save indicies as the second element in the array
97-
// (so that we can reconstruct triangle order after sorting)
98-
for (size_t i=0; i < tri_count*3; ++i)
99-
{
100-
verts[i].second = i;
101-
}
102-
103-
// Sort the set of vertices (to deduplicate)
104-
std::sort(verts.begin(), verts.end());
105-
106-
// This vector will store triangles as sets of 3 indices
107-
std::vector<GLuint> indices(tri_count*3);
108-
109-
// Go through the sorted vertex list, deduplicating and creating
110-
// an indexed geometry representation for the triangles.
111-
// Unique vertices are moved so that they occupy the first vertex_count
112-
// positions in the verts array.
113-
size_t vertex_count = 0;
114-
for (auto v : verts)
115-
{
116-
if (!vertex_count || v.first != verts[vertex_count-1].first)
117-
{
118-
verts[vertex_count++] = v;
119-
}
120-
indices[v.second] = vertex_count - 1;
121-
}
122-
verts.resize(vertex_count);
123-
124-
std::vector<GLfloat> flat_verts;
125-
flat_verts.reserve(vertex_count*3);
126-
for (auto v : verts)
127-
{
128-
flat_verts.push_back(v.first.x);
129-
flat_verts.push_back(v.first.y);
130-
flat_verts.push_back(v.first.z);
131-
}
132-
133-
return new Mesh(flat_verts, indices);
134-
}

src/mesh.h

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class Mesh
1010
{
1111
public:
1212
Mesh(std::vector<GLfloat> vertices, std::vector<GLuint> indices);
13-
static Mesh* load_stl(const QString& filename);
1413

1514
float min(size_t start) const;
1615
float max(size_t start) const;

0 commit comments

Comments
 (0)