]> git.sur5r.net Git - fstl/blob - src/mesh.cpp
Reinstate min/max methods
[fstl] / src / mesh.cpp
1 #include <QFile>
2 #include <QDataStream>
3 #include <QVector3D>
4
5 #include <algorithm>
6 #include <cmath>
7
8 #include "mesh.h"
9
10 ////////////////////////////////////////////////////////////////////////////////
11
12 Mesh::Mesh(std::vector<GLfloat> v, std::vector<GLuint> i)
13     : vertices(v), indices(i)
14 {
15     // Nothing to do here
16 }
17
18 float Mesh::min(size_t start) const
19 {
20     float v = vertices[start];
21     for (size_t i=start; i < vertices.size(); i += 3)
22     {
23         v = fmin(v, vertices[i]);
24     }
25     return v;
26 }
27
28 float Mesh::max(size_t start) const
29 {
30     float v = vertices[start];
31     for (size_t i=start; i < vertices.size(); i += 3)
32     {
33         v = fmax(v, vertices[i]);
34     }
35     return v;
36 }
37 ////////////////////////////////////////////////////////////////////////////////
38
39 struct Vec3
40 {
41     GLfloat x, y, z;
42     bool operator!=(const Vec3& rhs) const
43     {
44         return x != rhs.x || y != rhs.y || z != rhs.z;
45     }
46     bool operator<(const Vec3& rhs) const
47     {
48         if      (x != rhs.x)    return x < rhs.x;
49         else if (y != rhs.y)    return y < rhs.y;
50         else if (z != rhs.z)    return z < rhs.z;
51         else                    return false;
52     }
53 };
54
55 typedef std::pair<Vec3, GLuint> Vec3i;
56
57 ////////////////////////////////////////////////////////////////////////////////
58
59 Mesh* Mesh::load_stl(const QString& filename)
60 {
61     QFile file(filename);
62     file.open(QIODevice::ReadOnly);
63
64     QDataStream data(&file);
65     data.setByteOrder(QDataStream::LittleEndian);
66     data.setFloatingPointPrecision(QDataStream::SinglePrecision);
67
68     // Skip .stl file header
69     data.skipRawData(80);
70
71     // Load the triangle count from the .stl file
72     uint32_t tri_count;
73     data >> tri_count;
74
75     // Extract vertices into an array of xyz, unsigned pairs
76     QVector<Vec3i> verts(tri_count*3);
77
78     // Store vertices in the array, processing one triangle at a time.
79     for (auto v=verts.begin(); v != verts.end(); v += 3)
80     {
81         // Skip face's normal vector
82         data.skipRawData(3*sizeof(float));
83
84         // Load vertex data from .stl file into vertices
85         data >> v[0].first.x >> v[0].first.y >> v[0].first.z;
86         data >> v[1].first.x >> v[1].first.y >> v[1].first.z;
87         data >> v[2].first.x >> v[2].first.y >> v[2].first.z;
88
89         // Skip face attribute
90         data.skipRawData(sizeof(uint16_t));
91     }
92
93     // Save indicies as the second element in the array
94     // (so that we can reconstruct triangle order after sorting)
95     for (size_t i=0; i < tri_count*3; ++i)
96     {
97         verts[i].second = i;
98     }
99
100     // Sort the set of vertices (to deduplicate)
101     std::sort(verts.begin(), verts.end());
102
103     // This vector will store triangles as sets of 3 indices
104     std::vector<GLuint> indices(tri_count*3);
105
106     // Go through the sorted vertex list, deduplicating and creating
107     // an indexed geometry representation for the triangles.
108     // Unique vertices are moved so that they occupy the first vertex_count
109     // positions in the verts array.
110     size_t vertex_count = 0;
111     for (auto v : verts)
112     {
113         if (!vertex_count || v.first != verts[vertex_count-1].first)
114         {
115             verts[vertex_count++] = v;
116         }
117         indices[v.second] = vertex_count - 1;
118     }
119     verts.resize(vertex_count);
120
121     std::vector<float> flat_verts;
122     flat_verts.reserve(vertex_count*3);
123     for (auto v : verts)
124     {
125         flat_verts.push_back(v.first.x);
126         flat_verts.push_back(v.first.y);
127         flat_verts.push_back(v.first.z);
128     }
129
130     return new Mesh(flat_verts, indices);
131 }