From: Matt Keeter Date: Sat, 22 Mar 2014 17:03:54 +0000 (-0400) Subject: Hash is slower; back to sorted list X-Git-Tag: v0.9.0~5 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=b6368a26636027836b82f3950dc02de49260decc;p=fstl Hash is slower; back to sorted list --- diff --git a/src/loader.cpp b/src/loader.cpp index f0f4268..107d90f 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -11,11 +11,7 @@ void Loader::run() QTime timer; timer.start(); emit got_mesh(Mesh::load_stl(filename)); - qDebug() << "Sorted:" << timer.elapsed(); - - timer.start(); - emit got_mesh(Mesh::load_stl_hash(filename)); - qDebug() << "Hash:" << timer.elapsed(); + qDebug() << "Time taken:" << timer.elapsed(); emit loaded_file(filename); } diff --git a/src/mesh.cpp b/src/mesh.cpp index be36d72..3e092d6 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -56,59 +56,6 @@ typedef std::pair Vec3i; //////////////////////////////////////////////////////////////////////////////// -Mesh* Mesh::load_stl_hash(const QString& filename) -{ - QFile file(filename); - file.open(QIODevice::ReadOnly); - - QDataStream data(&file); - data.setByteOrder(QDataStream::LittleEndian); - data.setFloatingPointPrecision(QDataStream::SinglePrecision); - - // Skip .stl file header - data.skipRawData(80); - - // Load the triangle count from the .stl file - uint32_t tri_count; - data >> tri_count; - - // This vector will store triangles as sets of 3 indices - std::vector indices(tri_count * 3); - - std::vector verts; - verts.reserve(tri_count * 9); - - QHash map; - map.reserve(tri_count * 3); - - float xyz[3]; - QByteArray v(sizeof(xyz), 0); - for (unsigned i=0; i < tri_count; ++i) - { - // Skip face's normal vector - data.readRawData(reinterpret_cast(xyz), 3*sizeof(float)); - - for (int j=0; j < 3; ++j) - { - data >> xyz[0] >> xyz[1] >> xyz[2]; - memcpy(v.data(), xyz, sizeof(xyz)); - if (!map.contains(v)) - { - map[v] = verts.size() / 3; - verts.push_back(xyz[0]); - verts.push_back(xyz[1]); - verts.push_back(xyz[2]); - } - indices[i*3 + j] = map[v]; - } - - // Skip face attribute - data.readRawData(reinterpret_cast(xyz), sizeof(uint16_t)); - } - - return new Mesh(verts, indices); -} - Mesh* Mesh::load_stl(const QString& filename) { QFile file(filename); @@ -127,12 +74,15 @@ Mesh* Mesh::load_stl(const QString& filename) // Extract vertices into an array of xyz, unsigned pairs QVector verts(tri_count*3); - float xyz[3]; + + // Dummy array, because readRawData is faster than skipRawData + char buffer[sizeof(float)*3]; + // Store vertices in the array, processing one triangle at a time. for (auto v=verts.begin(); v != verts.end(); v += 3) { // Skip face's normal vector - data.readRawData(reinterpret_cast(xyz), 3*sizeof(float)); + data.readRawData(buffer, 3*sizeof(float)); // Load vertex data from .stl file into vertices data >> v[0].first.x >> v[0].first.y >> v[0].first.z; @@ -140,7 +90,7 @@ Mesh* Mesh::load_stl(const QString& filename) data >> v[2].first.x >> v[2].first.y >> v[2].first.z; // Skip face attribute - data.readRawData(reinterpret_cast(xyz), sizeof(uint16_t)); + data.readRawData(buffer, sizeof(uint16_t)); } // Save indicies as the second element in the array @@ -171,7 +121,7 @@ Mesh* Mesh::load_stl(const QString& filename) } verts.resize(vertex_count); - std::vector flat_verts; + std::vector flat_verts; flat_verts.reserve(vertex_count*3); for (auto v : verts) { diff --git a/src/mesh.h b/src/mesh.h index 80f2a1e..f83a477 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -11,7 +11,6 @@ class Mesh public: Mesh(std::vector vertices, std::vector indices); static Mesh* load_stl(const QString& filename); - static Mesh* load_stl_hash(const QString& filename); float min(size_t start) const; float max(size_t start) const;