ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.17
Committed: Thu Nov 15 15:26:52 2012 UTC (11 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +5 -4 lines
Log Message:
Fixed bug in handling of -c >1 with zero vectors -- trim pass to follow

File Contents

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