ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.5
Committed: Wed Jun 13 00:16:42 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +13 -10 lines
Log Message:
Fixed problem with recovering many files

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcontrib.c,v 2.4 2012/06/12 17:20:44 greg Exp $";
3 #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 char *shm_boundary = NULL; /* boundary of shared memory */
15
16 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 int rand_samp = 1; /* pure Monte Carlo sampling? */
29
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 int maxdepth = -10; /* maximum recursion depth */
49 double minweight = 2e-3; /* minimum ray weight */
50
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 RNUMBER lastray = 0; /* last ray number sent */
67 RNUMBER 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 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 if ((nproc > 1) & (accumulate <= 0))
213 put_zero_record(0); /* prime our queue to accumulate */
214
215 if (yres > 0) { /* set up flushing & ray counts */
216 if (xres > 0)
217 raysleft = (RNUMBER)xres*yres;
218 else
219 raysleft = yres;
220 } else
221 raysleft = 0;
222 if ((account = accumulate) > 1)
223 raysleft *= accumulate;
224 waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
225 /* tracing to sources as well */
226 for (i = 0; i < nsources; i++)
227 source[i].sflags |= SFOLLOW;
228
229 if (nproc > 1 && in_rchild()) /* forked child? */
230 return; /* return to main processing loop */
231
232 if (recover) { /* recover previous output? */
233 if (accumulate <= 0) {
234 reload_output();
235 if (nproc > 1)
236 queue_modifiers();
237 } else
238 recover_output();
239 }
240 if (nproc == 1) /* single process? */
241 return;
242
243 parental_loop(); /* else run controller */
244 quit(0); /* parent musn't return! */
245 }
246
247 /************************** MAIN CALCULATION PROCESS ***********************/
248
249 /* Our trace call to sum contributions */
250 static void
251 trace_contrib(RAY *r)
252 {
253 MODCONT *mp;
254 int bn;
255 RREAL contr[3];
256
257 if (r->ro == NULL || !inset(traset, r->ro->omod))
258 return;
259
260 mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data;
261
262 if (mp == NULL)
263 error(CONSISTENCY, "unexpected modifier in trace_contrib()");
264
265 worldfunc(RCCONTEXT, r); /* get bin number */
266 bn = (int)(evalue(mp->binv) + .5);
267 if ((bn < 0) | (bn >= mp->nbins)) {
268 error(WARNING, "bad bin number (ignored)");
269 return;
270 }
271 raycontrib(contr, r, PRIMARY);
272 if (contrib)
273 multcolor(contr, r->rcol);
274 addcolor(mp->cbin[bn], contr);
275 }
276
277
278 static void
279 rayirrad( /* compute irradiance rather than radiance */
280 RAY *r
281 )
282 {
283 r->rot = 1e-5; /* pretend we hit surface */
284 VSUM(r->rop, r->rorg, r->rdir, r->rot);
285 r->ron[0] = -r->rdir[0];
286 r->ron[1] = -r->rdir[1];
287 r->ron[2] = -r->rdir[2];
288 r->rod = 1.0;
289 /* compute result */
290 r->revf = raytrace;
291 (*ofun[Lamb.otype].funp)(&Lamb, r);
292 r->revf = rayirrad;
293 }
294
295
296 /* Evaluate ray contributions */
297 static void
298 eval_ray(FVECT org, FVECT dir, double dmax)
299 {
300 RAY thisray;
301 /* set up ray */
302 rayorigin(&thisray, PRIMARY, NULL, NULL);
303 if (imm_irrad) {
304 VSUM(thisray.rorg, org, dir, 1.1e-4);
305 thisray.rdir[0] = -dir[0];
306 thisray.rdir[1] = -dir[1];
307 thisray.rdir[2] = -dir[2];
308 thisray.rmax = 0.0;
309 thisray.revf = rayirrad;
310 } else {
311 VCOPY(thisray.rorg, org);
312 VCOPY(thisray.rdir, dir);
313 thisray.rmax = dmax;
314 }
315 samplendx++; /* call ray evaluation */
316 rayvalue(&thisray);
317 }
318
319
320 /* Accumulate and/or output ray contributions (child or only process) */
321 static void
322 done_contrib()
323 {
324 MODCONT *mp;
325 int i;
326
327 if (account <= 0 || --account)
328 return; /* not time yet */
329
330 for (i = 0; i < nmods; i++) { /* output records & clear */
331 mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
332 mod_output(mp);
333 memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
334 }
335 end_record(); /* end lines & flush if time */
336
337 account = accumulate; /* reset accumulation counter */
338 }
339
340
341 /* Principal calculation loop (called by main) */
342 void
343 rcontrib()
344 {
345 static int ignore_warning_given = 0;
346 FVECT orig, direc;
347 double d;
348 /* initialize (& fork more of us) */
349 rcinit();
350 /* load rays from stdin & process */
351 #ifdef getc_unlocked
352 flockfile(stdin); /* avoid mutex overhead */
353 #endif
354 while (getvec(orig) == 0 && getvec(direc) == 0) {
355 d = normalize(direc);
356 if (nchild != -1 && (d == 0.0) & (accumulate != 1)) {
357 if (!ignore_warning_given++)
358 error(WARNING,
359 "dummy ray(s) ignored during accumulation\n");
360 continue;
361 }
362 if (lastray+1 < lastray)
363 lastray = lastdone = 0;
364 ++lastray;
365 if (d == 0.0) { /* zero ==> flush */
366 if ((yres <= 0) | (xres <= 0))
367 waitflush = 1; /* flush right after */
368 account = 1;
369 } else { /* else compute */
370 eval_ray(orig, direc, lim_dist ? d : 0.0);
371 }
372 done_contrib(); /* accumulate/output */
373 ++lastdone;
374 if (raysleft && !--raysleft)
375 break; /* preemptive EOI */
376 }
377 if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) {
378 if (account < accumulate) {
379 error(WARNING, "partial accumulation in final record");
380 accumulate -= account;
381 }
382 account = 1; /* output accumulated totals */
383 done_contrib();
384 }
385 if (raysleft)
386 error(USER, "unexpected EOF on input");
387 lu_done(&ofiletab); /* close output files */
388 }