Back to index

radiance  4R0+20100331
Classes | Defines | Functions | Variables
tmesh2rad.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include "rtmath.h"
#include "tmesh.h"

Go to the source code of this file.

Classes

struct  VERTEX

Defines

#define VOIDID   "void" /* this is defined in object.h */
#define PATNAME   "T-pat" /* triangle pattern name (reused) */
#define TEXNAME   "T-nor" /* triangle texture name (reused) */
#define V_DEFINED   01 /* this vertex is defined */
#define V_HASNORM   02 /* vertex has surface normal */
#define V_HASINDX   04 /* vertex has index */
#define novert(i)   (((i)<0)|((i)>=nverts) || !(vlist[i].flags&V_DEFINED))
#define CHUNKSIZ   128 /* vertex allocation chunk size */

Functions

VERTEXvnew ()
void convert (char *fname, FILE *fp)
void triangle (char *pn, char *mod, char *obj, VERTEX *v1, VERTEX *v2, VERTEX *v3)
VERTEXvnew (int id, double x, double y, double z)
void syntax (char *fn, FILE *fp, char *er)
int main (int argc, char *argv[])
void triangle (char *pn, char *mod, char *obj, register VERTEX *v1, register VERTEX *v2, register VERTEX *v3)
VERTEXvnew (register int id, double x, double y, double z)
void syntax (char *fn, register FILE *fp, char *er)

Variables

static const char RCSid [] = "$Id: tmesh2rad.c,v 2.16 2008/11/10 19:08:18 greg Exp $"
VERTEXvlist = NULL
int nverts = 0
char * defmat = VOIDID
char * defpat = ""
char * defobj = "T"

Class Documentation

struct VERTEX

Definition at line 48 of file tmesh2rad.c.

Class Members
short flags
float ndx
FVECT nor
FVECT pos

Define Documentation

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

Definition at line 60 of file tmesh2rad.c.

#define novert (   i)    (((i)<0)|((i)>=nverts) || !(vlist[i].flags&V_DEFINED))

Definition at line 58 of file tmesh2rad.c.

#define PATNAME   "T-pat" /* triangle pattern name (reused) */

Definition at line 41 of file tmesh2rad.c.

#define TEXNAME   "T-nor" /* triangle texture name (reused) */

Definition at line 42 of file tmesh2rad.c.

#define V_DEFINED   01 /* this vertex is defined */

Definition at line 44 of file tmesh2rad.c.

#define V_HASINDX   04 /* vertex has index */

Definition at line 46 of file tmesh2rad.c.

#define V_HASNORM   02 /* vertex has surface normal */

Definition at line 45 of file tmesh2rad.c.

#define VOIDID   "void" /* this is defined in object.h */

Definition at line 39 of file tmesh2rad.c.


Function Documentation

void convert ( char *  fname,
FILE *  fp 
)

Definition at line 116 of file tmesh2rad.c.

{
       char   typ[4];
       int    id[3];
       double vec[3];
       char   picfile[128];
       char   matname[64];
       char   objname[64];
       register int  i;
       register VERTEX      *lastv;
                                   /* start fresh */
       i = nverts;
       lastv = vlist;
       while (i--)
              (lastv++)->flags = 0;
       lastv = NULL;
       strcpy(picfile, defpat);
       strcpy(matname, defmat);
       strcpy(objname, defobj);

       printf("\n## T-mesh read from: %s\n", fname);
                                   /* scan until EOF */
       while (fscanf(fp, "%1s", typ) == 1)
              switch (typ[0]) {
              case 'v':            /* vertex */
                     if (fscanf(fp, "%d %lf %lf %lf", &id[0],
                                   &vec[0], &vec[1], &vec[2]) != 4)
                            syntax(fname, fp, "Bad vertex");
                     lastv = vnew(id[0], vec[0], vec[1], vec[2]);
                     break;
              case 't':            /* triangle */
                     if (fscanf(fp, "%d %d %d", &id[0], &id[1], &id[2]) != 3)
                            syntax(fname, fp, "Bad triangle");
                     if (novert(id[0]) | novert(id[1]) | novert(id[2]))
                            syntax(fname, fp, "Undefined triangle vertex");
                     triangle(picfile, matname, objname, &vlist[id[0]],
                                   &vlist[id[1]], &vlist[id[2]]);
                     break;
              case 'n':            /* surface normal */
                     if (lastv == NULL)
                            syntax(fname, fp, "No vertex for normal");
                     if (fscanf(fp, "%lf %lf %lf",
                                   &vec[0], &vec[1], &vec[2]) != 3)
                            syntax(fname, fp, "Bad vertex normal");
                     lastv->nor[0] = vec[0];
                     lastv->nor[1] = vec[1];
                     lastv->nor[2] = vec[2];
                     if (normalize(lastv->nor) == 0.0)
                            syntax(fname, fp, "Zero vertex normal");
                     lastv->flags |= V_HASNORM;
                     break;
              case 'i':            /* index position */
                     if (lastv == NULL)
                            syntax(fname, fp, "No vertex for index");
                     if (fscanf(fp, "%lf %lf", &vec[0], &vec[1]) != 2)
                            syntax(fname, fp, "Bad index");
                     lastv->ndx[0] = vec[0];
                     lastv->ndx[1] = vec[1];
                     lastv->flags |= V_HASINDX;
                     break;
              case 'o':            /* object name */
                     if (fscanf(fp, "%s", objname) != 1)
                            syntax(fname, fp, "Bad object name");
                     break;
              case 'm':            /* material */
                     if (fscanf(fp, "%s", matname) != 1)
                            syntax(fname, fp, "Bad material");
                     if (matname[0] == '-' && !matname[1])
                            strcpy(matname, VOIDID);
                     break;
              case 'p':            /* picture */
                     if (fscanf(fp, "%s", picfile) != 1)
                            syntax(fname, fp, "Bad pattern");
                     if (picfile[0] == '-' && !picfile[1])
                            picfile[0] = '\0';
                     break;
              case '#':            /* comment */
                     fputs("\n#", stdout);
                     while ((i = getc(fp)) != EOF) {
                            putchar(i);
                            if (i == '\n')
                                   break;
                     }
                     break;
              default:
                     syntax(fname, fp, "Unknown type");
                     break;
              }
}

Here is the call graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 75 of file tmesh2rad.c.

{
       FILE   *fp;
       int    i;

       for (i = 1; i < argc && argv[i][0] == '-'; i++)
              switch (argv[i][1]) {
              case 'o':            /* object name */
                     defobj = argv[++i];
                     break;
              case 'm':            /* default material */
                     defmat = argv[++i];
                     break;
              case 'p':            /* default picture */
                     defpat = argv[++i];
                     break;
              default:
                     fprintf(stderr,
                     "Usage: %s [-o obj][-m mat][-p hdr] [file ..]\n",
                                   argv[0]);
                     exit(1);
              }
       if (i >= argc)
              convert("<stdin>", stdin);
       else
              for ( ; i < argc; i++) {
                     if ((fp = fopen(argv[i], "r")) == NULL) {
                            perror(argv[i]);
                            exit(1);
                     }
                     convert(argv[i], fp);
                     fclose(fp);
              }
       exit(0);
}

Here is the call graph for this function:

void syntax ( char *  fn,
FILE *  fp,
char *  er 
)
void syntax ( char *  fn,
register FILE *  fp,
char *  er 
)

Definition at line 315 of file tmesh2rad.c.

{
       extern long   ftell();
       register long cpos;
       register int  c;
       int    lineno;

       if (fp == stdin)
              fprintf(stderr, "%s: T-mesh format error: %s\n", fn, er);
       else {
              cpos = ftell(fp);
              fseek(fp, 0L, 0);
              lineno = 1;
              while (cpos-- > 0) {
                     if ((c = getc(fp)) == EOF)
                            break;
                     if (c == '\n')
                            lineno++;
              }
              fprintf(stderr, "%s: T-mesh format error at line %d: %s\n",
                            fn, lineno, er);
       }
       exit(1);
}
void triangle ( char *  pn,
char *  mod,
char *  obj,
VERTEX v1,
VERTEX v2,
VERTEX v3 
)

Here is the caller graph for this function:

void triangle ( char *  pn,
char *  mod,
char *  obj,
register VERTEX v1,
register VERTEX v2,
register VERTEX v3 
)

Definition at line 211 of file tmesh2rad.c.

{
       static char   vfmt[] = "%18.12g %18.12g %18.12g\n";
       static int    ntri = 0;
       int           flatness = ISFLAT;
       BARYCCM       bvecs;
       RREAL  bvm[3][3];
       register int  i;
                                   /* compute barycentric coordinates */
       if (v1->flags & v2->flags & v3->flags & (V_HASINDX|V_HASNORM))
              if (comp_baryc(&bvecs, v1->pos, v2->pos, v3->pos) < 0)
                     return;
                                   /* check flatness */
       if (v1->flags & v2->flags & v3->flags & V_HASNORM) {
              flatness = flat_tri(v1->pos, v2->pos, v3->pos,
                                   v1->nor, v2->nor, v3->nor);
              if (flatness == DEGEN)
                     return;
       }
                                   /* put out texture (if any) */
       if (flatness == ISBENT || flatness == RVBENT) {
              printf("\n%s texfunc %s\n", mod, TEXNAME);
              mod = TEXNAME;
              printf("4 dx dy dz %s\n", TCALNAME);
              printf("0\n");
              for (i = 0; i < 3; i++) {
                     bvm[i][0] = v1->nor[i];
                     bvm[i][1] = v2->nor[i];
                     bvm[i][2] = v3->nor[i];
              }
              put_baryc(&bvecs, bvm, 3);
       }
                                   /* put out pattern (if any) */
       if (*pn && (v1->flags & v2->flags & v3->flags & V_HASINDX)) {
              printf("\n%s colorpict %s\n", mod, PATNAME);
              mod = PATNAME;
              printf("7 noneg noneg noneg %s %s u v\n", pn, TCALNAME);
              printf("0\n");
              for (i = 0; i < 2; i++) {
                     bvm[i][0] = v1->ndx[i];
                     bvm[i][1] = v2->ndx[i];
                     bvm[i][2] = v3->ndx[i];
              }
              put_baryc(&bvecs, bvm, 2);
       }
                                   /* put out (reversed) triangle */
       printf("\n%s polygon %s.%d\n", mod, obj, ++ntri);
       printf("0\n0\n9\n");
       if (flatness == RVFLAT || flatness == RVBENT) {
              printf(vfmt, v3->pos[0],v3->pos[1],v3->pos[2]);
              printf(vfmt, v2->pos[0],v2->pos[1],v2->pos[2]);
              printf(vfmt, v1->pos[0],v1->pos[1],v1->pos[2]);
       } else {
              printf(vfmt, v1->pos[0],v1->pos[1],v1->pos[2]);
              printf(vfmt, v2->pos[0],v2->pos[1],v2->pos[2]);
              printf(vfmt, v3->pos[0],v3->pos[1],v3->pos[2]);
       }
}

Here is the call graph for this function:

VERTEX* vnew ( )

Here is the caller graph for this function:

VERTEX* vnew ( int  id,
double  x,
double  y,
double  z 
)
VERTEX* vnew ( register int  id,
double  x,
double  y,
double  z 
)

Definition at line 279 of file tmesh2rad.c.

{
       register int  i;

       if (id >= nverts) {         /* get some more */
              i = nverts;
              nverts = CHUNKSIZ*((id/CHUNKSIZ)+1);
              if (vlist == NULL)
                     vlist = (VERTEX *)malloc(nverts*sizeof(VERTEX));
              else
                     vlist = (VERTEX *)realloc((void *)vlist,
                                   nverts*sizeof(VERTEX));
              if (vlist == NULL) {
                     fprintf(stderr,
                     "Out of memory while allocating vertex %d\n", id);
                     exit(1);
              }
              while (i < nverts)          /* clear what's new */
                     vlist[i++].flags = 0;
       }
                                   /* assign new vertex */
       vlist[id].pos[0] = x;
       vlist[id].pos[1] = y;
       vlist[id].pos[2] = z;
       vlist[id].flags = V_DEFINED;
                                   /* return it */
       return(&vlist[id]);
}

Variable Documentation

char* defmat = VOIDID

Definition at line 64 of file tmesh2rad.c.

char* defobj = "T"

Definition at line 66 of file tmesh2rad.c.

char* defpat = ""

Definition at line 65 of file tmesh2rad.c.

int nverts = 0

Definition at line 56 of file tmesh2rad.c.

const char RCSid[] = "$Id: tmesh2rad.c,v 2.16 2008/11/10 19:08:18 greg Exp $" [static]

Definition at line 2 of file tmesh2rad.c.

VERTEX* vlist = NULL

Definition at line 55 of file tmesh2rad.c.