8 |
|
|
9 |
|
#include "copyright.h" |
10 |
|
|
11 |
– |
#include <ctype.h> |
11 |
|
#include "rcontrib.h" |
12 |
|
#include "otypes.h" |
13 |
|
#include "source.h" |
14 |
|
|
16 |
– |
char *shm_boundary = NULL; /* boundary of shared memory */ |
17 |
– |
|
15 |
|
CUBE thescene; /* our scene */ |
16 |
|
OBJECT nsceneobjs; /* number of objects in our scene */ |
17 |
|
|
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 */ |
26 |
< |
void (*trace)() = trace_contrib; |
22 |
> |
void (*trace)() = NULL; /* trace call (NULL before rcinit) */ |
23 |
|
|
24 |
|
int do_irrad = 0; /* compute irradiance? */ |
25 |
|
|
26 |
|
int rand_samp = 1; /* pure Monte Carlo sampling? */ |
27 |
|
|
28 |
|
double dstrsrc = 0.9; /* square source distribution */ |
29 |
< |
double shadthresh = .03; /* shadow threshold */ |
29 |
> |
double shadthresh = 0.; /* shadow threshold */ |
30 |
|
double shadcert = .75; /* shadow certainty */ |
31 |
|
int directrelay = 3; /* number of source relays */ |
32 |
|
int vspretest = 512; /* virtual source pretest density */ |
38 |
|
double seccg = 0.; /* global scattering eccentricity */ |
39 |
|
double ssampdist = 0.; /* scatter sampling distance */ |
40 |
|
|
41 |
< |
double specthresh = .15; /* specular sampling threshold */ |
41 |
> |
double specthresh = .02; /* specular sampling threshold */ |
42 |
|
double specjitter = 1.; /* specular sampling jitter */ |
43 |
|
|
44 |
|
int backvis = 1; /* back face visibility */ |
49 |
|
char *ambfile = NULL; /* ambient file name */ |
50 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
51 |
|
int ambvwt = 0; /* initial weight for ambient value */ |
52 |
< |
double ambacc = 0; /* ambient accuracy */ |
52 |
> |
double ambacc = 0.; /* ambient accuracy */ |
53 |
|
int ambres = 256; /* ambient resolution */ |
54 |
|
int ambdiv = 350; /* ambient divisions */ |
55 |
|
int ambssamp = 0; /* ambient super-samples */ |
64 |
|
RNUMBER lastray = 0; /* last ray number sent */ |
65 |
|
RNUMBER lastdone = 0; /* last ray output */ |
66 |
|
|
67 |
< |
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } |
67 |
> |
static void trace_contrib(RAY *r); /* our trace callback */ |
68 |
|
|
69 |
+ |
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv,1); free(p); } |
70 |
+ |
|
71 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
72 |
|
|
73 |
|
/************************** INITIALIZATION ROUTINES ***********************/ |
74 |
|
|
75 |
< |
char * |
75 |
> |
const char * |
76 |
|
formstr( /* return format identifier */ |
77 |
|
int f |
78 |
|
) |
81 |
|
case 'a': return("ascii"); |
82 |
|
case 'f': return("float"); |
83 |
|
case 'd': return("double"); |
84 |
< |
case 'c': return(COLRFMT); |
84 |
> |
case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT); |
85 |
|
} |
86 |
|
return("unknown"); |
87 |
|
} |
91 |
|
MODCONT * |
92 |
|
addmodifier(char *modn, char *outf, char *prms, char *binv, int bincnt) |
93 |
|
{ |
94 |
< |
LUENT *lep = lu_find(&modconttab,modn); |
95 |
< |
MODCONT *mp; |
96 |
< |
EPNODE *ebinv; |
97 |
< |
int i; |
98 |
< |
|
94 |
> |
static int lastNCS = 0; |
95 |
> |
LUENT *lep = lu_find(&modconttab,modn); |
96 |
> |
MODCONT *mp; |
97 |
> |
EPNODE *ebinv; |
98 |
> |
int i; |
99 |
> |
|
100 |
> |
if (!lastNCS) |
101 |
> |
lastNCS = NCSAMP; |
102 |
> |
else if (NCSAMP != lastNCS) |
103 |
> |
error(INTERNAL, |
104 |
> |
"number of spectral samples must be set before first modifier"); |
105 |
|
if (lep->data != NULL) { |
106 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
107 |
|
error(USER, errmsg); |
108 |
|
} |
109 |
< |
if (nmods >= MAXMODLIST) |
110 |
< |
error(INTERNAL, "too many modifiers"); |
109 |
> |
if (!strcmp(modn, VOIDID)) { |
110 |
> |
sprintf(errmsg, "cannot track '%s' modifier", VOIDID); |
111 |
> |
error(USER, errmsg); |
112 |
> |
} |
113 |
> |
if (nmods >= modasiz) { /* need bigger modifier array */ |
114 |
> |
modasiz += modasiz/2 + 64; |
115 |
> |
if (modname == NULL) |
116 |
> |
modname = (char **)malloc(modasiz*sizeof(char *)); |
117 |
> |
else |
118 |
> |
modname = (char **)realloc(modname, modasiz*sizeof(char *)); |
119 |
> |
if (modname == NULL) |
120 |
> |
error(SYSTEM, "out of memory in addmodifier()"); |
121 |
> |
} |
122 |
|
modname[nmods++] = modn; /* XXX assumes static string */ |
123 |
|
lep->key = modn; /* XXX assumes static string */ |
124 |
|
if (binv == NULL) |
138 |
|
error(USER, errmsg); |
139 |
|
} |
140 |
|
/* initialize results holder */ |
141 |
< |
mp = (MODCONT *)malloc(sizeof(MODCONT)+sizeof(DCOLOR)*(bincnt-1)); |
141 |
> |
mp = (MODCONT *)malloc(mcsize(bincnt)); |
142 |
|
if (mp == NULL) |
143 |
|
error(SYSTEM, "out of memory in addmodifier"); |
144 |
|
mp->outspec = outf; /* XXX assumes static string */ |
145 |
|
mp->modname = modn; /* XXX assumes static string */ |
146 |
< |
mp->params = prms; |
146 |
> |
mp->params = prms; /* XXX assumes static string */ |
147 |
|
mp->binv = ebinv; |
148 |
+ |
mp->bin0 = 0; |
149 |
|
mp->nbins = bincnt; |
150 |
< |
memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); |
151 |
< |
/* allocate output streams */ |
152 |
< |
for (i = bincnt; i-- > 0; ) |
153 |
< |
getostream(mp->outspec, mp->modname, i, 1); |
150 |
> |
memset(mp->cbin, 0, DCOLORSIZ*bincnt); |
151 |
> |
/* figure out starting bin */ |
152 |
> |
while (!getostream(mp->outspec, mp->modname, mp->bin0, 1)) |
153 |
> |
mp->bin0++; |
154 |
> |
/* allocate other output streams */ |
155 |
> |
for (i = 0; ++i < mp->nbins; ) |
156 |
> |
getostream(mp->outspec, mp->modname, mp->bin0+i, 1); |
157 |
|
lep->data = (char *)mp; |
158 |
|
return(mp); |
159 |
|
} |
163 |
|
void |
164 |
|
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) |
165 |
|
{ |
166 |
< |
char *mname[MAXMODLIST]; |
167 |
< |
int i; |
168 |
< |
/* find the file & store strings */ |
169 |
< |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
170 |
< |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
166 |
> |
char *path = getpath(fname, getrlibpath(), R_OK); |
167 |
> |
char mod[MAXSTR]; |
168 |
> |
FILE *fp; |
169 |
> |
|
170 |
> |
if (path == NULL || (fp = fopen(path, "r")) == NULL) { |
171 |
> |
if (path == NULL) |
172 |
> |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
173 |
> |
else |
174 |
> |
sprintf(errmsg, "cannot load modifier file '%s'", path); |
175 |
|
error(SYSTEM, errmsg); |
176 |
|
} |
177 |
< |
for (i = 0; mname[i]; i++) /* add each one */ |
178 |
< |
addmodifier(mname[i], outf, prms, binv, bincnt); |
177 |
> |
while (fgetword(mod, sizeof(mod), fp) != NULL) |
178 |
> |
addmodifier(savqstr(mod), outf, prms, binv, bincnt); |
179 |
> |
fclose(fp); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
+ |
/* Check if we have any more rays left (and report progress) */ |
184 |
+ |
int |
185 |
+ |
morays(void) |
186 |
+ |
{ |
187 |
+ |
static RNUMBER total_rays; |
188 |
+ |
static time_t tstart, last_report; |
189 |
+ |
time_t tnow; |
190 |
+ |
|
191 |
+ |
if (!raysleft) |
192 |
+ |
return(1); /* unknown total, so nothing to do or say */ |
193 |
+ |
|
194 |
+ |
if (report_intvl > 0 && (tnow = time(0)) >= last_report+report_intvl) { |
195 |
+ |
if (!total_rays) { |
196 |
+ |
total_rays = raysleft; |
197 |
+ |
tstart = tnow; |
198 |
+ |
} else { |
199 |
+ |
sprintf(errmsg, "%.2f%% done after %.3f hours\n", |
200 |
+ |
100.-100.*raysleft/total_rays, |
201 |
+ |
(1./3600.)*(tnow - tstart)); |
202 |
+ |
eputs(errmsg); |
203 |
+ |
} |
204 |
+ |
last_report = tnow; |
205 |
+ |
} |
206 |
+ |
return(--raysleft); |
207 |
+ |
} |
208 |
+ |
|
209 |
+ |
|
210 |
+ |
/* Quit program */ |
211 |
|
void |
212 |
< |
quit( /* quit program */ |
212 |
> |
quit( |
213 |
|
int code |
214 |
|
) |
215 |
|
{ |
216 |
|
if (nchild > 0) /* close children if any */ |
217 |
|
end_children(code != 0); |
218 |
+ |
else if (nchild < 0) |
219 |
+ |
_exit(code); /* avoid flush() in child */ |
220 |
|
exit(code); |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
/* Initialize our process(es) */ |
225 |
|
static void |
226 |
< |
rcinit() |
226 |
> |
rcinit(void) |
227 |
|
{ |
228 |
|
int i; |
229 |
|
|
230 |
|
if (nproc > MAXPROCESS) |
231 |
|
sprintf(errmsg, "too many processes requested -- reducing to %d", |
232 |
|
nproc = MAXPROCESS); |
233 |
< |
if (nproc > 1) { |
234 |
< |
preload_objs(); /* preload auxiliary data */ |
235 |
< |
/* set shared memory boundary */ |
182 |
< |
shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); |
183 |
< |
} |
233 |
> |
if (nproc > 1) |
234 |
> |
cow_memshare(); /* preload auxiliary data */ |
235 |
> |
trace = trace_contrib; /* set up trace call-back */ |
236 |
|
for (i = 0; i < nsources; i++) /* tracing to sources as well */ |
237 |
|
source[i].sflags |= SFOLLOW; |
238 |
|
if (yres > 0) { /* set up flushing & ray counts */ |
267 |
|
|
268 |
|
/************************** MAIN CALCULATION PROCESS ***********************/ |
269 |
|
|
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 |
– |
|
270 |
|
/* Our trace call to sum contributions */ |
271 |
|
static void |
272 |
|
trace_contrib(RAY *r) |
274 |
|
MODCONT *mp; |
275 |
|
double bval; |
276 |
|
int bn; |
277 |
< |
RREAL contr[3]; |
277 |
> |
SCOLOR contr; |
278 |
> |
DCOLORV *dvp; |
279 |
> |
int i; |
280 |
|
|
281 |
|
if (r->ro == NULL || r->ro->omod == OVOID) |
282 |
|
return; |
294 |
|
if ((bval = evalue(mp->binv)) <= -.5) /* and get bin number */ |
295 |
|
return; /* silently ignore negatives */ |
296 |
|
if ((bn = (int)(bval + .5)) >= mp->nbins) { |
297 |
< |
error(WARNING, "bad bin number (ignored)"); |
297 |
> |
sprintf(errmsg, "bad bin number (%d ignored)", bn); |
298 |
> |
error(WARNING, errmsg); |
299 |
|
return; |
300 |
|
} |
301 |
|
raycontrib(contr, r, PRIMARY); /* compute coefficient */ |
302 |
|
if (contrib) |
303 |
< |
multcolor(contr, r->rcol); /* -> contribution */ |
304 |
< |
addcolor(mp->cbin[bn], contr); |
303 |
> |
smultscolor(contr, r->rcol); /* -> contribution */ |
304 |
> |
dvp = mcbin(mp, bn); |
305 |
> |
for (i = 0; i < NCSAMP; i++) /* add it in */ |
306 |
> |
*dvp++ += contr[i]; |
307 |
|
} |
308 |
|
|
309 |
|
|
320 |
|
thisray.rmax = 0.0; |
321 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
322 |
|
/* pretend we hit surface */ |
323 |
< |
thisray.rt = thisray.rot = 1e-5; |
323 |
> |
thisray.rxt = thisray.rot = 1e-5; |
324 |
|
thisray.rod = 1.0; |
325 |
|
VCOPY(thisray.ron, dir); |
326 |
|
VSUM(thisray.rop, org, dir, 1e-4); |
346 |
|
|
347 |
|
/* Accumulate and/or output ray contributions (child or only process) */ |
348 |
|
static void |
349 |
< |
done_contrib() |
349 |
> |
done_contrib(void) |
350 |
|
{ |
351 |
|
MODCONT *mp; |
352 |
|
int i; |
357 |
|
for (i = 0; i < nmods; i++) { /* output records & clear */ |
358 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
359 |
|
mod_output(mp); |
360 |
< |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
360 |
> |
memset(mp->cbin, 0, DCOLORSIZ*mp->nbins); |
361 |
|
} |
362 |
|
end_record(); /* end lines & flush if time */ |
363 |
|
|
367 |
|
|
368 |
|
/* Principal calculation loop (called by main) */ |
369 |
|
void |
370 |
< |
rcontrib() |
370 |
> |
rcontrib(void) |
371 |
|
{ |
372 |
|
static int ignore_warning_given = 0; |
373 |
|
FVECT orig, direc; |
401 |
|
} |
402 |
|
done_contrib(); /* accumulate/output */ |
403 |
|
++lastdone; |
404 |
< |
if (raysleft && !--raysleft) |
404 |
> |
if (!morays()) |
405 |
|
break; /* preemptive EOI */ |
406 |
|
} |
407 |
|
if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { |