22 |
|
extern int nowarn; /* don't report warnings? */ |
23 |
|
|
24 |
|
/* current sensor's perspective */ |
25 |
< |
VIEW ourview = STDVIEW; |
25 |
> |
VIEW ourview = {VT_ANG,{0.,0.,0.},{0.,0.,1.},{1.,0.,0.}, |
26 |
> |
1.,180.,180.,0.,0.,0.,0., |
27 |
> |
{0.,0.,0.},{0.,0.,0.},0.,0.}; |
28 |
|
|
29 |
|
unsigned long nsamps = 10000; /* desired number of initial samples */ |
30 |
|
unsigned long nssamps = 9000; /* number of super-samples */ |
31 |
< |
int ndsamps = 16; /* number of direct samples */ |
31 |
> |
int ndsamps = 32; /* number of direct samples */ |
32 |
|
int nprocs = 1; /* number of rendering processes */ |
33 |
|
|
34 |
|
float *sensor = NULL; /* current sensor data */ |
40 |
|
int nphi = 0; /* azimuthal angle divisions */ |
41 |
|
double gscale = 1.; /* global scaling value */ |
42 |
|
|
43 |
+ |
#define s_theta(t) sensor[(t+1)*(sntp[1]+1)] |
44 |
+ |
#define s_phi(p) sensor[(p)+1] |
45 |
+ |
#define s_val(t,p) sensor[(p)+1+(t+1)*(sntp[1]+1)] |
46 |
+ |
|
47 |
|
static void comp_sensor(char *sfile); |
48 |
|
|
49 |
|
static void |
50 |
< |
print_defaults() |
50 |
> |
over_options() /* overriding options */ |
51 |
|
{ |
52 |
+ |
directvis = (ndsamps <= 0); |
53 |
+ |
do_irrad = 0; |
54 |
+ |
} |
55 |
+ |
|
56 |
+ |
static void |
57 |
+ |
print_defaults() /* print out default parameters */ |
58 |
+ |
{ |
59 |
+ |
over_options(); |
60 |
|
printf("-n %-9d\t\t\t# number of processes\n", nprocs); |
61 |
|
printf("-rd %-9ld\t\t\t# ray directions\n", nsamps); |
62 |
|
/* printf("-rs %-9ld\t\t\t# ray super-samples\n", nssamps); */ |
71 |
|
print_rdefaults(); |
72 |
|
} |
73 |
|
|
74 |
+ |
|
75 |
+ |
void |
76 |
+ |
quit(ec) /* make sure exit is called */ |
77 |
+ |
int ec; |
78 |
+ |
{ |
79 |
+ |
if (ray_pnprocs > 0) /* close children if any */ |
80 |
+ |
ray_pclose(0); |
81 |
+ |
exit(ec); |
82 |
+ |
} |
83 |
+ |
|
84 |
+ |
|
85 |
|
int |
86 |
|
main( |
87 |
|
int argc, |
89 |
|
) |
90 |
|
{ |
91 |
|
int doheader = 1; |
92 |
+ |
int optwarn = 0; |
93 |
|
int i, rval; |
94 |
|
|
95 |
|
progname = argv[0]; |
96 |
|
/* set up rendering defaults */ |
97 |
< |
dstrsrc = 0.25; |
97 |
> |
rand_samp = 1; |
98 |
> |
dstrsrc = 0.65; |
99 |
> |
srcsizerat = 0.1; |
100 |
|
directrelay = 3; |
101 |
|
ambounce = 1; |
102 |
< |
/* just asking defaults? */ |
75 |
< |
if (argc == 2 && !strcmp(argv[1], "-defaults")) { |
76 |
< |
print_defaults(); |
77 |
< |
return(0); |
78 |
< |
} |
79 |
< |
/* check octree */ |
80 |
< |
if (argc < 2 || argv[argc-1][0] == '-') |
81 |
< |
error(USER, "missing octree argument"); |
102 |
> |
maxdepth = -10; |
103 |
|
/* get options from command line */ |
104 |
< |
for (i = 1; i < argc-1; i++) { |
104 |
> |
for (i = 1; i < argc; i++) { |
105 |
|
while ((rval = expandarg(&argc, &argv, i)) > 0) |
106 |
|
; |
107 |
|
if (rval < 0) { |
108 |
|
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
109 |
|
error(SYSTEM, errmsg); |
110 |
|
} |
111 |
< |
if (argv[i][0] != '-') { /* process a sensor file */ |
111 |
> |
if (argv[i][0] != '-') { |
112 |
> |
if (i >= argc-1) |
113 |
> |
break; /* final octree argument */ |
114 |
|
if (!ray_pnprocs) { |
115 |
< |
/* overriding options */ |
93 |
< |
directvis = (ndsamps <= 0); |
94 |
< |
do_irrad = 0; |
115 |
> |
over_options(); |
116 |
|
if (doheader) { /* print header */ |
117 |
+ |
newheader("RADIANCE", stdout); |
118 |
|
printargs(argc, argv, stdout); |
119 |
|
fputformat("ascii", stdout); |
120 |
|
putchar('\n'); |
122 |
|
/* start process(es) */ |
123 |
|
ray_pinit(argv[argc-1], nprocs); |
124 |
|
} |
125 |
< |
comp_sensor(argv[i]); |
125 |
> |
comp_sensor(argv[i]); /* process a sensor file */ |
126 |
|
continue; |
127 |
|
} |
128 |
|
if (argv[i][1] == 'r') { /* sampling options */ |
165 |
|
sprintf(errmsg, "bad view option at '%s'", argv[i]); |
166 |
|
error(USER, errmsg); |
167 |
|
} |
168 |
< |
if (!strcmp(argv[i], "-w")) { /* turn off warnings */ |
169 |
< |
nowarn = 1; |
168 |
> |
if (!strcmp(argv[i], "-w")) { /* toggle warnings */ |
169 |
> |
nowarn = !nowarn; |
170 |
|
continue; |
171 |
|
} |
172 |
|
if (ray_pnprocs) { |
173 |
< |
error(WARNING, |
173 |
> |
if (!optwarn++) |
174 |
> |
error(WARNING, |
175 |
|
"rendering options should appear before first sensor"); |
176 |
|
} else if (!strcmp(argv[i], "-defaults")) { |
177 |
|
print_defaults(); |
194 |
|
} |
195 |
|
i += rval; |
196 |
|
} |
197 |
+ |
if (!ray_pnprocs) |
198 |
+ |
error(USER, i<argc ? "missing sensor file" : "missing octree"); |
199 |
|
quit(0); |
200 |
|
} |
201 |
|
|
307 |
|
if (pvals != NULL) |
308 |
|
free((void *)pvals); |
309 |
|
if (sfile == NULL || !*sfile) { |
310 |
+ |
sensor = NULL; |
311 |
+ |
sntp[0] = sntp[1] = 0; |
312 |
|
pvals = NULL; |
313 |
|
ntheta = nphi = 0; |
314 |
|
return; |
326 |
|
error(INTERNAL, errmsg); |
327 |
|
} |
328 |
|
/* compute boundary angles */ |
329 |
< |
maxtheta = 1.5f*sensor[sntp[0]*(sntp[1]+1)] - |
303 |
< |
0.5f*sensor[sntp[0]*sntp[1]]; |
329 |
> |
maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2); |
330 |
|
thdiv[0] = .0; |
331 |
|
for (t = 1; t < sntp[0]; t++) |
332 |
< |
thdiv[t] = DEGREE/2.*(sensor[t*(sntp[1]+1)] + |
307 |
< |
sensor[(t+1)*(sntp[1]+1)]); |
332 |
> |
thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t)); |
333 |
|
thdiv[sntp[0]] = maxtheta*DEGREE; |
334 |
|
phdiv[0] = .0; |
335 |
|
for (p = 1; p < sntp[1]; p++) |
336 |
< |
phdiv[p] = DEGREE/2.*(sensor[p] + sensor[p+1]); |
336 |
> |
phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p)); |
337 |
|
phdiv[sntp[1]] = 2.*PI; |
338 |
|
/* size our table */ |
339 |
|
tsize = 1. - cos(maxtheta*DEGREE); |
340 |
< |
psize = PI*tsize/maxtheta; |
340 |
> |
psize = PI*tsize/(maxtheta*DEGREE); |
341 |
|
if (sntp[0]*sntp[1] < samptot) /* don't overdo resolution */ |
342 |
|
samptot = sntp[0]*sntp[1]; |
343 |
< |
ntheta = (int)(sqrt(samptot*tsize/psize) + 0.5); |
343 |
> |
ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5); |
344 |
|
if (ntheta > MAXNT) |
345 |
|
ntheta = MAXNT; |
346 |
|
nphi = samptot/ntheta; |
349 |
|
error(SYSTEM, "out of memory in init_ptable()"); |
350 |
|
gscale = .0; /* compute our inverse table */ |
351 |
|
for (i = 0; i < sntp[0]; i++) { |
352 |
< |
rowp = sensor + (i+1)*(sntp[1]+1) + 1; |
352 |
> |
rowp = &s_val(i,0); |
353 |
|
rowsum[i] = 0.; |
354 |
|
for (j = 0; j < sntp[1]; j++) |
355 |
|
rowsum[i] += *rowp++; |
357 |
|
rowomega[i] *= 2.*PI / (double)sntp[1]; |
358 |
|
gscale += rowsum[i] * rowomega[i]; |
359 |
|
} |
360 |
< |
tvals[0] = .0f; |
336 |
< |
for (i = 1; i < ntheta; i++) { |
360 |
> |
for (i = 0; i < ntheta; i++) { |
361 |
|
prob = (double)i / (double)ntheta; |
362 |
|
for (t = 0; t < sntp[0]; t++) |
363 |
|
if ((prob -= rowsum[t]*rowomega[t]/gscale) <= .0) |
367 |
|
frac = 1. + prob/(rowsum[t]*rowomega[t]/gscale); |
368 |
|
tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) + |
369 |
|
frac*cos(thdiv[t+1]) ); |
370 |
+ |
/* offset b/c sensor values are centered */ |
371 |
+ |
if (t <= 0 || frac > 0.5) |
372 |
+ |
frac -= 0.5; |
373 |
+ |
else if (t >= sntp[0]-1 || frac < 0.5) { |
374 |
+ |
frac += 0.5; |
375 |
+ |
--t; |
376 |
+ |
} |
377 |
|
pvals[i*(nphi+1)] = .0f; |
378 |
|
for (j = 1; j < nphi; j++) { |
379 |
|
prob = (double)j / (double)nphi; |
380 |
< |
rowp = sensor + t*(sntp[1]+1) + 1; |
381 |
< |
rowp1 = rowp + sntp[1]+1; |
380 |
> |
rowp = &s_val(t,0); |
381 |
> |
rowp1 = &s_val(t+1,0); |
382 |
|
for (p = 0; p < sntp[1]; p++) { |
383 |
< |
if ((prob -= (1.-frac)*rowp[p]/rowsum[t-1] + |
384 |
< |
frac*rowp1[p]/rowsum[t]) <= .0) |
383 |
> |
if ((prob -= (1.-frac)*rowp[p]/rowsum[t] + |
384 |
> |
frac*rowp1[p]/rowsum[t+1]) <= .0) |
385 |
|
break; |
386 |
|
if (p >= sntp[1]) |
387 |
|
error(INTERNAL, |
388 |
|
"code error 2 in init_ptable()"); |
389 |
< |
frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t-1] |
390 |
< |
+ frac*rowp1[p]/rowsum[t]); |
389 |
> |
frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t] |
390 |
> |
+ frac*rowp1[p]/rowsum[t+1]); |
391 |
|
pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] + |
392 |
|
frac1*phdiv[p+1]; |
393 |
|
} |
394 |
|
} |
395 |
|
pvals[i*(nphi+1) + nphi] = (float)(2.*PI); |
396 |
|
} |
397 |
+ |
tvals[0] = .0f; |
398 |
|
tvals[ntheta] = (float)tsize; |
399 |
|
} |
400 |
|
|
450 |
|
t = (int)(theta/maxtheta * sntp[0]); |
451 |
|
p = (int)(phi*(1./360.) * sntp[1]); |
452 |
|
/* hack for non-uniform sensor grid */ |
453 |
< |
while (t+1 < sntp[0] && theta >= sensor[(t+2)*(sntp[1]+1)]) |
453 |
> |
while (t+1 < sntp[0] && theta >= s_theta(t+1)) |
454 |
|
++t; |
455 |
< |
while (t-1 >= 0 && theta < sensor[t*(sntp[1]+1)]) |
455 |
> |
while (t-1 >= 0 && theta <= s_theta(t-1)) |
456 |
|
--t; |
457 |
< |
while (p+1 < sntp[1] && phi >= sensor[p+2]) |
457 |
> |
while (p+1 < sntp[1] && phi >= s_phi(p+1)) |
458 |
|
++p; |
459 |
< |
while (p-1 >= 0 && phi < sensor[p]) |
459 |
> |
while (p-1 >= 0 && phi <= s_phi(p-1)) |
460 |
|
--p; |
461 |
< |
return(sensor[t*(sntp[1]+1) + p + 1]); |
461 |
> |
return(s_val(t,p)); |
462 |
|
} |
463 |
|
|
464 |
|
/* Compute sensor output */ |
473 |
|
int nt, np; |
474 |
|
COLOR vsum; |
475 |
|
RAY rr; |
476 |
+ |
double sf; |
477 |
|
int i, j; |
478 |
|
/* set view */ |
479 |
|
ourview.type = VT_ANG; |
484 |
|
error(USER, err); |
485 |
|
/* assign probability table */ |
486 |
|
init_ptable(sfile); |
487 |
< |
/* do Monte Carlo sampling */ |
487 |
> |
/* stratified MC sampling */ |
488 |
|
setcolor(vsum, .0f, .0f, .0f); |
489 |
|
nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5); |
490 |
|
np = nsamps/nt; |
491 |
< |
VCOPY(rr.rorg, ourview.vp); |
459 |
< |
rr.rmax = .0; |
491 |
> |
sf = gscale/nsamps; |
492 |
|
for (i = 0; i < nt; i++) |
493 |
< |
for (j =0; j < np; j++) { |
494 |
< |
get_direc(rr.rdir, (i+frandom())/nt, |
495 |
< |
(j + frandom())/np); |
493 |
> |
for (j = 0; j < np; j++) { |
494 |
> |
VCOPY(rr.rorg, ourview.vp); |
495 |
> |
get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np); |
496 |
> |
if (ourview.vfore > FTINY) |
497 |
> |
VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); |
498 |
> |
rr.rmax = .0; |
499 |
|
rayorigin(&rr, PRIMARY, NULL, NULL); |
500 |
+ |
scalecolor(rr.rcoef, sf); |
501 |
|
if (ray_pqueue(&rr) == 1) |
502 |
|
addcolor(vsum, rr.rcol); |
503 |
|
} |
504 |
< |
/* finish MC calculation */ |
505 |
< |
while (ray_presult(&rr, 0) > 0) |
506 |
< |
addcolor(vsum, rr.rcol); |
507 |
< |
scalecolor(vsum, gscale/(nt*np)); |
508 |
< |
/* compute direct component */ |
504 |
> |
/* remaining rays pure MC */ |
505 |
> |
for (i = nsamps - nt*np; i-- > 0; ) { |
506 |
> |
VCOPY(rr.rorg, ourview.vp); |
507 |
> |
get_direc(rr.rdir, frandom(), frandom()); |
508 |
> |
if (ourview.vfore > FTINY) |
509 |
> |
VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); |
510 |
> |
rr.rmax = .0; |
511 |
> |
rayorigin(&rr, PRIMARY, NULL, NULL); |
512 |
> |
scalecolor(rr.rcoef, sf); |
513 |
> |
if (ray_pqueue(&rr) == 1) |
514 |
> |
addcolor(vsum, rr.rcol); |
515 |
> |
} |
516 |
> |
/* scale partial result */ |
517 |
> |
scalecolor(vsum, sf); |
518 |
> |
/* add direct component */ |
519 |
|
for (i = ndirs; i-- > 0; ) { |
520 |
|
SRCINDEX si; |
521 |
|
initsrcindex(&si); |
522 |
|
while (srcray(&rr, NULL, &si)) { |
523 |
< |
double d = sens_val(rr.rdir); |
524 |
< |
if (d <= FTINY) |
523 |
> |
sf = sens_val(rr.rdir); |
524 |
> |
if (sf <= FTINY) |
525 |
|
continue; |
526 |
< |
d *= si.dom/ndirs; |
527 |
< |
scalecolor(rr.rcoef, d); |
526 |
> |
sf *= si.dom/ndirs; |
527 |
> |
scalecolor(rr.rcoef, sf); |
528 |
|
if (ray_pqueue(&rr) == 1) { |
529 |
|
multcolor(rr.rcol, rr.rcoef); |
530 |
|
addcolor(vsum, rr.rcol); |
531 |
|
} |
532 |
|
} |
533 |
|
} |
534 |
< |
/* finish direct calculation */ |
534 |
> |
/* finish our calculation */ |
535 |
|
while (ray_presult(&rr, 0) > 0) { |
536 |
|
multcolor(rr.rcol, rr.rcoef); |
537 |
|
addcolor(vsum, rr.rcol); |