ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.23
Committed: Mon Aug 25 13:18:30 2014 UTC (9 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad4R2P1
Changes since 2.22: +3 -2 lines
Log Message:
Added bad bin number reporting

File Contents

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