aboutsummaryrefslogtreecommitdiffstats
path: root/src/stlfile.c
blob: 953b5d76dda83887f7042d9b3f74e145d7ce75f6 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#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 = realloc(stack->data, sizeof(int) * stack->cap);
		checkp(stack->data);
	}

	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;
}

int
isws(char c)
{
	return c && strchr(wsset, c);
}

char*
skipws(char *p)
{
	for (; isws(*p); p++);
	return p;
}

char*
consume_arg(char **start, char **end)
{
	char *c, *tmp;

	*start = skipws(*start);
	if (!*start) return NULL;
	for (c = *start; *c && !isws(*c); c++);
	tmp = *start;
	*start = c + 1;
	*end = c;
	return tmp;
}

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

	bp = skipws(*start);

	for (i = 0; i < ARRSIZE(kwmap); i++) {
		len = strlen(kwmap[i].str);
		if (!strncmp(kwmap[i].str, bp, len) && (!bp[len] || isws(bp[len]))) {
			*start = bp + len + (bp[len] ? 1 : 0);
			return kwmap[i].code;
		}
	}

	return KW_UNKNOWN;
}

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

	stack_init(&states);

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

	memset(info->header, 0, 80);

	for (i = 0; i < 3; i++) {
		info->bbmin[i] = INFINITY;
		info->bbmax[i] = -INFINITY;
	}

	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) {
				FMT_ERR("Multiple nested solids!\n");
				goto fail;
			}
			tmp = bp;
			if (!consume_keyword(&bp)
					&& (arg = consume_arg(&bp, &end))) {
				info->solidname = strndup(arg, end - arg);
			} else {
				bp = tmp;
			}
			break;
		case KW_SOLID_END:
			if ((kw = stack_pop(&states)) != STATE_SOLID) {
				FMT_ERR("Improper nesting, parent: %s\n",
					kwmap[kw].str);
				goto fail;
			}
			tmp = bp;
			if (info->solidname && !consume_keyword(&bp)
					&& (arg = consume_arg(&bp, &end))) {
				if (strncmp(info->solidname, arg, end - arg)) {
					FMT_ERR("Solid names do not match!\n");
					goto fail;
				}
			} else {
				bp = tmp;
			}
			break;
		case KW_LOOP_BEGIN:
			stack_push(&states, STATE_LOOP);
			if (stack_ind(&states, KW_LOOP_BEGIN) != -1) {
				FMT_ERR("Multiple nested loops!\n");
				goto fail;
			}
			break;
		case KW_LOOP_END:
			if ((kw = stack_pop(&states)) != STATE_LOOP) {
				FMT_ERR("Improper nesting, parent: %s\n",
					kwmap[kw].str);
				goto fail;
			}
			info->loopcount++;
			break;
		case KW_FACET_BEGIN:
			stack_push(&states, STATE_FACET);
			if (stack_ind(&states, KW_LOOP_BEGIN) != -1) {
				FMT_ERR("Multiple nested facets!\n");
				goto fail;
			}
			for (i = 0; i < 3; i++) {
				if (!(arg = consume_arg(&bp, &end))) {
					FMT_ERR("Facet with < 3 args!\n");
					goto fail;
				}
				farg = strtof(arg, &tmp);
				if (!isws(*tmp)) {
					FMT_ERR("Facet arg '%s'\n", arg);
					goto fail;
				}
			}
			break;
		case KW_FACET_END:
			if ((kw = stack_pop(&states)) != STATE_FACET) {
				FMT_ERR("Improper nesting, parent: %s\n",
					kwmap[kw].str);
				goto fail;
			}
			break;
		case KW_VERTEX:
			for (i = 0; i < 3; i++) {
				if (!(arg = consume_arg(&bp, &end))) {
					FMT_ERR("Vertex with < 3 args\n");
					goto fail;
				}
				farg = strtof(arg, &tmp);
				if (!isws(*tmp)) {
					FMT_ERR("Vertex arg '%s'\n", arg);
					goto fail;
				}
				info->bbmin[i] = MIN(info->bbmin[i], farg);
				info->bbmax[i] = MAX(info->bbmax[i], farg);
			}
			break;
		case KW_UNKNOWN:
			prev = skipws(prev);
			FMT_ERR("Expected keyword, got:\n%.*s...\n", 30, prev);
			goto fail;
		}
		prev = bp;
	}

	if (states.count) {
		FMT_ERR("Expected keyword, got:\n%.*s...\n", 30, bp);
		goto fail;
	}

	bp = skipws(bp);
	if (*bp) {
		FMT_ERR("Extraneous data at end of file\n");
		goto fail;
	}

	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;
	int i, k;
	float v;

	info->type = TYPE_BIN;

	if (len < 84) {
		FMT_ERR("Truncated data! (header missing)\n");
		goto fail;
	}

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

	if (*buf == '#' && strlen(buf + 1))
		info->solidname = checkp(strdup(buf + 1));

	bp = buf + 80;

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

	if (!info->loopcount) {
		memset(info->bbmax, 0, sizeof(float) * 3);
		memset(info->bbmin, 0, sizeof(float) * 3);
		return OK;
	}

	for (i = 0; i < 3; i++) {
		info->bbmin[i] = INFINITY;
		info->bbmax[i] = -INFINITY;
	}

	for (i = 0; i < info->loopcount; i++) {
		if (bp + 50 > end) {
			FMT_ERR("Truncated data! (loops missing)\n");
			goto fail;
		}
		for (k = 0; k < 12; k++, bp += 4) {
			v = fle32toh(*(float*)bp);
			if (v == INFINITY || v == NAN) {
				FMT_ERR("Encountered invalid float\n");
				goto fail;
			}
			if (k >= 3) {
				info->bbmin[k % 3] = MIN(info->bbmin[k % 3], v);
				info->bbmax[k % 3] = MAX(info->bbmax[k % 3], v);
			}
		}
		bp += 2;
	}

	if (bp != end) {
		FMT_ERR("Extraneous data at end of file\n");
		goto fail;
	}

	return OK;

fail:
	FREE(info->solidname);
	return FAIL;
}

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

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

	if (len < 10) {
		ERR("File too small!\n");
		return FAIL;
	}

	info->filesize = len;

	/* check bin vs ascii with first keyword */
	for (bp = buf; isws(*bp); bp++);
	status = !strncmp("solid", bp, 5) && isws(bp[5])
		? parse_file_ascii(info, buf, len)
		: parse_file_bin(info, buf, len);
	if (status == FAIL) return FAIL;

	if (!info->modelname) {
		resp = ask("Please enter your model name: ");
		if (strlen(resp) < 4) {
			ERR("Model name is too short!\n");
			return FAIL;
		}
		info->modelname = checkp(strdup(resp));
	}

	if (!info->solidname) info->solidname = checkp(strdup(""));

	info->hash = checkp(strdup(mhash(info->modelname, -1)));

	return OK;
}

int
save_info(struct parseinfo *info, FILE *f)
{
	size_t nwrote = 0;
	int i;

	nwrote += fwrite(&info->type, sizeof(int), 1, f);
	nwrote += fwrite(&info->loopcount, sizeof(int), 1, f);
	nwrote += fwrite(&info->filesize, sizeof(unsigned), 1, f);

	for (i = 0; i < 3; i++) {
		nwrote += fwrite(&info->bbmin[i], sizeof(float), 1, f);
		nwrote += fwrite(&info->bbmax[i], sizeof(float), 1, f);
	}

	nwrote += fwrite(info->header, 80, 1, f);

	if (nwrote != 10) return FAIL;

	fputstr(f, info->solidname);
	fputstr(f, info->hash);
	fputstr(f, info->modelname);

	return OK;
}

int
load_info(struct parseinfo *info, FILE *f)
{
	size_t nread = 0;
	int i;

	nread += fread(&info->type, sizeof(int), 1, f);
	nread += fread(&info->loopcount, sizeof(int), 1, f);
	nread += fread(&info->filesize, sizeof(unsigned), 1, f);

	for (i = 0; i < 3; i++) {
		nread += fread(&info->bbmin[i], sizeof(float), 1, f);
		nread += fread(&info->bbmax[i], sizeof(float), 1, f);
	}

	nread += fread(info->header, 80, 1, f);

	if (nread != 10) return FAIL;

	freadstr(f, &info->solidname);
	freadstr(f, &info->hash);
	freadstr(f, &info->modelname);

	info->valid = 1;

	return OK;
}

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

	printf(" === Model info === \n");

	printf("  File Size: %u\n", info->filesize);
	if (info->type == TYPE_BIN) {
		printf("  Header:\n");
		for (i = 0; i < 80; i += k) {
			printf("  ");
			for (k = 0; k < MIN(80 - i, 20); k++)
				printf(" %02x", (uint8_t) info->header[i+k]);
			printf(" | ");
			for (k = 0; k < MIN(80 - i, 20); k++)
				putchar(PRINTABLE(info->header[i+k]));
			printf("\n");
		}
	}
	printf("  Model ID: %s\n", info->hash);
	printf("  Model Name: %s\n", info->modelname);
	printf("  Solid Name: %s\n", info->solidname);
	printf("  Triangle Count: %i\n", info->loopcount);
	printf("  Bounding Box Size: %.2f x %.2f x %.2f\n",
			info->bbmax[0] - info->bbmin[0],
			info->bbmax[1] - info->bbmin[1],
			info->bbmax[2] - info->bbmin[2]);
	printf("  Bounding Box Origin: %.2f x %.2f x %.2f\n",
			info->bbmin[0], info->bbmin[1], info->bbmin[2]);

	printf(" ================== \n");
}

void
free_info(struct parseinfo *info)
{
	FREE(info->hash);
	FREE(info->modelname);
	FREE(info->solidname);
	info->valid = 0;
}

float fle32toh(float v)
{
	union {
		uint32_t u;
		float f;
	} conv;

	conv.f = v;
	conv.u = le32toh(conv.u);
	return conv.f;
}