ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.31
Committed: Fri Feb 23 03:21:24 2018 UTC (6 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +3 -3 lines
Log Message:
Corrected default setting, which is reset anyway

File Contents

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