6 |
|
* Initialization and calculation routines |
7 |
|
*/ |
8 |
|
|
9 |
+ |
#include "copyright.h" |
10 |
+ |
|
11 |
+ |
#include <ctype.h> |
12 |
|
#include "rcontrib.h" |
10 |
– |
#include "source.h" |
13 |
|
#include "otypes.h" |
14 |
< |
#include "platform.h" |
14 |
> |
#include "source.h" |
15 |
|
|
16 |
|
char *shm_boundary = NULL; /* boundary of shared memory */ |
17 |
|
|
29 |
|
|
30 |
|
int rand_samp = 1; /* pure Monte Carlo sampling? */ |
31 |
|
|
32 |
< |
double dstrsrc = 0.0; /* square source distribution */ |
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 */ |
72 |
|
|
73 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
74 |
|
|
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 |
– |
|
77 |
|
char * |
78 |
|
formstr( /* return format identifier */ |
79 |
|
int f |
91 |
|
|
92 |
|
/* Add modifier to our list to track */ |
93 |
|
MODCONT * |
94 |
< |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
94 |
> |
addmodifier(char *modn, char *outf, char *prms, char *binv, int bincnt) |
95 |
|
{ |
96 |
|
LUENT *lep = lu_find(&modconttab,modn); |
97 |
|
MODCONT *mp; |
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); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
< |
/* add modifiers from a file list */ |
143 |
> |
/* Add modifiers from a file list */ |
144 |
|
void |
145 |
< |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
145 |
> |
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) |
146 |
|
{ |
147 |
|
char *mname[MAXMODLIST]; |
148 |
|
int i; |
152 |
|
error(SYSTEM, errmsg); |
153 |
|
} |
154 |
|
for (i = 0; mname[i]; i++) /* add each one */ |
155 |
< |
addmodifier(mname[i], outf, binv, bincnt); |
155 |
> |
addmodifier(mname[i], outf, prms, binv, bincnt); |
156 |
|
} |
157 |
|
|
158 |
|
|
162 |
|
) |
163 |
|
{ |
164 |
|
if (nchild > 0) /* close children if any */ |
165 |
< |
end_children(); |
165 |
> |
end_children(code != 0); |
166 |
|
exit(code); |
167 |
|
} |
168 |
|
|
181 |
|
/* set shared memory boundary */ |
182 |
|
shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); |
183 |
|
} |
184 |
< |
if ((nproc > 1) & (accumulate <= 0)) |
185 |
< |
put_zero_record(0); /* prime our queue to accumulate */ |
214 |
< |
|
215 |
< |
if (recover) { /* recover previous output? */ |
216 |
< |
if (accumulate <= 0) { |
217 |
< |
reload_output(); |
218 |
< |
if (nproc > 1) |
219 |
< |
queue_modifiers(); |
220 |
< |
} else |
221 |
< |
recover_output(); |
222 |
< |
} |
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; |
193 |
|
if ((account = accumulate) > 1) |
194 |
|
raysleft *= accumulate; |
195 |
|
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
233 |
– |
/* tracing to sources as well */ |
234 |
– |
for (i = 0; i < nsources; i++) |
235 |
– |
source[i].sflags |= SFOLLOW; |
196 |
|
|
197 |
< |
if (nproc == 1 || in_rchild()) /* single process or child */ |
197 |
> |
if (nproc > 1 && in_rchild()) /* forked child? */ |
198 |
|
return; /* return to main processing loop */ |
199 |
|
|
200 |
< |
parental_loop(); /* else run controller */ |
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 |
+ |
static void |
220 |
+ |
set_eparams(char *prms) |
221 |
+ |
{ |
222 |
+ |
char vname[64]; |
223 |
+ |
double value; |
224 |
+ |
char *cps, *cpd; |
225 |
+ |
/* assign each variable */ |
226 |
+ |
for (cps = prms; *cps; cps++) { |
227 |
+ |
if (isspace(*cps)) |
228 |
+ |
continue; |
229 |
+ |
if (!isalpha(*cps)) |
230 |
+ |
goto bad_params; |
231 |
+ |
cpd = vname; |
232 |
+ |
while (*cps && (*cps != '=') & !isspace(*cps)) { |
233 |
+ |
if (!isid(*cps)) |
234 |
+ |
goto bad_params; |
235 |
+ |
*cpd++ = *cps++; |
236 |
+ |
} |
237 |
+ |
if (cpd == vname) |
238 |
+ |
goto bad_params; |
239 |
+ |
*cpd = '\0'; |
240 |
+ |
while (isspace(*cps)) cps++; |
241 |
+ |
if (*cps++ != '=') |
242 |
+ |
goto bad_params; |
243 |
+ |
value = atof(cps); |
244 |
+ |
if ((cps = fskip(cps)) == NULL) |
245 |
+ |
goto bad_params; |
246 |
+ |
while (isspace(*cps)) cps++; |
247 |
+ |
cps += (*cps == ',') | (*cps == ';'); |
248 |
+ |
varset(vname, '=', value); |
249 |
+ |
} |
250 |
+ |
return; |
251 |
+ |
bad_params: |
252 |
+ |
sprintf(errmsg, "bad parameter list '%s'", prms); |
253 |
+ |
error(USER, errmsg); |
254 |
+ |
} |
255 |
+ |
|
256 |
|
/* Our trace call to sum contributions */ |
257 |
|
static void |
258 |
|
trace_contrib(RAY *r) |
259 |
|
{ |
260 |
+ |
static char *last_params = NULL; |
261 |
|
MODCONT *mp; |
262 |
+ |
double bval; |
263 |
|
int bn; |
264 |
|
RREAL contr[3]; |
265 |
|
|
266 |
< |
if (r->ro == NULL || !inset(traset, r->ro->omod)) |
266 |
> |
if (r->ro == NULL || r->ro->omod == OVOID) |
267 |
|
return; |
268 |
+ |
/* shadow ray not on source? */ |
269 |
+ |
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
270 |
+ |
return; |
271 |
|
|
272 |
|
mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; |
273 |
|
|
274 |
< |
if (mp == NULL) |
275 |
< |
error(CONSISTENCY, "unexpected modifier in trace_contrib()"); |
274 |
> |
if (mp == NULL) /* not in our list? */ |
275 |
> |
return; |
276 |
|
|
277 |
< |
worldfunc(RCCONTEXT, r); /* get bin number */ |
278 |
< |
bn = (int)(evalue(mp->binv) + .5); |
279 |
< |
if ((bn < 0) | (bn >= mp->nbins)) { |
277 |
> |
worldfunc(RCCONTEXT, r); /* else get bin number */ |
278 |
> |
if (mp->params != last_params) |
279 |
> |
set_eparams(last_params = (char *)mp->params); |
280 |
> |
if ((bval = evalue(mp->binv)) <= -.5) |
281 |
> |
return; /* silently ignore */ |
282 |
> |
if ((bn = (int)(bval + .5)) >= mp->nbins) { |
283 |
|
error(WARNING, "bad bin number (ignored)"); |
284 |
|
return; |
285 |
|
} |
286 |
< |
raycontrib(contr, r, PRIMARY); |
286 |
> |
raycontrib(contr, r, PRIMARY); /* compute coefficient */ |
287 |
|
if (contrib) |
288 |
< |
multcolor(contr, r->rcol); |
288 |
> |
multcolor(contr, r->rcol); /* -> contribution */ |
289 |
|
addcolor(mp->cbin[bn], contr); |
290 |
|
} |
291 |
|
|
292 |
|
|
293 |
+ |
/* Evaluate irradiance contributions */ |
294 |
|
static void |
295 |
< |
rayirrad( /* compute irradiance rather than radiance */ |
277 |
< |
RAY *r |
278 |
< |
) |
295 |
> |
eval_irrad(FVECT org, FVECT dir) |
296 |
|
{ |
297 |
< |
r->rot = 1e-5; /* pretend we hit surface */ |
298 |
< |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
299 |
< |
r->ron[0] = -r->rdir[0]; |
300 |
< |
r->ron[1] = -r->rdir[1]; |
301 |
< |
r->ron[2] = -r->rdir[2]; |
302 |
< |
r->rod = 1.0; |
303 |
< |
/* compute result */ |
304 |
< |
r->revf = raytrace; |
305 |
< |
(*ofun[Lamb.otype].funp)(&Lamb, r); |
306 |
< |
r->revf = rayirrad; |
297 |
> |
RAY thisray; |
298 |
> |
|
299 |
> |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
300 |
> |
thisray.rdir[0] = -dir[0]; |
301 |
> |
thisray.rdir[1] = -dir[1]; |
302 |
> |
thisray.rdir[2] = -dir[2]; |
303 |
> |
thisray.rmax = 0.0; |
304 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
305 |
> |
/* pretend we hit surface */ |
306 |
> |
thisray.rt = thisray.rot = 1e-5; |
307 |
> |
thisray.rod = 1.0; |
308 |
> |
VCOPY(thisray.ron, dir); |
309 |
> |
VSUM(thisray.rop, org, dir, 1e-4); |
310 |
> |
samplendx++; /* compute result */ |
311 |
> |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
312 |
|
} |
313 |
|
|
314 |
|
|
315 |
< |
/* Evaluate ray contributions */ |
315 |
> |
/* Evaluate radiance contributions */ |
316 |
|
static void |
317 |
< |
eval_ray(FVECT org, FVECT dir, double dmax) |
317 |
> |
eval_rad(FVECT org, FVECT dir, double dmax) |
318 |
|
{ |
319 |
|
RAY thisray; |
320 |
|
/* set up ray */ |
321 |
+ |
VCOPY(thisray.rorg, org); |
322 |
+ |
VCOPY(thisray.rdir, dir); |
323 |
+ |
thisray.rmax = dmax; |
324 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
300 |
– |
if (imm_irrad) { |
301 |
– |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
302 |
– |
thisray.rdir[0] = -dir[0]; |
303 |
– |
thisray.rdir[1] = -dir[1]; |
304 |
– |
thisray.rdir[2] = -dir[2]; |
305 |
– |
thisray.rmax = 0.0; |
306 |
– |
thisray.revf = rayirrad; |
307 |
– |
} else { |
308 |
– |
VCOPY(thisray.rorg, org); |
309 |
– |
VCOPY(thisray.rdir, dir); |
310 |
– |
thisray.rmax = dmax; |
311 |
– |
} |
325 |
|
samplendx++; /* call ray evaluation */ |
326 |
|
rayvalue(&thisray); |
327 |
|
} |
363 |
|
#endif |
364 |
|
while (getvec(orig) == 0 && getvec(direc) == 0) { |
365 |
|
d = normalize(direc); |
366 |
< |
if (nchild != -1 && (d == 0.0) & (accumulate != 1)) { |
366 |
> |
if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { |
367 |
|
if (!ignore_warning_given++) |
368 |
|
error(WARNING, |
369 |
|
"dummy ray(s) ignored during accumulation\n"); |
373 |
|
lastray = lastdone = 0; |
374 |
|
++lastray; |
375 |
|
if (d == 0.0) { /* zero ==> flush */ |
376 |
< |
if ((yres <= 0) | (xres <= 0)) |
377 |
< |
waitflush = 1; /* flush right after */ |
378 |
< |
account = 1; |
379 |
< |
} else { /* else compute */ |
380 |
< |
eval_ray(orig, direc, lim_dist ? d : 0.0); |
376 |
> |
if ((yres <= 0) | (xres <= 1)) |
377 |
> |
waitflush = 1; /* flush after */ |
378 |
> |
if (nchild == -1) |
379 |
> |
account = 1; |
380 |
> |
} else if (imm_irrad) { /* else compute */ |
381 |
> |
eval_irrad(orig, direc); |
382 |
> |
} else { |
383 |
> |
eval_rad(orig, direc, lim_dist ? d : 0.0); |
384 |
|
} |
385 |
|
done_contrib(); /* accumulate/output */ |
386 |
|
++lastdone; |
395 |
|
account = 1; /* output accumulated totals */ |
396 |
|
done_contrib(); |
397 |
|
} |
398 |
+ |
lu_done(&ofiletab); /* close output files */ |
399 |
|
if (raysleft) |
400 |
|
error(USER, "unexpected EOF on input"); |
384 |
– |
lu_done(&ofiletab); /* close output files */ |
401 |
|
} |