ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.3
Committed: Mon Jun 11 05:07:55 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +4 -24 lines
Log Message:
Fixed a number of bugs -- still testing

File Contents

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