2015-01-08 01:38:40 +02:00
|
|
|
/*
|
|
|
|
* stl.c - STL file reading
|
|
|
|
*
|
2015-01-08 02:26:50 +02:00
|
|
|
* Written 2014-2015 by Werner Almesberger
|
|
|
|
* Copyright 2014-2015 by Werner Almesberger
|
2015-01-08 01:38:40 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
2015-01-08 13:11:12 +02:00
|
|
|
/*
|
|
|
|
* Format description:
|
|
|
|
* http://en.wikipedia.org/wiki/STL_%28file_format%29
|
|
|
|
*/
|
|
|
|
|
2015-01-08 01:38:40 +02:00
|
|
|
|
2015-01-08 05:15:37 +02:00
|
|
|
#include <stdint.h>
|
2015-01-08 01:38:40 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "stl.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_LINE 100
|
|
|
|
|
|
|
|
|
|
|
|
enum state {
|
|
|
|
s_init,
|
|
|
|
s_facet,
|
|
|
|
s_loop,
|
|
|
|
s_vertices,
|
|
|
|
s_endloop,
|
|
|
|
s_endfacet,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-08 05:15:37 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
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]))
|
2015-01-08 01:38:40 +02:00
|
|
|
{
|
2015-01-08 02:26:50 +02:00
|
|
|
char buf[MAX_LINE + 1];
|
2015-01-08 01:38:40 +02:00
|
|
|
enum state state = s_init;
|
2015-01-08 02:26:50 +02:00
|
|
|
struct v f[3];
|
2015-01-08 01:38:40 +02:00
|
|
|
int num_v = 0;
|
|
|
|
char *s, *e;
|
|
|
|
int n = 0;
|
|
|
|
int end, got;
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
|
|
n++;
|
|
|
|
if (!(n & 1023))
|
|
|
|
fprintf(stderr, "%d\r", n);
|
|
|
|
|
|
|
|
for (s = buf; *s && isspace(*s); s++);
|
|
|
|
e = strchr(s, 0);
|
|
|
|
while (e != s && isspace(e[-1]))
|
|
|
|
e--;
|
|
|
|
*e = 0;
|
|
|
|
end = 0;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case s_init:
|
2015-01-08 05:15:37 +02:00
|
|
|
sscanf(s, " %*s%n", &end);
|
2015-01-08 01:38:40 +02:00
|
|
|
state = s_facet;
|
|
|
|
break;
|
|
|
|
case s_facet:
|
|
|
|
if (!strncmp(s, "endsolid", 8))
|
|
|
|
return;
|
|
|
|
sscanf(s, "facet normal %*f %*f %*f%n", &end);
|
|
|
|
state = s_loop;
|
|
|
|
break;
|
|
|
|
case s_loop:
|
|
|
|
sscanf(s, "outer loop%n", &end);
|
|
|
|
state = s_vertices;
|
|
|
|
num_v = 0;
|
|
|
|
break;
|
|
|
|
case s_vertices:
|
2015-01-08 02:26:50 +02:00
|
|
|
got = sscanf(s, "vertex %f %f %f%n",
|
|
|
|
&f[num_v].x, &f[num_v].y, &f[num_v].z, &end);
|
2015-01-08 01:38:40 +02:00
|
|
|
if (got < 3)
|
|
|
|
break;
|
|
|
|
if (++num_v == 3) {
|
2015-01-08 02:26:50 +02:00
|
|
|
facet(f);
|
2015-01-08 01:38:40 +02:00
|
|
|
state = s_endloop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case s_endloop:
|
|
|
|
sscanf(s, "endloop%n", &end);
|
|
|
|
state = s_endfacet;
|
|
|
|
break;
|
|
|
|
case s_endfacet:
|
|
|
|
sscanf(s, "endfacet%n", &end);
|
|
|
|
state = s_facet;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-08 02:26:50 +02:00
|
|
|
if (end != e - s) {
|
2015-01-08 01:38:40 +02:00
|
|
|
fprintf(stderr, "cannot parse line %d (%d %ld)\n",
|
2015-01-08 02:26:50 +02:00
|
|
|
n, end, e - s);
|
2015-01-08 01:38:40 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "incomplete STL file\n");
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-08 05:15:37 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-08 02:26:50 +02:00
|
|
|
void stl_load(const char *name, void (*facet)(struct v f[3]))
|
2015-01-08 01:38:40 +02:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
|
|
|
if (!file) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-08 02:26:50 +02:00
|
|
|
stl_load_file(file, facet);
|
2015-01-08 01:38:40 +02:00
|
|
|
fclose(file);
|
|
|
|
}
|