--- ray/src/cv/mgflib/parser.c 1995/12/01 11:03:57 1.22 +++ ray/src/cv/mgflib/parser.c 2003/11/15 17:54:06 1.28 @@ -1,14 +1,12 @@ -/* Copyright (c) 1995 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: parser.c,v 1.28 2003/11/15 17:54:06 schorsch Exp $"; #endif - /* * Parse an MGF file, converting or discarding unsupported entities */ #include +#include #include #include #include @@ -61,22 +59,19 @@ int mg_nqcdivs = MG_NQCD; /* number of divisions per q #define e_ies e_any_toss /* alternate handler routines */ -static int e_any_toss(), /* discard unneeded entity */ - e_ies(), /* IES luminaire file */ - e_include(), /* include file */ - e_sph(), /* sphere */ - e_cct(), /* color temperature */ - e_cmix(), /* color mixtures */ - e_cspec(), /* color spectra */ - e_cyl(), /* cylinder */ - e_cone(), /* cone */ - e_prism(), /* prism */ - e_ring(), /* ring */ - e_torus(); /* torus */ +static void make_axes(FVECT u, FVECT v, FVECT w); +static int put_cxy(void); +static int put_cspec(void); +static int e_any_toss(int ac, char **av); /* discard an unwanted entity */ +static int e_cspec(int ac, char **av); /* handle spectral color */ +static int e_cmix(int ac, char **av); /* handle mixing of colors */ +static int e_cct(int ac, char **av); /* handle color temperature */ + + /* alternate handler support functions */ -static int (*e_supp[MG_NENTITIES])(); +static int (*e_supp[MG_NENTITIES])(int ac, char **av); static char FLTFMT[] = "%.12g"; @@ -84,7 +79,7 @@ static int warpconends; /* hack for generating good n void -mg_init() /* initialize alternate entity handlers */ +mg_init(void) /* initialize alternate entity handlers */ { unsigned long ineed = 0, uneed = 0; register int i; @@ -123,6 +118,10 @@ mg_init() /* initialize alternate entity handlers */ ineed |= 1L<fname, '/')) != NULL) { + if (fn[0] != '/' && mg_file != NULL && + (cp = strrchr(mg_file->fname, '/')) != NULL) { strcpy(ctx->fname, mg_file->fname); strcpy(ctx->fname+(cp-mg_file->fname+1), fn); } else @@ -255,23 +258,21 @@ char *fn; void -mg_close() /* close input file */ +mg_close(void) /* close input file */ { register MG_FCTXT *ctx = mg_file; mg_file = ctx->prev; /* restore enclosing context */ - if (ctx->fp == stdin) - return; /* don't close standard input */ - fclose(ctx->fp); + if (ctx->fp != stdin) /* close file if it's a file */ + fclose(ctx->fp); } void -mg_fgetpos(pos) /* get current position in input file */ -register MG_FPOS *pos; +mg_fgetpos( /* get current position in input file */ + register MG_FPOS *pos +) { - extern long ftell(); - pos->fid = mg_file->fid; pos->lineno = mg_file->lineno; pos->offset = ftell(mg_file->fp); @@ -279,8 +280,9 @@ register MG_FPOS *pos; int -mg_fgoto(pos) /* reposition input file pointer */ -register MG_FPOS *pos; +mg_fgoto( /* reposition input file pointer */ + register MG_FPOS *pos +) { if (pos->fid != mg_file->fid) return(MG_ESEEK); @@ -296,7 +298,7 @@ register MG_FPOS *pos; int -mg_read() /* read next line from file */ +mg_read(void) /* read next line from file */ { register int len = 0; @@ -315,11 +317,10 @@ mg_read() /* read next line from file */ int -mg_parse() /* parse current input line */ +mg_parse(void) /* parse current input line */ { char abuf[MG_MAXLINE]; char *argv[MG_MAXARGC]; - int en; register char *cp, *cp2, **ap; /* copy line, removing escape chars */ cp = abuf; cp2 = mg_file->inpline; @@ -347,8 +348,9 @@ mg_parse() /* parse current input line */ int -mg_load(fn) /* load an MGF file */ -char *fn; +mg_load( /* load an MGF file */ + char *fn +) { MG_FCTXT cntxt; int rval; @@ -377,9 +379,10 @@ char *fn; int -mg_defuhand(ac, av) /* default handler for unknown entities */ -int ac; -char **av; +mg_defuhand( /* default handler for unknown entities */ + int ac, + char **av +) { if (mg_nunknown++ == 0) /* report first incident */ fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname, @@ -389,10 +392,11 @@ char **av; void -mg_clear() /* clear parser history */ +mg_clear(void) /* clear parser history */ { c_clearall(); /* clear context tables */ - mg_file = NULL; /* reset our context */ + while (mg_file != NULL) /* reset our file context */ + mg_close(); } @@ -402,18 +406,20 @@ mg_clear() /* clear parser history */ static int -e_any_toss(ac, av) /* discard an unwanted entity */ -int ac; -char **av; +e_any_toss( /* discard an unwanted entity */ + int ac, + char **av +) { return(MG_OK); } -static int -e_include(ac, av) /* include file */ -int ac; -char **av; +int +e_include( /* include file */ + int ac, + char **av +) { char *xfarg[MG_MAXARGC]; MG_FCTXT ictx; @@ -463,9 +469,45 @@ char **av; } +int +e_faceh( /* replace face+holes with single contour */ + int ac, + char **av +) +{ + char *newav[MG_MAXARGC]; + int lastp = 0; + register int i, j; + + newav[0] = mg_ename[MG_E_FACE]; + for (i = 1; i < ac; i++) + if (av[i][0] == '-') { + if (i < 4) + return(MG_EARGC); + if (i >= ac-1) + break; + if (!lastp) + lastp = i-1; + for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++) + ; + if (j - i < 3) + return(MG_EARGC); + newav[i] = av[j]; /* connect hole loop */ + } else + newav[i] = av[i]; /* hole or perimeter vertex */ + if (lastp) + newav[i++] = av[lastp]; /* finish seam to outside */ + newav[i] = NULL; + return(mg_handle(MG_E_FACE, i, newav)); +} + + static void -make_axes(u, v, w) /* compute u and v given w (normalized) */ -FVECT u, v, w; +make_axes( /* compute u and v given w (normalized) */ + FVECT u, + FVECT v, + FVECT w +) { register int i; @@ -480,10 +522,11 @@ FVECT u, v, w; } -static int -e_sph(ac, av) /* expand a sphere into cones */ -int ac; -char **av; +int +e_sph( /* expand a sphere into cones */ + int ac, + char **av +) { static char p2x[24], p2y[24], p2z[24], r1[24], r2[24]; static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"}; @@ -532,10 +575,11 @@ char **av; } -static int -e_torus(ac, av) /* expand a torus into cones */ -int ac; -char **av; +int +e_torus( /* expand a torus into cones */ + int ac, + char **av +) { static char p2[3][24], r1[24], r2[24]; static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"}; @@ -624,10 +668,11 @@ char **av; } -static int -e_cyl(ac, av) /* replace a cylinder with equivalent cone */ -int ac; -char **av; +int +e_cyl( /* replace a cylinder with equivalent cone */ + int ac, + char **av +) { static char *avnew[6] = {mg_ename[MG_E_CONE]}; @@ -641,10 +686,11 @@ char **av; } -static int -e_ring(ac, av) /* turn a ring into polygons */ -int ac; -char **av; +int +e_ring( /* turn a ring into polygons */ + int ac, + char **av +) { static char p3[3][24], p4[3][24]; static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"}; @@ -739,10 +785,11 @@ char **av; } -static int -e_cone(ac, av) /* turn a cone into polygons */ -int ac; -char **av; +int +e_cone( /* turn a cone into polygons */ + int ac, + char **av +) { static char p3[3][24], p4[3][24], n3[3][24], n4[3][24]; static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="}; @@ -781,7 +828,7 @@ char **av; if (rad2 == 0.) return(MG_EILL); } else if (rad2 != 0.) { - if (rad1 < 0. ^ rad2 < 0.) + if ((rad1 < 0.) ^ (rad2 < 0.)) return(MG_EILL); } else { /* swap */ C_VERTEX *cv; @@ -909,10 +956,11 @@ char **av; } -static int -e_prism(ac, av) /* turn a prism into polygons */ -int ac; -char **av; +int +e_prism( /* turn a prism into polygons */ + int ac, + char **av +) { static char p[3][24]; static char *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="}; @@ -1017,22 +1065,19 @@ char **av; static int -put_cxy() /* put out current xy chromaticities */ +put_cxy(void) /* put out current xy chromaticities */ { static char xbuf[24], ybuf[24]; static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf}; - int rv; sprintf(xbuf, "%.4f", c_ccolor->cx); sprintf(ybuf, "%.4f", c_ccolor->cy); - if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK) - return(rv); - return(MG_OK); + return(mg_handle(MG_E_CXY, 3, ccom)); } static int -put_cspec() /* put out current color spectrum */ +put_cspec(void) /* put out current color spectrum */ { char wl[2][6], vbuf[C_CNSS][24]; char *newav[C_CNSS+4]; @@ -1059,9 +1104,10 @@ put_cspec() /* put out current color spectrum */ static int -e_cspec(ac, av) /* handle spectral color */ -int ac; -char **av; +e_cspec( /* handle spectral color */ + int ac, + char **av +) { /* convert to xy chromaticity */ c_ccvt(c_ccolor, C_CSXY); @@ -1073,9 +1119,10 @@ char **av; static int -e_cmix(ac, av) /* handle mixing of colors */ -int ac; -char **av; +e_cmix( /* handle mixing of colors */ + int ac, + char **av +) { /* * Contorted logic works as follows: @@ -1096,9 +1143,10 @@ char **av; static int -e_cct(ac, av) /* handle color temperature */ -int ac; -char **av; +e_cct( /* handle color temperature */ + int ac, + char **av +) { /* * Logic is similar to e_cmix here. Support handler has already