155 lines
3.2 KiB
C
155 lines
3.2 KiB
C
/*
|
|
LIBBUBULLES
|
|
|
|
some functions for importing bubulles from dot-OBJ files.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "bubulles.h"
|
|
|
|
extern int verbosity;
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
#define LINE_SZ 666
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
/* --------------------------------------------------------------------- */
|
|
int try_to_read_an_OBJ_file(char *infname, char *outfname, int notused)
|
|
{
|
|
FILE *fpin;
|
|
char line[LINE_SZ+1], *cptr;
|
|
float x, y, z;
|
|
int foo, nbre, tokenid;
|
|
BBList *bublist;
|
|
Bubulle bubulle;
|
|
|
|
#if DEBUG_LEVEL
|
|
fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, fname, notused);
|
|
#endif
|
|
|
|
if (NULL==(fpin=fopen(infname, "r"))) {
|
|
perror(infname);
|
|
exit(1);
|
|
}
|
|
|
|
bublist = alloc_bubulles(infname, 800000, 0);
|
|
if (NULL==bublist) {
|
|
fprintf(stderr, "err in %s, aborting...\n", __func__);
|
|
abort();
|
|
}
|
|
print_bublist_desc(bublist, 0);
|
|
|
|
nbre = 0;
|
|
while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) {
|
|
|
|
if ('\n' != line[strlen(line)-1]) {
|
|
fprintf(stderr, "%s: short read on %s...\n",
|
|
__func__, infname);
|
|
// return -2;
|
|
break;
|
|
}
|
|
line[strlen(line)-1] = '\0'; /* kill the newline */
|
|
if (verbosity>1) fprintf(stderr, "line read ===%s===\n", line);
|
|
|
|
cptr = strtok(line, " ");
|
|
if (NULL == cptr) {
|
|
fprintf(stderr, "no token ?\n");
|
|
continue;
|
|
}
|
|
tokenid = type_of_the_line(cptr);
|
|
if (verbosity > 1) fprintf(stderr, "token '%s' --> %d\n", cptr, tokenid);
|
|
|
|
memset(&bubulle, 0, sizeof(Bubulle));
|
|
|
|
switch (tokenid) {
|
|
case T_comment:
|
|
/* do nothing */
|
|
break;
|
|
case T_vertice:
|
|
x = y = z = 0.0;
|
|
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:
|
|
// fprintf(stderr, "token %d ?\n", tokenid);
|
|
continue; /* wtf ? */
|
|
}
|
|
|
|
foo = push_bubulle(bublist, &bubulle);
|
|
if (foo) {
|
|
fprintf(stderr, "%s: err %d on push\n", __func__, foo);
|
|
break;
|
|
}
|
|
nbre++;
|
|
}
|
|
fclose(fpin);
|
|
|
|
if(verbosity) {
|
|
fprintf(stderr, "%s : %d vertices loaded\n", __func__, nbre);
|
|
}
|
|
|
|
bubulles_to_data(outfname, NULL, bublist, 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* --------------------------------------------------------------------- */
|