initial import

git-svn-id: svn://anubis/misc/stl@4 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2007-06-02 22:05:47 +00:00
parent 7fa04b3b2e
commit 3a310e181e
2 changed files with 72 additions and 0 deletions

28
stl.c Normal file
View File

@ -0,0 +1,28 @@
#include <sys/stat.h>
#include <stdio.h>
#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;
}

44
stl.h Normal file
View File

@ -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