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'); |
121 |
|
} |
122 |
|
/* start process(es) */ |
123 |
< |
ray_pinit(argv[argc-1], nprocs); |
123 |
> |
if (strcmp(argv[argc-1], ".")) |
124 |
> |
ray_pinit(argv[argc-1], nprocs); |
125 |
|
} |
126 |
< |
comp_sensor(argv[i]); |
126 |
> |
comp_sensor(argv[i]); /* process a sensor file */ |
127 |
|
continue; |
128 |
|
} |
129 |
|
if (argv[i][1] == 'r') { /* sampling options */ |
166 |
|
sprintf(errmsg, "bad view option at '%s'", argv[i]); |
167 |
|
error(USER, errmsg); |
168 |
|
} |
169 |
< |
if (!strcmp(argv[i], "-w")) { /* turn off warnings */ |
170 |
< |
nowarn = 1; |
169 |
> |
if (!strcmp(argv[i], "-w")) { /* toggle warnings */ |
170 |
> |
nowarn = !nowarn; |
171 |
|
continue; |
172 |
|
} |
173 |
|
if (ray_pnprocs) { |
174 |
< |
error(WARNING, |
174 |
> |
if (!optwarn++) |
175 |
> |
error(WARNING, |
176 |
|
"rendering options should appear before first sensor"); |
177 |
|
} else if (!strcmp(argv[i], "-defaults")) { |
178 |
|
print_defaults(); |
195 |
|
} |
196 |
|
i += rval; |
197 |
|
} |
198 |
+ |
if (sensor == NULL) |
199 |
+ |
error(USER, i<argc ? "missing sensor file" : "missing octree"); |
200 |
|
quit(0); |
201 |
|
} |
202 |
|
|
308 |
|
if (pvals != NULL) |
309 |
|
free((void *)pvals); |
310 |
|
if (sfile == NULL || !*sfile) { |
311 |
+ |
sensor = NULL; |
312 |
+ |
sntp[0] = sntp[1] = 0; |
313 |
|
pvals = NULL; |
314 |
|
ntheta = nphi = 0; |
315 |
|
return; |
327 |
|
error(INTERNAL, errmsg); |
328 |
|
} |
329 |
|
/* compute boundary angles */ |
330 |
< |
maxtheta = 1.5f*sensor[sntp[0]*(sntp[1]+1)] - |
303 |
< |
0.5f*sensor[sntp[0]*sntp[1]]; |
330 |
> |
maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2); |
331 |
|
thdiv[0] = .0; |
332 |
|
for (t = 1; t < sntp[0]; t++) |
333 |
< |
thdiv[t] = DEGREE/2.*(sensor[t*(sntp[1]+1)] + |
307 |
< |
sensor[(t+1)*(sntp[1]+1)]); |
333 |
> |
thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t)); |
334 |
|
thdiv[sntp[0]] = maxtheta*DEGREE; |
335 |
|
phdiv[0] = .0; |
336 |
|
for (p = 1; p < sntp[1]; p++) |
337 |
< |
phdiv[p] = DEGREE/2.*(sensor[p] + sensor[p+1]); |
337 |
> |
phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p)); |
338 |
|
phdiv[sntp[1]] = 2.*PI; |
339 |
|
/* size our table */ |
340 |
|
tsize = 1. - cos(maxtheta*DEGREE); |
341 |
< |
psize = PI*tsize/maxtheta; |
341 |
> |
psize = PI*tsize/(maxtheta*DEGREE); |
342 |
|
if (sntp[0]*sntp[1] < samptot) /* don't overdo resolution */ |
343 |
|
samptot = sntp[0]*sntp[1]; |
344 |
< |
ntheta = (int)(sqrt(samptot*tsize/psize) + 0.5); |
344 |
> |
ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5); |
345 |
|
if (ntheta > MAXNT) |
346 |
|
ntheta = MAXNT; |
347 |
|
nphi = samptot/ntheta; |
350 |
|
error(SYSTEM, "out of memory in init_ptable()"); |
351 |
|
gscale = .0; /* compute our inverse table */ |
352 |
|
for (i = 0; i < sntp[0]; i++) { |
353 |
< |
rowp = sensor + (i+1)*(sntp[1]+1) + 1; |
353 |
> |
rowp = &s_val(i,0); |
354 |
|
rowsum[i] = 0.; |
355 |
|
for (j = 0; j < sntp[1]; j++) |
356 |
|
rowsum[i] += *rowp++; |
358 |
|
rowomega[i] *= 2.*PI / (double)sntp[1]; |
359 |
|
gscale += rowsum[i] * rowomega[i]; |
360 |
|
} |
361 |
< |
tvals[0] = .0f; |
336 |
< |
for (i = 1; i < ntheta; i++) { |
361 |
> |
for (i = 0; i < ntheta; i++) { |
362 |
|
prob = (double)i / (double)ntheta; |
363 |
|
for (t = 0; t < sntp[0]; t++) |
364 |
|
if ((prob -= rowsum[t]*rowomega[t]/gscale) <= .0) |
368 |
|
frac = 1. + prob/(rowsum[t]*rowomega[t]/gscale); |
369 |
|
tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) + |
370 |
|
frac*cos(thdiv[t+1]) ); |
371 |
+ |
/* offset b/c sensor values are centered */ |
372 |
+ |
if (t <= 0 || frac > 0.5) |
373 |
+ |
frac -= 0.5; |
374 |
+ |
else if (t >= sntp[0]-1 || frac < 0.5) { |
375 |
+ |
frac += 0.5; |
376 |
+ |
--t; |
377 |
+ |
} |
378 |
|
pvals[i*(nphi+1)] = .0f; |
379 |
|
for (j = 1; j < nphi; j++) { |
380 |
|
prob = (double)j / (double)nphi; |
381 |
< |
rowp = sensor + t*(sntp[1]+1) + 1; |
382 |
< |
rowp1 = rowp + sntp[1]+1; |
381 |
> |
rowp = &s_val(t,0); |
382 |
> |
rowp1 = &s_val(t+1,0); |
383 |
|
for (p = 0; p < sntp[1]; p++) { |
384 |
< |
if ((prob -= (1.-frac)*rowp[p]/rowsum[t-1] + |
385 |
< |
frac*rowp1[p]/rowsum[t]) <= .0) |
384 |
> |
if ((prob -= (1.-frac)*rowp[p]/rowsum[t] + |
385 |
> |
frac*rowp1[p]/rowsum[t+1]) <= .0) |
386 |
|
break; |
387 |
|
if (p >= sntp[1]) |
388 |
|
error(INTERNAL, |
389 |
|
"code error 2 in init_ptable()"); |
390 |
< |
frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t-1] |
391 |
< |
+ frac*rowp1[p]/rowsum[t]); |
390 |
> |
frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t] |
391 |
> |
+ frac*rowp1[p]/rowsum[t+1]); |
392 |
|
pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] + |
393 |
|
frac1*phdiv[p+1]; |
394 |
|
} |
395 |
|
} |
396 |
|
pvals[i*(nphi+1) + nphi] = (float)(2.*PI); |
397 |
|
} |
398 |
+ |
tvals[0] = .0f; |
399 |
|
tvals[ntheta] = (float)tsize; |
400 |
|
} |
401 |
|
|
451 |
|
t = (int)(theta/maxtheta * sntp[0]); |
452 |
|
p = (int)(phi*(1./360.) * sntp[1]); |
453 |
|
/* hack for non-uniform sensor grid */ |
454 |
< |
while (t+1 < sntp[0] && theta >= sensor[(t+2)*(sntp[1]+1)]) |
454 |
> |
while (t+1 < sntp[0] && theta >= s_theta(t+1)) |
455 |
|
++t; |
456 |
< |
while (t-1 >= 0 && theta < sensor[t*(sntp[1]+1)]) |
456 |
> |
while (t-1 >= 0 && theta <= s_theta(t-1)) |
457 |
|
--t; |
458 |
< |
while (p+1 < sntp[1] && phi >= sensor[p+2]) |
458 |
> |
while (p+1 < sntp[1] && phi >= s_phi(p+1)) |
459 |
|
++p; |
460 |
< |
while (p-1 >= 0 && phi < sensor[p]) |
460 |
> |
while (p-1 >= 0 && phi <= s_phi(p-1)) |
461 |
|
--p; |
462 |
< |
return(sensor[t*(sntp[1]+1) + p + 1]); |
462 |
> |
return(s_val(t,p)); |
463 |
|
} |
464 |
|
|
465 |
+ |
/* Print origin and direction */ |
466 |
+ |
static void |
467 |
+ |
print_ray( |
468 |
+ |
FVECT rorg, |
469 |
+ |
FVECT rdir |
470 |
+ |
) |
471 |
+ |
{ |
472 |
+ |
printf("%.6g %.6g %.6g %.8f %.8f %.8f\n", |
473 |
+ |
rorg[0], rorg[1], rorg[2], |
474 |
+ |
rdir[0], rdir[1], rdir[2]); |
475 |
+ |
} |
476 |
+ |
|
477 |
|
/* Compute sensor output */ |
478 |
|
static void |
479 |
|
comp_sensor( |
486 |
|
int nt, np; |
487 |
|
COLOR vsum; |
488 |
|
RAY rr; |
489 |
+ |
double sf; |
490 |
|
int i, j; |
491 |
|
/* set view */ |
492 |
|
ourview.type = VT_ANG; |
497 |
|
error(USER, err); |
498 |
|
/* assign probability table */ |
499 |
|
init_ptable(sfile); |
500 |
< |
/* do Monte Carlo sampling */ |
500 |
> |
/* stratified MC sampling */ |
501 |
|
setcolor(vsum, .0f, .0f, .0f); |
502 |
|
nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5); |
503 |
|
np = nsamps/nt; |
504 |
< |
VCOPY(rr.rorg, ourview.vp); |
459 |
< |
rr.rmax = .0; |
504 |
> |
sf = gscale/nsamps; |
505 |
|
for (i = 0; i < nt; i++) |
506 |
< |
for (j =0; j < np; j++) { |
507 |
< |
get_direc(rr.rdir, (i+frandom())/nt, |
508 |
< |
(j + frandom())/np); |
506 |
> |
for (j = 0; j < np; j++) { |
507 |
> |
VCOPY(rr.rorg, ourview.vp); |
508 |
> |
get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np); |
509 |
> |
if (ourview.vfore > FTINY) |
510 |
> |
VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); |
511 |
> |
if (!ray_pnprocs) { |
512 |
> |
print_ray(rr.rorg, rr.rdir); |
513 |
> |
continue; |
514 |
> |
} |
515 |
> |
rr.rmax = .0; |
516 |
|
rayorigin(&rr, PRIMARY, NULL, NULL); |
517 |
+ |
scalecolor(rr.rcoef, sf); |
518 |
|
if (ray_pqueue(&rr) == 1) |
519 |
|
addcolor(vsum, rr.rcol); |
520 |
|
} |
521 |
< |
/* finish MC calculation */ |
522 |
< |
while (ray_presult(&rr, 0) > 0) |
523 |
< |
addcolor(vsum, rr.rcol); |
524 |
< |
scalecolor(vsum, gscale/(nt*np)); |
525 |
< |
/* compute direct component */ |
521 |
> |
/* remaining rays pure MC */ |
522 |
> |
for (i = nsamps - nt*np; i-- > 0; ) { |
523 |
> |
VCOPY(rr.rorg, ourview.vp); |
524 |
> |
get_direc(rr.rdir, frandom(), frandom()); |
525 |
> |
if (ourview.vfore > FTINY) |
526 |
> |
VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); |
527 |
> |
if (!ray_pnprocs) { |
528 |
> |
print_ray(rr.rorg, rr.rdir); |
529 |
> |
continue; |
530 |
> |
} |
531 |
> |
rr.rmax = .0; |
532 |
> |
rayorigin(&rr, PRIMARY, NULL, NULL); |
533 |
> |
scalecolor(rr.rcoef, sf); |
534 |
> |
if (ray_pqueue(&rr) == 1) |
535 |
> |
addcolor(vsum, rr.rcol); |
536 |
> |
} |
537 |
> |
if (!ray_pnprocs) /* just printing rays */ |
538 |
> |
return; |
539 |
> |
/* scale partial result */ |
540 |
> |
scalecolor(vsum, sf); |
541 |
> |
/* add direct component */ |
542 |
|
for (i = ndirs; i-- > 0; ) { |
543 |
|
SRCINDEX si; |
544 |
|
initsrcindex(&si); |
545 |
|
while (srcray(&rr, NULL, &si)) { |
546 |
< |
double d = sens_val(rr.rdir); |
547 |
< |
if (d <= FTINY) |
546 |
> |
sf = sens_val(rr.rdir); |
547 |
> |
if (sf <= FTINY) |
548 |
|
continue; |
549 |
< |
d *= si.dom/ndirs; |
550 |
< |
scalecolor(rr.rcoef, d); |
549 |
> |
sf *= si.dom/ndirs; |
550 |
> |
scalecolor(rr.rcoef, sf); |
551 |
|
if (ray_pqueue(&rr) == 1) { |
552 |
|
multcolor(rr.rcol, rr.rcoef); |
553 |
|
addcolor(vsum, rr.rcol); |
554 |
|
} |
555 |
|
} |
556 |
|
} |
557 |
< |
/* finish direct calculation */ |
557 |
> |
/* finish our calculation */ |
558 |
|
while (ray_presult(&rr, 0) > 0) { |
559 |
|
multcolor(rr.rcol, rr.rcoef); |
560 |
|
addcolor(vsum, rr.rcol); |