ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.9
Committed: Sat Jun 16 17:30:13 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +2 -2 lines
Log Message:
Minor fixes

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.9 static const char RCSid[] = "$Id: rcontrib.c,v 2.8 2012/06/16 17:09:49 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 greg 2.8 double dstrsrc = 0.9; /* square source distribution */
31 greg 2.1 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 (yres > 0) { /* set up flushing & ray counts */
182     if (xres > 0)
183     raysleft = (RNUMBER)xres*yres;
184     else
185     raysleft = yres;
186     } else
187     raysleft = 0;
188     if ((account = accumulate) > 1)
189     raysleft *= accumulate;
190     waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
191     /* tracing to sources as well */
192     for (i = 0; i < nsources; i++)
193     source[i].sflags |= SFOLLOW;
194    
195 greg 2.5 if (nproc > 1 && in_rchild()) /* forked child? */
196 greg 2.1 return; /* return to main processing loop */
197    
198 greg 2.5 if (recover) { /* recover previous output? */
199 greg 2.8 if (accumulate <= 0)
200 greg 2.5 reload_output();
201 greg 2.8 else
202 greg 2.5 recover_output();
203     }
204     if (nproc == 1) /* single process? */
205     return;
206 greg 2.8 /* else run appropriate controller */
207     if (accumulate <= 0)
208     feeder_loop();
209     else
210     parental_loop();
211 greg 2.1 quit(0); /* parent musn't return! */
212     }
213    
214     /************************** MAIN CALCULATION PROCESS ***********************/
215    
216     /* Our trace call to sum contributions */
217     static void
218     trace_contrib(RAY *r)
219     {
220     MODCONT *mp;
221     int bn;
222     RREAL contr[3];
223    
224 greg 2.6 if (r->ro == NULL || r->ro->omod == OVOID)
225 greg 2.1 return;
226    
227     mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data;
228    
229 greg 2.6 if (mp == NULL) /* not in our list? */
230     return;
231 greg 2.1
232     worldfunc(RCCONTEXT, r); /* get bin number */
233     bn = (int)(evalue(mp->binv) + .5);
234     if ((bn < 0) | (bn >= mp->nbins)) {
235     error(WARNING, "bad bin number (ignored)");
236     return;
237     }
238     raycontrib(contr, r, PRIMARY);
239     if (contrib)
240     multcolor(contr, r->rcol);
241     addcolor(mp->cbin[bn], contr);
242     }
243    
244    
245 greg 2.7 /* Evaluate irradiance contributions */
246 greg 2.1 static void
247 greg 2.7 eval_irrad(FVECT org, FVECT dir)
248 greg 2.1 {
249 greg 2.7 RAY thisray;
250    
251     VSUM(thisray.rorg, org, dir, 1.1e-4);
252     thisray.rdir[0] = -dir[0];
253     thisray.rdir[1] = -dir[1];
254     thisray.rdir[2] = -dir[2];
255     thisray.rmax = 0.0;
256     rayorigin(&thisray, PRIMARY, NULL, NULL);
257     thisray.rot = 1e-5; /* pretend we hit surface */
258     thisray.rod = 1.0;
259     VSUM(thisray.rop, org, dir, 1e-4);
260     samplendx++; /* compute result */
261     (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
262 greg 2.1 }
263    
264    
265 greg 2.7 /* Evaluate radiance contributions */
266 greg 2.1 static void
267 greg 2.7 eval_rad(FVECT org, FVECT dir, double dmax)
268 greg 2.1 {
269     RAY thisray;
270     /* set up ray */
271 greg 2.7 VCOPY(thisray.rorg, org);
272     VCOPY(thisray.rdir, dir);
273     thisray.rmax = dmax;
274 greg 2.1 rayorigin(&thisray, PRIMARY, NULL, NULL);
275     samplendx++; /* call ray evaluation */
276     rayvalue(&thisray);
277     }
278    
279    
280     /* Accumulate and/or output ray contributions (child or only process) */
281     static void
282     done_contrib()
283     {
284     MODCONT *mp;
285     int i;
286    
287     if (account <= 0 || --account)
288     return; /* not time yet */
289    
290     for (i = 0; i < nmods; i++) { /* output records & clear */
291     mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
292     mod_output(mp);
293     memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
294     }
295     end_record(); /* end lines & flush if time */
296    
297     account = accumulate; /* reset accumulation counter */
298     }
299    
300    
301     /* Principal calculation loop (called by main) */
302     void
303     rcontrib()
304     {
305     static int ignore_warning_given = 0;
306     FVECT orig, direc;
307     double d;
308 greg 2.2 /* initialize (& fork more of us) */
309 greg 2.1 rcinit();
310     /* load rays from stdin & process */
311     #ifdef getc_unlocked
312 greg 2.2 flockfile(stdin); /* avoid mutex overhead */
313 greg 2.1 #endif
314     while (getvec(orig) == 0 && getvec(direc) == 0) {
315     d = normalize(direc);
316 greg 2.4 if (nchild != -1 && (d == 0.0) & (accumulate != 1)) {
317 greg 2.1 if (!ignore_warning_given++)
318     error(WARNING,
319     "dummy ray(s) ignored during accumulation\n");
320     continue;
321     }
322     if (lastray+1 < lastray)
323     lastray = lastdone = 0;
324     ++lastray;
325     if (d == 0.0) { /* zero ==> flush */
326     if ((yres <= 0) | (xres <= 0))
327     waitflush = 1; /* flush right after */
328 greg 2.4 account = 1;
329 greg 2.7 } else if (imm_irrad) { /* else compute */
330     eval_irrad(orig, direc);
331     } else {
332     eval_rad(orig, direc, lim_dist ? d : 0.0);
333 greg 2.1 }
334     done_contrib(); /* accumulate/output */
335     ++lastdone;
336     if (raysleft && !--raysleft)
337     break; /* preemptive EOI */
338     }
339 greg 2.8 if ((accumulate <= 0) | (account < accumulate)) {
340 greg 2.1 if (account < accumulate) {
341     error(WARNING, "partial accumulation in final record");
342     accumulate -= account;
343     }
344     account = 1; /* output accumulated totals */
345     done_contrib();
346     }
347 greg 2.9 lu_done(&ofiletab); /* close output files */
348 greg 2.1 if (raysleft)
349     error(USER, "unexpected EOF on input");
350     }