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 */ |
34 |
|
float *sensor = NULL; /* current sensor data */ |
35 |
|
int sntp[2]; /* number of sensor theta and phi angles */ |
36 |
|
float maxtheta; /* maximum theta value for this sensor */ |
37 |
< |
float tvals[MAXNT+1]; /* theta values (1-D table of 1-cos(t)) */ |
38 |
< |
float *pvals = NULL; /* phi values (2-D table in radians) */ |
37 |
> |
float tvals[MAXNT+1]; /* theta prob. values (1-D table of 1-cos(t)) */ |
38 |
> |
float *pvals = NULL; /* phi prob. values (2-D table in radians) */ |
39 |
|
int ntheta = 0; /* polar angle divisions */ |
40 |
|
int nphi = 0; /* azimuthal angle divisions */ |
41 |
|
double gscale = 1.; /* global scaling value */ |
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, |
95 |
|
progname = argv[0]; |
96 |
|
/* set up rendering defaults */ |
97 |
|
rand_samp = 1; |
98 |
< |
dstrsrc = 0.5; |
98 |
> |
dstrsrc = 0.65; |
99 |
|
srcsizerat = 0.1; |
100 |
|
directrelay = 3; |
101 |
|
ambounce = 1; |
114 |
|
if (!ray_pnprocs) { |
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]); /* process a sensor file */ |
127 |
|
continue; |
195 |
|
} |
196 |
|
i += rval; |
197 |
|
} |
198 |
< |
if (!ray_pnprocs) |
198 |
> |
if (sensor == NULL) |
199 |
|
error(USER, i<argc ? "missing sensor file" : "missing octree"); |
200 |
|
quit(0); |
201 |
|
} |
235 |
|
cp = fskip(cp); |
236 |
|
if (cp == NULL) |
237 |
|
break; |
238 |
+ |
if (ntp[1] > 1 && sarr[ntp[1]+1] <= sarr[ntp[1]]) { |
239 |
+ |
sprintf(errmsg, |
240 |
+ |
"Phi values not monotinically increasing in sensor file '%s'", |
241 |
+ |
sfile); |
242 |
+ |
error(USER, errmsg); |
243 |
+ |
} |
244 |
|
++ntp[1]; |
245 |
|
} |
246 |
|
ntp[0] = 0; /* get thetas + data */ |
264 |
|
} |
265 |
|
if (i == ntp[0]*(ntp[1]+1)) |
266 |
|
break; |
267 |
+ |
if (ntp[0] > 1 && sarr[ntp[0]*(ntp[1]+1)] <= |
268 |
+ |
sarr[(ntp[0]-1)*(ntp[1]+1)]) { |
269 |
+ |
sprintf(errmsg, |
270 |
+ |
"Theta values not monotinically increasing in sensor file '%s'", |
271 |
+ |
sfile); |
272 |
+ |
error(USER, errmsg); |
273 |
+ |
} |
274 |
|
if (i != (ntp[0]+1)*(ntp[1]+1)) { |
275 |
|
sprintf(errmsg, |
276 |
|
"bad column count near line %d in sensor file '%s'", |
340 |
|
error(INTERNAL, errmsg); |
341 |
|
} |
342 |
|
/* compute boundary angles */ |
343 |
< |
maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2); |
343 |
> |
maxtheta = DEGREE*(1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2)); |
344 |
> |
if (maxtheta > PI) |
345 |
> |
maxtheta = PI; |
346 |
|
thdiv[0] = .0; |
347 |
|
for (t = 1; t < sntp[0]; t++) |
348 |
|
thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t)); |
349 |
< |
thdiv[sntp[0]] = maxtheta*DEGREE; |
350 |
< |
phdiv[0] = .0; |
349 |
> |
thdiv[sntp[0]] = maxtheta; |
350 |
> |
phdiv[0] = DEGREE*(1.5f*s_phi(0) - 0.5f*s_phi(1)); |
351 |
|
for (p = 1; p < sntp[1]; p++) |
352 |
|
phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p)); |
353 |
< |
phdiv[sntp[1]] = 2.*PI; |
353 |
> |
phdiv[sntp[1]] = DEGREE*(1.5f*s_phi(sntp[1]-1) - 0.5f*s_phi(sntp[1]-2)); |
354 |
|
/* size our table */ |
355 |
< |
tsize = 1. - cos(maxtheta*DEGREE); |
356 |
< |
psize = PI*tsize/(maxtheta*DEGREE); |
355 |
> |
tsize = 1. - cos(maxtheta); |
356 |
> |
psize = PI*tsize/maxtheta; |
357 |
|
if (sntp[0]*sntp[1] < samptot) /* don't overdo resolution */ |
358 |
|
samptot = sntp[0]*sntp[1]; |
359 |
|
ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5); |
360 |
|
if (ntheta > MAXNT) |
361 |
|
ntheta = MAXNT; |
362 |
|
nphi = samptot/ntheta; |
363 |
< |
pvals = (float *)malloc(sizeof(float)*ntheta*(nphi+1)); |
363 |
> |
pvals = (float *)malloc(sizeof(float)*(ntheta+1)*(nphi+1)); |
364 |
|
if (pvals == NULL) |
365 |
|
error(SYSTEM, "out of memory in init_ptable()"); |
366 |
|
gscale = .0; /* compute our inverse table */ |
367 |
|
for (i = 0; i < sntp[0]; i++) { |
368 |
|
rowp = &s_val(i,0); |
369 |
< |
rowsum[i] = 0.; |
369 |
> |
rowsum[i] = 1e-20; |
370 |
|
for (j = 0; j < sntp[1]; j++) |
371 |
|
rowsum[i] += *rowp++; |
372 |
|
rowomega[i] = cos(thdiv[i]) - cos(thdiv[i+1]); |
373 |
|
rowomega[i] *= 2.*PI / (double)sntp[1]; |
374 |
|
gscale += rowsum[i] * rowomega[i]; |
375 |
|
} |
376 |
+ |
if (gscale <= FTINY) { |
377 |
+ |
sprintf(errmsg, "Sensor values sum to zero in file '%s'", sfile); |
378 |
+ |
error(USER, errmsg); |
379 |
+ |
} |
380 |
|
for (i = 0; i < ntheta; i++) { |
381 |
|
prob = (double)i / (double)ntheta; |
382 |
|
for (t = 0; t < sntp[0]; t++) |
394 |
|
frac += 0.5; |
395 |
|
--t; |
396 |
|
} |
397 |
< |
pvals[i*(nphi+1)] = .0f; |
397 |
> |
pvals[i*(nphi+1)] = phdiv[0]; |
398 |
|
for (j = 1; j < nphi; j++) { |
399 |
|
prob = (double)j / (double)nphi; |
400 |
|
rowp = &s_val(t,0); |
401 |
|
rowp1 = &s_val(t+1,0); |
402 |
< |
for (p = 0; p < sntp[1]; p++) { |
402 |
> |
for (p = 0; p < sntp[1]; p++) |
403 |
|
if ((prob -= (1.-frac)*rowp[p]/rowsum[t] + |
404 |
|
frac*rowp1[p]/rowsum[t+1]) <= .0) |
405 |
|
break; |
406 |
< |
if (p >= sntp[1]) |
407 |
< |
error(INTERNAL, |
408 |
< |
"code error 2 in init_ptable()"); |
375 |
< |
frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t] |
376 |
< |
+ frac*rowp1[p]/rowsum[t+1]); |
377 |
< |
pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] + |
378 |
< |
frac1*phdiv[p+1]; |
406 |
> |
if (p >= sntp[1]) { |
407 |
> |
p = sntp[1] - 1; |
408 |
> |
prob = .5; |
409 |
|
} |
410 |
+ |
frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t] |
411 |
+ |
+ frac*rowp1[p]/rowsum[t+1]); |
412 |
+ |
pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] + |
413 |
+ |
frac1*phdiv[p+1]; |
414 |
|
} |
415 |
< |
pvals[i*(nphi+1) + nphi] = (float)(2.*PI); |
415 |
> |
pvals[i*(nphi+1) + nphi] = phdiv[sntp[1]]; |
416 |
|
} |
417 |
|
tvals[0] = .0f; |
418 |
|
tvals[ntheta] = (float)tsize; |
481 |
|
return(s_val(t,p)); |
482 |
|
} |
483 |
|
|
484 |
+ |
/* Print origin and direction */ |
485 |
+ |
static void |
486 |
+ |
print_ray( |
487 |
+ |
FVECT rorg, |
488 |
+ |
FVECT rdir |
489 |
+ |
) |
490 |
+ |
{ |
491 |
+ |
printf("%.6g %.6g %.6g %.8f %.8f %.8f\n", |
492 |
+ |
rorg[0], rorg[1], rorg[2], |
493 |
+ |
rdir[0], rdir[1], rdir[2]); |
494 |
+ |
} |
495 |
+ |
|
496 |
|
/* Compute sensor output */ |
497 |
|
static void |
498 |
|
comp_sensor( |
505 |
|
int nt, np; |
506 |
|
COLOR vsum; |
507 |
|
RAY rr; |
508 |
+ |
double sf; |
509 |
|
int i, j; |
510 |
|
/* set view */ |
511 |
|
ourview.type = VT_ANG; |
516 |
|
error(USER, err); |
517 |
|
/* assign probability table */ |
518 |
|
init_ptable(sfile); |
519 |
< |
/* do Monte Carlo sampling */ |
519 |
> |
/* stratified MC sampling */ |
520 |
|
setcolor(vsum, .0f, .0f, .0f); |
521 |
|
nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5); |
522 |
|
np = nsamps/nt; |
523 |
< |
VCOPY(rr.rorg, ourview.vp); |
477 |
< |
rr.rmax = .0; |
523 |
> |
sf = gscale/nsamps; |
524 |
|
for (i = 0; i < nt; i++) |
525 |
|
for (j = 0; j < np; j++) { |
526 |
+ |
VCOPY(rr.rorg, ourview.vp); |
527 |
|
get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np); |
528 |
+ |
if (ourview.vfore > FTINY) |
529 |
+ |
VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); |
530 |
+ |
if (!ray_pnprocs) { |
531 |
+ |
print_ray(rr.rorg, rr.rdir); |
532 |
+ |
continue; |
533 |
+ |
} |
534 |
+ |
rr.rmax = .0; |
535 |
|
rayorigin(&rr, PRIMARY, NULL, NULL); |
536 |
+ |
scalecolor(rr.rcoef, sf); |
537 |
|
if (ray_pqueue(&rr) == 1) |
538 |
|
addcolor(vsum, rr.rcol); |
539 |
|
} |
540 |
< |
/* finish MC calculation */ |
541 |
< |
while (ray_presult(&rr, 0) > 0) |
542 |
< |
addcolor(vsum, rr.rcol); |
543 |
< |
scalecolor(vsum, gscale/(nt*np)); |
544 |
< |
/* compute direct component */ |
540 |
> |
/* remaining rays pure MC */ |
541 |
> |
for (i = nsamps - nt*np; i-- > 0; ) { |
542 |
> |
VCOPY(rr.rorg, ourview.vp); |
543 |
> |
get_direc(rr.rdir, frandom(), frandom()); |
544 |
> |
if (ourview.vfore > FTINY) |
545 |
> |
VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); |
546 |
> |
if (!ray_pnprocs) { |
547 |
> |
print_ray(rr.rorg, rr.rdir); |
548 |
> |
continue; |
549 |
> |
} |
550 |
> |
rr.rmax = .0; |
551 |
> |
rayorigin(&rr, PRIMARY, NULL, NULL); |
552 |
> |
scalecolor(rr.rcoef, sf); |
553 |
> |
if (ray_pqueue(&rr) == 1) |
554 |
> |
addcolor(vsum, rr.rcol); |
555 |
> |
} |
556 |
> |
if (!ray_pnprocs) /* just printing rays */ |
557 |
> |
return; |
558 |
> |
/* scale partial result */ |
559 |
> |
scalecolor(vsum, sf); |
560 |
> |
/* add direct component */ |
561 |
|
for (i = ndirs; i-- > 0; ) { |
562 |
|
SRCINDEX si; |
563 |
|
initsrcindex(&si); |
564 |
|
while (srcray(&rr, NULL, &si)) { |
565 |
< |
double d = sens_val(rr.rdir); |
566 |
< |
if (d <= FTINY) |
565 |
> |
sf = sens_val(rr.rdir); |
566 |
> |
if (sf <= FTINY) |
567 |
|
continue; |
568 |
< |
d *= si.dom/ndirs; |
569 |
< |
scalecolor(rr.rcoef, d); |
568 |
> |
sf *= si.dom/ndirs; |
569 |
> |
scalecolor(rr.rcoef, sf); |
570 |
|
if (ray_pqueue(&rr) == 1) { |
571 |
|
multcolor(rr.rcol, rr.rcoef); |
572 |
|
addcolor(vsum, rr.rcol); |
573 |
|
} |
574 |
|
} |
575 |
|
} |
576 |
< |
/* finish direct calculation */ |
576 |
> |
/* finish our calculation */ |
577 |
|
while (ray_presult(&rr, 0) > 0) { |
578 |
|
multcolor(rr.rcol, rr.rcoef); |
579 |
|
addcolor(vsum, rr.rcol); |