/* LIBBUBULLES some functions for importing bubulles from dot-OBJ files. */ #include #include #include #include "../bubulles.h" #include "../edges.h" extern int verbosity; /* --------------------------------------------------------------------- */ #define LINE_SZ 666 static BBList *bublist; static EdgeList *edges; /* --------------------------------------------------------------------- */ typedef struct { char *token; int id; } Tokens; enum token_id { T_comment=1, T_vertice, T_group, T_face, T_vt }; Tokens TokenList[] = { { "#", T_comment }, { "v", T_vertice }, { "g", T_group }, // to be verified ! { "f", T_face }, { "vt", T_vt }, // c'est quoi ce truc ? { NULL, 0 } }; static int type_of_the_line(char *text) { Tokens *token; #if DEBUG_LEVEL > 1 fprintf(stderr, "%s is searching for '%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; } /* --------------------------------------------------------------------- */ 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; } /* --------------------------------------------------------------------- */ /* new Mon 27 Mar 2023 12:08:18 AM CEST */ 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; } // XXX fprintf(stderr, " %d got %d %d\n", ix, fa, fb); /* * yes, i've found 0-lenght edges, wtf ? */ 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 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 *file_vert, char *file_edges, int notused) { FILE *fpin; char line[LINE_SZ+1], *cptr; float x, y, z; int foo, nbre, tokenid; Bubulle bubulle; #if DEBUG_LEVEL fprintf(stderr, ">>> %s ( '%s' %d )\n\n", __func__, infname, notused); #endif if (NULL==(fpin=fopen(infname, "r"))) { perror(infname); exit(1); } 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", 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***********************************\n"); 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); if (3!=foo) { abort(); } bubulle.p.x = x; bubulle.p.y = y; bubulle.p.z = z; if (verbosity > 1) niceprint_bubulle(&bubulle, 0); foo = push_bubulle(bublist, &bubulle); if (foo) { abort(); } break; case T_group: cptr = strtok(NULL, " "); fprintf(stderr, "\tGroup: %s\n", cptr); break; case T_face: foo = parse_face(cptr, 0); if (foo) fprintf(stderr, " '%s' parse face -> %d\n", cptr, foo); break; default: // fprintf(stderr, "\ttoken %d ?\n", tokenid); continue; /* wtf ? */ } nbre++; } fclose(fpin); 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(file_vert, NULL, bublist, 0); free_bubulles(bublist, 0); print_edgelist_desc(edges, 0); print_the_edges(stdout, edges, 0); free_edgelist(edges, 0); return 0; } /* --------------------------------------------------------------------- */