commit 65a1a51121278e54e40e2a04ae096053d5a3c47d
parent 3b660c467f938df6898229e378074cfa5662ce9f
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 9 May 2021 20:12:14 +0200
added bounding box calculation
Diffstat:
3 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/service/src/stlfile.c b/service/src/stlfile.c
@@ -117,6 +117,11 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len)
info->type = TYPE_ASCII;
info->loopcount = 0;
+ for (i = 0; i < 3; i++) {
+ info->bbmin[i] = INFINITY;
+ info->bbmax[i] = -INFINITY;
+ }
+
bp = prev = buf;
while ((kw = consume_keyword(&bp))) {
switch (kw) {
@@ -161,7 +166,7 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len)
if (!(arg = consume_arg(&bp)))
PARSE_FAIL("Facet with less than 3 args!\n");
farg = strtof(arg, &tmp);
- if (tmp && *tmp)
+ if (*tmp)
PARSE_FAIL("Facet with invalid arg '%s'!\n", arg);
}
break;
@@ -170,13 +175,14 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len)
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)
+ if (*tmp)
PARSE_FAIL("Vertex with invalid arg '%s'!\n", arg);
+ info->bbmin[i] = MIN(info->bbmin[i], farg);
+ info->bbmax[i] = MAX(info->bbmax[i], farg);
}
break;
case KW_UNKNOWN:
@@ -201,7 +207,8 @@ int
parse_file_bin(struct parseinfo *info, char *buf, size_t len)
{
char *bp, *end = buf + len;
- unsigned int i;
+ int i, k, m;
+ float v;
info->type = TYPE_BIN;
@@ -213,11 +220,23 @@ parse_file_bin(struct parseinfo *info, char *buf, size_t len)
bp = buf + 80;
info->loopcount = le32toh(*(uint32_t*)bp);
+ 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)
PARSE_FAIL("Truncated data! (loops missing)\n");
- /* TODO: load floats and calc bounding box */
- bp += 50;
+ bp += 12;
+ for (k = 0; k < 3; k++, bp += 12) {
+ for (m = 0; m < 3; m++) {
+ v = le32toh(*(float *)(bp + 4 * m));
+ info->bbmin[m] = MIN(info->bbmin[m], v);
+ info->bbmax[m] = MAX(info->bbmax[m], v);
+ }
+ }
+ bp += 2;
}
return OK;
@@ -264,17 +283,17 @@ print_info(struct parseinfo *info)
{
int i, k;
-#define FILTERCHAR(c) ((c) >= 32 ? (c) : '?')
+#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]);
+ for (k = 0; k < MIN(80 - i, 20); k++)
+ printf(" %02x", (uint8_t) info->extra[i+k]);
printf(" | ");
- for (k = i; k < MIN(80, i + 20); k++)
+ for (k = 0; k < MIN(80 - i, 20); k++)
printf("%c", FILTERCHAR(info->extra[i+k]));
printf("\n");
}
diff --git a/service/src/stlfile.h b/service/src/stlfile.h
@@ -5,6 +5,7 @@
#include <string.h>
#include <stdint.h>
#include <endian.h>
+#include <math.h>
#include "util.h"
diff --git a/service/src/util.h b/service/src/util.h
@@ -8,6 +8,8 @@
#define ARRSIZE(x) (sizeof(x)/sizeof((x)[0]))
#define MIN(x,y) ((x) > (y) ? (y) : (x))
+#define MAX(x,y) ((x) < (y) ? (y) : (x))
+
#define NULLFREE(p) do { free(p); p = NULL; } while (0)
#define MHASHLEN 32