ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.7
Committed: Fri Jun 15 21:32:11 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +25 -30 lines
Log Message:
Simplified immediate irradiance call

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.7 static const char RCSid[] = "$Id: rcontrib.c,v 2.6 2012/06/15 00:57:40 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 greg 2.3 int rand_samp = 1; /* pure Monte Carlo sampling? */
29 greg 2.1
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 greg 2.3 int maxdepth = -10; /* maximum recursion depth */
49     double minweight = 2e-3; /* minimum ray weight */
50 greg 2.1
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 greg 2.4 RNUMBER lastray = 0; /* last ray number sent */
67     RNUMBER lastdone = 0; /* last ray output */
68 greg 2.1
69     static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
70    
71     LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */
72    
73     /************************** INITIALIZATION ROUTINES ***********************/
74    
75     char *
76     formstr( /* return format identifier */
77     int f
78     )
79     {
80     switch (f) {
81     case 'a': return("ascii");
82     case 'f': return("float");
83     case 'd': return("double");
84     case 'c': return(COLRFMT);
85     }
86     return("unknown");
87     }
88    
89    
90     /* Add modifier to our list to track */
91     MODCONT *
92     addmodifier(char *modn, char *outf, char *binv, int bincnt)
93     {
94     LUENT *lep = lu_find(&modconttab,modn);
95     MODCONT *mp;
96     EPNODE *ebinv;
97     int i;
98    
99     if (lep->data != NULL) {
100     sprintf(errmsg, "duplicate modifier '%s'", modn);
101     error(USER, errmsg);
102     }
103     if (nmods >= MAXMODLIST)
104     error(INTERNAL, "too many modifiers");
105     modname[nmods++] = modn; /* XXX assumes static string */
106     lep->key = modn; /* XXX assumes static string */
107     if (binv == NULL)
108     binv = "0"; /* use single bin if unspecified */
109     ebinv = eparse(binv);
110     if (ebinv->type == NUM) { /* check value if constant */
111     bincnt = (int)(evalue(ebinv) + 1.5);
112     if (bincnt != 1) {
113     sprintf(errmsg, "illegal non-zero constant for bin (%s)",
114     binv);
115     error(USER, errmsg);
116     }
117     } else if (bincnt <= 0) {
118     sprintf(errmsg,
119     "unspecified or illegal bin count for modifier '%s'",
120     modn);
121     error(USER, errmsg);
122     }
123     /* initialize results holder */
124     mp = (MODCONT *)malloc(sizeof(MODCONT)+sizeof(DCOLOR)*(bincnt-1));
125     if (mp == NULL)
126     error(SYSTEM, "out of memory in addmodifier");
127     mp->outspec = outf; /* XXX assumes static string */
128     mp->modname = modn; /* XXX assumes static string */
129     mp->binv = ebinv;
130     mp->nbins = bincnt;
131     memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt);
132     /* allocate output streams */
133     for (i = bincnt; i-- > 0; )
134     getostream(mp->outspec, mp->modname, i, 1);
135     lep->data = (char *)mp;
136     return(mp);
137     }
138    
139    
140     /* add modifiers from a file list */
141     void
142     addmodfile(char *fname, char *outf, char *binv, int bincnt)
143     {
144     char *mname[MAXMODLIST];
145     int i;
146     /* find the file & store strings */
147     if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
148     sprintf(errmsg, "cannot find modifier file '%s'", fname);
149     error(SYSTEM, errmsg);
150     }
151     for (i = 0; mname[i]; i++) /* add each one */
152     addmodifier(mname[i], outf, binv, bincnt);
153     }
154    
155    
156     void
157     quit( /* quit program */
158     int code
159     )
160     {
161     if (nchild > 0) /* close children if any */
162     end_children();
163     exit(code);
164     }
165    
166    
167     /* Initialize our process(es) */
168     static void
169     rcinit()
170     {
171     int i;
172    
173     if (nproc > MAXPROCESS)
174     sprintf(errmsg, "too many processes requested -- reducing to %d",
175     nproc = MAXPROCESS);
176 greg 2.2 if (nproc > 1) {
177     preload_objs(); /* preload auxiliary data */
178     /* set shared memory boundary */
179     shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY");
180     }
181 greg 2.1 if ((nproc > 1) & (accumulate <= 0))
182 greg 2.2 put_zero_record(0); /* prime our queue to accumulate */
183 greg 2.1
184     if (yres > 0) { /* set up flushing & ray counts */
185     if (xres > 0)
186     raysleft = (RNUMBER)xres*yres;
187     else
188     raysleft = yres;
189     } else
190     raysleft = 0;
191     if ((account = accumulate) > 1)
192     raysleft *= accumulate;
193     waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
194     /* tracing to sources as well */
195     for (i = 0; i < nsources; i++)
196     source[i].sflags |= SFOLLOW;
197    
198 greg 2.5 if (nproc > 1 && in_rchild()) /* forked child? */
199 greg 2.1 return; /* return to main processing loop */
200    
201 greg 2.5 if (recover) { /* recover previous output? */
202     if (accumulate <= 0) {
203     reload_output();
204     if (nproc > 1)
205     queue_modifiers();
206     } else
207     recover_output();
208     }
209     if (nproc == 1) /* single process? */
210     return;
211    
212 greg 2.1 parental_loop(); /* else run controller */
213     quit(0); /* parent musn't return! */
214     }
215    
216     /************************** MAIN CALCULATION PROCESS ***********************/
217    
218     /* Our trace call to sum contributions */
219     static void
220     trace_contrib(RAY *r)
221     {
222     MODCONT *mp;
223     int bn;
224     RREAL contr[3];
225    
226 greg 2.6 if (r->ro == NULL || r->ro->omod == OVOID)
227 greg 2.1 return;
228    
229     mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data;
230    
231 greg 2.6 if (mp == NULL) /* not in our list? */
232     return;
233 greg 2.1
234     worldfunc(RCCONTEXT, r); /* get bin number */
235     bn = (int)(evalue(mp->binv) + .5);
236     if ((bn < 0) | (bn >= mp->nbins)) {
237     error(WARNING, "bad bin number (ignored)");
238     return;
239     }
240     raycontrib(contr, r, PRIMARY);
241     if (contrib)
242     multcolor(contr, r->rcol);
243     addcolor(mp->cbin[bn], contr);
244     }
245    
246    
247 greg 2.7 /* Evaluate irradiance contributions */
248 greg 2.1 static void
249 greg 2.7 eval_irrad(FVECT org, FVECT dir)
250 greg 2.1 {
251 greg 2.7 RAY thisray;
252    
253     VSUM(thisray.rorg, org, dir, 1.1e-4);
254     thisray.rdir[0] = -dir[0];
255     thisray.rdir[1] = -dir[1];
256     thisray.rdir[2] = -dir[2];
257     thisray.rmax = 0.0;
258     rayorigin(&thisray, PRIMARY, NULL, NULL);
259     thisray.rot = 1e-5; /* pretend we hit surface */
260     thisray.rod = 1.0;
261     VSUM(thisray.rop, org, dir, 1e-4);
262     samplendx++; /* compute result */
263     (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
264 greg 2.1 }
265    
266    
267 greg 2.7 /* Evaluate radiance contributions */
268 greg 2.1 static void
269 greg 2.7 eval_rad(FVECT org, FVECT dir, double dmax)
270 greg 2.1 {
271     RAY thisray;
272     /* set up ray */
273 greg 2.7 VCOPY(thisray.rorg, org);
274     VCOPY(thisray.rdir, dir);
275     thisray.rmax = dmax;
276 greg 2.1 rayorigin(&thisray, PRIMARY, NULL, NULL);
277     samplendx++; /* call ray evaluation */
278     rayvalue(&thisray);
279     }
280    
281    
282     /* Accumulate and/or output ray contributions (child or only process) */
283     static void
284     done_contrib()
285     {
286     MODCONT *mp;
287     int i;
288    
289     if (account <= 0 || --account)
290     return; /* not time yet */
291    
292     for (i = 0; i < nmods; i++) { /* output records & clear */
293     mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
294     mod_output(mp);
295     memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
296     }
297     end_record(); /* end lines & flush if time */
298    
299     account = accumulate; /* reset accumulation counter */
300     }
301    
302    
303     /* Principal calculation loop (called by main) */
304     void
305     rcontrib()
306     {
307     static int ignore_warning_given = 0;
308     FVECT orig, direc;
309     double d;
310 greg 2.2 /* initialize (& fork more of us) */
311 greg 2.1 rcinit();
312     /* load rays from stdin & process */
313     #ifdef getc_unlocked
314 greg 2.2 flockfile(stdin); /* avoid mutex overhead */
315 greg 2.1 #endif
316     while (getvec(orig) == 0 && getvec(direc) == 0) {
317     d = normalize(direc);
318 greg 2.4 if (nchild != -1 && (d == 0.0) & (accumulate != 1)) {
319 greg 2.1 if (!ignore_warning_given++)
320     error(WARNING,
321     "dummy ray(s) ignored during accumulation\n");
322     continue;
323     }
324     if (lastray+1 < lastray)
325     lastray = lastdone = 0;
326     ++lastray;
327     if (d == 0.0) { /* zero ==> flush */
328     if ((yres <= 0) | (xres <= 0))
329     waitflush = 1; /* flush right after */
330 greg 2.4 account = 1;
331 greg 2.7 } else if (imm_irrad) { /* else compute */
332     eval_irrad(orig, direc);
333     } else {
334     eval_rad(orig, direc, lim_dist ? d : 0.0);
335 greg 2.1 }
336     done_contrib(); /* accumulate/output */
337     ++lastdone;
338     if (raysleft && !--raysleft)
339     break; /* preemptive EOI */
340     }
341 greg 2.4 if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) {
342 greg 2.1 if (account < accumulate) {
343     error(WARNING, "partial accumulation in final record");
344     accumulate -= account;
345     }
346     account = 1; /* output accumulated totals */
347     done_contrib();
348     }
349     if (raysleft)
350     error(USER, "unexpected EOF on input");
351     lu_done(&ofiletab); /* close output files */
352     }