ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.19
Committed: Sat Jul 19 00:07:03 2014 UTC (9 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +5 -3 lines
Log Message:
Silently ignore negative bin values.

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.19 static const char RCSid[] = "$Id: rcontrib.c,v 2.18 2012/11/15 19:41:03 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 greg 2.19 double bval;
220 greg 2.1 int bn;
221     RREAL contr[3];
222    
223 greg 2.6 if (r->ro == NULL || r->ro->omod == OVOID)
224 greg 2.1 return;
225    
226     mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data;
227    
228 greg 2.6 if (mp == NULL) /* not in our list? */
229     return;
230 greg 2.14 /* shadow ray not on source? */
231     if (r->rsrc >= 0 && source[r->rsrc].so != r->ro)
232     return;
233 greg 2.1
234 greg 2.14 worldfunc(RCCONTEXT, r); /* else get bin number */
235 greg 2.19 if ((bval = evalue(mp->binv)) <= -.5)
236     return; /* silently ignore */
237     if ((bn = (int)(bval + .5)) >= mp->nbins) {
238 greg 2.1 error(WARNING, "bad bin number (ignored)");
239     return;
240     }
241 greg 2.14 raycontrib(contr, r, PRIMARY); /* compute coefficient */
242 greg 2.1 if (contrib)
243 greg 2.14 multcolor(contr, r->rcol); /* -> contribution */
244 greg 2.1 addcolor(mp->cbin[bn], contr);
245     }
246    
247    
248 greg 2.7 /* Evaluate irradiance contributions */
249 greg 2.1 static void
250 greg 2.7 eval_irrad(FVECT org, FVECT dir)
251 greg 2.1 {
252 greg 2.7 RAY thisray;
253    
254     VSUM(thisray.rorg, org, dir, 1.1e-4);
255     thisray.rdir[0] = -dir[0];
256     thisray.rdir[1] = -dir[1];
257     thisray.rdir[2] = -dir[2];
258     thisray.rmax = 0.0;
259     rayorigin(&thisray, PRIMARY, NULL, NULL);
260 greg 2.16 /* pretend we hit surface */
261     thisray.rt = thisray.rot = 1e-5;
262 greg 2.7 thisray.rod = 1.0;
263 greg 2.15 VCOPY(thisray.ron, dir);
264 greg 2.7 VSUM(thisray.rop, org, dir, 1e-4);
265     samplendx++; /* compute result */
266     (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
267 greg 2.1 }
268    
269    
270 greg 2.7 /* Evaluate radiance contributions */
271 greg 2.1 static void
272 greg 2.7 eval_rad(FVECT org, FVECT dir, double dmax)
273 greg 2.1 {
274     RAY thisray;
275     /* set up ray */
276 greg 2.7 VCOPY(thisray.rorg, org);
277     VCOPY(thisray.rdir, dir);
278     thisray.rmax = dmax;
279 greg 2.1 rayorigin(&thisray, PRIMARY, NULL, NULL);
280     samplendx++; /* call ray evaluation */
281     rayvalue(&thisray);
282     }
283    
284    
285     /* Accumulate and/or output ray contributions (child or only process) */
286     static void
287     done_contrib()
288     {
289     MODCONT *mp;
290     int i;
291    
292     if (account <= 0 || --account)
293     return; /* not time yet */
294    
295     for (i = 0; i < nmods; i++) { /* output records & clear */
296     mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
297     mod_output(mp);
298     memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
299     }
300     end_record(); /* end lines & flush if time */
301    
302     account = accumulate; /* reset accumulation counter */
303     }
304    
305    
306     /* Principal calculation loop (called by main) */
307     void
308     rcontrib()
309     {
310     static int ignore_warning_given = 0;
311     FVECT orig, direc;
312     double d;
313 greg 2.2 /* initialize (& fork more of us) */
314 greg 2.1 rcinit();
315     /* load rays from stdin & process */
316     #ifdef getc_unlocked
317 greg 2.2 flockfile(stdin); /* avoid mutex overhead */
318 greg 2.1 #endif
319     while (getvec(orig) == 0 && getvec(direc) == 0) {
320     d = normalize(direc);
321 greg 2.17 if (nchild != -1 && (d == 0.0) & (accumulate == 0)) {
322 greg 2.1 if (!ignore_warning_given++)
323     error(WARNING,
324     "dummy ray(s) ignored during accumulation\n");
325     continue;
326     }
327     if (lastray+1 < lastray)
328     lastray = lastdone = 0;
329     ++lastray;
330     if (d == 0.0) { /* zero ==> flush */
331 greg 2.18 if ((yres <= 0) | (xres <= 1))
332 greg 2.17 waitflush = 1; /* flush after */
333     if (nchild == -1)
334     account = 1;
335 greg 2.7 } else if (imm_irrad) { /* else compute */
336     eval_irrad(orig, direc);
337     } else {
338     eval_rad(orig, direc, lim_dist ? d : 0.0);
339 greg 2.1 }
340     done_contrib(); /* accumulate/output */
341     ++lastdone;
342     if (raysleft && !--raysleft)
343     break; /* preemptive EOI */
344     }
345 greg 2.11 if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) {
346 greg 2.1 if (account < accumulate) {
347     error(WARNING, "partial accumulation in final record");
348     accumulate -= account;
349     }
350     account = 1; /* output accumulated totals */
351     done_contrib();
352     }
353 greg 2.9 lu_done(&ofiletab); /* close output files */
354 greg 2.1 if (raysleft)
355     error(USER, "unexpected EOF on input");
356     }