Root/slicer/stl.c

1/*
2 * stl.c - STL file reading
3 *
4 * Written 2014-2015 by Werner Almesberger
5 * Copyright 2014-2015 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13/*
14 * Format description:
15 * http://en.wikipedia.org/wiki/STL_%28file_format%29
16 */
17
18
19#include <stdint.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <ctype.h>
23#include <string.h>
24#include <math.h>
25
26#include "stl.h"
27
28
29#define MAX_LINE 100
30
31
32enum state {
33    s_init,
34    s_facet,
35    s_loop,
36    s_vertices,
37    s_endloop,
38    s_endfacet,
39};
40
41
42static void stl_load_binary(FILE *file, void (*facet)(struct v f[3]))
43{
44    char discard[75];
45    size_t n;
46    uint32_t nf;
47    uint16_t attr;
48    float tmp[4*3];
49    struct v f[3];
50    int i;
51
52    n = fread(discard, 1, sizeof(discard), file);
53    if (n != sizeof(discard)) {
54        fprintf(stderr, "incomplete header\n");
55        exit(1);
56    }
57    n = fread(&nf, 1, sizeof(nf), file);
58    if (n != sizeof(nf)) {
59        fprintf(stderr, "no number of facets\n");
60        exit(1);
61    }
62    while (nf--) {
63        n = fread(&tmp, 1, sizeof(tmp), file);
64        if (n != sizeof(tmp)) {
65            fprintf(stderr, "incomplete facet\n");
66            exit(1);
67        }
68        for (i = 0; i != 3; i++) {
69            f[i].x = tmp[3 * i + 3];
70            f[i].y = tmp[3 * i + 4];
71            f[i].z = tmp[3 * i + 5];
72        }
73        facet(f);
74        n = fread(&attr, 1, sizeof(attr), file);
75        if (n != sizeof(attr)) {
76            fprintf(stderr, "no attribute count\n");
77            exit(1);
78        }
79        if (attr) {
80            fprintf(stderr, "non-zero attribute count\n");
81            exit(1);
82        }
83    }
84}
85
86
87static void stl_load_text(FILE *file, void (*facet)(struct v f[3]))
88{
89    char buf[MAX_LINE + 1];
90    enum state state = s_init;
91    struct v f[3];
92    int num_v = 0;
93    char *s, *e;
94    int n = 0;
95    int end, got;
96
97    while (fgets(buf, sizeof(buf), file)) {
98        n++;
99        if (!(n & 1023))
100            fprintf(stderr, "%d\r", n);
101
102        for (s = buf; *s && isspace(*s); s++);
103        e = strchr(s, 0);
104        while (e != s && isspace(e[-1]))
105                e--;
106        *e = 0;
107        end = 0;
108
109        switch (state) {
110        case s_init:
111            sscanf(s, " %*s%n", &end);
112            state = s_facet;
113            break;
114        case s_facet:
115            if (!strncmp(s, "endsolid", 8))
116                return;
117            sscanf(s, "facet normal %*f %*f %*f%n", &end);
118            state = s_loop;
119            break;
120        case s_loop:
121            sscanf(s, "outer loop%n", &end);
122            state = s_vertices;
123            num_v = 0;
124            break;
125        case s_vertices:
126            got = sscanf(s, "vertex %f %f %f%n",
127                &f[num_v].x, &f[num_v].y, &f[num_v].z, &end);
128            if (got < 3)
129                break;
130            if (++num_v == 3) {
131                facet(f);
132                state = s_endloop;
133            }
134            break;
135        case s_endloop:
136            sscanf(s, "endloop%n", &end);
137            state = s_endfacet;
138            break;
139        case s_endfacet:
140            sscanf(s, "endfacet%n", &end);
141            state = s_facet;
142            break;
143        }
144        if (end != e - s) {
145            fprintf(stderr, "cannot parse line %d (%d %ld)\n",
146                 n, end, e - s);
147            exit(1);
148        }
149    }
150    fprintf(stderr, "incomplete STL file\n");
151    exit(1);
152
153}
154
155
156void stl_load_file(FILE *file, void (*facet)(struct v f[3]))
157{
158    char buf[5];
159    size_t n;
160
161    n = fread(buf, 1, sizeof(buf), file);
162    if (n != sizeof(buf)) {
163        fprintf(stderr, "file too short\n");
164        exit(1);
165    }
166
167    if (memcmp(buf, "solid", 5)) {
168        stl_load_binary(file, facet);
169    } else {
170        stl_load_text(file, facet);
171    }
172}
173
174
175void stl_load(const char *name, void (*facet)(struct v f[3]))
176{
177    FILE *file;
178
179    file = fopen(name, "r");
180    if (!file) {
181        perror(name);
182        exit(1);
183    }
184    stl_load_file(file, facet);
185    fclose(file);
186}
187

Archive Download this file

Branches:
master



interactive