| 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 |
|
|
| 27 |
|
|
| 28 |
|
int do_irrad = 0; /* compute irradiance? */ |
| 29 |
|
|
| 30 |
< |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
| 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 */ |
| 47 |
|
|
| 48 |
|
int backvis = 1; /* back face visibility */ |
| 49 |
|
|
| 50 |
< |
int maxdepth = 8; /* maximum recursion depth */ |
| 51 |
< |
double minweight = 5e-4; /* minimum ray weight */ |
| 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 */ |
| 65 |
|
RNUMBER raysleft; /* number of rays left to trace */ |
| 66 |
|
long waitflush; /* how long until next flush */ |
| 67 |
|
|
| 68 |
< |
int lastray = 0; /* last ray number sent */ |
| 69 |
< |
int lastdone = 0; /* last ray output */ |
| 68 |
> |
RNUMBER lastray = 0; /* last ray number sent */ |
| 69 |
> |
RNUMBER lastdone = 0; /* last ray output */ |
| 70 |
|
|
| 69 |
– |
/* Close output stream and free record */ |
| 70 |
– |
static void |
| 71 |
– |
closestream(void *p) |
| 72 |
– |
{ |
| 73 |
– |
STREAMOUT *sop = (STREAMOUT *)p; |
| 74 |
– |
int status = 0; |
| 75 |
– |
if (sop->outpipe) |
| 76 |
– |
status = pclose(sop->ofp); |
| 77 |
– |
else if (sop->ofp != stdout) |
| 78 |
– |
status = fclose(sop->ofp); |
| 79 |
– |
if (status) |
| 80 |
– |
error(SYSTEM, "error closing output stream"); |
| 81 |
– |
free(p); |
| 82 |
– |
} |
| 83 |
– |
|
| 84 |
– |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
| 85 |
– |
|
| 86 |
– |
#define OF_MODIFIER 01 |
| 87 |
– |
#define OF_BIN 02 |
| 88 |
– |
|
| 71 |
|
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } |
| 72 |
|
|
| 73 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
| 74 |
|
|
| 93 |
– |
static OBJECT traset[MAXTSET+1]={0}; /* trace include set */ |
| 94 |
– |
|
| 75 |
|
/************************** INITIALIZATION ROUTINES ***********************/ |
| 76 |
|
|
| 97 |
– |
void |
| 98 |
– |
tranotify( /* record new modifier */ |
| 99 |
– |
OBJECT obj |
| 100 |
– |
) |
| 101 |
– |
{ |
| 102 |
– |
static int hitlimit = 0; |
| 103 |
– |
OBJREC *o = objptr(obj); |
| 104 |
– |
int i; |
| 105 |
– |
|
| 106 |
– |
if (obj == OVOID) { /* starting over */ |
| 107 |
– |
traset[0] = 0; |
| 108 |
– |
hitlimit = 0; |
| 109 |
– |
return; |
| 110 |
– |
} |
| 111 |
– |
if (hitlimit || !ismodifier(o->otype)) |
| 112 |
– |
return; |
| 113 |
– |
for (i = nmods; i-- > 0; ) |
| 114 |
– |
if (!strcmp(o->oname, modname[i])) { |
| 115 |
– |
if (traset[0] >= MAXTSET) { |
| 116 |
– |
error(WARNING, "too many same-named modifiers"); |
| 117 |
– |
hitlimit++; |
| 118 |
– |
return; /* should this be fatal? */ |
| 119 |
– |
} |
| 120 |
– |
insertelem(traset, obj); |
| 121 |
– |
break; |
| 122 |
– |
} |
| 123 |
– |
} |
| 124 |
– |
|
| 125 |
– |
|
| 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 */ |
| 234 |
< |
|
| 235 |
< |
if (recover) { /* recover previous output? */ |
| 236 |
< |
if (accumulate <= 0) { |
| 237 |
< |
reload_output(); |
| 238 |
< |
if (nproc > 1) |
| 239 |
< |
queue_modifiers(); |
| 240 |
< |
} else |
| 241 |
< |
recover_output(); |
| 242 |
< |
} |
| 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; |
| 253 |
– |
/* tracing to sources as well */ |
| 254 |
– |
for (i = 0; i < nsources; i++) |
| 255 |
– |
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 |
+ |
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 || !inset(traset, r->ro->omod)) |
| 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) |
| 282 |
< |
error(CONSISTENCY, "unexpected modifier in trace_contrib()"); |
| 281 |
> |
if (mp == NULL) /* not in our list? */ |
| 282 |
> |
return; |
| 283 |
|
|
| 284 |
< |
worldfunc(RCCONTEXT, r); /* get bin number */ |
| 285 |
< |
bn = (int)(evalue(mp->binv) + .5); |
| 286 |
< |
if ((bn < 0) | (bn >= mp->nbins)) { |
| 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 |
|
error(WARNING, "bad bin number (ignored)"); |
| 290 |
|
return; |
| 291 |
|
} |
| 292 |
< |
raycontrib(contr, r, PRIMARY); |
| 292 |
> |
raycontrib(contr, r, PRIMARY); /* compute coefficient */ |
| 293 |
|
if (contrib) |
| 294 |
< |
multcolor(contr, r->rcol); |
| 294 |
> |
multcolor(contr, r->rcol); /* -> contribution */ |
| 295 |
|
addcolor(mp->cbin[bn], contr); |
| 296 |
|
} |
| 297 |
|
|
| 298 |
|
|
| 299 |
+ |
/* Evaluate irradiance contributions */ |
| 300 |
|
static void |
| 301 |
< |
rayirrad( /* compute irradiance rather than radiance */ |
| 297 |
< |
RAY *r |
| 298 |
< |
) |
| 301 |
> |
eval_irrad(FVECT org, FVECT dir) |
| 302 |
|
{ |
| 303 |
< |
r->rot = 1e-5; /* pretend we hit surface */ |
| 304 |
< |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
| 305 |
< |
r->ron[0] = -r->rdir[0]; |
| 306 |
< |
r->ron[1] = -r->rdir[1]; |
| 307 |
< |
r->ron[2] = -r->rdir[2]; |
| 308 |
< |
r->rod = 1.0; |
| 309 |
< |
/* compute result */ |
| 310 |
< |
r->revf = raytrace; |
| 311 |
< |
(*ofun[Lamb.otype].funp)(&Lamb, r); |
| 312 |
< |
r->revf = rayirrad; |
| 303 |
> |
RAY thisray; |
| 304 |
> |
|
| 305 |
> |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
| 306 |
> |
thisray.rdir[0] = -dir[0]; |
| 307 |
> |
thisray.rdir[1] = -dir[1]; |
| 308 |
> |
thisray.rdir[2] = -dir[2]; |
| 309 |
> |
thisray.rmax = 0.0; |
| 310 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
| 311 |
> |
/* pretend we hit surface */ |
| 312 |
> |
thisray.rt = thisray.rot = 1e-5; |
| 313 |
> |
thisray.rod = 1.0; |
| 314 |
> |
VCOPY(thisray.ron, dir); |
| 315 |
> |
VSUM(thisray.rop, org, dir, 1e-4); |
| 316 |
> |
samplendx++; /* compute result */ |
| 317 |
> |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
| 318 |
|
} |
| 319 |
|
|
| 320 |
|
|
| 321 |
< |
/* Evaluate ray contributions */ |
| 321 |
> |
/* Evaluate radiance contributions */ |
| 322 |
|
static void |
| 323 |
< |
eval_ray(FVECT org, FVECT dir, double dmax) |
| 323 |
> |
eval_rad(FVECT org, FVECT dir, double dmax) |
| 324 |
|
{ |
| 325 |
|
RAY thisray; |
| 326 |
|
/* set up ray */ |
| 327 |
+ |
VCOPY(thisray.rorg, org); |
| 328 |
+ |
VCOPY(thisray.rdir, dir); |
| 329 |
+ |
thisray.rmax = dmax; |
| 330 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
| 320 |
– |
if (imm_irrad) { |
| 321 |
– |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
| 322 |
– |
thisray.rdir[0] = -dir[0]; |
| 323 |
– |
thisray.rdir[1] = -dir[1]; |
| 324 |
– |
thisray.rdir[2] = -dir[2]; |
| 325 |
– |
thisray.rmax = 0.0; |
| 326 |
– |
thisray.revf = rayirrad; |
| 327 |
– |
} else { |
| 328 |
– |
VCOPY(thisray.rorg, org); |
| 329 |
– |
VCOPY(thisray.rdir, dir); |
| 330 |
– |
thisray.rmax = dmax; |
| 331 |
– |
} |
| 331 |
|
samplendx++; /* call ray evaluation */ |
| 332 |
|
rayvalue(&thisray); |
| 333 |
|
} |
| 369 |
|
#endif |
| 370 |
|
while (getvec(orig) == 0 && getvec(direc) == 0) { |
| 371 |
|
d = normalize(direc); |
| 372 |
< |
if ((d == 0.0) & (accumulate != 1)) { |
| 372 |
> |
if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { |
| 373 |
|
if (!ignore_warning_given++) |
| 374 |
|
error(WARNING, |
| 375 |
|
"dummy ray(s) ignored during accumulation\n"); |
| 379 |
|
lastray = lastdone = 0; |
| 380 |
|
++lastray; |
| 381 |
|
if (d == 0.0) { /* zero ==> flush */ |
| 382 |
< |
if ((yres <= 0) | (xres <= 0)) |
| 383 |
< |
waitflush = 1; /* flush right after */ |
| 384 |
< |
} else { /* else compute */ |
| 385 |
< |
eval_ray(orig, direc, lim_dist ? d : 0.0); |
| 382 |
> |
if ((yres <= 0) | (xres <= 1)) |
| 383 |
> |
waitflush = 1; /* flush after */ |
| 384 |
> |
if (nchild == -1) |
| 385 |
> |
account = 1; |
| 386 |
> |
} else if (imm_irrad) { /* else compute */ |
| 387 |
> |
eval_irrad(orig, direc); |
| 388 |
> |
} else { |
| 389 |
> |
eval_rad(orig, direc, lim_dist ? d : 0.0); |
| 390 |
|
} |
| 391 |
|
done_contrib(); /* accumulate/output */ |
| 392 |
|
++lastdone; |
| 393 |
|
if (raysleft && !--raysleft) |
| 394 |
|
break; /* preemptive EOI */ |
| 395 |
|
} |
| 396 |
< |
if (accumulate <= 0 || account < accumulate) { |
| 396 |
> |
if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { |
| 397 |
|
if (account < accumulate) { |
| 398 |
|
error(WARNING, "partial accumulation in final record"); |
| 399 |
|
accumulate -= account; |
| 401 |
|
account = 1; /* output accumulated totals */ |
| 402 |
|
done_contrib(); |
| 403 |
|
} |
| 404 |
+ |
lu_done(&ofiletab); /* close output files */ |
| 405 |
|
if (raysleft) |
| 406 |
|
error(USER, "unexpected EOF on input"); |
| 403 |
– |
lu_done(&ofiletab); /* close output files */ |
| 407 |
|
} |