]> git.sur5r.net Git - fstl/commitdiff
Reinstate min/max methods
authorMatt Keeter <matt.j.keeter@gmail.com>
Sat, 8 Mar 2014 21:47:51 +0000 (13:47 -0800)
committerMatt Keeter <matt.j.keeter@gmail.com>
Sat, 8 Mar 2014 21:47:51 +0000 (13:47 -0800)
src/canvas.cpp
src/mesh.cpp
src/mesh.h

index a5509c0f478a5a2163a74e0624fa384482325a97..39081b07554366626e61bf7185117f5f61994e32 100644 (file)
@@ -23,7 +23,6 @@ Canvas::~Canvas()
 void Canvas::load_mesh(Mesh* m)
 {
     mesh = new GLMesh(m);
 void Canvas::load_mesh(Mesh* m)
 {
     mesh = new GLMesh(m);
-    /*
     center = QVector3D(m->xmin() + m->xmax(),
                        m->ymin() + m->ymax(),
                        m->zmin() + m->zmax()) / 2;
     center = QVector3D(m->xmin() + m->xmax(),
                        m->ymin() + m->ymax(),
                        m->zmin() + m->zmax()) / 2;
@@ -31,7 +30,6 @@ void Canvas::load_mesh(Mesh* m)
                 pow(m->xmax() - m->xmin(), 2) +
                 pow(m->ymax() - m->ymin(), 2) +
                 pow(m->zmax() - m->zmin(), 2));
                 pow(m->xmax() - m->xmin(), 2) +
                 pow(m->ymax() - m->ymin(), 2) +
                 pow(m->zmax() - m->zmin(), 2));
-    */
     update();
 
     delete m;
     update();
 
     delete m;
index 3a971202ce46ba6e7d12d2bc463e6af27562e799..36ad10e0b884f9e8125f21c64063b8441cd698c8 100644 (file)
@@ -3,9 +3,39 @@
 #include <QVector3D>
 
 #include <algorithm>
 #include <QVector3D>
 
 #include <algorithm>
+#include <cmath>
 
 #include "mesh.h"
 
 
 #include "mesh.h"
 
+////////////////////////////////////////////////////////////////////////////////
+
+Mesh::Mesh(std::vector<GLfloat> v, std::vector<GLuint> i)
+    : vertices(v), indices(i)
+{
+    // 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;
 struct Vec3
 {
     GLfloat x, y, z;
@@ -22,12 +52,9 @@ struct Vec3
     }
 };
 
     }
 };
 
+typedef std::pair<Vec3, GLuint> Vec3i;
 
 
-Mesh::Mesh(std::vector<GLfloat> v, std::vector<GLuint> i)
-    : vertices(v), indices(i)
-{
-    // Nothing to do here
-}
+////////////////////////////////////////////////////////////////////////////////
 
 Mesh* Mesh::load_stl(const QString& filename)
 {
 
 Mesh* Mesh::load_stl(const QString& filename)
 {
@@ -46,7 +73,7 @@ Mesh* Mesh::load_stl(const QString& filename)
     data >> tri_count;
 
     // Extract vertices into an array of xyz, unsigned pairs
     data >> tri_count;
 
     // Extract vertices into an array of xyz, unsigned pairs
-    QVector<std::pair<Vec3, GLuint>> verts(tri_count*3);
+    QVector<Vec3i> verts(tri_count*3);
 
     // Store vertices in the array, processing one triangle at a time.
     for (auto v=verts.begin(); v != verts.end(); v += 3)
 
     // Store vertices in the array, processing one triangle at a time.
     for (auto v=verts.begin(); v != verts.end(); v += 3)
@@ -89,15 +116,16 @@ Mesh* Mesh::load_stl(const QString& filename)
         }
         indices[v.second] = vertex_count - 1;
     }
         }
         indices[v.second] = vertex_count - 1;
     }
+    verts.resize(vertex_count);
 
 
-    // Finally, pack unique vertices into a flat array.
-    std::vector<GLfloat> unique_verts(vertex_count*3);
-    for (size_t i=0; i < vertex_count; ++i)
+    std::vector<float> flat_verts;
+    flat_verts.reserve(vertex_count*3);
+    for (auto v : verts)
     {
     {
-        unique_verts[3*i]     = verts[i].first.x;
-        unique_verts[3*i + 1] = verts[i].first.y;
-        unique_verts[3*i + 2] = verts[i].first.z;
+        flat_verts.push_back(v.first.x);
+        flat_verts.push_back(v.first.y);
+        flat_verts.push_back(v.first.z);
     }
 
     }
 
-    return new Mesh(unique_verts, indices);
+    return new Mesh(flat_verts, indices);
 }
 }
index f8da3e3903e6854aeef6cb24d6d798a968d6aac9..f83a47708249bc007fd40e73ec6edc4b4d6b172d 100644 (file)
@@ -11,14 +11,17 @@ class Mesh
 public:
     Mesh(std::vector<GLfloat> vertices, std::vector<GLuint> indices);
     static Mesh* load_stl(const QString& filename);
 public:
     Mesh(std::vector<GLfloat> vertices, std::vector<GLuint> indices);
     static Mesh* load_stl(const QString& filename);
-/*
-    float xmin() const { return vertices.row(0).minCoeff(); }
-    float xmax() const { return vertices.row(0).maxCoeff(); }
-    float ymin() const { return vertices.row(1).minCoeff(); }
-    float ymax() const { return vertices.row(1).maxCoeff(); }
-    float zmin() const { return vertices.row(2).minCoeff(); }
-    float zmax() const { return vertices.row(2).maxCoeff(); }
-*/
+
+    float min(size_t start) const;
+    float max(size_t start) const;
+
+    float xmin() const { return min(0); }
+    float ymin() const { return min(1); }
+    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:
     std::vector<GLfloat> vertices;
     std::vector<GLuint> indices;
 private:
     std::vector<GLfloat> vertices;
     std::vector<GLuint> indices;