1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 09:55:32 +02:00

slicer/stl.c (stl_load_file): support binary STL as well (on little-endian hosts)

This commit is contained in:
Werner Almesberger 2015-01-08 00:15:37 -03:00
parent 3f2c6cdc30
commit c82ea10493

View File

@ -11,6 +11,7 @@
*/ */
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
@ -33,7 +34,53 @@ enum state {
}; };
void stl_load_file(FILE *file, void (*facet)(struct v f[3])) static void stl_load_binary(FILE *file, void (*facet)(struct v f[3]))
{
char discard[75];
size_t n;
uint32_t nf;
uint16_t attr;
float tmp[4*3];
struct v f[3];
int i;
n = fread(discard, 1, sizeof(discard), file);
if (n != sizeof(discard)) {
fprintf(stderr, "incomplete header\n");
exit(1);
}
n = fread(&nf, 1, sizeof(nf), file);
if (n != sizeof(nf)) {
fprintf(stderr, "no number of facets\n");
exit(1);
}
fprintf(stderr, "nf %u\n", (unsigned) nf);
while (nf--) {
n = fread(&tmp, 1, sizeof(tmp), file);
if (n != sizeof(tmp)) {
fprintf(stderr, "incomplete facet\n");
exit(1);
}
for (i = 0; i != 3; i++) {
f[i].x = tmp[3 * i + 3];
f[i].y = tmp[3 * i + 4];
f[i].z = tmp[3 * i + 5];
}
facet(f);
n = fread(&attr, 1, sizeof(attr), file);
if (n != sizeof(attr)) {
fprintf(stderr, "no attribute count\n");
exit(1);
}
if (attr) {
fprintf(stderr, "non-zero attribute count\n");
exit(1);
}
}
}
static void stl_load_text(FILE *file, void (*facet)(struct v f[3]))
{ {
char buf[MAX_LINE + 1]; char buf[MAX_LINE + 1];
enum state state = s_init; enum state state = s_init;
@ -57,7 +104,7 @@ void stl_load_file(FILE *file, void (*facet)(struct v f[3]))
switch (state) { switch (state) {
case s_init: case s_init:
sscanf(s, "solid %*s%n", &end); sscanf(s, " %*s%n", &end);
state = s_facet; state = s_facet;
break; break;
case s_facet: case s_facet:
@ -102,6 +149,25 @@ void stl_load_file(FILE *file, void (*facet)(struct v f[3]))
} }
void stl_load_file(FILE *file, void (*facet)(struct v f[3]))
{
char buf[5];
size_t n;
n = fread(buf, 1, sizeof(buf), file);
if (n != sizeof(buf)) {
fprintf(stderr, "file too short\n");
exit(1);
}
if (memcmp(buf, "solid", 5)) {
stl_load_binary(file, facet);
} else {
stl_load_text(file, facet);
}
}
void stl_load(const char *name, void (*facet)(struct v f[3])) void stl_load(const char *name, void (*facet)(struct v f[3]))
{ {
FILE *file; FILE *file;