-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTL_Reader.cpp
352 lines (318 loc) · 12.3 KB
/
STL_Reader.cpp
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
// Author : Adhitya Kamakshidasan
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <map>
#include <vector>
#include <cmath>
#include <limits>
// Error Codes
#define FAILURE 0
#define SUCCESS 1
#define INCORRECT_FILE_FORMAT 2
#define FILE_OPENING_ERROR 3
#define IMPROPER_SYNTAX 4
#define NULL_POINTER 5
// Different states while parsing
#define UNKNOWN 0
#define SOLID 1
#define FACET 2
#define NORMAL 3
#define OUTER 4
#define LOOP 5
#define VERTEX 6
#define ENDLOOP 7
#define ENDFACET 8
#define BRANCH 9
#define ENDSOLID 10
#define EPSILON 2.2204460492503131e-016
typedef int ERROR_TYPE;
struct TriangleMesh {
int vertex_count;
double* vertex_coords;
int triangle_count;
int* triangle_vertices;
double* normal_coords;
};
// Structure for storing a vertex
struct Vertex {
double x;
double y;
double z;
};
ERROR_TYPE init_mesh(TriangleMesh*);
ERROR_TYPE free_mesh(TriangleMesh*);
int compareToken(const char*, char*, int);
int checkFormat(const char*);
ERROR_TYPE test_mesh(TriangleMesh*);
ERROR_TYPE store_STL(TriangleMesh*, int, std::vector<Vertex>& vertex_coords, std::vector<int>& triangle_vertices, std::vector<Vertex>& normal_coords);
ERROR_TYPE read_STL(const char* filename, TriangleMesh* mesh);
// Initialise elements of a mesh
ERROR_TYPE init_mesh(TriangleMesh* mesh)
{
mesh->vertex_count = 0;
mesh->vertex_coords = NULL;
mesh->triangle_count = 0;
mesh->triangle_vertices = NULL;
mesh->normal_coords = NULL;
return 0;
}
// Free the elements of a mesh
ERROR_TYPE free_mesh(TriangleMesh* mesh)
{
if (mesh->vertex_coords != NULL && mesh->normal_coords != NULL && mesh->triangle_vertices != NULL) {
mesh->vertex_count = 0;
mesh->triangle_count = 0;
free(mesh->vertex_coords);
mesh->vertex_coords = NULL;
free(mesh->triangle_vertices);
mesh->triangle_vertices = NULL;
free(mesh->normal_coords);
mesh->normal_coords = NULL;
return SUCCESS;
}
else {
return NULL_POINTER;
}
}
// There are various token that are present,
// For example: solid, facet are all tokens
// If the expected token and the input does not match, this function returns an unknown state
int compareToken(const char* token, char* input, int state)
{
if (strcmp(token, input) == 0) {
return state;
}
else {
return UNKNOWN;
}
}
// Find if the file has .stl extension
int checkFormat(const char* filename)
{
int filename_len = strlen(filename);
//Take the last four characters of the filename and check!
if (filename_len >= 4 && strcmp(filename + filename_len - 4, ".stl") == 0) {
return 1;
}
else {
return 0;
}
}
// Print the elements of the mesh
ERROR_TYPE test_mesh(TriangleMesh* mesh)
{
// Check whether none of the elements are null
if (mesh->vertex_coords != NULL && mesh->normal_coords != NULL && mesh->triangle_vertices != NULL) {
printf("\nNormal Coordinates:\n");
for (int i = 0; i < mesh->triangle_count; i++) {
int j = 3 * i;
// Print only the first three decimal places
printf("%.3lf %.3lf %.3lf\n", mesh->normal_coords[j + 0], mesh->normal_coords[j + 1], mesh->normal_coords[j + 2]);
}
printf("\nTriangle Vertices:\n");
for (int i = 0; i < mesh->triangle_count; i++) {
int j = 3 * i;
printf("%d %d %d\n", mesh->triangle_vertices[j + 0], mesh->triangle_vertices[j + 1], mesh->triangle_vertices[j + 2]);
}
printf("\nVertex Coordinates:\n");
for (int i = 0; i < mesh->vertex_count; i++) {
int j = 3 * i;
// Print only the first decimal place
printf("%.1lf %.1lf %.1lf\n", mesh->vertex_coords[j + 0], mesh->vertex_coords[j + 1], mesh->vertex_coords[j + 2]);
}
printf("\nTriangle Count: %d\n", mesh->triangle_count);
printf("\nVertex Count: %d\n", mesh->vertex_count);
return SUCCESS;
}
else {
return NULL_POINTER;
}
}
// The function read_STL only *reads* and the checks the file for errors
// Values are stored in local variables in read_STL.
// In this function, those local variables are passed and are stored in the TriangleMesh structure
ERROR_TYPE store_STL(TriangleMesh* mesh, int triangle_count, std::vector<Vertex>& vertex_coords,
std::vector<int>& triangle_vertices, std::vector<Vertex>& normal_coords)
{
// Allocate exact memory that is needed - The number of normal coordinates = number of triangles
// In each normal coordinate, there are three more values - Therefore we multiply by 3
mesh->normal_coords = (double*)malloc(3 * triangle_count * sizeof(double));
if (mesh->normal_coords != NULL) {
for (int i = 0; i < normal_coords.size(); i++) {
int j = 3 * i;
mesh->normal_coords[j + 0] = normal_coords[i].x;
mesh->normal_coords[j + 1] = normal_coords[i].y;
mesh->normal_coords[j + 2] = normal_coords[i].z;
}
}
else {
return NULL_POINTER;
}
mesh->triangle_count = triangle_count;
mesh->vertex_count = vertex_coords.size();
mesh->triangle_vertices = (int*)malloc(3 * triangle_count * sizeof(int));
if (mesh->normal_coords != NULL) {
for (int i = 0; i < triangle_vertices.size(); i++) {
mesh->triangle_vertices[i] = triangle_vertices[i];
}
}
else {
return NULL_POINTER;
}
mesh->vertex_coords = (double*)malloc(3 * vertex_coords.size() * sizeof(double));
if (mesh->vertex_coords != NULL) {
for (int i = 0; i < vertex_coords.size(); i++) {
int j = 3 * i;
mesh->vertex_coords[j + 0] = vertex_coords[i].x;
mesh->vertex_coords[j + 1] = vertex_coords[i].y;
mesh->vertex_coords[j + 2] = vertex_coords[i].z;
}
}
else {
return NULL_POINTER;
}
return SUCCESS;
}
int findVertex(std::vector<Vertex>& vertex_coords, Vertex vertex1)
{
int j;
double dx, dy, dz, answer;
for (int i = 0; i < vertex_coords.size(); i++) {
j = 3 * i;
Vertex vertex2 = vertex_coords[i];
dx = (vertex2.x - vertex1.x);
dy = (vertex2.y - vertex1.y);
dz = (vertex2.z - vertex1.z);
answer = sqrt((dx * dx) + (dy * dy) + (dz * dz));
if (answer >= 0 && answer < EPSILON) {
return i;
}
}
return -1;
}
// The parsing of .stl file can be thought of as a state diagram
// There are different states that a token can exist in
// Here, each token is seen one by one and the next expecting state is found
// If the expecting state and the token doesn't match, then it exits giving out an error!
ERROR_TYPE read_STL(const char* filename, TriangleMesh* mesh)
{
if (checkFormat(filename)) {
FILE* fp;
char c;
char buffer[50]; // Buffer to store the token
Vertex vertex;
int vertex_count = 0;
int triangle_count = 0;
int position = -1;
std::vector<Vertex> vertex_coords; // unordered_map each vertex to the index where it was seen
std::vector<int> triangle_vertices; // Store the indices of each the vertex
std::vector<Vertex> normal_coords; // For each triangle, store the normal vector
int state = SOLID; // Start the state from solid
if ((fp = fopen(filename, "r"))) { // Open the file
while (!feof(fp) && state != 0 && state != ENDSOLID) { //Stop parsing if an unknown state is reached or 'endsolid' tag or end of file
fscanf(fp, "%s", buffer); // Get each token from the file
//printf("%s\n",buffer);
//printf("State: %d\n",state);
switch (state) { // Check the state to which the token belongs to
case SOLID:
state = compareToken("solid", buffer, state); // Check whether the expected token and the input matches
while ((c = fgetc(fp)) != '\n') { // Go to the end of the line - In .stl files there are comments present in the first line
} // Skip all of that!
state = FACET; // If it has reached here, then it means that an Unknown state was not reached and can proceed to next expected state
break;
case FACET:
state = compareToken("facet", buffer, state); // If 'facet' token is obtained then move on to the 'normal'
state = NORMAL;
break;
case NORMAL:
state = compareToken("normal", buffer, state);
fscanf(fp, "%lf %lf %lf", &vertex.x, &vertex.y, &vertex.z); // Get the normal vector
//printf("%.1lf %.1lf %.1lf\n",vertex.x, vertex.y, vertex.z);
normal_coords.push_back(vertex); //Push this vector into the Vector
state = OUTER;
break;
case OUTER:
state = compareToken("outer", buffer, state);
state = LOOP;
break;
case LOOP:
state = compareToken("loop", buffer, state);
state = VERTEX;
break;
case VERTEX:
state = compareToken("vertex", buffer, state);
fscanf(fp, "%lf %lf %lf", &vertex.x, &vertex.y, &vertex.z);
position = findVertex(vertex_coords, vertex);
if (position == -1) { // Check if vertex already exists in the unordered_map, if it does not
vertex_coords.push_back(vertex); // then insert this vertex and assign the position where it has been found as its value
position = vertex_coords.size();
}
//printf("%.1lf %.1lf %.1lf\n",vertex.x, vertex.y, vertex.z);
triangle_vertices.push_back(position); //Add current parsed vertex into the set of triangle vertices
vertex_count++; // Once three vertices has been parsed in this triangle, move to the next state
if (vertex_count == 3) {
vertex_count = 0;
state = ENDLOOP;
}
break;
case ENDLOOP:
state = compareToken("endloop", buffer, state);
state = ENDFACET;
break;
case ENDFACET:
// If 'endfacet' is reached then there are two options, either next can be 'facet' or 'endsolid'
state = compareToken("endfacet", buffer, state);
triangle_count++;
state = BRANCH;
break;
case BRANCH:
if (compareToken("facet", buffer, state)) {
state = NORMAL; // if the token is 'facet' then start expecting 'normal'
}
else if (compareToken("endsolid", buffer, state)) {
state = ENDSOLID; // if the token is 'endsolid' we should stop parsing - while loop will take care of this
}
else {
state = UNKNOWN; // Any other token, it's definitely an Unknown state!
}
break;
default:
state = UNKNOWN;
break;
}
}
fclose(fp);
if (state == UNKNOWN) {
printf("%s", "Please check the syntax of your file\n");
return IMPROPER_SYNTAX;
}
else {
// If the state was not unknown, then start storing the STL file
return store_STL(mesh, triangle_count, vertex_coords, triangle_vertices, normal_coords);
}
}
else {
printf("%s", "There was an error opening the file\n");
return FILE_OPENING_ERROR;
}
}
else {
printf("%s", "Please check if file format is correct\n");
return INCORRECT_FILE_FORMAT;
}
}
int main(int argc, char* argv[])
{
if (argc == 2) {
TriangleMesh mesh;
init_mesh(&mesh);
read_STL(argv[1], &mesh);
test_mesh(&mesh);
free_mesh(&mesh);
}
else {
printf("%s", "Please check the number of arguments\n");
return FAILURE;
}
}