#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)
////////////////////////////////////////////////////////////////////////////////
-struct Vec3
-{
- GLfloat x, y, z;
- bool operator!=(const Vec3& rhs) const
- {
- return x != rhs.x || y != rhs.y || z != rhs.z;
- }
- bool operator<(const Vec3& rhs) const
- {
- if (x != rhs.x) return x < rhs.x;
- else if (y != rhs.y) return y < rhs.y;
- else if (z != rhs.z) return z < rhs.z;
- else return false;
- }
-};
-
-typedef std::pair<Vec3, GLuint> Vec3i;
-
-void parallel_sort(Vec3i* begin, Vec3i* end, int threads)
+void parallel_sort(Vertex* begin, Vertex* end, int threads)
{
if (threads < 2 || end - begin < 2)
{
}
}
-Mesh* mesh_from_verts(uint32_t tri_count, QVector<Vec3i>& verts)
+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].second = 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(), 8);
+ parallel_sort(verts.begin(), verts.end(), threads);
// This vector will store triangles as sets of 3 indices
std::vector<GLuint> indices(tri_count*3);
size_t vertex_count = 0;
for (auto v : verts)
{
- if (!vertex_count || v.first != verts[vertex_count-1].first)
+ if (!vertex_count || v != verts[vertex_count-1])
{
verts[vertex_count++] = v;
}
- indices[v.second] = vertex_count - 1;
+ indices[v.i] = vertex_count - 1;
}
verts.resize(vertex_count);
flat_verts.reserve(vertex_count*3);
for (auto v : verts)
{
- flat_verts.push_back(v.first.x);
- flat_verts.push_back(v.first.y);
- flat_verts.push_back(v.first.z);
+ flat_verts.push_back(v.x);
+ flat_verts.push_back(v.y);
+ flat_verts.push_back(v.z);
}
- return new Mesh(flat_verts, indices);
+ return new Mesh(std::move(flat_verts), std::move(indices));
}
////////////////////////////////////////////////////////////////////////////////
}
// First, try to read the stl as an ASCII file
- if (file.read(6) == "solid ")
+ if (file.read(5) == "solid")
{
file.readLine(); // skip solid name
const auto line = file.readLine().trimmed();
}
// Extract vertices into an array of xyz, unsigned pairs
- QVector<Vec3i> verts(tri_count*3);
+ QVector<Vertex> verts(tri_count*3);
// Dummy array, because readRawData is faster than skipRawData
- uint8_t* buffer = (uint8_t*)malloc(tri_count * 50);
- data.readRawData((char*)buffer, tri_count * 50);
+ 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;
+ auto b = buffer.get() + 3 * sizeof(float);
for (auto v=verts.begin(); v != verts.end(); v += 3)
{
- // Skip face's normal vector
- b += 3 * sizeof(float);
-
// Load vertex data from .stl file into vertices
for (unsigned i=0; i < 3; ++i)
{
- memcpy(&v[i].first, b, 3*sizeof(float));
+ memcpy(&v[i], b, 3*sizeof(float));
b += 3 * sizeof(float);
}
- // Skip face attribute
- b += sizeof(uint16_t);
+ // Skip face attribute and next face's normal vector
+ b += 3 * sizeof(float) + sizeof(uint16_t);
}
if (confusing_stl)
{
emit warning_confusing_stl();
}
- free(buffer);
return mesh_from_verts(tri_count, verts);
}
{
file.readLine();
uint32_t tri_count = 0;
- QVector<Vec3i> verts(tri_count*3);
+ QVector<Vertex> verts(tri_count*3);
bool okay = true;
while (!file.atEnd() && okay)
const float x = line[1].toFloat(&okay);
const float y = line[2].toFloat(&okay);
const float z = line[3].toFloat(&okay);
- verts.push_back({{x, y, z}, 0});
+ verts.push_back(Vertex(x, y, z));
}
if (!file.readLine().trimmed().startsWith("endloop") ||
!file.readLine().trimmed().startsWith("endfacet"))