]> git.sur5r.net Git - fstl/blob - src/mesh.cpp
Adding hash-based mesh loading (better big-O)
[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_hash(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     // This vector will store triangles as sets of 3 indices
76     std::vector<GLuint> indices(tri_count * 3);
77
78     std::vector<GLfloat> verts;
79     verts.reserve(tri_count * 9);
80
81     QHash<QByteArray, GLuint> map;
82     map.reserve(tri_count * 3);
83
84     float xyz[3];
85     QByteArray v(sizeof(xyz), 0);
86     for (unsigned i=0; i < tri_count; ++i)
87     {
88         // Skip face's normal vector
89         data.skipRawData(3*sizeof(float));
90
91         for (int j=0; j < 3; ++j)
92         {
93             data >> xyz[0] >> xyz[1] >> xyz[2];
94             memcpy(v.data(), xyz, sizeof(xyz));
95             if (!map.contains(v))
96             {
97                 map[v] = verts.size() / 3;
98                 verts.push_back(xyz[0]);
99                 verts.push_back(xyz[1]);
100                 verts.push_back(xyz[2]);
101             }
102             indices[i*3 + j] = map[v];
103         }
104
105         // Skip face attribute
106         data.skipRawData(sizeof(uint16_t));
107     }
108
109     return new Mesh(verts, indices);
110 }
111
112 Mesh* Mesh::load_stl(const QString& filename)
113 {
114     QFile file(filename);
115     file.open(QIODevice::ReadOnly);
116
117     QDataStream data(&file);
118     data.setByteOrder(QDataStream::LittleEndian);
119     data.setFloatingPointPrecision(QDataStream::SinglePrecision);
120
121     // Skip .stl file header
122     data.skipRawData(80);
123
124     // Load the triangle count from the .stl file
125     uint32_t tri_count;
126     data >> tri_count;
127
128     // Extract vertices into an array of xyz, unsigned pairs
129     QVector<Vec3i> verts(tri_count*3);
130
131     // Store vertices in the array, processing one triangle at a time.
132     for (auto v=verts.begin(); v != verts.end(); v += 3)
133     {
134         // Skip face's normal vector
135         data.skipRawData(3*sizeof(float));
136
137         // Load vertex data from .stl file into vertices
138         data >> v[0].first.x >> v[0].first.y >> v[0].first.z;
139         data >> v[1].first.x >> v[1].first.y >> v[1].first.z;
140         data >> v[2].first.x >> v[2].first.y >> v[2].first.z;
141
142         // Skip face attribute
143         data.skipRawData(sizeof(uint16_t));
144     }
145
146     // Save indicies as the second element in the array
147     // (so that we can reconstruct triangle order after sorting)
148     for (size_t i=0; i < tri_count*3; ++i)
149     {
150         verts[i].second = i;
151     }
152
153     // Sort the set of vertices (to deduplicate)
154     std::sort(verts.begin(), verts.end());
155
156     // This vector will store triangles as sets of 3 indices
157     std::vector<GLuint> indices(tri_count*3);
158
159     // Go through the sorted vertex list, deduplicating and creating
160     // an indexed geometry representation for the triangles.
161     // Unique vertices are moved so that they occupy the first vertex_count
162     // positions in the verts array.
163     size_t vertex_count = 0;
164     for (auto v : verts)
165     {
166         if (!vertex_count || v.first != verts[vertex_count-1].first)
167         {
168             verts[vertex_count++] = v;
169         }
170         indices[v.second] = vertex_count - 1;
171     }
172     verts.resize(vertex_count);
173
174     std::vector<float> flat_verts;
175     flat_verts.reserve(vertex_count*3);
176     for (auto v : verts)
177     {
178         flat_verts.push_back(v.first.x);
179         flat_verts.push_back(v.first.y);
180         flat_verts.push_back(v.first.z);
181     }
182
183     return new Mesh(flat_verts, indices);
184 }