--- ray/src/util/rfluxmtx.c 2014/07/22 23:21:56 2.5 +++ ray/src/util/rfluxmtx.c 2016/08/20 03:43:24 2.41 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.5 2014/07/22 23:21:56 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.41 2016/08/20 03:43:24 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,31 +11,18 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.5 201 #include #include "rtio.h" #include "rtmath.h" +#include "paths.h" #include "bsdf.h" #include "bsdf_m.h" #include "random.h" #include "triangulate.h" #include "platform.h" -#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ -#undef getc -#define getc getc_unlocked -#endif - -#ifdef _WIN32 -#define SPECIALS " \t\"$*?" -#define QUOTCHAR '"' -#else -#define SPECIALS " \t\n'\"()${}*?[];|&" -#define QUOTCHAR '\'' -#define ALTQUOT '"' -#endif - #define MAXRCARG 512 char *progname; /* global argv[0] */ -int verbose = 0; /* verbose mode? */ +int verbose = 0; /* verbose mode (< 0 no warnings) */ char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; int nrcargs = 2; @@ -77,25 +64,27 @@ typedef struct { FVECT uva[2]; /* tangent axes */ int ntris; /* number of triangles */ struct ptri { - float afrac; /* fraction of total area */ + double afrac; /* fraction of total area */ short vndx[3]; /* vertex indices */ } tri[1]; /* triangle array (extends struct) */ } POLYTRIS; /* triangulated polygon */ typedef struct param_s { - char hemis[32]; /* hemispherical sampling spec. */ + char sign; /* '-' for axis reversal */ + char hemis[31]; /* hemispherical sampling spec. */ int hsiz; /* hemisphere basis size */ int nsurfs; /* number of surfaces */ SURF *slist; /* list of surfaces */ FVECT vup; /* up vector (zero if unset) */ FVECT nrm; /* average normal direction */ - FVECT udir, vdir; /* v-up tangent axes */ + FVECT udir, vdir; /* tangent axes */ char *outfn; /* output file name (receiver) */ int (*sample_basis)(struct param_s *p, int, FILE *); } PARAMS; /* sender/receiver parameters */ PARAMS curparams; char curmod[128]; +char newparams[1024]; typedef int SURFSAMP(FVECT, SURF *, double); @@ -109,7 +98,6 @@ SURFSAMP *orig_in_surf[4] = { static void clear_params(PARAMS *p, int reset_only) { - curmod[0] = '\0'; while (p->slist != NULL) { SURF *sdel = p->slist; p->slist = sdel->next; @@ -124,7 +112,7 @@ clear_params(PARAMS *p, int reset_only) p->outfn = NULL; return; } - memset(p, 0, sizeof(curparams)); + memset(p, 0, sizeof(PARAMS)); } /* Get surface type from name */ @@ -144,73 +132,38 @@ surf_type(const char *otype) static char * oconv_command(int ac, char *av[]) { - static char oconvbuf[2048] = "!oconv -f"; - char *cp = oconvbuf + 9; - + static char oconvbuf[2048] = "!oconv -f "; + char *cp = oconvbuf + 10; + char *recv = *av++; + + if (ac-- <= 0) + return(NULL); while (ac-- > 0) { - if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { - fputs(progname, stderr); - fputs(": too many file arguments!\n", stderr); - exit(1); - } - *cp++ = ' '; strcpy(cp, *av++); while (*cp) cp++; + *cp++ = ' '; + if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) + goto overrun; } - *cp = '\0'; + /* receiver goes last */ + if (matchany(recv, SPECIALS)) { + *cp++ = QUOTCHAR; + while (*recv) { + if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) + goto overrun; + *cp++ = *recv++; + } + *cp++ = QUOTCHAR; + *cp = '\0'; + } else + strcpy(cp, recv); return(oconvbuf); +overrun: + fputs(progname, stderr); + fputs(": too many file arguments!\n", stderr); + exit(1); } -/* Check if any of the characters in str2 are found in str1 */ -static int -matchany(const char *str1, const char *str2) -{ - while (*str1) { - const char *cp = str2; - while (*cp) - if (*cp++ == *str1) - return(*str1); - ++str1; - } - return(0); -} - - -/* Convert a set of arguments into a command line for pipe() or system() */ -static char * -convert_commandline(char *cmd, const int len, char *av[]) -{ - int match; - char *cp; - - for (cp = cmd; *av != NULL; av++) { - const int n = strlen(*av); - if (cp+n >= cmd+(len-3)) { - fputs(progname, stderr); - return(NULL); - } - if ((match = matchany(*av, SPECIALS))) { - const int quote = -#ifdef ALTQUOT - (match == QUOTCHAR) ? ALTQUOT : -#endif - QUOTCHAR; - *cp++ = quote; - strcpy(cp, *av); - cp += n; - *cp++ = quote; - } else { - strcpy(cp, *av); - cp += n; - } - *cp++ = ' '; - } - if (cp <= cmd) - return(NULL); - *--cp = '\0'; - return(cmd); -} - /* Open a pipe to/from a command given as an argument list */ static FILE * popen_arglist(char *av[], char *mode) @@ -222,13 +175,13 @@ popen_arglist(char *av[], char *mode) fputs(": command line too long in popen_arglist()\n", stderr); return(NULL); } - if (verbose) + if (verbose > 0) fprintf(stderr, "%s: opening pipe %s: %s\n", progname, (*mode=='w') ? "to" : "from", cmd); return(popen(cmd, mode)); } -#ifdef _WIN32 +#if defined(_WIN32) || defined(_WIN64) /* Execute system command (Windows version) */ static int my_exec(char *av[]) @@ -240,7 +193,7 @@ my_exec(char *av[]) fputs(": command line too long in my_exec()\n", stderr); return(1); } - if (verbose) + if (verbose > 0) fprintf(stderr, "%s: running: %s\n", progname, cmd); return(system(cmd)); } @@ -255,7 +208,7 @@ my_exec(char *av[]) fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); return(1); } - if (verbose) { + if (verbose > 0) { char cmd[4096]; if (!convert_commandline(cmd, sizeof(cmd), av)) strcpy(cmd, "COMMAND TOO LONG TO SHOW"); @@ -309,49 +262,64 @@ nextchar: /* Parse program parameters (directives) */ static int -parse_params(char *pargs) +parse_params(PARAMS *p, char *pargs) { char *cp = pargs; int nparams = 0; + int quot; int i; - for ( ; ; ) + for ( ; ; ) { switch (*cp++) { case 'h': if (*cp++ != '=') break; - curparams.hsiz = 0; + if ((*cp == '+') | (*cp == '-')) + p->sign = *cp++; + else + p->sign = '+'; + p->hsiz = 0; i = 0; while (*cp && !isspace(*cp)) { if (isdigit(*cp)) - curparams.hsiz = 10*curparams.hsiz + - *cp - '0'; - curparams.hemis[i++] = *cp++; + p->hsiz = 10*p->hsiz + *cp - '0'; + p->hemis[i++] = *cp++; } if (!i) break; - curparams.hemis[i] = '\0'; - curparams.hsiz += !curparams.hsiz; + p->hemis[i] = '\0'; + p->hsiz += !p->hsiz; ++nparams; continue; case 'u': if (*cp++ != '=') break; - if (!get_direction(curparams.vup, cp)) + if (!get_direction(p->vup, cp)) break; + while (*cp && !isspace(*cp++)) + ; ++nparams; continue; case 'o': if (*cp++ != '=') break; + quot = 0; + if ((*cp == '"') | (*cp == '\'')) + quot = *cp++; i = 0; - while (*cp && !isspace(*cp++)) - i++; + while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { + i++; cp++; + } if (!i) break; - *--cp = '\0'; - curparams.outfn = savqstr(cp-i); - *cp++ = ' '; + if (!*cp) { + if (quot) + break; + cp[1] = '\0'; + } + *cp = '\0'; + p->outfn = savqstr(cp-i); + *cp++ = quot ? quot : ' '; ++nparams; continue; case ' ': @@ -364,7 +332,9 @@ parse_params(char *pargs) default: break; } - fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs); + break; + } + fprintf(stderr, "%s: bad parameter string: %s", progname, pargs); exit(1); return(-1); /* pro forma return */ } @@ -402,14 +372,18 @@ finish_receiver(void) fputs(": undefined normal for hemisphere sampling\n", stderr); exit(1); } - if (normalize(curparams.vup) == 0) + if (normalize(curparams.vup) == 0) { if (fabs(curparams.nrm[2]) < .7) curparams.vup[2] = 1; else curparams.vup[1] = 1; + } /* determine sample type/bin */ if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { - binv = "0"; /* uniform sampling -- one bin */ + sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", + curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); + binv = savqstr(sbuf); + nbins = "1"; /* uniform sampling -- one bin */ uniform = 1; } else if (tolower(curparams.hemis[0]) == 's' && tolower(curparams.hemis[1]) == 'c') { @@ -420,20 +394,22 @@ finish_receiver(void) exit(1); } calfn = shirchiufn; shirchiufn = NULL; - sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", + sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", curparams.hsiz, curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], - curparams.vup[0], curparams.vup[1], curparams.vup[2]); + curparams.vup[0], curparams.vup[1], curparams.vup[2], + curparams.sign); params = savqstr(sbuf); binv = "scbin"; nbins = "SCdim*SCdim"; } else if ((tolower(curparams.hemis[0]) == 'r') | (tolower(curparams.hemis[0]) == 't')) { calfn = reinhfn; reinhfn = NULL; - sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", + sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", curparams.hsiz, curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], - curparams.vup[0], curparams.vup[1], curparams.vup[2]); + curparams.vup[0], curparams.vup[1], curparams.vup[2], + curparams.sign); params = savqstr(sbuf); binv = "rbin"; nbins = "Nrbins"; @@ -461,6 +437,10 @@ finish_receiver(void) progname, curparams.hemis); exit(1); } + if (tolower(curparams.hemis[0]) == 'k') { + sprintf(sbuf, "RHS=%c1", curparams.sign); + params = savqstr(sbuf); + } if (!uniform & (curparams.slist->styp == ST_SOURCE)) { SURF *sp; for (sp = curparams.slist; sp != NULL; sp = sp->next) @@ -508,21 +488,12 @@ make_axes(FVECT uva[2], const FVECT nrm) { int i; - uva[1][0] = 0.5 - frandom(); - uva[1][1] = 0.5 - frandom(); - uva[1][2] = 0.5 - frandom(); - for (i = 3; i--; ) - if ((-0.6 < nrm[i]) & (nrm[i] < 0.6)) - break; - if (i < 0) { + if (!getperpendicular(uva[0], nrm, 1)) { fputs(progname, stderr); fputs(": bad surface normal in make_axes!\n", stderr); exit(1); } - uva[1][i] = 1.0; - VCROSS(uva[0], uva[1], nrm); - normalize(uva[0]); - VCROSS(uva[1], nrm, uva[0]); + fcross(uva[1], nrm, uva[0]); } /* Illegal sender surfaces end up here */ @@ -554,7 +525,7 @@ ssamp_ring(FVECT orig, SURF *sp, double x) sp->priv = (void *)uva; } SDmultiSamp(samp2, 2, x); - samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI)); + samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]); samp2[1] *= 2.*PI; uv[0] = samp2[0]*tcos(samp2[1]); uv[1] = samp2[0]*tsin(samp2[1]); @@ -607,8 +578,12 @@ ssamp_poly(FVECT orig, SURF *sp, double x) } ptp->ntris = 0; v2l->p = (void *)ptp; - if (!polyTriangulate(v2l, add_triangle)) + if (!polyTriangulate(v2l, add_triangle)) { + fprintf(stderr, + "%s: cannot triangulate polygon '%s'\n", + progname, sp->sname); return(0); + } for (i = ptp->ntris; i--; ) { int a = ptp->tri[i].vndx[0]; int b = ptp->tri[i].vndx[1]; @@ -627,7 +602,7 @@ ssamp_poly(FVECT orig, SURF *sp, double x) sp->priv = (void *)ptp; } /* pick triangle by partial area */ - for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) + for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) x -= ptp->tri[i].afrac; SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); samp2[0] *= samp2[1] = sqrt(samp2[1]); @@ -657,14 +632,22 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, /* special case for lone surface */ if (p->nsurfs == 1) { sp = p->slist; - if (DOT(sp->snrm, rdir) >= -FTINY) - return(0); /* behind surface! */ + if (DOT(sp->snrm, rdir) >= FTINY) { + fprintf(stderr, + "%s: internal - sample behind sender '%s'\n", + progname, sp->sname); + return(0); + } return((*orig_in_surf[sp->styp])(orig, sp, x)); } if (p->nsurfs > nall) { /* (re)allocate surface area cache */ if (projsa) free(projsa); projsa = (double *)malloc(sizeof(double)*p->nsurfs); - if (!projsa) return(0); + if (projsa == NULL) { + fputs(progname, stderr); + fputs(": out of memory in sample_origin!\n", stderr); + exit(1); + } nall = p->nsurfs; } /* compute projected areas */ @@ -672,8 +655,12 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, projsa[i] = -DOT(sp->snrm, rdir) * sp->area; tarea += projsa[i] *= (double)(projsa[i] > FTINY); } - if (tarea <= FTINY) /* wrong side of sender? */ + if (tarea <= FTINY) { /* wrong side of sender? */ + fputs(progname, stderr); + fputs(": internal - sample behind all sender elements!\n", + stderr); return(0); + } tarea *= x; /* get surface from list */ for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next) tarea -= projsa[i++]; @@ -703,7 +690,7 @@ sample_uniform(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -733,7 +720,7 @@ sample_shirchiu(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -746,7 +733,7 @@ sample_reinhart(PARAMS *p, int b, FILE *fp) #define T_NALT 7 static const int tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6}; const int RowMax = T_NALT*p->hsiz + 1; - const double RAH = (.25*PI)/(RowMax-.5); + const double RAH = (.5*PI)/(RowMax-.5); #define rnaz(r) (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz]) int n = sampcnt; int row, col; @@ -771,9 +758,9 @@ sample_reinhart(PARAMS *p, int b, FILE *fp) SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); alt = (row+samp3[1])*RAH; azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); - duvw[2] = tcos(alt); /* measured from horizon */ - duvw[0] = tcos(azi)*duvw[2]; - duvw[1] = tsin(azi)*duvw[2]; + duvw[2] = cos(alt); /* measured from horizon */ + duvw[0] = tsin(azi)*duvw[2]; + duvw[1] = tcos(azi)*duvw[2]; duvw[2] = sqrt(1. - duvw[2]*duvw[2]); for (i = 3; i--; ) orig_dir[1][i] = -duvw[0]*p->udir[i] - @@ -781,7 +768,7 @@ sample_reinhart(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -824,15 +811,15 @@ sample_klems(PARAMS *p, int b, FILE *fp) while (n--) { /* stratified sampling */ SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); - if (!bo_getvec(duvw, b+samp2[1], kbasis[bi])) + if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) return(0); for (i = 3; i--; ) - orig_dir[1][i] = duvw[0]*p->udir[i] + - duvw[1]*p->vdir[i] + + orig_dir[1][i] = -duvw[0]*p->udir[i] - + duvw[1]*p->vdir[i] - duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -847,7 +834,8 @@ prepare_sampler(void) fputs(": no sender surface!\n", stderr); return(-1); } - if (curparams.outfn != NULL) /* misplaced output file spec. */ + /* misplaced output file spec. */ + if ((curparams.outfn != NULL) & (verbose >= 0)) fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", progname, curparams.outfn); /* check/set basis hemisphere */ @@ -861,18 +849,24 @@ prepare_sampler(void) fputs(": undefined normal for sender sampling\n", stderr); return(-1); } - if (normalize(curparams.vup) == 0) + if (normalize(curparams.vup) == 0) { if (fabs(curparams.nrm[2]) < .7) curparams.vup[2] = 1; else curparams.vup[1] = 1; - VCROSS(curparams.udir, curparams.vup, curparams.nrm); + } + fcross(curparams.udir, curparams.vup, curparams.nrm); if (normalize(curparams.udir) == 0) { fputs(progname, stderr); fputs(": up vector coincides with sender normal\n", stderr); return(-1); } - VCROSS(curparams.vdir, curparams.nrm, curparams.udir); + fcross(curparams.vdir, curparams.nrm, curparams.udir); + if (curparams.sign == '-') { /* left-handed coordinate system? */ + curparams.udir[0] *= -1.; + curparams.udir[1] *= -1.; + curparams.udir[2] *= -1.; + } if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') curparams.sample_basis = sample_uniform; else if (tolower(curparams.hemis[0]) == 's' && @@ -952,7 +946,7 @@ add_surface(int st, const char *oname, FILE *fp) snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); if (snew == NULL) { fputs(progname, stderr); - fputs(": out of memory!\n", stderr); + fputs(": out of memory in add_surface!\n", stderr); exit(1); } strncpy(snew->sname, oname, sizeof(snew->sname)-1); @@ -989,14 +983,15 @@ add_surface(int st, const char *oname, FILE *fp) case ST_SOURCE: if (snew->nfargs != 4) goto badcount; - VCOPY(snew->snrm, snew->farg); + for (n = 3; n--; ) /* need to reverse "normal" */ + snew->snrm[n] = -snew->farg[n]; if (normalize(snew->snrm) == 0) goto badnorm; snew->area = sin((PI/180./2.)*snew->farg[3]); snew->area *= PI*snew->area; break; } - if (snew->area <= FTINY) { + if ((snew->area <= FTINY) & (verbose >= 0)) { fprintf(stderr, "%s: warning - zero area for surface '%s'\n", progname, oname); free(snew); @@ -1008,11 +1003,11 @@ add_surface(int st, const char *oname, FILE *fp) curparams.nsurfs++; return; badcount: - fprintf(stderr, "%s: bad argument count for surface '%s'\n", + fprintf(stderr, "%s: bad argument count for surface element '%s'\n", progname, oname); exit(1); badnorm: - fprintf(stderr, "%s: bad orientation for surface '%s'\n", + fprintf(stderr, "%s: bad orientation for surface element '%s'\n", progname, oname); exit(1); } @@ -1044,17 +1039,19 @@ add_recv_object(FILE *fp) finish_receiver(); clear_params(&curparams, 1); } + parse_params(&curparams, newparams); + newparams[0] = '\0'; strcpy(curmod, thismod); } add_surface(st, oname, fp); /* read & store surface */ return(1); } /* else skip arguments */ - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*s"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*d"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*f"); return(0); } @@ -1064,10 +1061,10 @@ static int add_send_object(FILE *fp) { int st; - char otype[32], oname[128]; + char thismod[128], otype[32], oname[128]; int n; - if (fscanf(fp, "%*s %s %s", otype, oname) != 2) + if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) return(0); /* must have hit EOF! */ if (!strcmp(otype, "alias")) { fscanf(fp, "%*s"); /* skip alias */ @@ -1080,15 +1077,25 @@ add_send_object(FILE *fp) fputs(": cannot use source as a sender!\n", stderr); return(-1); } + if (strcmp(thismod, curmod)) { + if (curmod[0]) { + fputs(progname, stderr); + fputs(": warning - multiple modifiers in sender\n", + stderr); + } + strcpy(curmod, thismod); + } + parse_params(&curparams, newparams); + newparams[0] = '\0'; add_surface(st, oname, fp); /* read & store surface */ return(0); } /* else skip arguments */ - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*s"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*d"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*f"); return(0); } @@ -1128,10 +1135,10 @@ load_scene(const char *inspec, int (*ocb)(FILE *)) if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 && !strcmp(inpbuf, PARAMSTART)) { if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL) - parse_params(inpbuf); + strcat(newparams, inpbuf); continue; } - while ((c = getc(fp)) != EOF && c != '\n'); + while ((c = getc(fp)) != EOF && c != '\n') ; /* else skipping comment */ continue; } @@ -1156,6 +1163,7 @@ main(int argc, char *argv[]) { char fmtopt[6] = "-faa"; /* default output is ASCII */ char *xrs=NULL, *yrs=NULL, *ldopt=NULL; + char *iropt = NULL; char *sendfn; char sampcntbuf[32], nsbinbuf[32]; FILE *rcfp; @@ -1163,11 +1171,22 @@ main(int argc, char *argv[]) int a, i; /* screen rcontrib options */ progname = argv[0]; - for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) { - int na = 1; /* !! Keep consistent !! */ - switch (argv[a][1]) { + for (a = 1; a < argc-2; a++) { + int na; + /* check for argument expansion */ + while ((na = expandarg(&argc, &argv, a)) > 0) + ; + if (na < 0) { + fprintf(stderr, "%s: cannot expand '%s'\n", + progname, argv[a]); + return(1); + } + if (argv[a][0] != '-' || !argv[a][1]) + break; + na = 1; + switch (argv[a][1]) { /* !! Keep consistent !! */ case 'v': /* verbose mode */ - verbose = !verbose; + verbose = 1; na = 0; continue; case 'f': /* special case for -fo, -ff, etc. */ @@ -1180,9 +1199,9 @@ main(int argc, char *argv[]) case 'f': case 'd': case 'c': - if (!(fmtopt[4] = argv[a][3])) - fmtopt[4] = argv[a][2]; - fmtopt[3] = argv[a][2]; + if (!(fmtopt[3] = argv[a][3])) + fmtopt[3] = argv[a][2]; + fmtopt[2] = argv[a][2]; na = 0; continue; /* will pass later */ default: @@ -1198,15 +1217,20 @@ main(int argc, char *argv[]) na = 0; continue; case 'c': /* number of samples */ - sampcnt = atoi(argv[a+1]); + sampcnt = atoi(argv[++a]); if (sampcnt <= 0) goto userr; na = 0; /* we re-add this later */ continue; - case 'V': /* options without arguments */ - case 'w': - case 'u': + case 'I': /* only for pass-through mode */ case 'i': + iropt = argv[a]; + na = 0; + continue; + case 'w': /* options without arguments */ + if (argv[a][2] != '+') verbose = -1; + case 'V': + case 'u': case 'h': case 'r': break; @@ -1230,14 +1254,17 @@ main(int argc, char *argv[]) if (argv[a][2] != 'v') na = 2; break; case 'a': /* special case */ - na = (argv[a][2] == 'v') ? 4 : 2; + if (argv[a][2] == 'p') { + na = 2; /* photon map [+ bandwidth(s)] */ + if (a < argc-3 && atoi(argv[a+1]) > 0) + na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); + } else + na = (argv[a][2] == 'v') ? 4 : 2; break; case 'm': /* special case */ if (!argv[a][2]) goto userr; na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2; break; - case '\0': /* pass-through mode */ - goto done_opts; default: /* anything else is verbotten */ goto userr; } @@ -1247,13 +1274,16 @@ main(int argc, char *argv[]) while (--na) /* + arguments if any */ rcarg[nrcargs++] = argv[++a]; } -done_opts: if (a > argc-2) goto userr; /* check at end of options */ sendfn = argv[a++]; /* assign sender & receiver inputs */ if (sendfn[0] == '-') { /* user wants pass-through mode? */ if (sendfn[1]) goto userr; sendfn = NULL; + if (iropt) { + CHECKARGC(1); + rcarg[nrcargs++] = iropt; + } if (xrs) { CHECKARGC(2); rcarg[nrcargs++] = "-x"; @@ -1269,8 +1299,13 @@ done_opts: rcarg[nrcargs++] = ldopt; } if (sampcnt <= 0) sampcnt = 1; - } else { /* else FVECT determines input format */ - fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; + } else { /* else in sampling mode */ + if (iropt) { + fputs(progname, stderr); + fputs(": -i, -I supported for pass-through only\n", stderr); + return(1); + } + fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; if (sampcnt <= 0) sampcnt = 10000; } sprintf(sampcntbuf, "%d", sampcnt); @@ -1289,6 +1324,7 @@ done_opts: return(my_exec(rcarg)); /* rcontrib does everything */ } clear_params(&curparams, 0); /* else load sender surface & params */ + curmod[0] = '\0'; if (load_scene(sendfn, add_send_object) < 0) return(1); if ((nsbins = prepare_sampler()) <= 0) @@ -1306,21 +1342,21 @@ done_opts: #ifdef getc_unlocked flockfile(rcfp); #endif - if (verbose) { + if (verbose > 0) { fprintf(stderr, "%s: sampling %d directions", progname, nsbins); if (curparams.nsurfs > 1) - fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs); + fprintf(stderr, " (%d elements)\n", curparams.nsurfs); else fputc('\n', stderr); } for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ if (!(*curparams.sample_basis)(&curparams, i, rcfp)) return(1); - return(pclose(rcfp) == 0); /* all finished! */ + return(pclose(rcfp) < 0); /* all finished! */ userr: if (a < argc-2) fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); - fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n", + fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n", progname); return(1); }