libbubulle/importobj.c

152 lines
3.0 KiB
C
Raw Normal View History

2020-06-05 09:17:17 +11:00
/*
LIBBUBULLES
some functions for importing bubulles from dot-OBJ files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bubulles.h"
2020-06-05 10:05:01 +11:00
extern int verbosity;
2020-06-05 09:17:17 +11:00
/* --------------------------------------------------------------------- */
2020-06-05 10:05:01 +11:00
#define LINE_SZ 666
2021-05-19 02:14:00 +11:00
/* --------------------------------------------------------------------- */
typedef struct {
char *token;
int id;
} Tokens;
enum token_id { T_comment=1, T_vertice };
Tokens TokenList[] = {
{ "#", T_comment },
{ "v", T_vertice },
{ NULL, 0 }
};
static int type_of_the_line(char *text)
{
Tokens *token;
int id;
#if DEBUG_LEVEL
fprintf(stderr, "%s is searching '%s'\n", __func__, text);
#endif
for (token=TokenList; token->token; token++) {
// fprintf(stderr, " %5s -> %d\n", token->token, token->id);
if (!strcmp(token->token, text)) {
return token->id;
}
}
2020-06-05 10:05:01 +11:00
2021-05-19 02:14:00 +11:00
return -1;
}
/* --------------------------------------------------------------------- */
static int parse_vertice(char *cptr, float *px, float *py, float *pz)
{
float x, y, z;
int foo;
foo = 0;
cptr = strtok(NULL, " ");
foo += sscanf(cptr, "%f", &x);
cptr = strtok(NULL, " ");
foo += sscanf(cptr, "%f", &y);
cptr = strtok(NULL, " ");
foo += sscanf(cptr, "%f", &z);
if (3 == foo) {
*px = x; *py = y; *pz = z;
}
return foo;
}
/* --------------------------------------------------------------------- */
2020-06-05 09:17:17 +11:00
int try_to_read_an_OBJ_file(char *fname, int notused)
{
FILE *fpin;
2020-06-05 10:05:01 +11:00
char line[LINE_SZ+1], *cptr;
float x, y, z;
2021-05-19 02:14:00 +11:00
int foo, nbre, tokenid;
2020-06-05 19:57:43 +11:00
BBList *bublist;
Bubulle bubulle;
2020-06-05 09:17:17 +11:00
#if DEBUG_LEVEL
2020-06-05 10:05:01 +11:00
fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, fname, notused);
2020-06-05 09:17:17 +11:00
#endif
2020-06-05 10:05:01 +11:00
if (NULL==(fpin=fopen(fname, "r"))) {
perror(fname);
exit(1);
}
2020-06-05 19:57:43 +11:00
bublist = alloc_bubulles(fname, 1000, 0);
if (NULL==bublist) {
fprintf(stderr, "err in %s, aborting...\n", __func__);
abort();
}
print_bublist_desc(bublist, 0);
nbre = 0;
2021-05-07 21:27:16 +11:00
while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) {
2020-06-05 10:05:01 +11:00
2021-05-19 02:14:00 +11:00
if ('\n' != line[strlen(line)-1]) {
fprintf(stderr, "%s: short read, exiting...\n", __func__);
return -2;
}
line[strlen(line)-1] = '\0'; /* kill the newline */
if (verbosity>1) fprintf(stderr, "line read ===%s===\n", line);
2020-06-05 10:05:01 +11:00
cptr = strtok(line, " ");
2021-05-19 02:14:00 +11:00
if (NULL == cptr) {
fprintf(stderr, "no token ?\n");
continue;
}
tokenid = type_of_the_line(cptr);
fprintf(stderr, "tok '%s' --> %d\n", cptr, tokenid);
2020-06-05 10:05:01 +11:00
2020-06-05 19:57:43 +11:00
memset(&bubulle, 0, sizeof(Bubulle));
2020-06-05 10:05:01 +11:00
2021-05-19 02:14:00 +11:00
switch (tokenid) {
case T_comment:
/* do nothing */
break;
case T_vertice:
foo = parse_vertice(cptr, &x, &y, &z);
bubulle.p.x = x;
bubulle.p.y = y;
bubulle.p.z = z;
if (verbosity > 1) niceprint_bubulle(&bubulle, 0);
break;
default:
continue; /* wtf ? */
}
2020-06-05 10:05:01 +11:00
2020-06-05 19:57:43 +11:00
foo = push_bubulle(bublist, &bubulle);
if (foo) {
2020-06-12 00:30:03 +11:00
fprintf(stderr, "%s: err %d on push\n", __func__, foo);
2020-06-05 19:57:43 +11:00
break;
}
nbre++;
}
2020-06-05 10:05:01 +11:00
fclose(fpin);
2020-06-05 19:57:43 +11:00
if(verbosity) {
fprintf(stderr, "%s : %d vertices loaded\n", __func__, nbre);
}
2020-06-12 00:30:03 +11:00
bubulles_to_data("xyz", NULL, bublist, 0);
2020-06-05 19:57:43 +11:00
return 0;
2020-06-05 09:17:17 +11:00
}
/* --------------------------------------------------------------------- */