From 3a310e181e95e65609d64f7474b26446d4a1bc76 Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 2 Jun 2007 22:05:47 +0000 Subject: [PATCH] initial import git-svn-id: svn://anubis/misc/stl@4 bd8a9e45-a331-0410-811e-c64571078777 --- stl.c | 28 ++++++++++++++++++++++++++++ stl.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 stl.c create mode 100644 stl.h diff --git a/stl.c b/stl.c new file mode 100644 index 0000000..d9bcb37 --- /dev/null +++ b/stl.c @@ -0,0 +1,28 @@ + +#include +#include +#include "stl.h" + +/* load a STL file and return a pointer to the stl_t struct */ +stl_t * stl_load(const char * filename) +{ + struct stat s; + if (stat(filename, &s)) + return NULL; + int size = s.st_size; + if (size < sizeof(stl_t)) + return NULL; + FILE *fil; + if ((fil = fopen(filename, "rb")) == NULL) + return NULL; + stl_t * stl = (stl_t *) malloc(s.st_size); + fread(stl, 1, s.st_size, fil); + fclose(fil); + if (s.st_size < + sizeof(stl_t) + sizeof(stl_face_t) * (stl_num_faces(stl)-1)) + { + free(stl); + return NULL; + } + return stl; +} diff --git a/stl.h b/stl.h new file mode 100644 index 0000000..5ca140b --- /dev/null +++ b/stl.h @@ -0,0 +1,44 @@ +/* + * stl.h + * Author: Josh Holtrop + * Date: 2007-06-02 + * From information at http://en.wikipedia.org/wiki/STL_(file_format) + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* macros */ +#define STL_HEADER_LENGTH 80 +#define stl_num_faces(stlp) ((stlp)->n_faces) +#define stl_face(stlp,facenum) ((stlp)->faces[(facenum)]) +#define stl_attr_color_valid(attr) ((attr) & 0x8000) +#define stl_attr_color_r(attr) (((attr) & 0x7C00) >> 7) +#define stl_attr_color_g(attr) (((attr) & 0x03E0) >> 2) +#define stl_attr_color_b(attr) (((attr) & 0x001F) << 3) + + +/* types */ +typedef struct stl_face_s +{ + float normal[3]; + float vertices[3][3]; + unsigned short attribute; +} stl_face_t; + +typedef struct stl_s +{ + char header[STL_HEADER_LENGTH]; + unsigned int n_faces; + stl_face_t faces[1]; +} stl_t; + + +/* functions */ +stl_t * stl_load(const char * filename); + + +#ifdef __cplusplus +} +#endif