ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.6
Committed: Fri Jun 15 00:57:40 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +4 -35 lines
Log Message:
Simplified modifier lookup

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.6 static const char RCSid[] = "$Id: rcontrib.c,v 2.5 2012/06/13 00:16:42 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     static void
248     rayirrad( /* compute irradiance rather than radiance */
249     RAY *r
250     )
251     {
252     r->rot = 1e-5; /* pretend we hit surface */
253     VSUM(r->rop, r->rorg, r->rdir, r->rot);
254     r->ron[0] = -r->rdir[0];
255     r->ron[1] = -r->rdir[1];
256     r->ron[2] = -r->rdir[2];
257     r->rod = 1.0;
258     /* compute result */
259     r->revf = raytrace;
260     (*ofun[Lamb.otype].funp)(&Lamb, r);
261     r->revf = rayirrad;
262     }
263    
264    
265     /* Evaluate ray contributions */
266     static void
267     eval_ray(FVECT org, FVECT dir, double dmax)
268     {
269     RAY thisray;
270     /* set up ray */
271     rayorigin(&thisray, PRIMARY, NULL, NULL);
272     if (imm_irrad) {
273     VSUM(thisray.rorg, org, dir, 1.1e-4);
274     thisray.rdir[0] = -dir[0];
275     thisray.rdir[1] = -dir[1];
276     thisray.rdir[2] = -dir[2];
277     thisray.rmax = 0.0;
278     thisray.revf = rayirrad;
279     } else {
280     VCOPY(thisray.rorg, org);
281     VCOPY(thisray.rdir, dir);
282     thisray.rmax = dmax;
283     }
284     samplendx++; /* call ray evaluation */
285     rayvalue(&thisray);
286     }
287    
288    
289     /* Accumulate and/or output ray contributions (child or only process) */
290     static void
291     done_contrib()
292     {
293     MODCONT *mp;
294     int i;
295    
296     if (account <= 0 || --account)
297     return; /* not time yet */
298    
299     for (i = 0; i < nmods; i++) { /* output records & clear */
300     mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
301     mod_output(mp);
302     memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
303     }
304     end_record(); /* end lines & flush if time */
305    
306     account = accumulate; /* reset accumulation counter */
307     }
308    
309    
310     /* Principal calculation loop (called by main) */
311     void
312     rcontrib()
313     {
314     static int ignore_warning_given = 0;
315     FVECT orig, direc;
316     double d;
317 greg 2.2 /* initialize (& fork more of us) */
318 greg 2.1 rcinit();
319     /* load rays from stdin & process */
320     #ifdef getc_unlocked
321 greg 2.2 flockfile(stdin); /* avoid mutex overhead */
322 greg 2.1 #endif
323     while (getvec(orig) == 0 && getvec(direc) == 0) {
324     d = normalize(direc);
325 greg 2.4 if (nchild != -1 && (d == 0.0) & (accumulate != 1)) {
326 greg 2.1 if (!ignore_warning_given++)
327     error(WARNING,
328     "dummy ray(s) ignored during accumulation\n");
329     continue;
330     }
331     if (lastray+1 < lastray)
332     lastray = lastdone = 0;
333     ++lastray;
334     if (d == 0.0) { /* zero ==> flush */
335     if ((yres <= 0) | (xres <= 0))
336     waitflush = 1; /* flush right after */
337 greg 2.4 account = 1;
338 greg 2.1 } else { /* else compute */
339     eval_ray(orig, direc, lim_dist ? d : 0.0);
340     }
341     done_contrib(); /* accumulate/output */
342     ++lastdone;
343     if (raysleft && !--raysleft)
344     break; /* preemptive EOI */
345     }
346 greg 2.4 if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) {
347 greg 2.1 if (account < accumulate) {
348     error(WARNING, "partial accumulation in final record");
349     accumulate -= account;
350     }
351     account = 1; /* output accumulated totals */
352     done_contrib();
353     }
354     if (raysleft)
355     error(USER, "unexpected EOF on input");
356     lu_done(&ofiletab); /* close output files */
357     }