aboutsummaryrefslogtreecommitdiffstats
path: root/service/src/stlfile.c
blob: f15cc95b5bb6f3747646ac29c86fb4a7d038db1c (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "stlfile.h"

static const char wsset[] = " \r\n\t";
static const struct {
	int code;
	const char *str;
} kwmap[] = {
	{ KW_SOLID_BEGIN, "solid"        },
	{ KW_SOLID_END,   "endsolid"     },
	{ KW_LOOP_BEGIN,  "outer loop"   },
	{ KW_LOOP_END,    "endloop"      },
	{ KW_FACET_BEGIN, "facet normal" },
	{ KW_FACET_END,   "endfacet"     },
	{ KW_VERTEX,      "vertex"       },
};

void
stack_init(struct stack *stack)
{
	stack->cap = 10;
	stack->data = checkp(malloc(sizeof(int) * stack->cap));
	stack->count = 0;
}

void
stack_push(struct stack *stack, int v)
{
	if (stack->count == stack->cap) {
		stack->cap *= 2;
		stack->data = checkp(realloc(stack->data, sizeof(int) * stack->cap));
	}

	stack->data[stack->count] = v;
	stack->count++;
}

int
stack_pop(struct stack *stack)
{
	if (stack->count == 0)
		return -1;

	stack->count--;
	return stack->data[stack->count];
}

void
stack_free(struct stack *stack)
{
	free(stack->data);
}

int
stack_ind(struct stack *stack, int search)
{
	int i;
	for (i = 0; i < stack->count; i++)
		if (stack->data[i] == search)
			return i;
	return -1;
}

char*
skip_set(char *p, const char *set)
{
	for (; *p && strchr(set, *p); p++);
	return p;
}

char*
consume_arg(char **start)
{
	char *p, *tmp;

	p = skip_set(*start, wsset);
	for (; !strchr(wsset, *p); p++);
	if (!*p) return NULL;
	*p = '\0';
	tmp = *start;
	*start = p + 1;
	return tmp;
}

int
consume_keyword(char **start)
{
	char *bp;
	int i, len;

	bp = skip_set(*start, wsset);

	for (i = 0; i < ARRSIZE(kwmap); i++) {
		len = strlen(kwmap[i].str);
		if (!strncmp(kwmap[i].str, bp, len) && strchr(wsset, *(bp + len))) {
			printf("GOT: %s\n", kwmap[i].str);
			*start = bp + len + (bp[len] ? 1 : 0);
			return kwmap[i].code;
		}
	}

	return KW_UNKNOWN;
}

#define PARSE_FAIL(...) \
	do { fprintf(stderr, "FORMAT ERR: " __VA_ARGS__); goto fail; } while (0)

int
parse_file_ascii(struct parseinfo *info, char *buf, size_t len)
{
	char *bp, *arg, *prev, *tmp;
	struct stack states;
	float farg;
	int i, kw;

	stack_init(&states);

	info->type = TYPE_ASCII;
	info->loopcount = 0;

	bp = prev = buf;
	while ((kw = consume_keyword(&bp))) {
		switch (kw) {
		case KW_SOLID_BEGIN:
			stack_push(&states, STATE_SOLID);
			if (stack_ind(&states, KW_SOLID_BEGIN) != -1)
				PARSE_FAIL("Multiple nested solids!\n");
			tmp = bp;
			if (!consume_keyword(&bp) && (arg = consume_arg(&bp))) {
				strncpy(info->extra, arg, sizeof(info->extra));
			} else {
				bp = tmp;
			}
			break;
		case KW_SOLID_END:
			if ((kw = stack_pop(&states)) != STATE_SOLID)
				PARSE_FAIL("Improper nesting, parent: %s\n", kwmap[kw].str);
			tmp = bp;
			if (*info->extra && !consume_keyword(&bp)
					&& (arg = consume_arg(&bp))) {
				if (strncmp(info->extra, arg, sizeof(info->extra)))
					PARSE_FAIL("Solid end/begin names do not match!\n");
			} else {
				bp = tmp;
			}
			break;
		case KW_LOOP_BEGIN:
			stack_push(&states, STATE_LOOP);
			if (stack_ind(&states, KW_LOOP_BEGIN) != -1)
				PARSE_FAIL("Multiple nested loops!\n");
			break;
		case KW_LOOP_END:
			if ((kw = stack_pop(&states)) != STATE_LOOP)
				PARSE_FAIL("Improper nesting, parent: %s\n", kwmap[kw].str);
			info->loopcount++;
			break;
		case KW_FACET_BEGIN:
			stack_push(&states, STATE_FACET);
			if (stack_ind(&states, KW_LOOP_BEGIN) != -1)
				PARSE_FAIL("Multiple nested facets!\n");
			for (i = 0; i < 3; i++) {
				if (!(arg = consume_arg(&bp)))
					PARSE_FAIL("Facet with less than 3 args!\n");
				farg = strtof(arg, &tmp);
				if (tmp && *tmp)
					PARSE_FAIL("Facet with invalid arg '%s'!\n", arg);
			}
			break;
		case KW_FACET_END:
			if ((kw = stack_pop(&states)) != STATE_FACET)
				PARSE_FAIL("Improper nesting, parent: %s\n", kwmap[kw].str);
			break;
		case KW_VERTEX:
			/* TODO: calc bounding box */
			for (i = 0; i < 3; i++) {
				if (!(arg = consume_arg(&bp)))
					PARSE_FAIL("Vertex with less than 3 args!\n");
				farg = strtof(arg, &tmp);
				if (tmp && *tmp)
					PARSE_FAIL("Vertex with invalid arg '%s'!\n", arg);
			}
			break;
		case KW_UNKNOWN:
			prev = skip_set(prev, wsset);
			PARSE_FAIL("Expected keyword, got:\n%.*s...\n", 30, prev);
		}
		prev = bp;
	}

	if (states.count)
		PARSE_FAIL("Expected keyword, got:\n%.*s...\n", 30, bp);

	stack_free(&states);
	return OK;

fail:
	stack_free(&states);
	return FAIL;
}

int
parse_file_bin(struct parseinfo *info, char *buf, size_t len)
{
	char *bp, *end = buf + len;
	unsigned int i;

	info->type = TYPE_BIN;

	if (len < 84)
		PARSE_FAIL("Truncated data! (header missing)\n");

	memcpy(info->extra, buf, 80);

	bp = buf + 80;
	info->loopcount = le32toh(*(uint32_t*)bp);

	for (i = 0; i < info->loopcount; i++) {
		if (bp + 50 > end)
			PARSE_FAIL("Truncated data! (loops missing)\n");
		/* TODO: load floats and calc bounding box */
		bp += 50;
	}

	return OK;

fail:
	return FAIL;
}

int
parse_file(struct parseinfo *info, char *buf, size_t len)
{
	int status;
	char *bp;

	if (info->valid) free_info(info);

	/* check bin vs ascii */
	for (bp = buf; strchr(wsset, *bp); bp++);

	status = !strncmp("solid ", bp, 6)
		? parse_file_ascii(info, buf, len)
		: parse_file_bin(info, buf, len);
	if (status == FAIL) return FAIL;

	/* create model hash */
	strncpy(info->hash, mhash(info->extra, sizeof(info->extra)), MHASHLEN);

	return OK;
}

int
save_info(struct parseinfo *info, const char *resultdir)
{
	/*  <HASH>:
	 *	  - info : binary file info
	 *    - model : original stl file
	 */

	return FAIL;
}

void
print_info(struct parseinfo *info)
{
	int i, k;

#define FILTERCHAR(c) ((c) >= 32 ? (c) : '?')

	if (info->type == TYPE_ASCII) {
		printf("Modelname: %s\n", info->extra);
	} else {
		printf("Model Header:\n");
		for (i = 0; i < 80; i += k) {
			for (k = i; k < MIN(80, i + 20); k++)
				printf(" %02x", info->extra[i+k]);
			printf(" | ");
			for (k = i; k < MIN(80, i + 20); k++)
				printf("%c", FILTERCHAR(info->extra[i+k]));
			printf("\n");
		}
	}

	printf("Hash: %s\n", info->hash);
	printf("Triangles: %i\n", info->loopcount);
	printf("Bounding Box: %.2f x %.2f x %.2f\n",
			info->bbmax[0] - info->bbmin[0],
			info->bbmax[1] - info->bbmin[1],
			info->bbmax[2] - info->bbmin[2]);
}

void
free_info(struct parseinfo *info)
{
	NULLFREE(info->stlpath);
	NULLFREE(info->infopath);
	info->valid = 0;
}