forked from ch3ll0v3k/stlviewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stl.c
683 lines (529 loc) · 16.3 KB
/
stl.c
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
* Copyright (c) 2012, Vishal Patil
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <float.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>
#include <math.h>
#include "stl.h"
#define STL_MAGIC 0xdeadbeef
#define STL_STR_SOLID_START "solid"
#define STL_STR_SOLID_END "endsolid"
#define STL_STR_FACET_START "facet"
#define STL_STR_FACET_END "endfacet"
#define STL_STR_LOOP_START "outer"
#define STL_STR_LOOP_END "endloop"
#define STL_STR_VERTEX "vertex"
typedef enum {
STL_FILE_TYPE_INVALID,
STL_FILE_TYPE_TXT,
STL_FILE_TYPE_BIN
} stl_file_type_t;
typedef enum stl_token {
STL_TOKEN_INVALID,
STL_TOKEN_SOLID_START,
STL_TOKEN_SOLID_END,
STL_TOKEN_FACET_START,
STL_TOKEN_FACET_END,
STL_TOKEN_LOOP_START,
STL_TOKEN_LOOP_END,
STL_TOKEN_VERTEX
} stl_token_t;
struct {
const char *str;
stl_token_t token;
} stl_token_map[] = {
{STL_STR_SOLID_START, STL_TOKEN_SOLID_START},
{STL_STR_SOLID_END, STL_TOKEN_SOLID_END},
{STL_STR_FACET_START, STL_TOKEN_FACET_START},
{STL_STR_FACET_END, STL_TOKEN_FACET_END},
{STL_STR_LOOP_START, STL_TOKEN_LOOP_START},
{STL_STR_LOOP_END, STL_TOKEN_LOOP_END},
{STL_STR_VERTEX, STL_TOKEN_VERTEX},
};
#define STL_TOTAL_TOKENS (sizeof(stl_token_map)/sizeof(stl_token_map[0]))
typedef enum stl_state {
STL_STATE_START,
STL_STATE_SOLID_START,
STL_STATE_SOLID_END,
STL_STATE_FACET_START,
STL_STATE_FACET_END,
STL_STATE_LOOP_START,
STL_STATE_LOOP_END,
STL_STATE_VERTEX
} stl_state_t;
typedef struct {
STLFloat x;
STLFloat y;
STLFloat z;
} vector_t;
typedef vector_t vertex_t;
typedef vector_t normal_t;
struct stl_s {
int magic;
char *file;
stl_file_type_t type;
stl_state_t state;
STLuint32 facet_cnt;
STLuint vertex_cnt;
STLFloat *vertices;
STLFloat min_x;
STLFloat max_x;
STLFloat min_y;
STLFloat max_y;
STLFloat min_z;
STLFloat max_z;
int lineno;
int loaded;
};
stl_t *
stl_alloc(void)
{
stl_t *stl = NULL;
stl = (stl_t *)malloc(sizeof(*stl));
if (stl == NULL) {
return NULL;
}
memset(stl, 0, sizeof(*stl));
stl->magic = STL_MAGIC;
return stl;
}
void
stl_free(stl_t *stl)
{
if (stl) {
assert(stl->magic == STL_MAGIC);
if (stl->magic != STL_MAGIC) {
return;
}
if (stl->vertices) {
free(stl->vertices);
}
free(stl);
}
}
static stl_token_t
stl_str_token(const char* str)
{
stl_token_t token = STL_TOKEN_INVALID;
int i = 0;
for (i = 0; i < STL_TOTAL_TOKENS; i++) {
if (strcasecmp(stl_token_map[i].str, str) == 0) {
token = stl_token_map[i].token;
break;
}
}
return token;
}
static stl_error_t
stl_parse_txt(stl_t *stl)
{
FILE *fp = fopen(stl->file, "r");
char *str_token = NULL;
stl_token_t token;
int vertex_cnt = 0;
int error = 1;
stl_error_t ret = STL_ERR_NONE;
if (fp == NULL) {
return STL_ERR_FOPEN;
}
char buffer[256];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
stl->lineno++;
str_token = strtok(buffer, " \r\n");
/* Empty line */
if (str_token == NULL) {
continue;
}
/* Check for invalid line format */
token = stl_str_token(str_token);
if (token == STL_TOKEN_INVALID) {
STL_DBG("Error while processing token %s\n", str_token);
goto err;
}
switch (token) {
case STL_TOKEN_SOLID_START:
stl->state = STL_STATE_SOLID_START;
break;
case STL_TOKEN_SOLID_END:
error = 0;
stl->state = STL_STATE_SOLID_END;
break;
case STL_TOKEN_FACET_START:
if (stl->state != STL_STATE_SOLID_START &&
stl->state != STL_STATE_FACET_END) {
goto err;
}
stl->state = STL_STATE_FACET_START;
break;
case STL_TOKEN_FACET_END:
if (stl->state != STL_STATE_LOOP_END) {
goto err;
}
stl->state = STL_STATE_FACET_END;
stl->facet_cnt = stl->facet_cnt + 1;
break;
case STL_TOKEN_LOOP_START:
if (stl->state != STL_STATE_FACET_START) {
goto err;
}
stl->state = STL_STATE_LOOP_START;
vertex_cnt = 0;
break;
case STL_TOKEN_LOOP_END:
if (stl->state != STL_STATE_VERTEX ||
vertex_cnt != 3) {
goto err;
}
stl->state = STL_STATE_LOOP_END;
break;
case STL_TOKEN_VERTEX:
if (stl->state != STL_STATE_VERTEX &&
stl->state != STL_STATE_LOOP_START) {
goto err;
}
stl->state = STL_STATE_VERTEX;
stl->vertex_cnt = stl->vertex_cnt + 1;
vertex_cnt += 1;
break;
default:
goto err;
}
}
err:
ret = error ? STL_ERR_FILE_FORMAT : STL_ERR_NONE;
fclose(fp);
return ret;
}
static void
calculate_triangle_normal(vertex_t v1, vertex_t v2, vertex_t v3, normal_t *normal)
{
vector_t U;
vector_t V;
STLFloat length;
U.x = v2.x - v1.x;
U.y = v2.y - v1.y;
U.z = v2.z - v1.z;
V.x = v3.x - v1.x;
V.y = v3.y - v1.y;
V.z = v3.z - v1.z;
normal->x = U.y * V.z - U.z * V.y;
normal->y = U.z * V.x - U.x * V.z;
normal->z = U.x * V.y - U.y * V.x;
length = normal->x * normal->x + normal->y * normal->y + normal->z * normal->z;
length = sqrt(length);
normal->x = normal->x / length;
normal->y = normal->y / length;
normal->z = normal->z / length;
}
static void
stl_fill_vertex_normals(stl_t *stl)
{
STLuint vertex_cnt = stl->vertex_cnt;
STLuint triangle_cnt = vertex_cnt / 3;
STLuint i, idx = 0;
vertex_t *v1, *v2, *v3;
normal_t normal, *n1, *n2, *n3;
STLFloat *vertices = stl->vertices;
for (i = 0; i < triangle_cnt; i++) {
idx = i*18;
v1 = (vertex_t *)&vertices[idx + 0];
n1 = (normal_t *)&vertices[idx + 3];
v2 = (vertex_t *)&vertices[idx + 6];
n2 = (normal_t *)&vertices[idx + 9];
v3 = (vertex_t *)&vertices[idx + 12];
n3 = (normal_t *)&vertices[idx + 15];
calculate_triangle_normal(*v1, *v2, *v3, &normal);
*n1 = normal;
*n2 = normal;
*n3 = normal;
}
}
static stl_error_t
stl_get_vertices(stl_t *stl)
{
FILE *fp = fopen(stl->file, "r");
char *str_token = NULL;
char *vx = NULL;
char *vy = NULL;
char *vz = NULL;
STLFloat fvx, fvy, fvz;
stl_token_t token;
int vertex_idx = 0;
int error = 1;
stl_error_t ret = STL_ERR_NONE;
if (fp == NULL) {
return STL_ERR_FOPEN;
}
char buffer[256];
stl->min_x = stl->min_y = stl->min_z = FLT_MAX;
stl->max_x = stl->max_y = stl->max_z = FLT_MIN;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
str_token = strtok(buffer, " \r\n");
/* Empty line */
if (str_token == NULL) {
continue;
}
/* Check for invalid line format */
token = stl_str_token(str_token);
if (token == STL_TOKEN_INVALID) {
STL_DBG("Error while processing token %s\n", str_token);
goto err;
}
if (token == STL_TOKEN_VERTEX) {
vx = strtok(NULL, " ");
if (vx == NULL) {
goto err;
}
fvx = strtof(vx, NULL);
if (fvx < stl->min_x) {
stl->min_x = fvx;
}
if (fvx > stl->max_x) {
stl->max_x = fvx;
}
stl->vertices[vertex_idx++] = fvx;
vy = strtok(NULL, " ");
if (vy == NULL) {
goto err;
}
fvy = strtof(vy, NULL);
if (fvy < stl->min_y) {
stl->min_y = fvy;
}
if (fvy > stl->max_y) {
stl->max_y = fvy;
}
stl->vertices[vertex_idx++] = fvy;
vz = strtok(NULL, " ");
if (vz == NULL) {
goto err;
}
fvz = strtof(vz, NULL);
if (fvz < stl->min_z) {
stl->min_z = fvz;
}
if (fvz > stl->max_z) {
stl->max_z = fvz;
}
stl->vertices[vertex_idx++] = fvz;
vertex_idx += 3;
}
}
error = 0;
err:
ret = error ? STL_ERR_FILE_FORMAT : STL_ERR_NONE;
fclose(fp);
return ret;
}
static stl_file_type_t
stl_get_filetype(char *filename)
{
stl_file_type_t type = STL_FILE_TYPE_INVALID;
int fd = open(filename, O_RDONLY);
if (fd == -1) {
return STL_FILE_TYPE_INVALID;
}
STLuint8 c;
int bytes_read = -1;
while ((bytes_read = read(fd, &c, sizeof(c))) != 0 && c <= 127) {
}
type = (bytes_read == 0) ? STL_FILE_TYPE_TXT : STL_FILE_TYPE_BIN;
close(fd);
return type;
}
typedef struct {
STLFloat32 x;
STLFloat32 y;
STLFloat32 z;
} stl_vector_t;
#define STL_BIN_HEADER_SIZE 80
#define STL_TRIANGLE_VERTEX_CNT 3
static stl_error_t
stl_load_bin_file(stl_t *stl)
{
stl_error_t err = STL_ERR_NONE;
FILE *fp = fopen(stl->file, "r");
if (fp == NULL) {
return STL_ERR_FOPEN;
}
/* skip the stl file header */
if (fseek(fp, STL_BIN_HEADER_SIZE, SEEK_CUR) != 0) {
err = STL_ERR_FILE_FORMAT;
goto done;
}
/* Read the the facet count */
if (fread(&stl->facet_cnt, sizeof(stl->facet_cnt), 1, fp) != 1) {
err = STL_ERR_FILE_FORMAT;
goto done;
}
int expected_vertex_cnt = stl->facet_cnt*3;
stl->vertices = (STLFloat *)malloc(expected_vertex_cnt * 6 * sizeof(STLFloat));
if (stl->vertices == NULL) {
err = STL_ERR_MEM;
goto done;
}
stl_vector_t vec;
int vertex_idx = 0;
int triangle_idx = 0;
STLuint8 abc[2];
stl->min_x = stl->min_y = stl->min_z = FLT_MAX;
stl->max_x = stl->max_y = stl->max_z = FLT_MIN;
for (triangle_idx = 0; triangle_idx < stl->facet_cnt; triangle_idx++) {
/* Read the normal vector */
if (fread(&vec, sizeof(vec), 1, fp) != 1) {
err = STL_ERR_FILE_FORMAT;
goto done;
}
int idx = 0;
for (idx = 0; idx < STL_TRIANGLE_VERTEX_CNT; idx++) {
/* Read the vertex vector */
if (fread(&vec, sizeof(vec), 1, fp) != 1) {
err = STL_ERR_FILE_FORMAT;
goto done;
}
stl->vertex_cnt++;
stl->vertices[vertex_idx++] = vec.x;
stl->vertices[vertex_idx++] = vec.y;
stl->vertices[vertex_idx++] = vec.z;
vertex_idx += 3;
if (vec.x < stl->min_x) stl->min_x = vec.x;
if (vec.x > stl->max_x) stl->max_x = vec.x;
if (vec.y < stl->min_y) stl->min_y = vec.y;
if (vec.y > stl->max_y) stl->max_y = vec.y;
if (vec.z < stl->min_z) stl->min_z = vec.z;
if (vec.z > stl->max_z) stl->max_z = vec.z;
}
/* Read the Attribute Byte Count */
if (fread(&abc, sizeof(abc), 1, fp) != 1) {
err = STL_ERR_FILE_FORMAT;
goto done;
}
}
stl_fill_vertex_normals(stl);
stl->loaded = 1;
done:
fclose(fp);
return err;
}
static stl_error_t
stl_load_txt_file(stl_t *stl)
{
stl_error_t err = STL_ERR_NONE;
if ((err = stl_parse_txt(stl)) != STL_ERR_NONE) {
return err;
}
stl->vertices = (STLFloat *)malloc(6 * stl->vertex_cnt * sizeof(STLFloat));
if (stl->vertices == NULL) {
return STL_ERR_MEM;
}
if ((err = stl_get_vertices(stl)) != STL_ERR_NONE) {
return err;
}
stl_fill_vertex_normals(stl);
stl->loaded = 1;
return err;
}
stl_error_t
stl_load(stl_t *stl, char *filename)
{
stl_error_t err = STL_ERR_NONE;
stl_file_type_t type = STL_FILE_TYPE_INVALID;
stl->file = filename;
type = stl_get_filetype(filename);
switch (type) {
case STL_FILE_TYPE_TXT:
err = stl_load_txt_file(stl);
break;
case STL_FILE_TYPE_BIN:
err = stl_load_bin_file(stl);
break;
default:
err = STL_ERR_FILE_FORMAT;
}
return err;
}
STLFloat
stl_min_x(stl_t *stl)
{
return stl->min_x;
}
STLFloat
stl_max_x(stl_t *stl)
{
return stl->max_x;
}
STLFloat
stl_min_y(stl_t *stl)
{
return stl->min_y;
}
STLFloat
stl_max_y(stl_t *stl)
{
return stl->max_y;
}
STLFloat
stl_min_z(stl_t *stl)
{
return stl->min_z;
}
STLFloat
stl_max_z(stl_t *stl)
{
return stl->max_z;
}
STLuint
stl_facet_cnt(stl_t *stl)
{
return stl->facet_cnt;
}
STLuint
stl_vertex_cnt(stl_t *stl)
{
return stl->vertex_cnt;
}
stl_error_t
stl_vertices(stl_t *stl, STLFloat **points)
{
if (stl->loaded == 0) {
return STL_ERR_NOT_LOADED;
}
*points = stl->vertices;
return STL_ERR_NONE;
}
int
stl_error_lineno(stl_t *stl)
{
return stl->lineno;
}