--- ray/src/rt/rcontrib.c 2012/06/11 05:07:55 2.3 +++ ray/src/rt/rcontrib.c 2012/11/15 15:26:52 2.17 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcontrib.c,v 2.3 2012/06/11 05:07:55 greg Exp $"; +static const char RCSid[] = "$Id: rcontrib.c,v 2.17 2012/11/15 15:26:52 greg Exp $"; #endif /* * Accumulate ray contributions for a set of materials @@ -7,9 +7,8 @@ static const char RCSid[] = "$Id: rcontrib.c,v 2.3 201 */ #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 +26,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 */ @@ -63,46 +62,15 @@ int account; /* current accumulation count */ RNUMBER raysleft; /* number of rays left to trace */ long waitflush; /* how long until next flush */ -int lastray = 0; /* last ray number sent */ -int lastdone = 0; /* last ray output */ +RNUMBER lastray = 0; /* last ray number sent */ +RNUMBER lastdone = 0; /* last ray output */ static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ -static OBJECT traset[MAXTSET+1]={0}; /* trace include set */ - /************************** INITIALIZATION ROUTINES ***********************/ -void -tranotify( /* record new modifier */ - OBJECT obj -) -{ - static int hitlimit = 0; - OBJREC *o = objptr(obj); - int i; - - if (obj == OVOID) { /* starting over */ - traset[0] = 0; - hitlimit = 0; - return; - } - if (hitlimit || !ismodifier(o->otype)) - return; - for (i = nmods; i-- > 0; ) - if (!strcmp(o->oname, modname[i])) { - if (traset[0] >= MAXTSET) { - error(WARNING, "too many same-named modifiers"); - hitlimit++; - return; /* should this be fatal? */ - } - insertelem(traset, obj); - break; - } -} - - char * formstr( /* return format identifier */ int f @@ -168,7 +136,7 @@ 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) { @@ -190,7 +158,7 @@ quit( /* quit program */ ) { if (nchild > 0) /* close children if any */ - end_children(); + end_children(code != 0); exit(code); } @@ -209,17 +177,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 */ - - if (recover) { /* recover previous output? */ - if (accumulate <= 0) { - reload_output(); - if (nproc > 1) - queue_modifiers(); - } else - recover_output(); - } + 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; @@ -230,14 +189,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()) /* single process or child */ + if (nproc > 1 && in_rchild()) /* forked child? */ return; /* return to main processing loop */ - parental_loop(); /* else run controller */ + if (recover) { /* recover previous output? */ + if (accumulate <= 0) + reload_output(); + else + recover_output(); + } + if (nproc == 1) /* single process? */ + return; + /* else run appropriate controller */ + if (accumulate <= 0) + feeder_loop(); + else + parental_loop(); quit(0); /* parent musn't return! */ } @@ -251,64 +219,62 @@ trace_contrib(RAY *r) int bn; RREAL contr[3]; - if (r->ro == NULL || !inset(traset, r->ro->omod)) + if (r->ro == NULL || r->ro->omod == OVOID) return; mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; - if (mp == NULL) - error(CONSISTENCY, "unexpected modifier in trace_contrib()"); + if (mp == NULL) /* not in our list? */ + return; + /* shadow ray not on source? */ + if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) + return; - worldfunc(RCCONTEXT, r); /* get bin number */ + worldfunc(RCCONTEXT, r); /* else get bin number */ bn = (int)(evalue(mp->binv) + .5); if ((bn < 0) | (bn >= 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); } +/* 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.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); } -/* 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); } @@ -350,7 +316,7 @@ rcontrib() #endif while (getvec(orig) == 0 && getvec(direc) == 0) { d = normalize(direc); - if ((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"); @@ -361,16 +327,20 @@ rcontrib() ++lastray; if (d == 0.0) { /* zero ==> flush */ if ((yres <= 0) | (xres <= 0)) - waitflush = 1; /* flush right after */ - } else { /* else compute */ - eval_ray(orig, direc, lim_dist ? d : 0.0); + 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) break; /* preemptive EOI */ } - if (accumulate <= 0 || account < accumulate) { + if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { if (account < accumulate) { error(WARNING, "partial accumulation in final record"); accumulate -= account; @@ -378,7 +348,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 */ }