ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.13
Committed: Fri Jun 22 22:03:02 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +1 -5 lines
Log Message:
Moved source initialization

File Contents

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