Back to index

radiance  4R0+20100331
Defines | Functions | Variables
mgf2rad.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "mgflib/parser.h"
#include "color.h"
#include "tmesh.h"

Go to the source code of this file.

Defines

#define putv(v)   printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
#define invert   (xf_context != NULL && xf_context->rev)

Functions

int r_comment ()
int r_cone ()
int r_cyl ()
int r_face ()
int r_ies ()
int r_ring ()
int r_sph ()
char * material ()
char * object ()
char * addarg ()
 main (int argc, argv)
int r_comment (int ac, char **av)
int r_cone (int ac, char **av)
int r_cyl (int ac, char **av)
int r_sph (int ac, char **av)
int r_ring (int ac, char **av)
int r_face (int ac, char **av)
int r_ies (int ac, char **av)
 do_tri (char *mat, C_VERTEX *cv1, C_VERTEX *cv2, C_VERTEX *cv3, int iv)
 cvtcolor (COLOR radrgb, C_COLOR *ciec, double intensity)
char * addarg (char *op, char *arg)

Variables

static const char RCSid [] = "$Id: mgf2rad.c,v 1.3 2003/06/26 00:58:09 schorsch Exp $"
double glowdist = FHUGE
double emult = 1.
FILE * matfp

Define Documentation

#define invert   (xf_context != NULL && xf_context->rev)

Definition at line 18 of file mgf2rad.c.

#define putv (   v)    printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])

Definition at line 16 of file mgf2rad.c.


Function Documentation

char * addarg ( )
char* addarg ( char *  op,
char *  arg 
)

Definition at line 620 of file mgf2rad.c.

{
       *op = ' ';
       while (*++op = *arg++)
              ;
       return(op);
}

Here is the caller graph for this function:

cvtcolor ( COLOR  radrgb,
C_COLOR ciec,
double  intensity 
)

Definition at line 582 of file mgf2rad.c.

{
       static COLOR  ciexyz;

       c_ccvt(ciec, C_CSXY);              /* get xy representation */
       ciexyz[1] = intensity;
       ciexyz[0] = ciec->cx/ciec->cy*ciexyz[1];
       ciexyz[2] = ciexyz[1]*(1./ciec->cy - 1.) - ciexyz[0];
       cie_rgb(radrgb, ciexyz);
}

Here is the call graph for this function:

do_tri ( char *  mat,
C_VERTEX cv1,
C_VERTEX cv2,
C_VERTEX cv3,
int  iv 
)

Definition at line 417 of file mgf2rad.c.

{
       static int    ntris;
       BARYCCM       bvecs;
       RREAL  bcoor[3][3];
       C_VERTEX      *cvt;
       FVECT  v1, v2, v3;
       FVECT  n1, n2, n3;
       register int  i;

       if (iv) {                   /* swap vertex order if inverted */
              cvt = cv1;
              cv1 = cv3;
              cv3 = cvt;
       }
       xf_xfmpoint(v1, cv1->p);
       xf_xfmpoint(v2, cv2->p);
       xf_xfmpoint(v3, cv3->p);
                                   /* compute barycentric coords. */
       if (comp_baryc(&bvecs, v1, v2, v3) < 0)
              return;                            /* degenerate triangle! */
       printf("\n%s texfunc T-nor\n", mat);      /* put out texture */
       printf("4 dx dy dz %s\n0\n", TCALNAME);
       xf_rotvect(n1, cv1->n);
       xf_rotvect(n2, cv2->n);
       xf_rotvect(n3, cv3->n);
       for (i = 0; i < 3; i++) {
              bcoor[i][0] = n1[i];
              bcoor[i][1] = n2[i];
              bcoor[i][2] = n3[i];
       }
       put_baryc(&bvecs, bcoor, 3);
                                          /* put out triangle */
       printf("\nT-nor polygon %st%d\n", object(), ++ntris);
       printf("0\n0\n9\n");
       putv(v1);
       putv(v2);
       putv(v3);
}

Here is the call graph for this function:

main ( int  argc,
argv   
)

Definition at line 30 of file mgf2rad.c.

{
       int    i;

       matfp = stdout;
                            /* print out parser version */
       printf("## Translated from MGF Version %d.%d\n", MG_VMAJOR, MG_VMINOR);
                            /* initialize dispatch table */
       mg_ehand[MG_E_COMMENT] = r_comment;       /* we pass comments */
       mg_ehand[MG_E_COLOR] = c_hcolor;   /* they get color */
       mg_ehand[MG_E_CONE] = r_cone;             /* we do cones */
       mg_ehand[MG_E_CMIX] = c_hcolor;           /* they mix colors */
       mg_ehand[MG_E_CSPEC] = c_hcolor;   /* they get spectra */
       mg_ehand[MG_E_CXY] = c_hcolor;            /* they get chromaticities */
       mg_ehand[MG_E_CCT] = c_hcolor;            /* they get color temp's */
       mg_ehand[MG_E_CYL] = r_cyl;        /* we do cylinders */
       mg_ehand[MG_E_ED] = c_hmaterial;   /* they get emission */
       mg_ehand[MG_E_FACE] = r_face;             /* we do faces */
       mg_ehand[MG_E_IES] = r_ies;        /* we do IES files */
       mg_ehand[MG_E_IR] = c_hmaterial;   /* they get refractive index */
       mg_ehand[MG_E_MATERIAL] = c_hmaterial;    /* they get materials */
       mg_ehand[MG_E_NORMAL] = c_hvertex; /* they get normals */
       mg_ehand[MG_E_OBJECT] = obj_handler;      /* they track object names */
       mg_ehand[MG_E_POINT] = c_hvertex;  /* they get points */
       mg_ehand[MG_E_RD] = c_hmaterial;   /* they get diffuse refl. */
       mg_ehand[MG_E_RING] = r_ring;             /* we do rings */
       mg_ehand[MG_E_RS] = c_hmaterial;   /* they get specular refl. */
       mg_ehand[MG_E_SIDES] = c_hmaterial;       /* they get # sides */
       mg_ehand[MG_E_SPH] = r_sph;        /* we do spheres */
       mg_ehand[MG_E_TD] = c_hmaterial;   /* they get diffuse trans. */
       mg_ehand[MG_E_TS] = c_hmaterial;   /* they get specular trans. */
       mg_ehand[MG_E_VERTEX] = c_hvertex; /* they get vertices */
       mg_ehand[MG_E_XF] = xf_handler;           /* they track transforms */
       mg_init();           /* initialize the parser */
                                   /* get our options & print header */
       printf("## %s", argv[0]);
       for (i = 1; i < argc && argv[i][0] == '-'; i++) {
              printf(" %s", argv[i]);
              switch (argv[i][1]) {
              case 'g':                   /* glow distance (meters) */
                     if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f"))
                            goto userr;
                     glowdist = atof(argv[++i]);
                     printf(" %s", argv[i]);
                     break;
              case 'e':                   /* emitter multiplier */
                     if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f"))
                            goto userr;
                     emult = atof(argv[++i]);
                     printf(" %s", argv[i]);
                     break;
              case 'm':                   /* materials file */
                     matfp = fopen(argv[++i], "a");
                     if (matfp == NULL) {
                            fprintf(stderr, "%s: cannot append\n", argv[i]);
                            exit(1);
                     }
                     printf(" %s", argv[i]);
                     break;
              default:
                     goto userr;
              }
       }
       putchar('\n');
       if (i == argc) {            /* convert stdin */
              if (mg_load(NULL) != MG_OK)
                     exit(1);
              if (mg_nunknown)
                     printf("## %s: %u unknown entities\n",
                                   argv[0], mg_nunknown);
       } else                      /* convert each file */
              for ( ; i < argc; i++) {
                     printf("## %s %s ##############################\n",
                                   argv[0], argv[i]);
                     if (mg_load(argv[i]) != MG_OK)
                            exit(1);
                     if (mg_nunknown) {
                            printf("## %s %s: %u unknown entities\n",
                                          argv[0], argv[i], mg_nunknown);
                            mg_nunknown = 0;
                     }
              }
       exit(0);
userr:
       fprintf(stderr, "Usage: %s [-g dist][-e mult][-m matf] [file.mgf] ..\n",
                     argv[0]);
       exit(1);
}

Here is the call graph for this function:

char* material ( void  )
char * object ( void  )
int r_comment ( )
int r_comment ( int  ac,
char **  av 
)

Definition at line 123 of file mgf2rad.c.

{
       putchar('#');        /* use Radiance comment character */
       while (--ac) {                     /* pass through verbatim */
              putchar(' ');
              fputs(*++av, stdout);
       }
       putchar('\n');
       return(MG_OK);
}

Here is the caller graph for this function:

int r_cone ( )
int r_cone ( int  ac,
char **  av 
)

Definition at line 138 of file mgf2rad.c.

{
       static int    ncones;
       char   *mat;
       double r1, r2;
       C_VERTEX      *cv1, *cv2;
       FVECT  p1, p2;
       int    inv;
                                   /* check argument count and type */
       if (ac != 5)
              return(MG_EARGC);
       if (!isflt(av[2]) || !isflt(av[4]))
              return(MG_ETYPE);
                                   /* get the endpoint vertices */
       if ((cv1 = c_getvert(av[1])) == NULL ||
                     (cv2 = c_getvert(av[3])) == NULL)
              return(MG_EUNDEF);
       xf_xfmpoint(p1, cv1->p);    /* transform endpoints */
       xf_xfmpoint(p2, cv2->p);
       r1 = xf_scale(atof(av[2])); /* scale radii */
       r2 = xf_scale(atof(av[4]));
       inv = r1 < 0.;                     /* check for inverted cone */
       if (r1 == 0.) {                    /* check for illegal radii */
              if (r2 == 0.)
                     return(MG_EILL);
              inv = r2 < 0.;
       } else if (r2 != 0. && inv ^ r2 < 0.)
              return(MG_EILL);
       if (inv) {
              r1 = -r1;
              r2 = -r2;
       }
       if ((mat = material()) == NULL)    /* get material */
              return(MG_EBADMAT);
                                   /* spit the sucker out */
       printf("\n%s %s %sc%d\n", mat, inv ? "cup" : "cone",
                     object(), ++ncones);
       printf("0\n0\n8\n");
       putv(p1);
       putv(p2);
       printf("%18.12g %18.12g\n", r1, r2);
       return(MG_OK);
}

Here is the call graph for this function:

int r_cyl ( )
int r_cyl ( int  ac,
char **  av 
)

Definition at line 186 of file mgf2rad.c.

{
       static int    ncyls;
       char   *mat;
       double rad;
       C_VERTEX      *cv1, *cv2;
       FVECT  p1, p2;
       int    inv;
                                   /* check argument count and type */
       if (ac != 4)
              return(MG_EARGC);
       if (!isflt(av[2]))
              return(MG_ETYPE);
                                   /* get the endpoint vertices */
       if ((cv1 = c_getvert(av[1])) == NULL ||
                     (cv2 = c_getvert(av[3])) == NULL)
              return(MG_EUNDEF);
       xf_xfmpoint(p1, cv1->p);    /* transform endpoints */
       xf_xfmpoint(p2, cv2->p);
       rad = xf_scale(atof(av[2]));       /* scale radius */
       if ((inv = rad < 0.))              /* check for inverted cylinder */
              rad = -rad;
       if ((mat = material()) == NULL)    /* get material */
              return(MG_EBADMAT);
                                   /* spit out the primitive */
       printf("\n%s %s %scy%d\n", mat, inv ? "tube" : "cylinder",
                     object(), ++ncyls);
       printf("0\n0\n7\n");
       putv(p1);
       putv(p2);
       printf("%18.12g\n", rad);
       return(MG_OK);
}

Here is the call graph for this function:

int r_face ( )
int r_face ( int  ac,
char **  av 
)

Definition at line 294 of file mgf2rad.c.

{
       static int    nfaces;
       int           myi = invert;
       char   *mat;
       register int  i;
       register C_VERTEX    *cv;
       FVECT  v;
       int    rv;
                                   /* check argument count and type */
       if (ac < 4)
              return(MG_EARGC);
       if ((mat = material()) == NULL)    /* get material */
              return(MG_EBADMAT);
       if (ac <= 5) {                            /* check for smoothing */
              C_VERTEX      *cva[5];
              for (i = 1; i < ac; i++) {
                     if ((cva[i-1] = c_getvert(av[i])) == NULL)
                            return(MG_EUNDEF);
                     if (is0vect(cva[i-1]->n))
                            break;
              }
              if (i < ac)
                     i = ISFLAT;
              else
                     i = flat_tri(cva[0]->p, cva[1]->p, cva[2]->p,
                                   cva[0]->n, cva[1]->n, cva[2]->n);
              if (i == DEGEN)
                     return(MG_OK);              /* degenerate (error?) */
              if (i == RVBENT) {
                     myi = !myi;
                     i = ISBENT;
              } else if (i == RVFLAT) {
                     myi = !myi;
                     i = ISFLAT;
              }
              if (i == ISBENT) {          /* smoothed triangles */
                     do_tri(mat, cva[0], cva[1], cva[2], myi);
                     if (ac == 5)
                            do_tri(mat, cva[2], cva[3], cva[0], myi);
                     return(MG_OK);
              }
       }
                                   /* spit out unsmoothed primitive */
       printf("\n%s polygon %sf%d\n", mat, object(), ++nfaces);
       printf("0\n0\n%d\n", 3*(ac-1));
       for (i = 1; i < ac; i++) {  /* get, transform, print each vertex */
              if ((cv = c_getvert(av[myi ? ac-i : i])) == NULL)
                     return(MG_EUNDEF);
              xf_xfmpoint(v, cv->p);
              putv(v);
       }
       return(MG_OK);
}

Here is the call graph for this function:

int r_ies ( )
int r_ies ( int  ac,
char **  av 
)

Definition at line 353 of file mgf2rad.c.

{
       int    xa0 = 2;
       char   combuf[128];
       char   fname[48];
       char   *oname;
       register char *op;
       register int  i;
                                   /* check argument count */
       if (ac < 2)
              return(MG_EARGC);
                                   /* construct output file name */
       if ((op = strrchr(av[1], '/')) != NULL)
              op++;
       else
              op = av[1];
       (void)strcpy(fname, op);
       if ((op = strrchr(fname, '.')) == NULL)
              op = fname + strlen(fname);
       (void)strcpy(op, ".rad");
                                   /* see if we need to run ies2rad */
       if (access(fname, 0) == -1) {
              (void)strcpy(combuf, "ies2rad");/* build ies2rad command */
              op = combuf + 7;            /* get -m option (first) */
              if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) {
                     if (!isflt(av[xa0+1]))
                            return(MG_ETYPE);
                     op = addarg(addarg(op, "-m"), av[xa0+1]);
                     xa0 += 2;
              }
              *op++ = ' ';                /* build IES filename */
              i = 0;
              if (mg_file != NULL &&
                            (oname = strrchr(mg_file->fname,'/')) != NULL) {
                     i = oname - mg_file->fname + 1;
                     (void)strcpy(op, mg_file->fname);
              }
              (void)strcpy(op+i, av[1]);
              if (access(op, 0) == -1)    /* check for file existence */
                     return(MG_ENOFILE);
              system(combuf);                    /* run ies2rad */
              if (access(fname, 0) == -1) /* check success */
                     return(MG_EINCL);
       }
       printf("\n!xform");                /* put out xform command */
       oname = object();
       if (*oname) {
              printf(" -n ");
              for (op = oname; op[1]; op++)      /* remove trailing separator */
                     putchar(*op);
       }
       for (i = xa0; i < ac; i++)
              printf(" %s", av[i]);
       if (ac > xa0 && xf_argc > 0)
              printf(" -i 1");
       for (i = 0; i < xf_argc; i++)
              printf(" %s", xf_argv[i]);
       printf(" %s\n", fname);
       return(MG_OK);
}

Here is the call graph for this function:

int r_ring ( )
int r_ring ( int  ac,
char **  av 
)

Definition at line 257 of file mgf2rad.c.

{
       static int    nrings;
       char   *mat;
       double r1, r2;
       C_VERTEX      *cv;
       FVECT  cent, norm;
                                   /* check argument count and type */
       if (ac != 4)
              return(MG_EARGC);
       if (!isflt(av[2]) || !isflt(av[3]))
              return(MG_ETYPE);
       if ((cv = c_getvert(av[1])) == NULL)      /* get center vertex */
              return(MG_EUNDEF);
       if (is0vect(cv->n))                /* make sure we have normal */
              return(MG_EILL);
       xf_xfmpoint(cent, cv->p);          /* transform center */
       xf_rotvect(norm, cv->n);           /* rotate normal */
       r1 = xf_scale(atof(av[2]));        /* scale radii */
       r2 = xf_scale(atof(av[3]));
       if (r1 < 0. | r2 <= r1)
              return(MG_EILL);
       if ((mat = material()) == NULL)           /* get material */
              return(MG_EBADMAT);
                                          /* spit out primitive */
       printf("\n%s ring %sr%d\n", mat, object(), ++nrings);
       printf("0\n0\n8\n");
       putv(cent);
       putv(norm);
       printf("%18.12g %18.12g\n", r1, r2);
       return(MG_OK);
}

Here is the call graph for this function:

int r_sph ( )
int r_sph ( int  ac,
char **  av 
)

Definition at line 224 of file mgf2rad.c.

{
       static int    nsphs;
       char   *mat;
       double rad;
       C_VERTEX      *cv;
       FVECT  cent;
       int    inv;
                                   /* check argument count and type */
       if (ac != 3)
              return(MG_EARGC);
       if (!isflt(av[2]))
              return(MG_ETYPE);
       if ((cv = c_getvert(av[1])) == NULL)      /* get center vertex */
              return(MG_EUNDEF);
       xf_xfmpoint(cent, cv->p);          /* transform center */
       rad = xf_scale(atof(av[2]));              /* scale radius */
       if ((inv = rad < 0.))                     /* check for inversion */
              rad = -rad;
       if ((mat = material()) == NULL)           /* get material */
              return(MG_EBADMAT);
                                          /* spit out primitive */
       printf("\n%s %s %ss%d\n", mat, inv ? "bubble" : "sphere",
                     object(), ++nsphs);
       printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n",
                     cent[0], cent[1], cent[2], rad);
       return(MG_OK);
}

Here is the call graph for this function:


Variable Documentation

double emult = 1.

Definition at line 22 of file mgf2rad.c.

double glowdist = FHUGE

Definition at line 20 of file mgf2rad.c.

FILE* matfp

Definition at line 24 of file mgf2rad.c.

const char RCSid[] = "$Id: mgf2rad.c,v 1.3 2003/06/26 00:58:09 schorsch Exp $" [static]

Definition at line 2 of file mgf2rad.c.