slicer/stl.c |
11 | 11 | */ |
12 | 12 | |
13 | 13 | |
| 14 | #include <stdint.h> |
14 | 15 | #include <stdlib.h> |
15 | 16 | #include <stdio.h> |
16 | 17 | #include <ctype.h> |
... | ... | |
33 | 34 | }; |
34 | 35 | |
35 | 36 | |
36 | | void stl_load_file(FILE *file, void (*facet)(struct v f[3])) |
| 37 | static 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 | } |
| 57 | fprintf(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 | |
| 83 | static void stl_load_text(FILE *file, void (*facet)(struct v f[3])) |
37 | 84 | { |
38 | 85 | char buf[MAX_LINE + 1]; |
39 | 86 | enum state state = s_init; |
... | ... | |
57 | 104 | |
58 | 105 | switch (state) { |
59 | 106 | case s_init: |
60 | | sscanf(s, "solid %*s%n", &end); |
| 107 | sscanf(s, " %*s%n", &end); |
61 | 108 | state = s_facet; |
62 | 109 | break; |
63 | 110 | case s_facet: |
... | ... | |
102 | 149 | } |
103 | 150 | |
104 | 151 | |
| 152 | void 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 | |
105 | 171 | void stl_load(const char *name, void (*facet)(struct v f[3])) |
106 | 172 | { |
107 | 173 | FILE *file; |