libbubulle/tools/importobj.c

178 lines
3.7 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
/* --------------------------------------------------------------------- */
2020-06-05 10:05:01 +11:00
#define LINE_SZ 666
2021-05-19 02:14:00 +11:00
/* --------------------------------------------------------------------- */
typedef struct {
char *token;
int id;
} Tokens;
2023-03-22 06:31:50 +11:00
enum token_id { T_comment=1, T_vertice, T_group, T_face };
2021-05-19 02:14:00 +11:00
Tokens TokenList[] = {
{ "#", T_comment },
{ "v", T_vertice },
2023-03-22 06:31:50 +11:00
{ "g", T_group }, // to be verified
{ "f", T_face },
2021-05-19 02:14:00 +11:00
{ NULL, 0 }
};
static int type_of_the_line(char *text)
{
Tokens *token;
#if DEBUG_LEVEL
fprintf(stderr, "%s is searching '%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;
}
}
2020-06-05 10:05:01 +11:00
2021-05-19 02:14:00 +11:00
return -1;
}
/* --------------------------------------------------------------------- */
static 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;
}
/* --------------------------------------------------------------------- */
2022-05-21 22:41:20 +11:00
int try_to_read_an_OBJ_file(char *infname, char *outfname, 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-22 06:31:50 +11:00
int foo, nbre, tokenid, fa, fb, ix;
2020-06-05 19:57:43 +11:00
BBList *bublist;
Bubulle bubulle;
2020-06-05 09:17:17 +11:00
#if DEBUG_LEVEL
2023-03-22 06:31:50 +11:00
fprintf(stderr, ">>> %s ( '%s' %d )\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);
}
2022-06-06 20:43:19 +11:00
bublist = alloc_bubulles(infname, 800000, 0);
2020-06-05 19:57:43 +11:00
if (NULL==bublist) {
fprintf(stderr, "err in %s, aborting...\n", __func__);
abort();
}
print_bublist_desc(bublist, 0);
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 */
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);
bubulle.p.x = x;
bubulle.p.y = y;
bubulle.p.z = z;
if (verbosity > 1) niceprint_bubulle(&bubulle, 0);
break;
2023-03-22 06:31:50 +11:00
case T_group:
cptr = strtok(NULL, " ");
fprintf(stderr, "Group: %s\n", cptr);
break;
case T_face:
fprintf(stderr, "Face A: %s\n", cptr);
for (ix=0; ix<3; ix++) {
cptr = strtok(NULL, " ");
fprintf(stderr, "Piste %d: %s\n", ix, cptr);
if (2==sscanf(cptr, "%d/%d", &fa, &fb))
fprintf(stderr, " %d %d %d\n", foo, fa, fb);
else
return -3;
}
break;
2021-05-19 02:14:00 +11:00
default:
2022-05-21 22:41:20 +11:00
// fprintf(stderr, "token %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
foo = push_bubulle(bublist, &bubulle);
if (foo) {
2023-03-22 06:31:50 +11:00
fprintf(stderr, "*** %s: error %d on push\n", __func__, foo);
exit(1);
2020-06-05 19:57:43 +11:00
break;
}
nbre++;
}
2020-06-05 10:05:01 +11:00
fclose(fpin);
2020-06-05 19:57:43 +11:00
if(verbosity) {
fprintf(stderr, "%s : %d vertices loaded\n", __func__, nbre);
}
2022-05-21 22:41:20 +11:00
bubulles_to_data(outfname, NULL, bublist, 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
}
/* --------------------------------------------------------------------- */