Date:2015-01-08 04:15:37 (9 years 2 months ago)
Author:Werner Almesberger
Commit:c82ea10493e560499e7b90e59092005e22ffb3c3
Message:slicer/stl.c (stl_load_file): support binary STL as well (on little-endian hosts)

Files: slicer/stl.c (4 diffs)

Change Details

slicer/stl.c
1111 */
1212
1313
14#include <stdint.h>
1415#include <stdlib.h>
1516#include <stdio.h>
1617#include <ctype.h>
...... 
3334};
3435
3536
36void stl_load_file(FILE *file, void (*facet)(struct v f[3]))
37static void stl_load_binary(FILE *file, void (*facet)(struct v f[3]))
38{
39    char discard[75];
40    size_t n;
41    uint32_t nf;
42    uint16_t attr;
43    float tmp[4*3];
44    struct v f[3];
45    int i;
46
47    n = fread(discard, 1, sizeof(discard), file);
48    if (n != sizeof(discard)) {
49        fprintf(stderr, "incomplete header\n");
50        exit(1);
51    }
52    n = fread(&nf, 1, sizeof(nf), file);
53    if (n != sizeof(nf)) {
54        fprintf(stderr, "no number of facets\n");
55        exit(1);
56    }
57fprintf(stderr, "nf %u\n", (unsigned) nf);
58    while (nf--) {
59        n = fread(&tmp, 1, sizeof(tmp), file);
60        if (n != sizeof(tmp)) {
61            fprintf(stderr, "incomplete facet\n");
62            exit(1);
63        }
64        for (i = 0; i != 3; i++) {
65            f[i].x = tmp[3 * i + 3];
66            f[i].y = tmp[3 * i + 4];
67            f[i].z = tmp[3 * i + 5];
68        }
69        facet(f);
70        n = fread(&attr, 1, sizeof(attr), file);
71        if (n != sizeof(attr)) {
72            fprintf(stderr, "no attribute count\n");
73            exit(1);
74        }
75        if (attr) {
76            fprintf(stderr, "non-zero attribute count\n");
77            exit(1);
78        }
79    }
80}
81
82
83static void stl_load_text(FILE *file, void (*facet)(struct v f[3]))
3784{
3885    char buf[MAX_LINE + 1];
3986    enum state state = s_init;
...... 
57104
58105        switch (state) {
59106        case s_init:
60            sscanf(s, "solid %*s%n", &end);
107            sscanf(s, " %*s%n", &end);
61108            state = s_facet;
62109            break;
63110        case s_facet:
...... 
102149}
103150
104151
152void stl_load_file(FILE *file, void (*facet)(struct v f[3]))
153{
154    char buf[5];
155    size_t n;
156
157    n = fread(buf, 1, sizeof(buf), file);
158    if (n != sizeof(buf)) {
159        fprintf(stderr, "file too short\n");
160        exit(1);
161    }
162
163    if (memcmp(buf, "solid", 5)) {
164        stl_load_binary(file, facet);
165    } else {
166        stl_load_text(file, facet);
167    }
168}
169
170
105171void stl_load(const char *name, void (*facet)(struct v f[3]))
106172{
107173    FILE *file;

Archive Download the corresponding diff file

Branches:
master



interactive