--- ray/src/rt/rcontrib.c 2012/06/15 00:57:40 2.6 +++ ray/src/rt/rcontrib.c 2024/02/23 03:45:52 2.44 @@ -1,15 +1,16 @@ #ifndef lint -static const char RCSid[] = "$Id: rcontrib.c,v 2.6 2012/06/15 00:57:40 greg Exp $"; +static const char RCSid[] = "$Id: rcontrib.c,v 2.44 2024/02/23 03:45:52 greg Exp $"; #endif /* * Accumulate ray contributions for a set of materials * Initialization and calculation routines */ +#include "copyright.h" + #include "rcontrib.h" -#include "source.h" #include "otypes.h" -#include "platform.h" +#include "source.h" char *shm_boundary = NULL; /* boundary of shared memory */ @@ -20,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.0; /* square source distribution */ -double shadthresh = .03; /* shadow threshold */ +double dstrsrc = 0.9; /* square source distribution */ +double shadthresh = 0.; /* shadow threshold */ double shadcert = .75; /* shadow certainty */ int directrelay = 3; /* number of source relays */ int vspretest = 512; /* virtual source pretest density */ @@ -40,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 */ @@ -51,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 */ @@ -66,13 +66,15 @@ long waitflush; /* how long until next flush */ RNUMBER lastray = 0; /* last ray number sent */ RNUMBER lastdone = 0; /* last ray output */ -static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } +static void trace_contrib(RAY *r); /* our trace callback */ +static void mcfree(void *p) { epfree((*(MODCONT *)p).binv,1); free(p); } + LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ /************************** INITIALIZATION ROUTINES ***********************/ -char * +const char * formstr( /* return format identifier */ int f ) @@ -81,7 +83,7 @@ formstr( /* return format identifier */ case 'a': return("ascii"); case 'f': return("float"); case 'd': return("double"); - case 'c': return(COLRFMT); + case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT); } return("unknown"); } @@ -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; @@ -100,8 +102,19 @@ addmodifier(char *modn, char *outf, char *binv, int bi sprintf(errmsg, "duplicate modifier '%s'", modn); error(USER, errmsg); } - if (nmods >= MAXMODLIST) - error(INTERNAL, "too many modifiers"); + if (!strcmp(modn, VOIDID)) { + sprintf(errmsg, "cannot track '%s' modifier", VOIDID); + error(USER, errmsg); + } + if (nmods >= modasiz) { /* need bigger modifier array */ + modasiz += modasiz/2 + 64; + if (modname == NULL) + modname = (char **)malloc(modasiz*sizeof(char *)); + else + modname = (char **)realloc(modname, modasiz*sizeof(char *)); + if (modname == NULL) + error(SYSTEM, "Out of memory in addmodifier()"); + } modname[nmods++] = modn; /* XXX assumes static string */ lep->key = modn; /* XXX assumes static string */ if (binv == NULL) @@ -121,52 +134,92 @@ addmodifier(char *modn, char *outf, char *binv, int bi error(USER, errmsg); } /* initialize results holder */ - mp = (MODCONT *)malloc(sizeof(MODCONT)+sizeof(DCOLOR)*(bincnt-1)); + mp = (MODCONT *)malloc(mcsize(bincnt)); if (mp == NULL) error(SYSTEM, "out of memory in addmodifier"); mp->outspec = outf; /* XXX assumes static string */ mp->modname = modn; /* XXX assumes static string */ + 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); + memset(mp->cbin, 0, DCOLORSIZ*bincnt); + /* 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); } -/* 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; - /* find the file & store strings */ - if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { - sprintf(errmsg, "cannot find modifier file '%s'", fname); + char *path = getpath(fname, getrlibpath(), R_OK); + char mod[MAXSTR]; + FILE *fp; + + if (path == NULL || (fp = fopen(path, "r")) == NULL) { + if (path == NULL) + sprintf(errmsg, "cannot find modifier file '%s'", fname); + else + sprintf(errmsg, "cannot load modifier file '%s'", path); error(SYSTEM, errmsg); } - for (i = 0; mname[i]; i++) /* add each one */ - addmodifier(mname[i], outf, binv, bincnt); + while (fgetword(mod, sizeof(mod), fp) != NULL) + addmodifier(savqstr(mod), outf, prms, binv, bincnt); + fclose(fp); } +/* 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; + } else { + sprintf(errmsg, "%.2f%% done after %.3f hours\n", + 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(); + 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; @@ -178,9 +231,9 @@ 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 */ - + 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 */ if (xres > 0) raysleft = (RNUMBER)xres*yres; @@ -191,25 +244,23 @@ 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! */ } @@ -220,67 +271,73 @@ static void trace_contrib(RAY *r) { MODCONT *mp; + double bval; int bn; - RREAL contr[3]; + SCOLOR contr; + DCOLORV *dvp; + int i; 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)) { - error(WARNING, "bad bin number (ignored)"); + 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) { + sprintf(errmsg, "bad bin number (%d ignored)", bn); + error(WARNING, errmsg); return; } - raycontrib(contr, r, PRIMARY); + raycontrib(contr, r, PRIMARY); /* compute coefficient */ if (contrib) - multcolor(contr, r->rcol); - addcolor(mp->cbin[bn], contr); + smultscolor(contr, r->rcol); /* -> contribution */ + dvp = mcbin(mp, bn); + for (i = 0; i < NCSAMP; i++) /* add it in */ + *dvp++ += contr[i]; } +/* Evaluate irradiance contributions */ static void -rayirrad( /* compute irradiance rather than radiance */ - RAY *r -) +eval_irrad(FVECT org, FVECT dir) { - r->rot = 1e-5; /* pretend we hit surface */ - VSUM(r->rop, r->rorg, r->rdir, r->rot); - r->ron[0] = -r->rdir[0]; - r->ron[1] = -r->rdir[1]; - r->ron[2] = -r->rdir[2]; - r->rod = 1.0; - /* compute result */ - r->revf = raytrace; - (*ofun[Lamb.otype].funp)(&Lamb, r); - r->revf = rayirrad; + RAY thisray; + + VSUM(thisray.rorg, org, dir, 1.1e-4); + thisray.rdir[0] = -dir[0]; + thisray.rdir[1] = -dir[1]; + thisray.rdir[2] = -dir[2]; + thisray.rmax = 0.0; + rayorigin(&thisray, PRIMARY, NULL, NULL); + /* pretend we hit surface */ + thisray.rxt = 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); } -/* Evaluate ray contributions */ +/* Evaluate radiance contributions */ static void -eval_ray(FVECT org, FVECT dir, double dmax) +eval_rad(FVECT org, FVECT dir, double dmax) { RAY thisray; /* set up ray */ + VCOPY(thisray.rorg, org); + VCOPY(thisray.rdir, dir); + thisray.rmax = dmax; rayorigin(&thisray, PRIMARY, NULL, NULL); - if (imm_irrad) { - VSUM(thisray.rorg, org, dir, 1.1e-4); - thisray.rdir[0] = -dir[0]; - thisray.rdir[1] = -dir[1]; - thisray.rdir[2] = -dir[2]; - thisray.rmax = 0.0; - thisray.revf = rayirrad; - } else { - VCOPY(thisray.rorg, org); - VCOPY(thisray.rdir, dir); - thisray.rmax = dmax; - } samplendx++; /* call ray evaluation */ rayvalue(&thisray); } @@ -288,7 +345,7 @@ eval_ray(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; @@ -299,7 +356,7 @@ done_contrib() for (i = 0; i < nmods; i++) { /* output records & clear */ mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; mod_output(mp); - memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); + memset(mp->cbin, 0, DCOLORSIZ*mp->nbins); } end_record(); /* end lines & flush if time */ @@ -309,7 +366,7 @@ done_contrib() /* Principal calculation loop (called by main) */ void -rcontrib() +rcontrib(void) { static int ignore_warning_given = 0; FVECT orig, direc; @@ -322,7 +379,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"); @@ -332,15 +389,18 @@ rcontrib() lastray = lastdone = 0; ++lastray; if (d == 0.0) { /* zero ==> flush */ - if ((yres <= 0) | (xres <= 0)) - waitflush = 1; /* flush right after */ - account = 1; - } else { /* else compute */ - eval_ray(orig, direc, lim_dist ? d : 0.0); + 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 { + eval_rad(orig, direc, lim_dist ? d : 0.0); } done_contrib(); /* accumulate/output */ ++lastdone; - if (raysleft && !--raysleft) + if (!morays()) break; /* preemptive EOI */ } if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { @@ -351,7 +411,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 */ }