diff --git a/app/package.sh b/app/package.sh index d51c470..da97313 100755 --- a/app/package.sh +++ b/app/package.sh @@ -10,5 +10,5 @@ rm empty.lproj cd ../../.. cp -r fstl.app .. cd .. -zip -r fstl_mac.zip fstl.app README.md +zip -r fstl.zip fstl.app README.md diff --git a/exe/package.sh b/exe/package.sh index ef81b62..5faecf1 100644 --- a/exe/package.sh +++ b/exe/package.sh @@ -1,3 +1,3 @@ cd .. cp build/release/fstl.exe . -/c/Program\ Files/7-Zip/7z.exe a fstl_win.zip fstl.exe README.md +/c/Program\ Files/7-Zip/7z.exe a fstl.zip fstl.exe README.md diff --git a/src/app.cpp b/src/app.cpp index b6eb3af..afa4ea7 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -8,10 +8,7 @@ App::App(int argc, char *argv[]) : QApplication(argc, argv), window(new Window()) { window->show(); - if (argc > 1) - window->load_stl(argv[1]); - else - window->load_stl(":gl/sphere.stl"); + window->load_stl(":gl/sphere.stl"); } bool App::event(QEvent* e) diff --git a/src/loader.cpp b/src/loader.cpp index 0d6b165..d7ce46a 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -1,129 +1,23 @@ #include "loader.h" +#include "mesh.h" Loader::Loader(QObject* parent, const QString& filename) : QThread(parent), filename(filename) { - // Nothing to do here } void Loader::run() { - Mesh* mesh = load_stl(); - if (mesh) - { - emit got_mesh(mesh); - emit loaded_file(filename); - } -} - - -//////////////////////////////////////////////////////////////////////////////// - -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 Vec3i; - -//////////////////////////////////////////////////////////////////////////////// - -Mesh* Loader::load_stl() -{ - QFile file(filename); - file.open(QIODevice::ReadOnly); - if (file.read(5) == "solid") - { - emit error_ascii_stl(); - return NULL; - } - // Skip the rest of the header material - file.read(75); - - QDataStream data(&file); - data.setByteOrder(QDataStream::LittleEndian); - data.setFloatingPointPrecision(QDataStream::SinglePrecision); - - // Load the triangle count from the .stl file - 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 verts(tri_count*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(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; - data >> v[1].first.x >> v[1].first.y >> v[1].first.z; - data >> v[2].first.x >> v[2].first.y >> v[2].first.z; - - // Skip face attribute - data.readRawData(buffer, sizeof(uint16_t)); - } - - // 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; - } - - // Sort the set of vertices (to deduplicate) - std::sort(verts.begin(), verts.end()); - - // This vector will store triangles as sets of 3 indices - std::vector 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.first != verts[vertex_count-1].first) + { // Verify that this isn't an ascii stl file + QFile file(filename); + file.open(QIODevice::ReadOnly); + if (file.read(5) == "solid") { - verts[vertex_count++] = v; + emit error_ascii_stl(); + return; } - indices[v.second] = vertex_count - 1; - } - verts.resize(vertex_count); - - std::vector flat_verts; - 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); } - return new Mesh(flat_verts, indices); + emit got_mesh(Mesh::load_stl(filename)); + emit loaded_file(filename); } - diff --git a/src/loader.h b/src/loader.h index fb0c8d8..85e0c53 100644 --- a/src/loader.h +++ b/src/loader.h @@ -12,15 +12,10 @@ public: explicit Loader(QObject* parent, const QString& filename); void run(); -protected: - Mesh* load_stl(); - signals: void loaded_file(QString filename); void got_mesh(Mesh* m); - void error_ascii_stl(); - void error_bad_stl(); private: const QString filename; diff --git a/src/mesh.cpp b/src/mesh.cpp index cfb4ca4..3e092d6 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "mesh.h" @@ -33,3 +34,101 @@ float Mesh::max(size_t start) const } return v; } +//////////////////////////////////////////////////////////////////////////////// + +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 Vec3i; + +//////////////////////////////////////////////////////////////////////////////// + +Mesh* Mesh::load_stl(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; + + // Extract vertices into an array of xyz, unsigned pairs + QVector verts(tri_count*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(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; + data >> v[1].first.x >> v[1].first.y >> v[1].first.z; + data >> v[2].first.x >> v[2].first.y >> v[2].first.z; + + // Skip face attribute + data.readRawData(buffer, sizeof(uint16_t)); + } + + // 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; + } + + // Sort the set of vertices (to deduplicate) + std::sort(verts.begin(), verts.end()); + + // This vector will store triangles as sets of 3 indices + std::vector 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.first != verts[vertex_count-1].first) + { + verts[vertex_count++] = v; + } + indices[v.second] = vertex_count - 1; + } + verts.resize(vertex_count); + + std::vector flat_verts; + 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); + } + + return new Mesh(flat_verts, indices); +} diff --git a/src/mesh.h b/src/mesh.h index e8a02f0..f83a477 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -10,6 +10,7 @@ class Mesh { public: Mesh(std::vector vertices, std::vector indices); + static Mesh* load_stl(const QString& filename); float min(size_t start) const; float max(size_t start) const; diff --git a/src/window.cpp b/src/window.cpp index 3002ecc..5f8ac9e 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -14,7 +14,6 @@ Window::Window(QWidget *parent) : { setWindowTitle("fstl"); - setAcceptDrops(true); QFile styleFile(":/qt/style.qss"); styleFile.open( QFile::ReadOnly ); @@ -78,14 +77,6 @@ void Window::on_ascii_stl() "Please convert to binary .stl and retry"); } -void Window::on_bad_stl() -{ - QMessageBox::critical(this, "Error", - "Error:
" - "This .stl file is invalid or corrupted.
" - "Please export it from the original source, verify, and retry."); -} - void Window::enable_open() { open_action->setEnabled(true); @@ -110,8 +101,6 @@ bool Window::load_stl(const QString& filename) canvas, &Canvas::load_mesh); connect(loader, &Loader::error_ascii_stl, this, &Window::on_ascii_stl); - connect(loader, &Loader::error_bad_stl, - this, &Window::on_bad_stl); connect(loader, &Loader::finished, loader, &Loader::deleteLater); @@ -129,18 +118,3 @@ bool Window::load_stl(const QString& filename) loader->start(); return true; } - -void Window::dragEnterEvent(QDragEnterEvent *event) -{ - if (event->mimeData()->hasUrls()) - { - auto urls = event->mimeData()->urls(); - if (urls.size() == 1 && urls.front().path().endsWith(".stl")) - event->acceptProposedAction(); - } -} - -void Window::dropEvent(QDropEvent *event) -{ - load_stl(event->mimeData()->urls().front().toLocalFile()); -} diff --git a/src/window.h b/src/window.h index cb92ebb..8484822 100644 --- a/src/window.h +++ b/src/window.h @@ -12,15 +12,10 @@ public: explicit Window(QWidget* parent=0); bool load_stl(const QString& filename); -protected: - void dragEnterEvent(QDragEnterEvent* event); - void dropEvent(QDropEvent* event); - public slots: void on_open(); void on_about(); void on_ascii_stl(); - void on_bad_stl(); void enable_open(); void disable_open();