libbubulle/tools/importobj.c

244 lines
5.1 KiB
C
Raw Normal View History

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