ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.2
Committed: Sun Jun 10 05:25:42 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +12 -6 lines
Log Message:
Tweaks and bug fixes to new code

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.2 static const char RCSid[] = "$Id: rcontrib.c,v 2.1 2012/06/09 07:16:47 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Accumulate ray contributions for a set of materials
6     * Initialization and calculation routines
7     */
8    
9     #include "rcontrib.h"
10     #include "source.h"
11     #include "otypes.h"
12     #include "platform.h"
13    
14 greg 2.2 char *shm_boundary = NULL; /* boundary of shared memory */
15    
16 greg 2.1 CUBE thescene; /* our scene */
17     OBJECT nsceneobjs; /* number of objects in our scene */
18    
19     int dimlist[MAXDIM]; /* sampling dimensions */
20     int ndims = 0; /* number of sampling dimensions */
21     int samplendx = 0; /* index for this sample */
22    
23     static void trace_contrib(RAY *r); /* our trace callback */
24     void (*trace)() = trace_contrib;
25    
26     int do_irrad = 0; /* compute irradiance? */
27    
28     int rand_samp = 0; /* pure Monte Carlo sampling? */
29    
30     double dstrsrc = 0.0; /* square source distribution */
31     double shadthresh = .03; /* shadow threshold */
32     double shadcert = .75; /* shadow certainty */
33     int directrelay = 3; /* number of source relays */
34     int vspretest = 512; /* virtual source pretest density */
35     int directvis = 1; /* sources visible? */
36     double srcsizerat = .2; /* maximum ratio source size/dist. */
37    
38     COLOR cextinction = BLKCOLOR; /* global extinction coefficient */
39     COLOR salbedo = BLKCOLOR; /* global scattering albedo */
40     double seccg = 0.; /* global scattering eccentricity */
41     double ssampdist = 0.; /* scatter sampling distance */
42    
43     double specthresh = .15; /* specular sampling threshold */
44     double specjitter = 1.; /* specular sampling jitter */
45    
46     int backvis = 1; /* back face visibility */
47    
48     int maxdepth = 8; /* maximum recursion depth */
49     double minweight = 5e-4; /* minimum ray weight */
50    
51     char *ambfile = NULL; /* ambient file name */
52     COLOR ambval = BLKCOLOR; /* ambient value */
53     int ambvwt = 0; /* initial weight for ambient value */
54     double ambacc = 0; /* ambient accuracy */
55     int ambres = 256; /* ambient resolution */
56     int ambdiv = 350; /* ambient divisions */
57     int ambssamp = 0; /* ambient super-samples */
58     int ambounce = 1; /* ambient bounces */
59     char *amblist[AMBLLEN+1]; /* ambient include/exclude list */
60     int ambincl = -1; /* include == 1, exclude == 0 */
61    
62     int account; /* current accumulation count */
63     RNUMBER raysleft; /* number of rays left to trace */
64     long waitflush; /* how long until next flush */
65    
66     int lastray = 0; /* last ray number sent */
67     int lastdone = 0; /* last ray output */
68    
69     /* Close output stream and free record */
70     static void
71     closestream(void *p)
72     {
73     STREAMOUT *sop = (STREAMOUT *)p;
74     int status = 0;
75     if (sop->outpipe)
76     status = pclose(sop->ofp);
77     else if (sop->ofp != stdout)
78     status = fclose(sop->ofp);
79     if (status)
80     error(SYSTEM, "error closing output stream");
81     free(p);
82     }
83    
84     LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */
85    
86     #define OF_MODIFIER 01
87     #define OF_BIN 02
88    
89     static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
90    
91     LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */
92    
93     static OBJECT traset[MAXTSET+1]={0}; /* trace include set */
94    
95     /************************** INITIALIZATION ROUTINES ***********************/
96    
97     void
98     tranotify( /* record new modifier */
99     OBJECT obj
100     )
101     {
102     static int hitlimit = 0;
103     OBJREC *o = objptr(obj);
104     int i;
105    
106     if (obj == OVOID) { /* starting over */
107     traset[0] = 0;
108     hitlimit = 0;
109     return;
110     }
111     if (hitlimit || !ismodifier(o->otype))
112     return;
113     for (i = nmods; i-- > 0; )
114     if (!strcmp(o->oname, modname[i])) {
115     if (traset[0] >= MAXTSET) {
116     error(WARNING, "too many same-named modifiers");
117     hitlimit++;
118     return; /* should this be fatal? */
119     }
120     insertelem(traset, obj);
121     break;
122     }
123     }
124    
125    
126     char *
127     formstr( /* return format identifier */
128     int f
129     )
130     {
131     switch (f) {
132     case 'a': return("ascii");
133     case 'f': return("float");
134     case 'd': return("double");
135     case 'c': return(COLRFMT);
136     }
137     return("unknown");
138     }
139    
140    
141     /* Add modifier to our list to track */
142     MODCONT *
143     addmodifier(char *modn, char *outf, char *binv, int bincnt)
144     {
145     LUENT *lep = lu_find(&modconttab,modn);
146     MODCONT *mp;
147     EPNODE *ebinv;
148     int i;
149    
150     if (lep->data != NULL) {
151     sprintf(errmsg, "duplicate modifier '%s'", modn);
152     error(USER, errmsg);
153     }
154     if (nmods >= MAXMODLIST)
155     error(INTERNAL, "too many modifiers");
156     modname[nmods++] = modn; /* XXX assumes static string */
157     lep->key = modn; /* XXX assumes static string */
158     if (binv == NULL)
159     binv = "0"; /* use single bin if unspecified */
160     ebinv = eparse(binv);
161     if (ebinv->type == NUM) { /* check value if constant */
162     bincnt = (int)(evalue(ebinv) + 1.5);
163     if (bincnt != 1) {
164     sprintf(errmsg, "illegal non-zero constant for bin (%s)",
165     binv);
166     error(USER, errmsg);
167     }
168     } else if (bincnt <= 0) {
169     sprintf(errmsg,
170     "unspecified or illegal bin count for modifier '%s'",
171     modn);
172     error(USER, errmsg);
173     }
174     /* initialize results holder */
175     mp = (MODCONT *)malloc(sizeof(MODCONT)+sizeof(DCOLOR)*(bincnt-1));
176     if (mp == NULL)
177     error(SYSTEM, "out of memory in addmodifier");
178     mp->outspec = outf; /* XXX assumes static string */
179     mp->modname = modn; /* XXX assumes static string */
180     mp->binv = ebinv;
181     mp->nbins = bincnt;
182     memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt);
183     /* allocate output streams */
184     for (i = bincnt; i-- > 0; )
185     getostream(mp->outspec, mp->modname, i, 1);
186     lep->data = (char *)mp;
187     return(mp);
188     }
189    
190    
191     /* add modifiers from a file list */
192     void
193     addmodfile(char *fname, char *outf, char *binv, int bincnt)
194     {
195     char *mname[MAXMODLIST];
196     int i;
197     /* find the file & store strings */
198     if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
199     sprintf(errmsg, "cannot find modifier file '%s'", fname);
200     error(SYSTEM, errmsg);
201     }
202     for (i = 0; mname[i]; i++) /* add each one */
203     addmodifier(mname[i], outf, binv, bincnt);
204     }
205    
206    
207     void
208     quit( /* quit program */
209     int code
210     )
211     {
212     if (nchild > 0) /* close children if any */
213     end_children();
214     exit(code);
215     }
216    
217    
218     /* Initialize our process(es) */
219     static void
220     rcinit()
221     {
222     int i;
223    
224     if (nproc > MAXPROCESS)
225     sprintf(errmsg, "too many processes requested -- reducing to %d",
226     nproc = MAXPROCESS);
227 greg 2.2 if (nproc > 1) {
228     preload_objs(); /* preload auxiliary data */
229     /* set shared memory boundary */
230     shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY");
231     }
232 greg 2.1 if ((nproc > 1) & (accumulate <= 0))
233 greg 2.2 put_zero_record(0); /* prime our queue to accumulate */
234 greg 2.1
235     if (recover) { /* recover previous output? */
236     if (accumulate <= 0) {
237     reload_output();
238 greg 2.2 if (nproc > 1)
239 greg 2.1 queue_modifiers();
240     } else
241     recover_output();
242     }
243     if (yres > 0) { /* set up flushing & ray counts */
244     if (xres > 0)
245     raysleft = (RNUMBER)xres*yres;
246     else
247     raysleft = yres;
248     } else
249     raysleft = 0;
250     if ((account = accumulate) > 1)
251     raysleft *= accumulate;
252     waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
253     /* tracing to sources as well */
254     for (i = 0; i < nsources; i++)
255     source[i].sflags |= SFOLLOW;
256    
257     if (nproc == 1 || in_rchild()) /* single process or child */
258     return; /* return to main processing loop */
259    
260     parental_loop(); /* else run controller */
261     quit(0); /* parent musn't return! */
262     }
263    
264     /************************** MAIN CALCULATION PROCESS ***********************/
265    
266     /* Our trace call to sum contributions */
267     static void
268     trace_contrib(RAY *r)
269     {
270     MODCONT *mp;
271     int bn;
272     RREAL contr[3];
273    
274     if (r->ro == NULL || !inset(traset, r->ro->omod))
275     return;
276    
277     mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data;
278    
279     if (mp == NULL)
280     error(CONSISTENCY, "unexpected modifier in trace_contrib()");
281    
282     worldfunc(RCCONTEXT, r); /* get bin number */
283     bn = (int)(evalue(mp->binv) + .5);
284     if ((bn < 0) | (bn >= mp->nbins)) {
285     error(WARNING, "bad bin number (ignored)");
286     return;
287     }
288     raycontrib(contr, r, PRIMARY);
289     if (contrib)
290     multcolor(contr, r->rcol);
291     addcolor(mp->cbin[bn], contr);
292     }
293    
294    
295     static void
296     rayirrad( /* compute irradiance rather than radiance */
297     RAY *r
298     )
299     {
300     r->rot = 1e-5; /* pretend we hit surface */
301     VSUM(r->rop, r->rorg, r->rdir, r->rot);
302     r->ron[0] = -r->rdir[0];
303     r->ron[1] = -r->rdir[1];
304     r->ron[2] = -r->rdir[2];
305     r->rod = 1.0;
306     /* compute result */
307     r->revf = raytrace;
308     (*ofun[Lamb.otype].funp)(&Lamb, r);
309     r->revf = rayirrad;
310     }
311    
312    
313     /* Evaluate ray contributions */
314     static void
315     eval_ray(FVECT org, FVECT dir, double dmax)
316     {
317     RAY thisray;
318     /* set up ray */
319     rayorigin(&thisray, PRIMARY, NULL, NULL);
320     if (imm_irrad) {
321     VSUM(thisray.rorg, org, dir, 1.1e-4);
322     thisray.rdir[0] = -dir[0];
323     thisray.rdir[1] = -dir[1];
324     thisray.rdir[2] = -dir[2];
325     thisray.rmax = 0.0;
326     thisray.revf = rayirrad;
327     } else {
328     VCOPY(thisray.rorg, org);
329     VCOPY(thisray.rdir, dir);
330     thisray.rmax = dmax;
331     }
332     samplendx++; /* call ray evaluation */
333     rayvalue(&thisray);
334     }
335    
336    
337     /* Accumulate and/or output ray contributions (child or only process) */
338     static void
339     done_contrib()
340     {
341     MODCONT *mp;
342     int i;
343    
344     if (account <= 0 || --account)
345     return; /* not time yet */
346    
347     for (i = 0; i < nmods; i++) { /* output records & clear */
348     mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
349     mod_output(mp);
350     memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
351     }
352     end_record(); /* end lines & flush if time */
353    
354     account = accumulate; /* reset accumulation counter */
355     }
356    
357    
358     /* Principal calculation loop (called by main) */
359     void
360     rcontrib()
361     {
362     static int ignore_warning_given = 0;
363     FVECT orig, direc;
364     double d;
365 greg 2.2 /* initialize (& fork more of us) */
366 greg 2.1 rcinit();
367     /* load rays from stdin & process */
368     #ifdef getc_unlocked
369 greg 2.2 flockfile(stdin); /* avoid mutex overhead */
370 greg 2.1 #endif
371     while (getvec(orig) == 0 && getvec(direc) == 0) {
372     d = normalize(direc);
373     if ((d == 0.0) & (accumulate != 1)) {
374     if (!ignore_warning_given++)
375     error(WARNING,
376     "dummy ray(s) ignored during accumulation\n");
377     continue;
378     }
379     if (lastray+1 < lastray)
380     lastray = lastdone = 0;
381     ++lastray;
382     if (d == 0.0) { /* zero ==> flush */
383     if ((yres <= 0) | (xres <= 0))
384     waitflush = 1; /* flush right after */
385     } else { /* else compute */
386     eval_ray(orig, direc, lim_dist ? d : 0.0);
387     }
388     done_contrib(); /* accumulate/output */
389     ++lastdone;
390     if (raysleft && !--raysleft)
391     break; /* preemptive EOI */
392     }
393     if (accumulate <= 0 || account < accumulate) {
394     if (account < accumulate) {
395     error(WARNING, "partial accumulation in final record");
396     accumulate -= account;
397     }
398     account = 1; /* output accumulated totals */
399     done_contrib();
400     }
401     if (raysleft)
402     error(USER, "unexpected EOF on input");
403     lu_done(&ofiletab); /* close output files */
404     }