Repository navigation
Expand file tree
/
Copy pathSTLParser.cpp
More file actions
400 lines (350 loc) · 11.8 KB
/
Copy pathSTLParser.cpp
File metadata and controls
400 lines (350 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* This file is part of PyMesh. Copyright (c) 2015 by Qingnan Zhou */
#include "STLParser.h"
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <tbb/parallel_sort.h>
#include <vector>
#include <limits>
#include <Core/Exception.h>
#include "IOUtils.h"
using namespace PyMesh;
using namespace IOUtils;
bool STLParser::parse(const std::string& filename) {
bool success = false;
if (is_binary(filename)) {
success = parse_binary(filename);
} else {
success = parse_ascii(filename);
}
if (!success) return false;
validate_normals();
merge_identical_vertices();
return true;
}
size_t STLParser::num_attributes() const {
if (has_normal())
return 1;
else
return 0;
}
STLParser::AttrNames STLParser::get_attribute_names() const {
AttrNames names;
if (has_normal())
names.push_back("face_normal");
return names;
}
size_t STLParser::get_attribute_size(const std::string& name) const {
if (!attribute_exists(name)) {
std::cerr << "Warning: mesh does not have attribute with name "
<< name << std::endl;
return 0;
}
if (has_normal())
return m_facet_normals.size() * 3;
else
return 0;
}
void STLParser::export_vertices(Float* buffer) {
size_t count=0;
for (VertexList::const_iterator vi=m_vertices.begin();
vi != m_vertices.end(); vi++) {
const Vector3F& v = *vi;
buffer[count*3 ] = v[0];
buffer[count*3+1] = v[1];
buffer[count*3+2] = v[2];
count++;
}
}
void STLParser::export_faces(int* buffer) {
std::copy(m_faces.data(), m_faces.data()+m_faces.size(), buffer);
}
void STLParser::export_voxels(int* buffer) {
size_t num_vertex_per_voxel = vertex_per_voxel();
size_t count=0;
for (VoxelList::const_iterator vi=m_voxels.begin();
vi != m_voxels.end(); vi++) {
const VectorI& v = *vi;
const int* voxel = v.data();
std::copy(voxel, voxel + num_vertex_per_voxel,
buffer + num_vertex_per_voxel * count);
count++;
}
}
void STLParser::export_attribute(const std::string& name, Float* buffer) {
if (!attribute_exists(name)) {
std::cerr << "Warning: mesh does not have attribute with name "
<< name << std::endl;
return;
}
size_t count=0;
for (NormalList::const_iterator ni = m_facet_normals.begin();
ni != m_facet_normals.end(); ni++) {
const Vector3F& normal = *ni;
buffer[count*3 ] = normal[0];
buffer[count*3+1] = normal[1];
buffer[count*3+2] = normal[2];
count++;
}
}
bool STLParser::attribute_exists(const std::string& name) const {
return has_normal() && (name == "face_normal");
}
bool STLParser::is_binary(const std::string& filename) {
const size_t HEADER_SIZE = 80;
char header[HEADER_SIZE];
std::ifstream fin(filename.c_str(), std::ifstream::binary);
fin.read(header, HEADER_SIZE);
if (!is_prefix("solid", header)) return true;
if (!fin.good()) return false;
// Check if filesize matches the number of faces claimed.
char buf[4];
fin.read(buf, 4);
size_t num_faces = *reinterpret_cast<unsigned int*>(buf);
fin.seekg(0, fin.end);
size_t file_size = fin.tellg();
if (file_size == 80 + 4 + (4*12 + 2) * num_faces) return true;
else return false;
}
bool STLParser::parse_ascii(const std::string& filename) {
const size_t LINE_SIZE = 256;
char line[LINE_SIZE];
bool success = true;
std::ifstream fin(filename.c_str());
if (!fin.is_open()) {
std::stringstream err_msg;
err_msg << "failed to open file \"" << filename << "\"";
throw IOError(err_msg.str());
}
// skip header line.
fin.getline(line, LINE_SIZE);
while (!fin.eof()) {
success = parse_ascii_facet(fin);
if (!success) return false;
}
fin.close();
m_faces = VectorI::LinSpaced(m_vertices.size(), 0, m_vertices.size()-1);
return success;
}
bool STLParser::parse_ascii_facet(std::ifstream& fin) {
const size_t LINE_SIZE = 256;
const size_t WORD_SIZE = 128;
char line[LINE_SIZE];
char first_word[WORD_SIZE];
const std::string face_begin("facet");
const std::string face_end("endfacet");
const std::string loop_begin("outer");
const std::string loop_end("endloop");
const std::string vertex_flag("vertex");
bool reading_facet = false;
bool reading_loop = false;
bool success = true;
size_t num_vts = 0;
while (!fin.eof()) {
fin.getline(line, LINE_SIZE);
size_t n = sscanf(line, " %s", first_word);
if (n == 0) continue;
if (is_prefix(face_begin.c_str(), first_word)) {
success = parse_ascii_normal(line);
assert(success);
reading_facet = true;
} else if (is_prefix(face_end.c_str(), first_word)) {
assert(reading_facet);
reading_facet = false;
} else if (is_prefix(loop_begin.c_str(), first_word)) {
reading_loop = true;
} else if (is_prefix(loop_end.c_str(), first_word)) {
assert(reading_loop);
reading_loop = false;
} else if (is_prefix(vertex_flag.c_str(), first_word)) {
assert(reading_facet);
assert(reading_loop);
success = parse_ascii_vertex(line);
assert(success);
num_vts += 1;
}
if (!success) return false;
if (!reading_facet) break;
}
if (num_vts == 0) return true;
assert(num_vts == 3);
if (num_vts != 3) {
std::cerr << "Warning: mesh contain face made of "
<< num_vts << " vertices" << std::endl;
return false;
}
//VectorI f(num_vts);
//size_t start_idx = m_vertices.size() - num_vts;
//for (size_t i=0; i<num_vts; i++) {
// f[i] = start_idx + i;
//}
//m_faces.push_back(f);
return true;
}
bool STLParser::parse_ascii_normal(char* line) {
Float x,y,z;
size_t n = sscanf(line, " facet normal %lf %lf %lf", &x, &y, &z);
assert(n == 3);
if (n != 3) return false;
Vector3F normal(x,y,z);
m_facet_normals.push_back(normal);
return true;
}
bool STLParser::parse_ascii_vertex(char* line) {
Float x,y,z;
size_t n = sscanf(line, " vertex %lf %lf %lf", &x, &y, &z);
assert(n == 3);
if (n != 3) return false;
Vector3F v(x,y,z);
m_vertices.push_back(v);
return true;
}
bool STLParser::parse_binary(const std::string& filename) {
std::ifstream fin(filename.c_str());
if (!fin.is_open()) {
std::stringstream err_msg;
err_msg << "failed to open file \"" << filename << "\"";
throw IOError(err_msg.str());
}
const size_t FLOAT_SIZE = sizeof(float);
assert(FLOAT_SIZE == 4);
const size_t LINE_SIZE=256;
char buf[LINE_SIZE];
// 80 bytes header, no data significance.
fin.read(buf, 80);
if (!fin.good()) {
throw IOError("Unable to parse STL header.");
}
fin.read(buf, 4);
const size_t num_faces = *reinterpret_cast<unsigned int*>(buf);
if (!fin.good()) {
throw IOError("Unable to parse STL number of faces.");
}
for (size_t i=0; i<num_faces; i++) {
// Parse normal
fin.read(buf, FLOAT_SIZE*3);
const float nx = *reinterpret_cast<float*>(buf);
const float ny = *reinterpret_cast<float*>(buf + FLOAT_SIZE);
const float nz = *reinterpret_cast<float*>(buf + FLOAT_SIZE * 2);
assert(fin.good());
// vertex 1
fin.read(buf, FLOAT_SIZE*3);
const float v1x = *reinterpret_cast<float*>(buf);
const float v1y = *reinterpret_cast<float*>(buf + FLOAT_SIZE);
const float v1z = *reinterpret_cast<float*>(buf + FLOAT_SIZE * 2);
assert(fin.good());
// vertex 2
fin.read(buf, FLOAT_SIZE*3);
const float v2x = *reinterpret_cast<float*>(buf);
const float v2y = *reinterpret_cast<float*>(buf + FLOAT_SIZE);
const float v2z = *reinterpret_cast<float*>(buf + FLOAT_SIZE * 2);
assert(fin.good());
// vertex 3
fin.read(buf, FLOAT_SIZE*3);
const float v3x = *reinterpret_cast<float*>(buf);
const float v3y = *reinterpret_cast<float*>(buf + FLOAT_SIZE);
const float v3z = *reinterpret_cast<float*>(buf + FLOAT_SIZE * 2);
assert(fin.good());
// attribute (2 bytes), not sure what purpose they serve.
fin.read(buf, 2);
m_facet_normals.emplace_back(nx, ny, nz);
m_vertices.emplace_back(v1x, v1y, v1z);
m_vertices.emplace_back(v2x, v2y, v2z);
m_vertices.emplace_back(v3x, v3y, v3z);
assert(fin.good());
if (!fin.good()) {
std::stringstream err_msg;
err_msg << "Failed to parse face " << i << " from STL file";
throw IOError(err_msg.str());
}
}
std::for_each(m_vertices.begin(), m_vertices.end(),
[](const Vector3F& v) {
if (!v.allFinite()) {
throw IOError("NaN or Inf detected in input file.");
}
});
m_faces = VectorI::LinSpaced(m_vertices.size(), 0, m_vertices.size()-1);
fin.close();
return true;
}
void STLParser::merge_identical_vertices() {
using Index = VectorI::Scalar;
const auto index_comp = [this](Index i1, Index i2) -> bool {
const auto& v1 = m_vertices[i1];
const auto& v2 = m_vertices[i2];
if (v1[0] != v2[0]) {
return v1[0] < v2[0];
} else if (v1[1] != v2[1]) {
return v1[1] < v2[1];
} else if (v1[2] != v2[2]){
return v1[2] < v2[2];
}
return false;
};
const size_t num_vertices = m_vertices.size();
VectorI indices(num_vertices);
if (num_vertices > 0) {
indices.setLinSpaced(num_vertices, 0, num_vertices-1);
}
tbb::parallel_sort(indices.data(), indices.data()+num_vertices, index_comp);
VectorI index_map(num_vertices);
index_map.setConstant(-1);
VertexList sorted_vertices;
sorted_vertices.reserve(num_vertices);
size_t count = 0;
size_t idx = 0;
while (idx < num_vertices) {
const auto& v = m_vertices[indices[idx]];
sorted_vertices.push_back(v);
while (idx < num_vertices) {
if (m_vertices[indices[idx]] == v) {
index_map[indices[idx]] = count;
idx++;
} else {
break;
}
}
count++;
}
assert((index_map.array() >= 0).all());
std::swap(m_vertices, sorted_vertices);
std::transform(m_faces.data(), m_faces.data()+m_faces.size(), m_faces.data(),
[&index_map](int vi) { return index_map[vi]; });
}
void STLParser::validate_normals() {
const Float EPS = 1e-16;
bool valid = true;
for (NormalList::iterator itr = m_facet_normals.begin();
itr != m_facet_normals.end(); itr++) {
Float n_norm = itr->norm();
if (n_norm < 1.0 - EPS || n_norm > 1.0 + EPS) {
valid = false;
break;
}
}
if (!valid) {
m_facet_normals.clear();
}
}
bool STLParser::has_normal() const {
return !m_facet_normals.empty();
}
Float STLParser::compute_bbox_diagonal_length() const {
const Float max_float = std::numeric_limits<Float>::max();
const Float min_float = std::numeric_limits<Float>::min();
Vector3F b_max(min_float, min_float, min_float),
b_min(max_float, max_float, max_float);
for (VertexList::const_iterator itr = m_vertices.begin();
itr != m_vertices.end(); itr++) {
const Vector3F& v = *itr;
b_max[0] = std::max(v[0], b_max[0]);
b_max[1] = std::max(v[1], b_max[1]);
b_max[2] = std::max(v[2], b_max[2]);
b_min[0] = std::min(v[0], b_min[0]);
b_min[1] = std::min(v[1], b_min[1]);
b_min[2] = std::min(v[2], b_min[2]);
}
return (b_max - b_min).norm();
}