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