summaryrefslogtreecommitdiffstats
path: root/include/stl.h
blob: 14ec11ac14f4263d0ce378a812b00d19d7753671 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#include <stdbool.h>
#include <stdlib.h>

#define STL_BUFMAX 256

#define STL_STRERROR_INIT \
	[STL_OK] = "Success", \
	[STL_DONE] = "Done processing", \
	[STL_INVALID] = "Invalid stl file", \
	[STL_INVALID_ARG] = "Invalid argument", \
	[STL_INCOMPLETE] = "Unexpected end of file" \

enum {
	STL_OK,
	STL_DONE,
	STL_INVALID,
	STL_INVALID_ARG,
	STL_INCOMPLETE,
};

enum {
	STL_TYPE_DETECT,
	STL_TYPE_ASCII,
	STL_TYPE_BINARY,
};

enum {
	STL_RES_SOLID_NAME,
	STL_RES_HEADER,
	STL_RES_TRIANGLE,
	STL_RES_FILETYPE
};

struct stl_vertex {
	union {
		struct {
			float x, y, z;
		};
		struct {
			float dim[3];
		};
	};
};

struct stl_triangle {
	struct stl_vertex vtx[3];
	struct stl_vertex normal;
};

struct stl_span {
	const char *str;
	size_t len;
};

struct stl_result {
	int type;
	union {
		struct stl_span solid_name;
		struct stl_span header;
		struct stl_triangle tri;
		int filetype;
	};
};

struct stl {
	char buf[STL_BUFMAX];
	size_t buflen;

	const void *chunk;
	const void *pos;
	size_t nleft;

	struct stl_triangle tri;

	int type;
	union {
		struct {
			int state;
		} ascii;
		struct {
			int state;
			size_t index;
			size_t count;
		} bin;
	};
};

void stl_init(struct stl *stl, int type);
int stl_feed(struct stl *stl, struct stl_result *res,
	const void *chunk, size_t size);