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