libbubulle/tools/importobj.c

244 lines
5.1 KiB
C
Raw Normal View History

2020-06-05 09:17:17 +11:00
/*
LIBBUBULLES
some functions for importing bubulles from dot-OBJ files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2023-03-22 06:31:50 +11:00
#include "../bubulles.h"
#include "../edges.h"
2020-06-05 09:17:17 +11:00
2020-06-05 10:05:01 +11:00
extern int verbosity;
2020-06-05 09:17:17 +11:00
/* --------------------------------------------------------------------- */
2023-03-28 23:50:40 +11:00
#define LINE_SZ 666
static BBList *bublist;
static EdgeList *edges;
2021-05-19 02:14:00 +11:00
/* --------------------------------------------------------------------- */
typedef struct {
char *token;
int id;
} Tokens;
2023-03-28 23:50:40 +11:00
enum token_id { T_comment=1, T_vertice, T_group, T_face, T_vt };
2021-05-19 02:14:00 +11:00
Tokens TokenList[] = {
{ "#", T_comment },
{ "v", T_vertice },
2023-03-28 23:50:40 +11:00
{ "g", T_group }, // to be verified !
2023-03-22 06:31:50 +11:00
{ "f", T_face },
2023-03-28 23:50:40 +11:00
{ "vt", T_vt }, // c'est quoi ce truc ?
2021-05-19 02:14:00 +11:00
{ NULL, 0 }
};
static int type_of_the_line(char *text)
{
Tokens *token;
2023-03-28 23:50:40 +11:00
#if DEBUG_LEVEL > 1
fprintf(stderr, "%s is searching for '%s'\n", __func__, text);
2021-05-19 02:14:00 +11:00
#endif
for (token=TokenList; token->token; token++) {
// fprintf(stderr, " %5s -> %d\n", token->token, token->id);
if (!strcmp(token->token, text)) {
return token->id;
}
}
2020-06-05 10:05:01 +11:00
2021-05-19 02:14:00 +11:00
return -1;
}
/* --------------------------------------------------------------------- */
2023-03-28 23:50:40 +11:00
int parse_vertice(char *cptr, float *px, float *py, float *pz)
2021-05-19 02:14:00 +11:00
{
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;
}
/* --------------------------------------------------------------------- */
2023-03-28 23:50:40 +11:00
/* new Mon 27 Mar 2023 12:08:18 AM CEST */
int parse_face(char *cptr, int phy)
{
int ix, foo;
int fa, fb;
2023-03-30 14:39:36 +11:00
#if DEBUG_LEVEL
2023-03-28 23:50:40 +11:00
fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, cptr, phy);
2023-03-30 14:39:36 +11:00
#endif
2023-03-28 23:50:40 +11:00
for (ix=0; ix<3; ix++) {
cptr = strtok(NULL, " ");
2023-03-30 14:39:36 +11:00
if (NULL == cptr) {
fprintf(stderr, "incomplete face in %s\n", __func__);
return -4;
}
2023-03-28 23:50:40 +11:00
if (2 != sscanf(cptr, "%d/%d", &fa, &fb)) {
fprintf(stderr, "%s: err sscanf\n", __func__);
exit(1);
return -3;
}
2023-03-30 14:39:36 +11:00
// XXX fprintf(stderr, " %d got %d %d\n", ix, fa, fb);
2023-03-28 23:50:40 +11:00
/*
* yes, i've found 0-lenght edges, wtf ?
*/
2023-03-30 14:39:36 +11:00
if (fa == fb) {
// fprintf(stderr, "'%s' dublicate\n", cptr);
continue;
}
2023-03-28 23:50:40 +11:00
foo = push_a_missing_edge(edges, fa, fb);
if (foo) {
fprintf(stderr, "%s: disaster #%d\n", __func__, foo);
#if MUST_ABORT
2023-03-30 14:39:36 +11:00
fflush(stderr), abort();
2023-03-28 23:50:40 +11:00
#endif
return -2;
}
}
2023-03-30 14:39:36 +11:00
#if DEBUG_LEVEL
fprintf(stderr, "<<< %s\n", __func__);
#endif
2023-03-28 23:50:40 +11:00
return 0;
}
/* --------------------------------------------------------------------- */
2023-03-30 14:39:36 +11:00
int try_to_read_an_OBJ_file(char *infname, char *file_vert, char *file_edges,
int notused)
2020-06-05 09:17:17 +11:00
{
FILE *fpin;
2020-06-05 10:05:01 +11:00
char line[LINE_SZ+1], *cptr;
float x, y, z;
2023-03-28 23:50:40 +11:00
int foo, nbre, tokenid;
2020-06-05 19:57:43 +11:00
Bubulle bubulle;
2020-06-05 09:17:17 +11:00
2023-03-28 23:50:40 +11:00
2020-06-05 09:17:17 +11:00
#if DEBUG_LEVEL
2023-03-28 23:50:40 +11:00
fprintf(stderr, ">>> %s ( '%s' %d )\n\n", __func__, infname, notused);
2020-06-05 09:17:17 +11:00
#endif
2022-05-21 22:41:20 +11:00
if (NULL==(fpin=fopen(infname, "r"))) {
perror(infname);
2020-06-05 10:05:01 +11:00
exit(1);
}
2023-03-30 14:39:36 +11:00
bublist = alloc_bubulles(infname, 500000, 0);
2020-06-05 19:57:43 +11:00
if (NULL==bublist) {
2023-03-28 23:50:40 +11:00
fprintf(stderr, "in %s, no mem for bubls, aborting...\n", __func__);
2020-06-05 19:57:43 +11:00
abort();
}
print_bublist_desc(bublist, 0);
2023-03-30 14:39:36 +11:00
edges = alloc_edgelist("krkrkr", 200000, 0);
2023-03-28 23:50:40 +11:00
if (NULL==edges) {
fprintf(stderr, "no mem for edges in %s, aborting...\n", __func__);
abort();
}
print_edgelist_desc(edges, 0);
2023-03-30 14:39:36 +11:00
fprintf(stderr, "\n***********************************\n");
2023-03-28 23:50:40 +11:00
2020-06-05 19:57:43 +11:00
nbre = 0;
2021-05-07 21:27:16 +11:00
while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) {
2020-06-05 10:05:01 +11:00
2021-05-19 02:14:00 +11:00
if ('\n' != line[strlen(line)-1]) {
2022-06-06 20:43:19 +11:00
fprintf(stderr, "%s: short read on %s...\n",
__func__, infname);
// return -2;
break;
2021-05-19 02:14:00 +11:00
}
line[strlen(line)-1] = '\0'; /* kill the newline */
2023-03-28 23:50:40 +11:00
if (verbosity>1) fprintf(stderr, "line read ==|%s|==\n", line);
2020-06-05 10:05:01 +11:00
cptr = strtok(line, " ");
2021-05-19 02:14:00 +11:00
if (NULL == cptr) {
fprintf(stderr, "no token ?\n");
continue;
}
tokenid = type_of_the_line(cptr);
2023-03-22 06:31:50 +11:00
if (verbosity > 1)
fprintf(stderr, "token '%s' --> %d\n", cptr, tokenid);
2020-06-05 10:05:01 +11:00
2020-06-05 19:57:43 +11:00
memset(&bubulle, 0, sizeof(Bubulle));
2020-06-05 10:05:01 +11:00
2021-05-19 02:14:00 +11:00
switch (tokenid) {
case T_comment:
/* do nothing */
break;
2023-03-22 06:31:50 +11:00
2021-05-19 02:14:00 +11:00
case T_vertice:
2022-05-21 22:41:20 +11:00
x = y = z = 0.0;
2021-05-19 02:14:00 +11:00
foo = parse_vertice(cptr, &x, &y, &z);
2023-03-28 23:50:40 +11:00
if (3!=foo) {
abort();
}
2021-05-19 02:14:00 +11:00
bubulle.p.x = x;
bubulle.p.y = y;
bubulle.p.z = z;
if (verbosity > 1) niceprint_bubulle(&bubulle, 0);
2023-03-28 23:50:40 +11:00
foo = push_bubulle(bublist, &bubulle);
if (foo) {
abort();
}
2021-05-19 02:14:00 +11:00
break;
2023-03-22 06:31:50 +11:00
case T_group:
cptr = strtok(NULL, " ");
2023-03-28 23:50:40 +11:00
fprintf(stderr, "\tGroup: %s\n", cptr);
2023-03-22 06:31:50 +11:00
break;
case T_face:
2023-03-28 23:50:40 +11:00
foo = parse_face(cptr, 0);
2023-03-30 14:39:36 +11:00
if (foo) fprintf(stderr, " '%s' parse face -> %d\n",
cptr, foo);
2023-03-22 06:31:50 +11:00
break;
2021-05-19 02:14:00 +11:00
default:
2023-03-30 14:39:36 +11:00
// fprintf(stderr, "\ttoken %d ?\n", tokenid);
2021-05-19 02:14:00 +11:00
continue; /* wtf ? */
}
2020-06-05 10:05:01 +11:00
2020-06-05 19:57:43 +11:00
nbre++;
}
2020-06-05 10:05:01 +11:00
fclose(fpin);
2023-03-30 14:39:36 +11:00
fprintf(stderr, "\n***********************************\n");
2023-03-28 23:50:40 +11:00
2020-06-05 19:57:43 +11:00
if(verbosity) {
2023-03-28 23:50:40 +11:00
fprintf(stderr, "in %s, %d vertices loaded\n", __func__, bublist->fidx);
2020-06-05 19:57:43 +11:00
}
2023-03-28 23:50:40 +11:00
print_bublist_desc(bublist, 0);
2023-03-30 14:39:36 +11:00
bubulles_to_data(file_vert, NULL, bublist, 0);
2023-03-28 23:50:40 +11:00
free_bubulles(bublist, 0);
print_edgelist_desc(edges, 0);
2023-03-30 14:39:36 +11:00
print_the_edges(stdout, edges, 0);
2023-03-28 23:50:40 +11:00
free_edgelist(edges, 0);
2020-06-12 00:30:03 +11:00
2020-06-05 19:57:43 +11:00
return 0;
2020-06-05 09:17:17 +11:00
}
/* --------------------------------------------------------------------- */