refactoring
This commit is contained in:
@@ -3,5 +3,8 @@ BBFUNCS = ../libbubulles.a
|
||||
|
||||
OPT = -Wall -g -DDEBUG_LEVEL=1 -DMUST_ABORT
|
||||
|
||||
read_obj: read_obj.c Makefile $(BBFUNCS)
|
||||
gcc $(OPT) $< $(BBFUNCS) -o $@
|
||||
read_obj: read_obj.c Makefile importobj.o $(BBFUNCS)
|
||||
gcc $(OPT) $< importobj.o $(BBFUNCS) -o $@
|
||||
|
||||
importobj.o: importobj.c ../bubulles.h Makefile
|
||||
$(CC) $(OPT) -c $<
|
||||
|
||||
175
tools/importobj.c
Normal file
175
tools/importobj.c
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
LIBBUBULLES
|
||||
|
||||
some functions for importing bubulles from dot-OBJ files.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../bubulles.h"
|
||||
#include "../edges.h"
|
||||
|
||||
extern int verbosity;
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
#define LINE_SZ 666
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
typedef struct {
|
||||
char *token;
|
||||
int id;
|
||||
} Tokens;
|
||||
|
||||
enum token_id { T_comment=1, T_vertice, T_group, T_face };
|
||||
|
||||
Tokens TokenList[] = {
|
||||
{ "#", T_comment },
|
||||
{ "v", T_vertice },
|
||||
{ "g", T_group }, // to be verified
|
||||
{ "f", T_face },
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static int type_of_the_line(char *text)
|
||||
{
|
||||
Tokens *token;
|
||||
|
||||
#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, fa, fb, ix;
|
||||
BBList *bublist;
|
||||
Bubulle bubulle;
|
||||
|
||||
#if DEBUG_LEVEL
|
||||
fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, infname, notused);
|
||||
#endif
|
||||
|
||||
if (NULL==(fpin=fopen(infname, "r"))) {
|
||||
perror(infname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
bublist = alloc_bubulles(infname, 150000, 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, exiting...\n", __func__);
|
||||
return -2;
|
||||
}
|
||||
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;
|
||||
|
||||
case T_group:
|
||||
cptr = strtok(NULL, " ");
|
||||
fprintf(stderr, "Group: %s\n", cptr);
|
||||
break;
|
||||
|
||||
case T_face:
|
||||
fprintf(stderr, "Face A: %s\n", cptr);
|
||||
for (ix=0; ix<3; ix++) {
|
||||
cptr = strtok(NULL, " ");
|
||||
fprintf(stderr, "Piste %d: %s\n", ix, cptr);
|
||||
if (2==sscanf(cptr, "%d/%d", &fa, &fb))
|
||||
fprintf(stderr, " %d %d %d\n", foo, fa, fb);
|
||||
else
|
||||
return -3;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// fprintf(stderr, "token %d ?\n", tokenid);
|
||||
continue; /* wtf ? */
|
||||
}
|
||||
|
||||
foo = push_bubulle(bublist, &bubulle);
|
||||
if (foo) {
|
||||
fprintf(stderr, "*** %s: error %d on push\n", __func__, foo);
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
nbre++;
|
||||
}
|
||||
fclose(fpin);
|
||||
|
||||
if(verbosity) {
|
||||
fprintf(stderr, "%s : %d vertices loaded\n", __func__, nbre);
|
||||
}
|
||||
|
||||
bubulles_to_data(outfname, NULL, bublist, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
@@ -18,10 +18,10 @@ if (2 != argc) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
verbosity = 2;
|
||||
verbosity = 1;
|
||||
|
||||
foo = try_to_read_an_OBJ_file(argv[1], "bulles.xyz", 0);
|
||||
fprintf(stderr, "try to read -> %d\n", foo);
|
||||
fprintf(stderr, "try to read '%s' --> %d\n", argv[1], foo);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user