--- ray/src/rt/rcontrib.c 2014/07/19 18:19:33 2.20 +++ ray/src/rt/rcontrib.c 2020/09/09 21:28:19 2.37 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcontrib.c,v 2.20 2014/07/19 18:19:33 greg Exp $"; +static const char RCSid[] = "$Id: rcontrib.c,v 2.37 2020/09/09 21:28:19 greg Exp $"; #endif /* * Accumulate ray contributions for a set of materials @@ -8,7 +8,6 @@ static const char RCSid[] = "$Id: rcontrib.c,v 2.20 20 #include "copyright.h" -#include #include "rcontrib.h" #include "otypes.h" #include "source.h" @@ -22,15 +21,14 @@ int dimlist[MAXDIM]; /* sampling dimensions */ int ndims = 0; /* number of sampling dimensions */ int samplendx = 0; /* index for this sample */ -static void trace_contrib(RAY *r); /* our trace callback */ -void (*trace)() = trace_contrib; +void (*trace)() = NULL; /* trace call (NULL before rcinit) */ int do_irrad = 0; /* compute irradiance? */ int rand_samp = 1; /* pure Monte Carlo sampling? */ double dstrsrc = 0.9; /* square source distribution */ -double shadthresh = .03; /* shadow threshold */ +double shadthresh = 0.; /* shadow threshold */ double shadcert = .75; /* shadow certainty */ int directrelay = 3; /* number of source relays */ int vspretest = 512; /* virtual source pretest density */ @@ -42,7 +40,7 @@ COLOR salbedo = BLKCOLOR; /* global scattering albedo double seccg = 0.; /* global scattering eccentricity */ double ssampdist = 0.; /* scatter sampling distance */ -double specthresh = .15; /* specular sampling threshold */ +double specthresh = .02; /* specular sampling threshold */ double specjitter = 1.; /* specular sampling jitter */ int backvis = 1; /* back face visibility */ @@ -53,7 +51,7 @@ double minweight = 2e-3; /* minimum ray weight */ char *ambfile = NULL; /* ambient file name */ COLOR ambval = BLKCOLOR; /* ambient value */ int ambvwt = 0; /* initial weight for ambient value */ -double ambacc = 0; /* ambient accuracy */ +double ambacc = 0.; /* ambient accuracy */ int ambres = 256; /* ambient resolution */ int ambdiv = 350; /* ambient divisions */ int ambssamp = 0; /* ambient super-samples */ @@ -68,6 +66,8 @@ long waitflush; /* how long until next flush */ RNUMBER lastray = 0; /* last ray number sent */ RNUMBER lastdone = 0; /* last ray output */ +static void trace_contrib(RAY *r); /* our trace callback */ + static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ @@ -102,8 +102,14 @@ addmodifier(char *modn, char *outf, char *prms, char * sprintf(errmsg, "duplicate modifier '%s'", modn); error(USER, errmsg); } - if (nmods >= MAXMODLIST) - error(INTERNAL, "too many modifiers"); + if (nmods >= MAXMODLIST) { + sprintf(errmsg, "too many modifiers (%d limit)", MAXMODLIST); + error(INTERNAL, errmsg); + } + if (!strcmp(modn, VOIDID)) { + sprintf(errmsg, "cannot track '%s' modifier", VOIDID); + error(USER, errmsg); + } modname[nmods++] = modn; /* XXX assumes static string */ lep->key = modn; /* XXX assumes static string */ if (binv == NULL) @@ -128,13 +134,17 @@ addmodifier(char *modn, char *outf, char *prms, char * 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->params = prms; /* XXX assumes static string */ mp->binv = ebinv; + mp->bin0 = 0; mp->nbins = bincnt; memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); - /* allocate output streams */ - for (i = bincnt; i-- > 0; ) - getostream(mp->outspec, mp->modname, i, 1); + /* figure out starting bin */ + while (!getostream(mp->outspec, mp->modname, mp->bin0, 1)) + mp->bin0++; + /* allocate other output streams */ + for (i = 0; ++i < mp->nbins; ) + getostream(mp->outspec, mp->modname, mp->bin0+i, 1); lep->data = (char *)mp; return(mp); } @@ -147,29 +157,64 @@ addmodfile(char *fname, char *outf, char *prms, char * char *mname[MAXMODLIST]; int i; /* find the file & store strings */ - if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { + i = wordfile(mname, MAXMODLIST, getpath(fname, getrlibpath(), R_OK)); + if (i < 0) { sprintf(errmsg, "cannot find modifier file '%s'", fname); error(SYSTEM, errmsg); } + if (i >= MAXMODLIST-1) { + sprintf(errmsg, "too many modifiers (%d limit) in file '%s'", + MAXMODLIST-1, fname); + error(INTERNAL, errmsg); + } for (i = 0; mname[i]; i++) /* add each one */ addmodifier(mname[i], outf, prms, binv, bincnt); } +/* Check if we have any more rays left (and report progress) */ +int +morays(void) +{ + static RNUMBER total_rays; + static time_t tstart, last_report; + time_t tnow; + + if (!raysleft) + return(1); /* unknown total, so nothing to do or say */ + + if (report_intvl > 0 && (tnow = time(0)) >= last_report+report_intvl) { + if (!total_rays) { + total_rays = raysleft; + tstart = tnow; + } + sprintf(errmsg, "%.2f%% done after %.3f hours", + 100.-100.*raysleft/total_rays, + (1./3600.)*(tnow - tstart)); + eputs(errmsg); + last_report = tnow; + } + return(--raysleft); +} + + +/* Quit program */ void -quit( /* quit program */ +quit( int code ) { if (nchild > 0) /* close children if any */ end_children(code != 0); + else if (nchild < 0) + _exit(code); /* avoid flush() in child */ exit(code); } /* Initialize our process(es) */ static void -rcinit() +rcinit(void) { int i; @@ -181,6 +226,7 @@ rcinit() /* set shared memory boundary */ shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); } + trace = trace_contrib; /* set up trace call-back */ for (i = 0; i < nsources; i++) /* tracing to sources as well */ source[i].sflags |= SFOLLOW; if (yres > 0) { /* set up flushing & ray counts */ @@ -215,49 +261,10 @@ rcinit() /************************** 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; @@ -274,13 +281,13 @@ trace_contrib(RAY *r) if (mp == NULL) /* not in our list? */ return; - worldfunc(RCCONTEXT, r); /* else get bin number */ - if (mp->params != last_params) - set_eparams(last_params = (char *)mp->params); - if ((bval = evalue(mp->binv)) <= -.5) - return; /* silently ignore */ + worldfunc(RCCONTEXT, r); /* else set context */ + set_eparams((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)"); + sprintf(errmsg, "bad bin number (%d ignored)", bn); + error(WARNING, errmsg); return; } raycontrib(contr, r, PRIMARY); /* compute coefficient */ @@ -303,7 +310,7 @@ eval_irrad(FVECT org, FVECT dir) thisray.rmax = 0.0; rayorigin(&thisray, PRIMARY, NULL, NULL); /* pretend we hit surface */ - thisray.rt = thisray.rot = 1e-5; + thisray.rxt = thisray.rot = 1e-5; thisray.rod = 1.0; VCOPY(thisray.ron, dir); VSUM(thisray.rop, org, dir, 1e-4); @@ -329,7 +336,7 @@ eval_rad(FVECT org, FVECT dir, double dmax) /* Accumulate and/or output ray contributions (child or only process) */ static void -done_contrib() +done_contrib(void) { MODCONT *mp; int i; @@ -350,7 +357,7 @@ done_contrib() /* Principal calculation loop (called by main) */ void -rcontrib() +rcontrib(void) { static int ignore_warning_given = 0; FVECT orig, direc; @@ -384,7 +391,7 @@ rcontrib() } done_contrib(); /* accumulate/output */ ++lastdone; - if (raysleft && !--raysleft) + if (!morays()) break; /* preemptive EOI */ } if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) {