Back to index

radiance  4R0+20100331
Defines | Typedefs | Functions | Variables
wfconv.c File Reference
#include "copyright.h"
#include "standard.h"
#include "cvmesh.h"
#include <ctype.h>

Go to the source code of this file.

Defines

#define CHUNKSIZ   1024 /* vertex allocation chunk size */
#define MAXARG   512 /* maximum # arguments in a statement */

Typedefs

typedef int VNDX [3]

Functions

static int getstmt (char *av[MAXARG], FILE *fp)
static int cvtndx (VNDX vi, char *vs)
static int putface (int ac, char **av)
static OBJECT getmod (void)
static int puttri (char *v1, char *v2, char *v3)
static void freeverts (void)
static int newv (double x, double y, double z)
static int newvn (double x, double y, double z)
static int newvt (double x, double y)
static void syntax (char *er)
void wfreadobj (char *objfn)
static int getstmt (register char *av[MAXARG], FILE *fp)
static int cvtndx (register VNDX vi, register char *vs)
static int putface (int ac, register char **av)

Variables

static const char RCSid [] = "$Id: wfconv.c,v 2.9 2004/04/23 16:20:56 greg Exp $"
static FVECTvlist
static int nvs
static FVECTvnlist
static int nvns
static RREAL(* vtlist )[2]
static int nvts
static char * inpfile
static int havemats
static char material [64]
static char group [64]
static int lineno
static int faceno

Define Documentation

#define CHUNKSIZ   1024 /* vertex allocation chunk size */

Definition at line 16 of file wfconv.c.

#define MAXARG   512 /* maximum # arguments in a statement */

Definition at line 18 of file wfconv.c.


Typedef Documentation

typedef int VNDX[3]

Definition at line 14 of file wfconv.c.


Function Documentation

static int cvtndx ( VNDX  vi,
char *  vs 
) [static]
static int cvtndx ( register VNDX  vi,
register char *  vs 
) [static]

Definition at line 200 of file wfconv.c.

{
                                   /* get point */
       vi[0] = atoi(vs);
       if (vi[0] > 0) {
              if (vi[0]-- > nvs)
                     return(0);
       } else if (vi[0] < 0) {
              vi[0] += nvs;
              if (vi[0] < 0)
                     return(0);
       } else
              return(0);
                                   /* get map coord. */
       while (*vs)
              if (*vs++ == '/')
                     break;
       vi[1] = atoi(vs);
       if (vi[1] > 0) {
              if (vi[1]-- > nvts)
                     return(0);
       } else if (vi[1] < 0) {
              vi[1] += nvts;
              if (vi[1] < 0)
                     return(0);
       } else
              vi[1] = -1;
                                   /* get normal */
       while (*vs)
              if (*vs++ == '/')
                     break;
       vi[2] = atoi(vs);
       if (vi[2] > 0) {
              if (vi[2]-- > nvns)
                     return(0);
       } else if (vi[2] < 0) {
              vi[2] += nvns;
              if (vi[2] < 0)
                     return(0);
       } else
              vi[2] = -1;
       return(1);
}
static void freeverts ( void  ) [static]

Definition at line 331 of file wfconv.c.

{
       if (nvs) {
              free((void *)vlist);
              nvs = 0;
       }
       if (nvts) {
              free((void *)vtlist);
              nvts = 0;
       }
       if (nvns) {
              free((void *)vnlist);
              nvns = 0;
       }
}

Here is the caller graph for this function:

static OBJECT getmod ( void  ) [static]

Definition at line 271 of file wfconv.c.

{
       char   *mnam;
       OBJECT mod;

       if (!havemats)
              return(OVOID);
       if (!strcmp(material, VOIDID))
              return(OVOID);
       if (material[0])            /* prefer usemtl statements */
              mnam = material;
       else if (group[0])          /* else use group name */
              mnam = group;
       else
              return(OVOID);
       mod = modifier(mnam);
       if (mod == OVOID) {
              sprintf(errmsg, "%s: undefined modifier \"%s\"",
                            inpfile, mnam);
              error(USER, errmsg);
       }
       return(mod);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int getstmt ( char *  av[MAXARG],
FILE *  fp 
) [static]
static int getstmt ( register char *  av[MAXARG],
FILE *  fp 
) [static]

Definition at line 160 of file wfconv.c.

{
       static char   sbuf[MAXARG*16];
       register char *cp;
       register int  i;

       do {
              if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL)
                     return(0);
              i = 0;
              for ( ; ; ) {
                     while (isspace(*cp) || *cp == '\\') {
                            if (*cp == '\n')
                                   lineno++;
                            *cp++ = '\0';
                     }
                     if (!*cp)
                            break;
                     if (i >= MAXARG-1) {
                            sprintf(errmsg,
                     "%s: too many arguments near line %d (limit %d)\n",
                                   inpfile, lineno+1, MAXARG-1);
                            break;
                     }
                     av[i++] = cp;
                     while (*++cp && !isspace(*cp))
                            ;
              }
              av[i] = NULL;
              lineno++;
       } while (!i);

       return(i);
}

Here is the call graph for this function:

static int newv ( double  x,
double  y,
double  z 
) [static]

Definition at line 349 of file wfconv.c.

{
       if (!(nvs%CHUNKSIZ)) {             /* allocate next block */
              if (nvs == 0)
                     vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
              else
                     vlist = (FVECT *)realloc((void *)vlist,
                                   (nvs+CHUNKSIZ)*sizeof(FVECT));
              if (vlist == NULL)
                     error(SYSTEM, "out of memory in newv");
       }
                                   /* assign new vertex */
       vlist[nvs][0] = x;
       vlist[nvs][1] = y;
       vlist[nvs][2] = z;
       return(++nvs);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int newvn ( double  x,
double  y,
double  z 
) [static]

Definition at line 373 of file wfconv.c.

{
       if (!(nvns%CHUNKSIZ)) {            /* allocate next block */
              if (nvns == 0)
                     vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
              else
                     vnlist = (FVECT *)realloc((void *)vnlist,
                                   (nvns+CHUNKSIZ)*sizeof(FVECT));
              if (vnlist == NULL)
                     error(SYSTEM, "out of memory in newvn");
       }
                                   /* assign new normal */
       vnlist[nvns][0] = x;
       vnlist[nvns][1] = y;
       vnlist[nvns][2] = z;
       if (normalize(vnlist[nvns]) == 0.0)
              return(0);
       return(++nvns);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int newvt ( double  x,
double  y 
) [static]

Definition at line 399 of file wfconv.c.

{
       if (!(nvts%CHUNKSIZ)) {            /* allocate next block */
              if (nvts == 0)
                     vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
              else
                     vtlist = (RREAL (*)[2])realloc((void *)vtlist,
                                   (nvts+CHUNKSIZ)*2*sizeof(RREAL));
              if (vtlist == NULL)
                     error(SYSTEM, "out of memory in newvt");
       }
                                   /* assign new vertex */
       vtlist[nvts][0] = x;
       vtlist[nvts][1] = y;
       return(++nvts);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int putface ( int  ac,
char **  av 
) [static]
static int putface ( int  ac,
register char **  av 
) [static]

Definition at line 249 of file wfconv.c.

{
       char          *cp;
       register int  i;

       while (ac > 3) {            /* break into triangles */
              if (!puttri(av[0], av[1], av[2]))
                     return(0);
              ac--;                /* remove vertex & rotate */
              cp = av[0];
              for (i = 0; i < ac-1; i++)
                     av[i] = av[i+2];
              av[i] = cp;
       }
       return(puttri(av[0], av[1], av[2]));
}

Here is the call graph for this function:

static int puttri ( char *  v1,
char *  v2,
char *  v3 
) [static]

Definition at line 297 of file wfconv.c.

{
       VNDX   v1i, v2i, v3i;
       RREAL  *v1c, *v2c, *v3c;
       RREAL  *v1n, *v2n, *v3n;
       
       if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) {
              error(WARNING, "bad vertex reference");
              return(0);
       }
       if (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0) {
              v1c = vtlist[v1i[1]];
              v2c = vtlist[v2i[1]];
              v3c = vtlist[v3i[1]];
       } else
              v1c = v2c = v3c = NULL;

       if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) {
              v1n = vnlist[v1i[2]];
              v2n = vnlist[v2i[2]];
              v3n = vnlist[v3i[2]];
       } else
              v1n = v2n = v3n = NULL;
       
       return(cvtri(getmod(), vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
                     v1n, v2n, v3n, v1c, v2c, v3c) >= 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void syntax ( char *  er) [static]
void wfreadobj ( char *  objfn)

Definition at line 47 of file wfconv.c.

{
       FILE   *fp;
       char   *argv[MAXARG];
       int    argc;
       int    nstats, nunknown;

       if (objfn == NULL) {
              inpfile = "<stdin>";
              fp = stdin;
       } else if ((fp = fopen(inpfile=objfn, "r")) == NULL) {
              sprintf(errmsg, "cannot open \"%s\"", inpfile);
              error(USER, errmsg);
       }
       havemats = (nobjects > 0);
       nstats = nunknown = 0;
       material[0] = '\0';
       group[0] = '\0';
       lineno = 0; faceno = 0;
                                   /* scan until EOF */
       while ( (argc = getstmt(argv, fp)) ) {
              switch (argv[0][0]) {
              case 'v':            /* vertex */
                     switch (argv[0][1]) {
                     case '\0':                  /* point */
                            if (badarg(argc-1,argv+1,"fff"))
                                   syntax("bad vertex");
                            newv(atof(argv[1]), atof(argv[2]),
                                          atof(argv[3]));
                            break;
                     case 'n':                   /* normal */
                            if (argv[0][2])
                                   goto unknown;
                            if (badarg(argc-1,argv+1,"fff"))
                                   syntax("bad normal");
                            if (!newvn(atof(argv[1]), atof(argv[2]),
                                          atof(argv[3])))
                                   syntax("zero normal");
                            break;
                     case 't':                   /* coordinate */
                            if (argv[0][2])
                                   goto unknown;
                            if (badarg(argc-1,argv+1,"ff"))
                                   goto unknown;
                            newvt(atof(argv[1]), atof(argv[2]));
                            break;
                     default:
                            goto unknown;
                     }
                     break;
              case 'f':                          /* face */
                     if (argv[0][1])
                            goto unknown;
                     faceno++;
                     switch (argc-1) {
                     case 0: case 1: case 2:
                            syntax("too few vertices");
                            break;
                     case 3:
                            if (!puttri(argv[1], argv[2], argv[3]))
                                   syntax("bad triangle");
                            break;
                     default:
                            if (!putface(argc-1, argv+1))
                                   syntax("bad face");
                            break;
                     }
                     break;
              case 'u':                          /* usemtl/usemap */
                     if (!strcmp(argv[0], "usemap"))
                            break;
                     if (strcmp(argv[0], "usemtl"))
                            goto unknown;
                     if (argc > 1)
                            strcpy(material, argv[1]);
                     else
                            material[0] = '\0';
                     break;
              case 'o':            /* object name */
                     if (argv[0][1])
                            goto unknown;
                     break;
              case 'g':            /* group name */
                     if (argv[0][1])
                            goto unknown;
                     if (argc > 1)
                            strcpy(group, argv[1]);
                     else
                            group[0] = '\0';
                     break;
              case '#':            /* comment */
                     break;
              default:;            /* something we don't deal with */
              unknown:
                     nunknown++;
                     break;
              }
              nstats++;
       }
                            /* clean up */
       freeverts();
       fclose(fp);
       if (nunknown > 0) {
              sprintf(errmsg, "%d of %d statements unrecognized",
                            nunknown, nstats);
              error(WARNING, errmsg);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int faceno [static]

Definition at line 32 of file wfconv.c.

char group[64] [static]

Definition at line 30 of file wfconv.c.

int havemats [static]

Definition at line 28 of file wfconv.c.

char* inpfile [static]

Definition at line 27 of file wfconv.c.

int lineno [static]

Definition at line 31 of file wfconv.c.

char material[64] [static]

Definition at line 29 of file wfconv.c.

int nvns [static]

Definition at line 23 of file wfconv.c.

int nvs [static]

Definition at line 21 of file wfconv.c.

int nvts [static]

Definition at line 25 of file wfconv.c.

const char RCSid[] = "$Id: wfconv.c,v 2.9 2004/04/23 16:20:56 greg Exp $" [static]

Definition at line 2 of file wfconv.c.

FVECT* vlist [static]

Definition at line 20 of file wfconv.c.

FVECT* vnlist [static]

Definition at line 22 of file wfconv.c.

RREAL(* vtlist)[2] [static]

Definition at line 24 of file wfconv.c.