Merge branch 'remove-eigen'

This commit is contained in:
Matt Keeter 2014-03-08 14:37:41 -08:00
commit a960f8b2a8
4 changed files with 102 additions and 55 deletions

View file

@ -1,6 +1,8 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <QDebug> #include <QDebug>
#include <cmath>
#include "canvas.h" #include "canvas.h"
#include "backdrop.h" #include "backdrop.h"
#include "glmesh.h" #include "glmesh.h"
@ -28,7 +30,6 @@ void Canvas::load_mesh(Mesh* m)
pow(m->xmax() - m->xmin(), 2) + pow(m->xmax() - m->xmin(), 2) +
pow(m->ymax() - m->ymin(), 2) + pow(m->ymax() - m->ymin(), 2) +
pow(m->zmax() - m->zmin(), 2)); pow(m->zmax() - m->zmin(), 2));
update(); update();
delete m; delete m;

View file

@ -22,7 +22,5 @@ HEADERS += \
CONFIG += c++11 CONFIG += c++11
INCLUDEPATH += /usr/local/include/eigen3
RESOURCES += \ RESOURCES += \
resources.qrc resources.qrc

View file

@ -1,16 +1,61 @@
#include <QFile> #include <QFile>
#include <QDataStream> #include <QDataStream>
#include <QVector3D>
#include <algorithm> #include <algorithm>
#include <cmath>
#include "mesh.h" #include "mesh.h"
Mesh::Mesh(const Eigen::Matrix3Xf& v, const Eigen::Matrix3Xi& i) ////////////////////////////////////////////////////////////////////////////////
Mesh::Mesh(std::vector<GLfloat> v, std::vector<GLuint> i)
: vertices(v), indices(i) : vertices(v), indices(i)
{ {
// Nothing to do here // Nothing to do here
} }
float Mesh::min(size_t start) const
{
float v = vertices[start];
for (size_t i=start; i < vertices.size(); i += 3)
{
v = fmin(v, vertices[i]);
}
return v;
}
float Mesh::max(size_t start) const
{
float v = vertices[start];
for (size_t i=start; i < vertices.size(); i += 3)
{
v = fmax(v, vertices[i]);
}
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<Vec3, GLuint> Vec3i;
////////////////////////////////////////////////////////////////////////////////
Mesh* Mesh::load_stl(const QString& filename) Mesh* Mesh::load_stl(const QString& filename)
{ {
QFile file(filename); QFile file(filename);
@ -20,68 +65,67 @@ Mesh* Mesh::load_stl(const QString& filename)
data.setByteOrder(QDataStream::LittleEndian); data.setByteOrder(QDataStream::LittleEndian);
data.setFloatingPointPrecision(QDataStream::SinglePrecision); data.setFloatingPointPrecision(QDataStream::SinglePrecision);
// Skip .stl file header
data.skipRawData(80); data.skipRawData(80);
// Load the triangle count from the .stl file
uint32_t tri_count; uint32_t tri_count;
data >> tri_count; data >> tri_count;
// Extract vertices into a vector of Vector4d objects // Extract vertices into an array of xyz, unsigned pairs
std::vector<Eigen::Vector4d> verts(tri_count*3); QVector<Vec3i> verts(tri_count*3);
for (unsigned i=0; i < tri_count; ++i)
// 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.skipRawData(3*sizeof(float)); data.skipRawData(3*sizeof(float));
for (int j=0; j < 3; ++j)
{ // Load vertex data from .stl file into vertices
float x, y, z; data >> v[0].first.x >> v[0].first.y >> v[0].first.z;
data >> x >> y >> z; data >> v[1].first.x >> v[1].first.y >> v[1].first.z;
verts[3*i + j] << x, y, z, 3*i + j; data >> v[2].first.x >> v[2].first.y >> v[2].first.z;
}
// Skip face attribute
data.skipRawData(sizeof(uint16_t)); data.skipRawData(sizeof(uint16_t));
} }
// Sort the set of vertices (to deduplicate) // Save indicies as the second element in the array
std::sort(verts.begin(), verts.end(), // (so that we can reconstruct triangle order after sorting)
[](const Eigen::Vector4d& lhs, const Eigen::Vector4d& rhs) for (size_t i=0; i < tri_count*3; ++i)
{ {
if (lhs[0] != rhs[0]) return lhs[0] < rhs[0]; verts[i].second = i;
else if (lhs[1] != rhs[1]) return lhs[1] < rhs[1];
else if (lhs[2] != rhs[2]) return lhs[2] < rhs[2];
else return false;
} }
);
// This list will store unique vertices // Sort the set of vertices (to deduplicate)
std::list<Eigen::Vector3f> unique; std::sort(verts.begin(), verts.end());
// This vector will store triangles as rows of indices // This vector will store triangles as sets of 3 indices
Eigen::Matrix3Xi indices; std::vector<GLuint> indices(tri_count*3);
indices.resize(Eigen::NoChange, tri_count);
// Go through the sorted vertex list, deduplicating and creating // Go through the sorted vertex list, deduplicating and creating
// an indexed geometry representation for the triangles. // 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) for (auto v : verts)
{ {
if (!unique.size() || v[0] != unique.back()[0] || if (!vertex_count || v.first != verts[vertex_count-1].first)
v[1] != unique.back()[1] ||
v[2] != unique.back()[2])
{ {
// Switch to a float vector and save in the list. verts[vertex_count++] = v;
Eigen::Vector3f v_;
v_ << v[0], v[1], v[2];
unique.push_back(v_);
} }
indices(int(v[3]) % 3, int(v[3]) / 3) = unique.size() - 1; indices[v.second] = vertex_count - 1;
}
verts.resize(vertex_count);
std::vector<float> 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);
} }
// Finally, pack unique vertices into a matrix. return new Mesh(flat_verts, indices);
Eigen::Matrix3Xf unique_verts;
unique_verts.resize(Eigen::NoChange, unique.size());
{
auto v = unique.begin();
for (unsigned i=0; i < unique.size(); ++i)
{
unique_verts.col(i) = *(v++);
}
}
return new Mesh(unique_verts, indices);
} }

View file

@ -2,25 +2,29 @@
#define MESH_H #define MESH_H
#include <QString> #include <QString>
#include <QtOpenGL/QtOpenGL>
#include <Eigen/Dense> #include <vector>
class Mesh class Mesh
{ {
public: public:
Mesh(const Eigen::Matrix3Xf &vertices, const Eigen::Matrix3Xi &indices); Mesh(std::vector<GLfloat> vertices, std::vector<GLuint> indices);
static Mesh* load_stl(const QString& filename); static Mesh* load_stl(const QString& filename);
float xmin() const { return vertices.row(0).minCoeff(); } float min(size_t start) const;
float xmax() const { return vertices.row(0).maxCoeff(); } float max(size_t start) const;
float ymin() const { return vertices.row(1).minCoeff(); }
float ymax() const { return vertices.row(1).maxCoeff(); } float xmin() const { return min(0); }
float zmin() const { return vertices.row(2).minCoeff(); } float ymin() const { return min(1); }
float zmax() const { return vertices.row(2).maxCoeff(); } float zmin() const { return min(2); }
float xmax() const { return max(0); }
float ymax() const { return max(1); }
float zmax() const { return max(2); }
private: private:
const Eigen::Matrix3Xf vertices; std::vector<GLfloat> vertices;
const Eigen::Matrix3Xi indices; std::vector<GLuint> indices;
friend class GLMesh; friend class GLMesh;
}; };