10 ////////////////////////////////////////////////////////////////////////////////
12 Mesh::Mesh(std::vector<GLfloat> v, std::vector<GLuint> i)
13 : vertices(v), indices(i)
18 float Mesh::min(size_t start) const
20 float v = vertices[start];
21 for (size_t i=start; i < vertices.size(); i += 3)
23 v = fmin(v, vertices[i]);
28 float Mesh::max(size_t start) const
30 float v = vertices[start];
31 for (size_t i=start; i < vertices.size(); i += 3)
33 v = fmax(v, vertices[i]);
37 ////////////////////////////////////////////////////////////////////////////////
42 bool operator!=(const Vec3& rhs) const
44 return x != rhs.x || y != rhs.y || z != rhs.z;
46 bool operator<(const Vec3& rhs) const
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;
55 typedef std::pair<Vec3, GLuint> Vec3i;
57 ////////////////////////////////////////////////////////////////////////////////
59 Mesh* Mesh::load_stl_hash(const QString& filename)
62 file.open(QIODevice::ReadOnly);
64 QDataStream data(&file);
65 data.setByteOrder(QDataStream::LittleEndian);
66 data.setFloatingPointPrecision(QDataStream::SinglePrecision);
68 // Skip .stl file header
71 // Load the triangle count from the .stl file
75 // This vector will store triangles as sets of 3 indices
76 std::vector<GLuint> indices(tri_count * 3);
78 std::vector<GLfloat> verts;
79 verts.reserve(tri_count * 9);
81 QHash<QByteArray, GLuint> map;
82 map.reserve(tri_count * 3);
85 QByteArray v(sizeof(xyz), 0);
86 for (unsigned i=0; i < tri_count; ++i)
88 // Skip face's normal vector
89 data.skipRawData(3*sizeof(float));
91 for (int j=0; j < 3; ++j)
93 data >> xyz[0] >> xyz[1] >> xyz[2];
94 memcpy(v.data(), xyz, sizeof(xyz));
97 map[v] = verts.size() / 3;
98 verts.push_back(xyz[0]);
99 verts.push_back(xyz[1]);
100 verts.push_back(xyz[2]);
102 indices[i*3 + j] = map[v];
105 // Skip face attribute
106 data.skipRawData(sizeof(uint16_t));
109 return new Mesh(verts, indices);
112 Mesh* Mesh::load_stl(const QString& filename)
114 QFile file(filename);
115 file.open(QIODevice::ReadOnly);
117 QDataStream data(&file);
118 data.setByteOrder(QDataStream::LittleEndian);
119 data.setFloatingPointPrecision(QDataStream::SinglePrecision);
121 // Skip .stl file header
122 data.skipRawData(80);
124 // Load the triangle count from the .stl file
128 // Extract vertices into an array of xyz, unsigned pairs
129 QVector<Vec3i> verts(tri_count*3);
131 // Store vertices in the array, processing one triangle at a time.
132 for (auto v=verts.begin(); v != verts.end(); v += 3)
134 // Skip face's normal vector
135 data.skipRawData(3*sizeof(float));
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;
142 // Skip face attribute
143 data.skipRawData(sizeof(uint16_t));
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)
153 // Sort the set of vertices (to deduplicate)
154 std::sort(verts.begin(), verts.end());
156 // This vector will store triangles as sets of 3 indices
157 std::vector<GLuint> indices(tri_count*3);
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;
166 if (!vertex_count || v.first != verts[vertex_count-1].first)
168 verts[vertex_count++] = v;
170 indices[v.second] = vertex_count - 1;
172 verts.resize(vertex_count);
174 std::vector<float> flat_verts;
175 flat_verts.reserve(vertex_count*3);
178 flat_verts.push_back(v.first.x);
179 flat_verts.push_back(v.first.y);
180 flat_verts.push_back(v.first.z);
183 return new Mesh(flat_verts, indices);