/* LIBBUBULLES IMPORT_OBJ some functions for importing bubulles from dot-OBJ files. https://git.tetalab.org/tTh/libbubulle/src/branch/master/tools/ http://fegemo.github.io/cefet-cg/attachments/obj-spec.pdf */ #include #include #include #include #include "../bubulles.h" #include "../edges.h" #include "objtrucs.h" extern int verbosity; /* --------------------------------------------------------------------- */ #define LINE_SZ 666 static BBList *bublist; static EdgeList *edges; static int dropped; static int linenumber; static char obj_filename[LINE_SZ+1]; /* --------------------------------------------------------------------- */ typedef struct { char *token; int id; } Tokens; enum token_id { T_comment=1, T_vertice, T_group, T_face, T_vt, T_vn, T_line, T_object, T_smoothing, T_usemtl, T_mtllib }; Tokens TokenList[] = { { "#", T_comment }, { "v", T_vertice }, { "g", T_group }, // to be verified ! { "f", T_face }, { "vt", T_vt }, // c'est quoi ce truc ? { "vn", T_vt }, // c'est quoi ce truc ? { "l", T_line }, { "o", T_object }, { "s", T_smoothing }, { "usemtl", T_usemtl }, { "mtllib", T_mtllib }, /* and more to come... */ { 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; } /* --------------------------------------------------------------------- */ static int parse_vertice(char *cptr, float *px, float *py, float *pz) { float x, y, z; int foo; /* /!\ this function kill the input buffer */ 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 * * mmmm... complex thing to do... * and what is this "phy" parameter ? */ static int parse_face(char *cptr, int phy) { int ix, foo, a, b; int pts[3]; #if DEBUG_LEVEL fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, cptr, phy); #endif #if (0) fprintf(stderr, "parse_face"); for (foo=0; foo<16; foo++) { fprintf(stderr, " %02X", ((unsigned char *)cptr)[foo]); } fprintf(stderr, "\n"); #endif for (ix=0; ix<3; ix++) { cptr = strtok(NULL, " "); if (NULL == cptr) { fprintf(stderr, "incomplete face in %s\n", __func__); return -4; } if (1 != sscanf(cptr, "%d", &pts[ix])) { fprintf(stderr, "%s: err sscanf\n", __func__); exit(1); return -3; } } /** check the freshly read datas **/ if ( pts[0]==pts[1] || pts[0]==pts[2] || pts[2]==pts[1] ) { fprintf(stderr, "%s: degenerated face ( %d %d %d )\n", __func__, pts[0], pts[1], pts[2]); dropped++; } /* * may be we can check the "degenerated cylinder" here ? */ for (ix=0; ix<3; ix++) { a = ix % 3; b = (ix+1) % 3; foo = push_a_missing_edge(edges, pts[a], pts[b]); if (foo) { fprintf(stderr, "%s: disaster #%d line %d\n", __func__, foo, linenumber); return -2; } } if (dropped) { fprintf(stderr, "%s: %d dropped...\n", __func__, dropped); // exit(1); } #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, *token; 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); } linenumber = 0; bublist = alloc_bubulles(infname, 400000, 0); if (NULL==bublist) { fprintf(stderr, "in %s, no mem for bubls, aborting...\n", __func__); abort(); } if (verbosity > 1) print_bublist_desc(bublist, 0); edges = alloc_edgelist("krkrkr", 600000, 0); if (NULL==edges) { fprintf(stderr, "no mem for edges in %s, aborting...\n", __func__); abort(); } if (verbosity > 1) 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); linenumber++; cptr = strtok(line, " "); if (NULL == cptr) { /* this is an empty line */ // fprintf(stderr, "no token ?\n"); continue; } token = cptr; 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_face: /* experimental code here */ foo = parse_face(cptr, 0); if (foo) { fprintf(stderr, "line %d '%s' parse face -> %d\n", linenumber, cptr, foo); exit(1); } break; case T_object: cptr = strtok(NULL, " "); fprintf(stderr, "\tObject: %s\n", cptr); break; case T_group: cptr = strtok(NULL, " "); fprintf(stderr, "\tGroup: %s\n", cptr); break; case T_usemtl: cptr = strtok(NULL, " "); fprintf(stderr, "\tUsemtl: %s\n", cptr); break; case T_mtllib: cptr = strtok(NULL, " "); fprintf(stderr, "\tMtllib: %s\n", cptr); break; case T_line: break; case T_smoothing: break; case T_vt: break; default: fprintf(stderr, "token %s -> %d ?\n", token, tokenid); break; } nbre++; } fclose(fpin); fprintf(stderr, " ***********************************\n"); if(verbosity) { fprintf(stderr, "%s(): %d vertices loaded\n", __func__, bublist->fidx); fprintf(stderr, "%s(): %d edges loaded\n", __func__, edges->fidx); } if (verbosity > 1) { print_bublist_desc(bublist, 0); print_edgelist_desc(edges, 0); } bubulles_to_data(file_vert, NULL, bublist, 0); edges_to_data(file_edges, edges, 0); foo = x_write_vertedges("foo.evblob", bublist, edges); // Cleanup free_bubulles(bublist, 0); free_edgelist(edges, 0); return 0; } /* --------------------------------------------------------------------- */