1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rcontrib.c,v 2.43 2023/11/15 18:02:53 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 "rcontrib.h" |
12 |
#include "otypes.h" |
13 |
#include "source.h" |
14 |
|
15 |
char *shm_boundary = NULL; /* boundary of shared memory */ |
16 |
|
17 |
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 |
void (*trace)() = NULL; /* trace call (NULL before rcinit) */ |
25 |
|
26 |
int do_irrad = 0; /* compute irradiance? */ |
27 |
|
28 |
int rand_samp = 1; /* pure Monte Carlo sampling? */ |
29 |
|
30 |
double dstrsrc = 0.9; /* square source distribution */ |
31 |
double shadthresh = 0.; /* 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 = .02; /* 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 trace_contrib(RAY *r); /* our trace callback */ |
70 |
|
71 |
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv,1); free(p); } |
72 |
|
73 |
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
74 |
|
75 |
/************************** INITIALIZATION ROUTINES ***********************/ |
76 |
|
77 |
const 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(NCSAMP==3 ? COLRFMT : SPECFMT); |
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 (!strcmp(modn, VOIDID)) { |
106 |
sprintf(errmsg, "cannot track '%s' modifier", VOIDID); |
107 |
error(USER, errmsg); |
108 |
} |
109 |
if (nmods >= modasiz) { /* need bigger modifier array */ |
110 |
modasiz += modasiz/2 + 64; |
111 |
if (modname == NULL) |
112 |
modname = (char **)malloc(modasiz*sizeof(char *)); |
113 |
else |
114 |
modname = (char **)realloc(modname, modasiz*sizeof(char *)); |
115 |
if (modname == NULL) |
116 |
error(SYSTEM, "Out of memory in addmodifier()"); |
117 |
} |
118 |
modname[nmods++] = modn; /* XXX assumes static string */ |
119 |
lep->key = modn; /* XXX assumes static string */ |
120 |
if (binv == NULL) |
121 |
binv = "0"; /* use single bin if unspecified */ |
122 |
ebinv = eparse(binv); |
123 |
if (ebinv->type == NUM) { /* check value if constant */ |
124 |
bincnt = (int)(evalue(ebinv) + 1.5); |
125 |
if (bincnt != 1) { |
126 |
sprintf(errmsg, "illegal non-zero constant for bin (%s)", |
127 |
binv); |
128 |
error(USER, errmsg); |
129 |
} |
130 |
} else if (bincnt <= 0) { |
131 |
sprintf(errmsg, |
132 |
"unspecified or illegal bin count for modifier '%s'", |
133 |
modn); |
134 |
error(USER, errmsg); |
135 |
} |
136 |
/* initialize results holder */ |
137 |
mp = (MODCONT *)malloc(mcsize(bincnt)); |
138 |
if (mp == NULL) |
139 |
error(SYSTEM, "out of memory in addmodifier"); |
140 |
mp->outspec = outf; /* XXX assumes static string */ |
141 |
mp->modname = modn; /* XXX assumes static string */ |
142 |
mp->params = prms; /* XXX assumes static string */ |
143 |
mp->binv = ebinv; |
144 |
mp->bin0 = 0; |
145 |
mp->nbins = bincnt; |
146 |
memset(mp->cbin, 0, DCOLORSIZ*bincnt); |
147 |
/* figure out starting bin */ |
148 |
while (!getostream(mp->outspec, mp->modname, mp->bin0, 1)) |
149 |
mp->bin0++; |
150 |
/* allocate other output streams */ |
151 |
for (i = 0; ++i < mp->nbins; ) |
152 |
getostream(mp->outspec, mp->modname, mp->bin0+i, 1); |
153 |
lep->data = (char *)mp; |
154 |
return(mp); |
155 |
} |
156 |
|
157 |
|
158 |
/* Add modifiers from a file list */ |
159 |
void |
160 |
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) |
161 |
{ |
162 |
char *path = getpath(fname, getrlibpath(), R_OK); |
163 |
char mod[MAXSTR]; |
164 |
FILE *fp; |
165 |
|
166 |
if (path == NULL || (fp = fopen(path, "r")) == NULL) { |
167 |
if (path == NULL) |
168 |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
169 |
else |
170 |
sprintf(errmsg, "cannot load modifier file '%s'", path); |
171 |
error(SYSTEM, errmsg); |
172 |
} |
173 |
while (fgetword(mod, sizeof(mod), fp) != NULL) |
174 |
addmodifier(savqstr(mod), outf, prms, binv, bincnt); |
175 |
fclose(fp); |
176 |
} |
177 |
|
178 |
|
179 |
/* Check if we have any more rays left (and report progress) */ |
180 |
int |
181 |
morays(void) |
182 |
{ |
183 |
static RNUMBER total_rays; |
184 |
static time_t tstart, last_report; |
185 |
time_t tnow; |
186 |
|
187 |
if (!raysleft) |
188 |
return(1); /* unknown total, so nothing to do or say */ |
189 |
|
190 |
if (report_intvl > 0 && (tnow = time(0)) >= last_report+report_intvl) { |
191 |
if (!total_rays) { |
192 |
total_rays = raysleft; |
193 |
tstart = tnow; |
194 |
} else { |
195 |
sprintf(errmsg, "%.2f%% done after %.3f hours\n", |
196 |
100.-100.*raysleft/total_rays, |
197 |
(1./3600.)*(tnow - tstart)); |
198 |
eputs(errmsg); |
199 |
} |
200 |
last_report = tnow; |
201 |
} |
202 |
return(--raysleft); |
203 |
} |
204 |
|
205 |
|
206 |
/* Quit program */ |
207 |
void |
208 |
quit( |
209 |
int code |
210 |
) |
211 |
{ |
212 |
if (nchild > 0) /* close children if any */ |
213 |
end_children(code != 0); |
214 |
else if (nchild < 0) |
215 |
_exit(code); /* avoid flush() in child */ |
216 |
exit(code); |
217 |
} |
218 |
|
219 |
|
220 |
/* Initialize our process(es) */ |
221 |
static void |
222 |
rcinit(void) |
223 |
{ |
224 |
int i; |
225 |
|
226 |
if (nproc > MAXPROCESS) |
227 |
sprintf(errmsg, "too many processes requested -- reducing to %d", |
228 |
nproc = MAXPROCESS); |
229 |
if (nproc > 1) { |
230 |
preload_objs(); /* preload auxiliary data */ |
231 |
/* set shared memory boundary */ |
232 |
shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); |
233 |
} |
234 |
trace = trace_contrib; /* set up trace call-back */ |
235 |
for (i = 0; i < nsources; i++) /* tracing to sources as well */ |
236 |
source[i].sflags |= SFOLLOW; |
237 |
if (yres > 0) { /* set up flushing & ray counts */ |
238 |
if (xres > 0) |
239 |
raysleft = (RNUMBER)xres*yres; |
240 |
else |
241 |
raysleft = yres; |
242 |
} else |
243 |
raysleft = 0; |
244 |
if ((account = accumulate) > 1) |
245 |
raysleft *= accumulate; |
246 |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
247 |
|
248 |
if (nproc > 1 && in_rchild()) /* forked child? */ |
249 |
return; /* return to main processing loop */ |
250 |
|
251 |
if (recover) { /* recover previous output? */ |
252 |
if (accumulate <= 0) |
253 |
reload_output(); |
254 |
else |
255 |
recover_output(); |
256 |
} |
257 |
if (nproc == 1) /* single process? */ |
258 |
return; |
259 |
/* else run appropriate controller */ |
260 |
if (accumulate <= 0) |
261 |
feeder_loop(); |
262 |
else |
263 |
parental_loop(); |
264 |
quit(0); /* parent musn't return! */ |
265 |
} |
266 |
|
267 |
/************************** MAIN CALCULATION PROCESS ***********************/ |
268 |
|
269 |
/* Our trace call to sum contributions */ |
270 |
static void |
271 |
trace_contrib(RAY *r) |
272 |
{ |
273 |
MODCONT *mp; |
274 |
double bval; |
275 |
int bn; |
276 |
SCOLOR contr; |
277 |
DCOLORV *dvp; |
278 |
int i; |
279 |
|
280 |
if (r->ro == NULL || r->ro->omod == OVOID) |
281 |
return; |
282 |
/* shadow ray not on source? */ |
283 |
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
284 |
return; |
285 |
|
286 |
mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; |
287 |
|
288 |
if (mp == NULL) /* not in our list? */ |
289 |
return; |
290 |
|
291 |
worldfunc(RCCONTEXT, r); /* else set context */ |
292 |
set_eparams((char *)mp->params); |
293 |
if ((bval = evalue(mp->binv)) <= -.5) /* and get bin number */ |
294 |
return; /* silently ignore negatives */ |
295 |
if ((bn = (int)(bval + .5)) >= mp->nbins) { |
296 |
sprintf(errmsg, "bad bin number (%d ignored)", bn); |
297 |
error(WARNING, errmsg); |
298 |
return; |
299 |
} |
300 |
raycontrib(contr, r, PRIMARY); /* compute coefficient */ |
301 |
if (contrib) |
302 |
smultscolor(contr, r->rcol); /* -> contribution */ |
303 |
dvp = mcbin(mp, bn); |
304 |
for (i = 0; i < NCSAMP; i++) /* add it in */ |
305 |
*dvp++ += contr[i]; |
306 |
} |
307 |
|
308 |
|
309 |
/* Evaluate irradiance contributions */ |
310 |
static void |
311 |
eval_irrad(FVECT org, FVECT dir) |
312 |
{ |
313 |
RAY thisray; |
314 |
|
315 |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
316 |
thisray.rdir[0] = -dir[0]; |
317 |
thisray.rdir[1] = -dir[1]; |
318 |
thisray.rdir[2] = -dir[2]; |
319 |
thisray.rmax = 0.0; |
320 |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
321 |
/* pretend we hit surface */ |
322 |
thisray.rxt = thisray.rot = 1e-5; |
323 |
thisray.rod = 1.0; |
324 |
VCOPY(thisray.ron, dir); |
325 |
VSUM(thisray.rop, org, dir, 1e-4); |
326 |
samplendx++; /* compute result */ |
327 |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
328 |
} |
329 |
|
330 |
|
331 |
/* Evaluate radiance contributions */ |
332 |
static void |
333 |
eval_rad(FVECT org, FVECT dir, double dmax) |
334 |
{ |
335 |
RAY thisray; |
336 |
/* set up ray */ |
337 |
VCOPY(thisray.rorg, org); |
338 |
VCOPY(thisray.rdir, dir); |
339 |
thisray.rmax = dmax; |
340 |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
341 |
samplendx++; /* call ray evaluation */ |
342 |
rayvalue(&thisray); |
343 |
} |
344 |
|
345 |
|
346 |
/* Accumulate and/or output ray contributions (child or only process) */ |
347 |
static void |
348 |
done_contrib(void) |
349 |
{ |
350 |
MODCONT *mp; |
351 |
int i; |
352 |
|
353 |
if (account <= 0 || --account) |
354 |
return; /* not time yet */ |
355 |
|
356 |
for (i = 0; i < nmods; i++) { /* output records & clear */ |
357 |
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
358 |
mod_output(mp); |
359 |
memset(mp->cbin, 0, DCOLORSIZ*mp->nbins); |
360 |
} |
361 |
end_record(); /* end lines & flush if time */ |
362 |
|
363 |
account = accumulate; /* reset accumulation counter */ |
364 |
} |
365 |
|
366 |
|
367 |
/* Principal calculation loop (called by main) */ |
368 |
void |
369 |
rcontrib(void) |
370 |
{ |
371 |
static int ignore_warning_given = 0; |
372 |
FVECT orig, direc; |
373 |
double d; |
374 |
/* initialize (& fork more of us) */ |
375 |
rcinit(); |
376 |
/* load rays from stdin & process */ |
377 |
#ifdef getc_unlocked |
378 |
flockfile(stdin); /* avoid mutex overhead */ |
379 |
#endif |
380 |
while (getvec(orig) == 0 && getvec(direc) == 0) { |
381 |
d = normalize(direc); |
382 |
if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { |
383 |
if (!ignore_warning_given++) |
384 |
error(WARNING, |
385 |
"dummy ray(s) ignored during accumulation\n"); |
386 |
continue; |
387 |
} |
388 |
if (lastray+1 < lastray) |
389 |
lastray = lastdone = 0; |
390 |
++lastray; |
391 |
if (d == 0.0) { /* zero ==> flush */ |
392 |
if ((yres <= 0) | (xres <= 1)) |
393 |
waitflush = 1; /* flush after */ |
394 |
if (nchild == -1) |
395 |
account = 1; |
396 |
} else if (imm_irrad) { /* else compute */ |
397 |
eval_irrad(orig, direc); |
398 |
} else { |
399 |
eval_rad(orig, direc, lim_dist ? d : 0.0); |
400 |
} |
401 |
done_contrib(); /* accumulate/output */ |
402 |
++lastdone; |
403 |
if (!morays()) |
404 |
break; /* preemptive EOI */ |
405 |
} |
406 |
if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { |
407 |
if (account < accumulate) { |
408 |
error(WARNING, "partial accumulation in final record"); |
409 |
accumulate -= account; |
410 |
} |
411 |
account = 1; /* output accumulated totals */ |
412 |
done_contrib(); |
413 |
} |
414 |
lu_done(&ofiletab); /* close output files */ |
415 |
if (raysleft) |
416 |
error(USER, "unexpected EOF on input"); |
417 |
} |