From b861e8c86be9958e451e0d0ce9d2a0a64d14fa6c Mon Sep 17 00:00:00 2001 From: tTh Date: Thu, 30 Mar 2023 05:39:36 +0200 Subject: [PATCH] more work done on .OBJ import --- tools/Makefile | 2 +- tools/README.md | 7 ++++--- tools/importobj.c | 39 +++++++++++++++++++++++++++------------ tools/minimal.obj | 7 +++++++ tools/read_obj.c | 18 ++++++++++++++---- 5 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 tools/minimal.obj diff --git a/tools/Makefile b/tools/Makefile index 7734a48..57cdf3c 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,7 +1,7 @@ BBFUNCS = ../libbubulles.a -OPT = -Wall -g -DDEBUG_LEVEL=1 -DMUST_ABORT +OPT = -Wall -g -pg -DDEBUG_LEVEL=0 -DMUST_ABORT=0 read_obj: read_obj.c Makefile importobj.o $(BBFUNCS) gcc $(OPT) $< importobj.o $(BBFUNCS) -o $@ diff --git a/tools/README.md b/tools/README.md index 817f22e..f44598a 100644 --- a/tools/README.md +++ b/tools/README.md @@ -1,6 +1,7 @@ # Importer des fichiers .OBJ + ``` v -1.177934647 -6.833468914 -73.19773865 vn -0.1279897094 -0.4501263499 -0.8837448359 @@ -11,10 +12,10 @@ vn -0.05844723806 -0.09480132163 -0.993778944 Première étape : en lisant les vertices, nous saurons positionner nos bubulles. -Seconde étape : bien comprendre à quoi correspondent les -lignes `vn` ? Des normales ? +Attention, mon parser EXIGE des fichiers Unix bien conformés : +c'est-à-dire que la dernière ligne du .obj DOIT être terminée +par un newline ! -Et troisème étape, que faire des vertices ? Quatrième étape : aller vivre à la campagne ? diff --git a/tools/importobj.c b/tools/importobj.c index ee34fab..99e0b1b 100644 --- a/tools/importobj.c +++ b/tools/importobj.c @@ -82,36 +82,50 @@ int parse_face(char *cptr, int phy) int ix, foo; int fa, fb; +#if DEBUG_LEVEL fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, cptr, phy); +#endif for (ix=0; ix<3; ix++) { cptr = strtok(NULL, " "); + if (NULL == cptr) { + fprintf(stderr, "incomplete face in %s\n", __func__); + return -4; + } if (2 != sscanf(cptr, "%d/%d", &fa, &fb)) { fprintf(stderr, "%s: err sscanf\n", __func__); exit(1); return -3; } - fprintf(stderr, " %d got %d %d\n", ix, fa, fb); + // XXX fprintf(stderr, " %d got %d %d\n", ix, fa, fb); /* * yes, i've found 0-lenght edges, wtf ? */ - if (fa == fb) continue; + if (fa == fb) { + // fprintf(stderr, "'%s' dublicate\n", cptr); + continue; + } foo = push_a_missing_edge(edges, fa, fb); if (foo) { fprintf(stderr, "%s: disaster #%d\n", __func__, foo); #if MUST_ABORT - abort(); + fflush(stderr), abort(); #endif return -2; } } +#if DEBUG_LEVEL +fprintf(stderr, "<<< %s\n", __func__); +#endif + return 0; } /* --------------------------------------------------------------------- */ -int try_to_read_an_OBJ_file(char *infname, char *outfname, int notused) +int try_to_read_an_OBJ_file(char *infname, char *file_vert, char *file_edges, + int notused) { FILE *fpin; char line[LINE_SZ+1], *cptr; @@ -129,21 +143,21 @@ if (NULL==(fpin=fopen(infname, "r"))) { exit(1); } -bublist = alloc_bubulles(infname, 800000, 0); +bublist = alloc_bubulles(infname, 500000, 0); if (NULL==bublist) { fprintf(stderr, "in %s, no mem for bubls, aborting...\n", __func__); abort(); } print_bublist_desc(bublist, 0); -edges = alloc_edgelist("krkrkr", 100000, 0); +edges = alloc_edgelist("krkrkr", 200000, 0); if (NULL==edges) { fprintf(stderr, "no mem for edges in %s, aborting...\n", __func__); abort(); } print_edgelist_desc(edges, 0); -fprintf(stderr, "\n\t************************\n"); +fprintf(stderr, "\n***********************************\n"); nbre = 0; while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) { @@ -196,11 +210,12 @@ while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) { case T_face: foo = parse_face(cptr, 0); - fprintf(stderr, " parse face -> %d\n", foo); + if (foo) fprintf(stderr, " '%s' parse face -> %d\n", + cptr, foo); break; default: - fprintf(stderr, "\ttoken %d ?\n", tokenid); + // fprintf(stderr, "\ttoken %d ?\n", tokenid); continue; /* wtf ? */ } @@ -208,18 +223,18 @@ while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) { } fclose(fpin); -fprintf(stderr, "\n\t************************\n"); +fprintf(stderr, "\n***********************************\n"); if(verbosity) { fprintf(stderr, "in %s, %d vertices loaded\n", __func__, bublist->fidx); } print_bublist_desc(bublist, 0); -bubulles_to_data(outfname, NULL, bublist, 0); +bubulles_to_data(file_vert, NULL, bublist, 0); free_bubulles(bublist, 0); print_edgelist_desc(edges, 0); -print_the_edges(edges, 0); +print_the_edges(stdout, edges, 0); free_edgelist(edges, 0); return 0; diff --git a/tools/minimal.obj b/tools/minimal.obj new file mode 100644 index 0000000..94932b7 --- /dev/null +++ b/tools/minimal.obj @@ -0,0 +1,7 @@ +g minimal +v 0 0 0 +v 1 0 0 +v 0 1 0 +v 0 0 1 +f 1/2, 2/3, 3/1 +f 0/1, 0/1, 0/2 diff --git a/tools/read_obj.c b/tools/read_obj.c index d3527f5..836670b 100644 --- a/tools/read_obj.c +++ b/tools/read_obj.c @@ -4,23 +4,33 @@ #include #include +#include // for basename(3) + #include "../bubulles.h" -int try_to_read_an_OBJ_file(char *fname, char *outfname, int notused); -int verbosity; +int try_to_read_an_OBJ_file(char *fname, + char *outfname, char *file_edges, + int notused); + + +int verbosity = 0; int main(int argc, char *argv[]) { int foo; +char *fname; /* see manpage basename(3) */ if (2 != argc) { bubulles_version(1); exit(0); } -verbosity = 2; +verbosity = 0; -foo = try_to_read_an_OBJ_file(argv[1], "bubulles.asc", 0); +fname = basename(argv[1]); +fprintf (stderr, "input fname is '%s'\n", fname); + +foo = try_to_read_an_OBJ_file(argv[1], "bulles.vertices", "bulles.edges", 0); fprintf(stderr, "try to read '%s' -> %d\n", argv [1], foo); return 0;