5 Loader::Loader(QObject* parent, const QString& filename, bool is_reload)
6 : QThread(parent), filename(filename), is_reload(is_reload)
13 Mesh* mesh = load_stl();
18 emit error_empty_mesh();
23 emit got_mesh(mesh, is_reload);
24 emit loaded_file(filename);
29 ////////////////////////////////////////////////////////////////////////////////
34 bool operator!=(const Vec3& rhs) const
36 return x != rhs.x || y != rhs.y || z != rhs.z;
38 bool operator<(const Vec3& rhs) const
40 if (x != rhs.x) return x < rhs.x;
41 else if (y != rhs.y) return y < rhs.y;
42 else if (z != rhs.z) return z < rhs.z;
47 typedef std::pair<Vec3, GLuint> Vec3i;
49 void parallel_sort(Vec3i* begin, Vec3i* end, int threads)
51 if (threads < 2 || end - begin < 2)
53 std::sort(begin, end);
57 const auto mid = begin + (end - begin) / 2;
60 auto future = std::async(parallel_sort, begin, mid, threads / 2);
66 auto a = std::async(std::launch::async, parallel_sort, begin, mid, threads / 2);
67 auto b = std::async(std::launch::async, parallel_sort, mid, end, threads / 2);
71 std::inplace_merge(begin, mid, end);
75 Mesh* mesh_from_verts(uint32_t tri_count, QVector<Vec3i>& verts)
77 // Save indicies as the second element in the array
78 // (so that we can reconstruct triangle order after sorting)
79 for (size_t i=0; i < tri_count*3; ++i)
84 // Sort the set of vertices (to deduplicate)
85 parallel_sort(verts.begin(), verts.end(), 8);
87 // This vector will store triangles as sets of 3 indices
88 std::vector<GLuint> indices(tri_count*3);
90 // Go through the sorted vertex list, deduplicating and creating
91 // an indexed geometry representation for the triangles.
92 // Unique vertices are moved so that they occupy the first vertex_count
93 // positions in the verts array.
94 size_t vertex_count = 0;
97 if (!vertex_count || v.first != verts[vertex_count-1].first)
99 verts[vertex_count++] = v;
101 indices[v.second] = vertex_count - 1;
103 verts.resize(vertex_count);
105 std::vector<GLfloat> flat_verts;
106 flat_verts.reserve(vertex_count*3);
109 flat_verts.push_back(v.first.x);
110 flat_verts.push_back(v.first.y);
111 flat_verts.push_back(v.first.z);
114 return new Mesh(flat_verts, indices);
117 ////////////////////////////////////////////////////////////////////////////////
119 Mesh* Loader::load_stl()
121 QFile file(filename);
122 if (!file.open(QIODevice::ReadOnly))
124 emit error_missing_file();
128 // First, try to read the stl as an ASCII file
129 if (file.read(6) == "solid ")
131 file.readLine(); // skip solid name
132 const auto line = file.readLine().trimmed();
133 if (line.startsWith("facet") ||
134 line.startsWith("endsolid"))
137 return read_stl_ascii(file);
139 confusing_stl = true;
143 confusing_stl = false;
146 // Otherwise, skip the rest of the header material and read as binary
148 return read_stl_binary(file);
151 Mesh* Loader::read_stl_binary(QFile& file)
153 QDataStream data(&file);
154 data.setByteOrder(QDataStream::LittleEndian);
155 data.setFloatingPointPrecision(QDataStream::SinglePrecision);
157 // Load the triangle count from the .stl file
162 // Verify that the file is the right size
163 if (file.size() != 84 + tri_count*50)
165 emit error_bad_stl();
169 // Extract vertices into an array of xyz, unsigned pairs
170 QVector<Vec3i> verts(tri_count*3);
172 // Dummy array, because readRawData is faster than skipRawData
173 uint8_t* buffer = (uint8_t*)malloc(tri_count * 50);
174 data.readRawData((char*)buffer, tri_count * 50);
176 // Store vertices in the array, processing one triangle at a time.
178 for (auto v=verts.begin(); v != verts.end(); v += 3)
180 // Skip face's normal vector
181 b += 3 * sizeof(float);
183 // Load vertex data from .stl file into vertices
184 for (unsigned i=0; i < 3; ++i)
186 memcpy(&v[i].first, b, 3*sizeof(float));
187 b += 3 * sizeof(float);
190 // Skip face attribute
191 b += sizeof(uint16_t);
196 emit warning_confusing_stl();
200 return mesh_from_verts(tri_count, verts);
203 Mesh* Loader::read_stl_ascii(QFile& file)
206 uint32_t tri_count = 0;
207 QVector<Vec3i> verts(tri_count*3);
210 while (!file.atEnd() && okay)
212 const auto line = file.readLine().simplified();
213 if (line.startsWith("endsolid"))
217 else if (!line.startsWith("facet normal") ||
218 !file.readLine().simplified().startsWith("outer loop"))
224 for (int i=0; i < 3; ++i)
226 auto line = file.readLine().simplified().split(' ');
227 if (line[0] != "vertex")
232 const float x = line[1].toFloat(&okay);
233 const float y = line[2].toFloat(&okay);
234 const float z = line[3].toFloat(&okay);
235 verts.push_back({{x, y, z}, 0});
237 if (!file.readLine().trimmed().startsWith("endloop") ||
238 !file.readLine().trimmed().startsWith("endfacet"))
248 return mesh_from_verts(tri_count, verts);
252 emit error_bad_stl();