--- ray/src/rt/rcontrib.c 2012/06/15 21:32:11 2.7 +++ ray/src/rt/rcontrib.c 2014/07/19 18:39:35 2.21 @@ -1,15 +1,17 @@ #ifndef lint -static const char RCSid[] = "$Id: rcontrib.c,v 2.7 2012/06/15 21:32:11 greg Exp $"; +static const char RCSid[] = "$Id: rcontrib.c,v 2.21 2014/07/19 18:39:35 greg Exp $"; #endif /* * Accumulate ray contributions for a set of materials * Initialization and calculation routines */ +#include "copyright.h" + +#include #include "rcontrib.h" -#include "source.h" #include "otypes.h" -#include "platform.h" +#include "source.h" char *shm_boundary = NULL; /* boundary of shared memory */ @@ -27,7 +29,7 @@ int do_irrad = 0; /* compute irradiance? */ int rand_samp = 1; /* pure Monte Carlo sampling? */ -double dstrsrc = 0.0; /* square source distribution */ +double dstrsrc = 0.9; /* square source distribution */ double shadthresh = .03; /* shadow threshold */ double shadcert = .75; /* shadow certainty */ int directrelay = 3; /* number of source relays */ @@ -89,7 +91,7 @@ formstr( /* return format identifier */ /* Add modifier to our list to track */ MODCONT * -addmodifier(char *modn, char *outf, char *binv, int bincnt) +addmodifier(char *modn, char *outf, char *prms, char *binv, int bincnt) { LUENT *lep = lu_find(&modconttab,modn); MODCONT *mp; @@ -126,6 +128,7 @@ addmodifier(char *modn, char *outf, char *binv, int bi error(SYSTEM, "out of memory in addmodifier"); mp->outspec = outf; /* XXX assumes static string */ mp->modname = modn; /* XXX assumes static string */ + mp->params = prms; mp->binv = ebinv; mp->nbins = bincnt; memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); @@ -137,9 +140,9 @@ addmodifier(char *modn, char *outf, char *binv, int bi } -/* add modifiers from a file list */ +/* Add modifiers from a file list */ void -addmodfile(char *fname, char *outf, char *binv, int bincnt) +addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) { char *mname[MAXMODLIST]; int i; @@ -149,7 +152,7 @@ addmodfile(char *fname, char *outf, char *binv, int bi error(SYSTEM, errmsg); } for (i = 0; mname[i]; i++) /* add each one */ - addmodifier(mname[i], outf, binv, bincnt); + addmodifier(mname[i], outf, prms, binv, bincnt); } @@ -159,7 +162,7 @@ quit( /* quit program */ ) { if (nchild > 0) /* close children if any */ - end_children(); + end_children(code != 0); exit(code); } @@ -178,9 +181,8 @@ rcinit() /* set shared memory boundary */ shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); } - if ((nproc > 1) & (accumulate <= 0)) - put_zero_record(0); /* prime our queue to accumulate */ - + for (i = 0; i < nsources; i++) /* tracing to sources as well */ + source[i].sflags |= SFOLLOW; if (yres > 0) { /* set up flushing & ray counts */ if (xres > 0) raysleft = (RNUMBER)xres*yres; @@ -191,55 +193,100 @@ rcinit() if ((account = accumulate) > 1) raysleft *= accumulate; waitflush = (yres > 0) & (xres > 1) ? 0 : xres; - /* tracing to sources as well */ - for (i = 0; i < nsources; i++) - source[i].sflags |= SFOLLOW; if (nproc > 1 && in_rchild()) /* forked child? */ return; /* return to main processing loop */ if (recover) { /* recover previous output? */ - if (accumulate <= 0) { + if (accumulate <= 0) reload_output(); - if (nproc > 1) - queue_modifiers(); - } else + else recover_output(); } if (nproc == 1) /* single process? */ return; - - parental_loop(); /* else run controller */ + /* else run appropriate controller */ + if (accumulate <= 0) + feeder_loop(); + else + parental_loop(); quit(0); /* parent musn't return! */ } /************************** MAIN CALCULATION PROCESS ***********************/ +/* Set parameters for current bin evaluation */ +static void +set_eparams(char *prms) +{ + char vname[64]; + double value; + char *cps, *cpd; + /* assign each variable */ + for (cps = prms; *cps; cps++) { + if (isspace(*cps)) + continue; + if (!isalpha(*cps)) + goto bad_params; + cpd = vname; + while (*cps && (*cps != '=') & !isspace(*cps)) { + if (!isid(*cps)) + goto bad_params; + *cpd++ = *cps++; + } + if (cpd == vname) + goto bad_params; + *cpd = '\0'; + while (isspace(*cps)) cps++; + if (*cps++ != '=') + goto bad_params; + value = atof(cps); + if ((cps = fskip(cps)) == NULL) + goto bad_params; + while (isspace(*cps)) cps++; + cps += (*cps == ',') | (*cps == ';'); + varset(vname, '=', value); + } + return; +bad_params: + sprintf(errmsg, "bad parameter list '%s'", prms); + error(USER, errmsg); +} + /* Our trace call to sum contributions */ static void trace_contrib(RAY *r) { + static char *last_params = NULL; MODCONT *mp; + double bval; int bn; RREAL contr[3]; if (r->ro == NULL || r->ro->omod == OVOID) return; + /* shadow ray not on source? */ + if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) + return; mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; if (mp == NULL) /* not in our list? */ return; - worldfunc(RCCONTEXT, r); /* get bin number */ - bn = (int)(evalue(mp->binv) + .5); - if ((bn < 0) | (bn >= mp->nbins)) { + worldfunc(RCCONTEXT, r); /* else set context */ + if ((mp->params != NULL) & (mp->params != last_params) && + (last_params == NULL || strcmp(mp->params, last_params))) + set_eparams(last_params = (char *)mp->params); + if ((bval = evalue(mp->binv)) <= -.5) /* and get bin number */ + return; /* silently ignore negatives */ + if ((bn = (int)(bval + .5)) >= mp->nbins) { error(WARNING, "bad bin number (ignored)"); return; } - raycontrib(contr, r, PRIMARY); + raycontrib(contr, r, PRIMARY); /* compute coefficient */ if (contrib) - multcolor(contr, r->rcol); + multcolor(contr, r->rcol); /* -> contribution */ addcolor(mp->cbin[bn], contr); } @@ -256,8 +303,10 @@ eval_irrad(FVECT org, FVECT dir) thisray.rdir[2] = -dir[2]; thisray.rmax = 0.0; rayorigin(&thisray, PRIMARY, NULL, NULL); - thisray.rot = 1e-5; /* pretend we hit surface */ + /* pretend we hit surface */ + thisray.rt = thisray.rot = 1e-5; thisray.rod = 1.0; + VCOPY(thisray.ron, dir); VSUM(thisray.rop, org, dir, 1e-4); samplendx++; /* compute result */ (*ofun[Lamb.otype].funp)(&Lamb, &thisray); @@ -315,7 +364,7 @@ rcontrib() #endif while (getvec(orig) == 0 && getvec(direc) == 0) { d = normalize(direc); - if (nchild != -1 && (d == 0.0) & (accumulate != 1)) { + if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { if (!ignore_warning_given++) error(WARNING, "dummy ray(s) ignored during accumulation\n"); @@ -325,9 +374,10 @@ rcontrib() lastray = lastdone = 0; ++lastray; if (d == 0.0) { /* zero ==> flush */ - if ((yres <= 0) | (xres <= 0)) - waitflush = 1; /* flush right after */ - account = 1; + if ((yres <= 0) | (xres <= 1)) + waitflush = 1; /* flush after */ + if (nchild == -1) + account = 1; } else if (imm_irrad) { /* else compute */ eval_irrad(orig, direc); } else { @@ -346,7 +396,7 @@ rcontrib() account = 1; /* output accumulated totals */ done_contrib(); } + lu_done(&ofiletab); /* close output files */ if (raysleft) error(USER, "unexpected EOF on input"); - lu_done(&ofiletab); /* close output files */ }