33 |
|
#include "view.h" |
34 |
|
#include "random.h" |
35 |
|
#include "paths.h" |
36 |
< |
#include "rtmisc.h" /* myhostname() */ |
36 |
> |
#include "hilbert.h" |
37 |
> |
#include "pmapbias.h" |
38 |
> |
#include "pmapdiag.h" |
39 |
|
|
38 |
– |
|
40 |
|
#define RFTEMPLATE "rfXXXXXX" |
41 |
|
|
42 |
|
#ifndef SIGCONT |
48 |
|
CUBE thescene; /* our scene */ |
49 |
|
OBJECT nsceneobjs; /* number of objects in our scene */ |
50 |
|
|
51 |
+ |
extern RGBPRIMP out_prims; /* output color primitives (NULL if spectral) */ |
52 |
+ |
|
53 |
|
int dimlist[MAXDIM]; /* sampling dimensions */ |
54 |
|
int ndims = 0; /* number of sampling dimensions */ |
55 |
|
int samplendx; /* sample index number */ |
94 |
|
int backvis = 1; /* back face visibility */ |
95 |
|
|
96 |
|
int maxdepth = 7; /* maximum recursion depth */ |
97 |
< |
double minweight = 1e-3; /* minimum ray weight */ |
97 |
> |
double minweight = 1e-4; /* minimum ray weight */ |
98 |
|
|
99 |
|
char *ambfile = NULL; /* ambient file name */ |
100 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
136 |
|
|
137 |
|
|
138 |
|
|
136 |
– |
#ifdef RHAS_STAT |
137 |
– |
#include <sys/types.h> |
138 |
– |
#include <sys/stat.h> |
139 |
– |
int |
140 |
– |
file_exists(fname) /* ordinary file exists? */ |
141 |
– |
char *fname; |
142 |
– |
{ |
143 |
– |
struct stat sbuf; |
144 |
– |
if (stat(fname, &sbuf) < 0) return(0); |
145 |
– |
return((sbuf.st_mode & S_IFREG) != 0); |
146 |
– |
} |
147 |
– |
#else |
148 |
– |
#define file_exists(f) (access(f,F_OK)==0) |
149 |
– |
#endif |
150 |
– |
|
151 |
– |
|
139 |
|
void |
140 |
< |
quit(code) /* quit program */ |
154 |
< |
int code; |
140 |
> |
quit(int code) /* quit program */ |
141 |
|
{ |
142 |
|
if (code) /* report status */ |
143 |
|
report(0); |
153 |
|
static void |
154 |
|
report(int dummy) /* report progress */ |
155 |
|
{ |
156 |
< |
double u, s; |
156 |
> |
char bcStat [128]; |
157 |
> |
double u, s; |
158 |
|
#ifdef BSD |
159 |
< |
struct rusage rubuf; |
159 |
> |
struct rusage rubuf; |
160 |
|
#else |
161 |
< |
struct tms tbuf; |
162 |
< |
double period; |
161 |
> |
double period = 1.0 / 60.0; |
162 |
> |
struct tms tbuf; |
163 |
|
#endif |
164 |
|
|
165 |
|
tlastrept = time((time_t *)NULL); |
166 |
|
#ifdef BSD |
167 |
|
getrusage(RUSAGE_SELF, &rubuf); |
168 |
< |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
169 |
< |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
168 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; |
169 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; |
170 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
171 |
< |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
172 |
< |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
171 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; |
172 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; |
173 |
|
#else |
174 |
|
times(&tbuf); |
175 |
|
#ifdef _SC_CLK_TCK |
176 |
|
period = 1.0 / sysconf(_SC_CLK_TCK); |
190 |
– |
#else |
191 |
– |
period = 1.0 / 60.0; |
177 |
|
#endif |
178 |
|
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
179 |
|
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
180 |
|
#endif |
181 |
|
|
182 |
+ |
/* PMAP: Get photon map bias compensation statistics */ |
183 |
+ |
pmapBiasCompReport(bcStat); |
184 |
+ |
|
185 |
|
sprintf(errmsg, |
186 |
< |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
187 |
< |
nrays, pctdone, u/3600., s/3600., |
188 |
< |
(tlastrept-tstart)/3600., myhostname()); |
186 |
> |
"%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n", |
187 |
> |
nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.), |
188 |
> |
(tlastrept-tstart)*(1./3600.), myhostname(), getpid()); |
189 |
|
eputs(errmsg); |
190 |
|
#ifdef SIGCONT |
191 |
|
signal(SIGCONT, report); |
195 |
|
static void |
196 |
|
report(int dummy) /* report progress */ |
197 |
|
{ |
198 |
+ |
char bcStat [128]; |
199 |
+ |
|
200 |
|
tlastrept = time((time_t *)NULL); |
201 |
< |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
202 |
< |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
201 |
> |
|
202 |
> |
/* PMAP: Get photon map bias compensation statistics */ |
203 |
> |
pmapBiasCompReport(bcStat); |
204 |
> |
|
205 |
> |
sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n", |
206 |
> |
nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0); |
207 |
|
eputs(errmsg); |
208 |
|
} |
209 |
|
#endif |
210 |
|
|
211 |
|
|
212 |
< |
extern void |
212 |
> |
void |
213 |
|
rpict( /* generate image(s) */ |
214 |
|
int seq, |
215 |
|
char *pout, |
289 |
|
break; |
290 |
|
pctdone = 0.0; |
291 |
|
if (pout != NULL) { |
292 |
+ |
int myfd; |
293 |
+ |
close(1); /* reassign stdout */ |
294 |
|
sprintf(fbuf, pout, seq); |
295 |
< |
if (file_exists(fbuf)) { |
296 |
< |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
295 |
> |
tryagain: |
296 |
> |
errno = 0; /* exclusive open */ |
297 |
> |
if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { |
298 |
> |
if ((errno != EEXIST) | (prvr != NULL) || |
299 |
> |
!strcmp(fbuf, pout)) { |
300 |
|
sprintf(errmsg, |
301 |
< |
"output file \"%s\" exists", |
301 |
> |
"cannot open output file \"%s\"", |
302 |
|
fbuf); |
303 |
< |
error(USER, errmsg); |
303 |
> |
error(SYSTEM, errmsg); |
304 |
|
} |
305 |
|
setview(&ourview); |
306 |
|
continue; /* don't clobber */ |
307 |
|
} |
308 |
< |
if (freopen(fbuf, "w", stdout) == NULL) { |
309 |
< |
sprintf(errmsg, |
310 |
< |
"cannot open output file \"%s\"", fbuf); |
312 |
< |
error(SYSTEM, errmsg); |
308 |
> |
if (myfd != 1) { |
309 |
> |
unlink(fbuf); |
310 |
> |
goto tryagain; /* leave it open */ |
311 |
|
} |
312 |
< |
SET_FILE_BINARY(stdout); |
312 |
> |
SET_FD_BINARY(1); |
313 |
|
dupheader(); |
314 |
|
} |
315 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
342 |
|
if ((pa < .99) | (pa > 1.01)) |
343 |
|
fputaspect(pa, stdout); |
344 |
|
fputnow(stdout); |
345 |
< |
fputformat(COLRFMT, stdout); |
346 |
< |
putchar('\n'); |
345 |
> |
if (out_prims == xyzprims) { |
346 |
> |
fputformat(CIEFMT, stdout); |
347 |
> |
} else { |
348 |
> |
fputprims(out_prims, stdout); |
349 |
> |
fputformat(COLRFMT, stdout); |
350 |
> |
} |
351 |
> |
putchar('\n'); /* close header */ |
352 |
|
if (zout != NULL) |
353 |
|
sprintf(cp=fbuf, zout, seq); |
354 |
|
else |
384 |
|
char *oldfile |
385 |
|
) |
386 |
|
{ |
387 |
+ |
const int srcdrawing = /* manually draw tiny light sources? */ |
388 |
+ |
(directvis && dblur <= FTINY && (mblur <= FTINY) | !lastview.type); |
389 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
390 |
|
float *zbar[MAXDIV+1]; /* z values */ |
391 |
|
char *sampdens; /* previous sample density */ |
452 |
|
signal(SIGCONT, report); |
453 |
|
#endif |
454 |
|
ypos = vres-1 - i; /* initialize sampling */ |
455 |
< |
if (directvis) |
455 |
> |
if (srcdrawing) |
456 |
|
init_drawsources(psample); |
457 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
458 |
|
/* compute scanlines */ |
473 |
|
hres, ypos, hstep); |
474 |
|
/* fill bar */ |
475 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
476 |
< |
if (directvis) /* add bitty sources */ |
477 |
< |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
476 |
> |
if (srcdrawing) /* add bitty sources */ |
477 |
> |
drawsources((COLORV **)scanbar, zbar, 0, hres, ypos, ystep); |
478 |
|
/* write it out */ |
479 |
|
#ifdef SIGCONT |
480 |
|
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
668 |
|
int y |
669 |
|
) |
670 |
|
{ |
671 |
+ |
static COLORMAT xyz2myrgbmat; |
672 |
|
RAY thisray; |
673 |
|
FVECT lorg, ldir; |
674 |
< |
double hpos, vpos, vdist, lmax; |
674 |
> |
double hpos, vpos, lmax; |
675 |
|
int i; |
676 |
|
/* compute view ray */ |
677 |
|
setcolor(col, 0.0, 0.0, 0.0); |
680 |
|
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
681 |
|
&ourview, hpos, vpos)) < -FTINY) |
682 |
|
return(0.0); |
677 |
– |
|
678 |
– |
vdist = ourview.vdist; |
683 |
|
/* set pixel index */ |
684 |
|
samplendx = pixnumber(x,y,hres,vres); |
685 |
|
/* optional motion blur */ |
694 |
|
} |
695 |
|
if (normalize(thisray.rdir) == 0.0) |
696 |
|
return(0.0); |
693 |
– |
vdist = (1.-d)*vdist + d*lastview.vdist; |
697 |
|
} |
698 |
< |
/* optional depth-of-field */ |
699 |
< |
if (dblur > FTINY) { |
700 |
< |
double vc, dfh, dfv; |
698 |
< |
/* square/circle conv. */ |
699 |
< |
dfh = vc = 1. - 2.*frandom(); |
700 |
< |
dfv = 1. - 2.*frandom(); |
701 |
< |
dfh *= .5*dblur*sqrt(1. - .5*dfv*dfv); |
702 |
< |
dfv *= .5*dblur*sqrt(1. - .5*vc*vc); |
703 |
< |
if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) { |
704 |
< |
double adj = 1.0; |
705 |
< |
if (ourview.type == VT_PER) |
706 |
< |
adj /= DOT(thisray.rdir, ourview.vdir); |
707 |
< |
dfh /= sqrt(ourview.hn2); |
708 |
< |
dfv /= sqrt(ourview.vn2); |
709 |
< |
for (i = 3; i--; ) { |
710 |
< |
vc = ourview.vp[i] + adj*vdist*thisray.rdir[i]; |
711 |
< |
thisray.rorg[i] += dfh*ourview.hvec[i] + |
712 |
< |
dfv*ourview.vvec[i] ; |
713 |
< |
thisray.rdir[i] = vc - thisray.rorg[i]; |
714 |
< |
} |
715 |
< |
} else { /* non-standard view case */ |
716 |
< |
double dfd = PI/4.*dblur*(.5 - frandom()); |
717 |
< |
if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) { |
718 |
< |
if (ourview.type != VT_CYL) |
719 |
< |
dfh /= sqrt(ourview.hn2); |
720 |
< |
dfv /= sqrt(ourview.vn2); |
721 |
< |
} |
722 |
< |
for (i = 3; i--; ) { |
723 |
< |
vc = ourview.vp[i] + vdist*thisray.rdir[i]; |
724 |
< |
thisray.rorg[i] += dfh*ourview.hvec[i] + |
725 |
< |
dfv*ourview.vvec[i] + |
726 |
< |
dfd*ourview.vdir[i] ; |
727 |
< |
thisray.rdir[i] = vc - thisray.rorg[i]; |
728 |
< |
} |
729 |
< |
} |
730 |
< |
if (normalize(thisray.rdir) == 0.0) |
731 |
< |
return(0.0); |
732 |
< |
} |
698 |
> |
/* depth-of-field */ |
699 |
> |
if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur)) |
700 |
> |
return(0.0); |
701 |
|
|
702 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
703 |
|
|
704 |
|
rayvalue(&thisray); /* trace ray */ |
705 |
|
|
706 |
< |
copycolor(col, thisray.rcol); /* return color */ |
706 |
> |
if (out_prims == stdprims) { /* return color */ |
707 |
> |
scolor_rgb(col, thisray.rcol); |
708 |
> |
} else if (out_prims == xyzprims) { |
709 |
> |
scolor_cie(col, thisray.rcol); |
710 |
> |
scalecolor(col, WHTEFFICACY); |
711 |
> |
} else if (NCSAMP > 3) { |
712 |
> |
COLOR xyz; |
713 |
> |
if (xyz2myrgbmat[0][0] == 0) |
714 |
> |
compxyz2rgbWBmat(xyz2myrgbmat, out_prims); |
715 |
> |
scolor_cie(xyz, thisray.rcol); |
716 |
> |
colortrans(col, xyz2myrgbmat, xyz); |
717 |
> |
clipgamut(col, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite); |
718 |
> |
} else |
719 |
> |
copycolor(col, thisray.rcol); |
720 |
|
|
721 |
< |
return(thisray.rt); /* return distance */ |
721 |
> |
return(raydistance(&thisray)); /* return distance */ |
722 |
|
} |
723 |
|
|
724 |
|
|
783 |
|
} |
784 |
|
|
785 |
|
static int |
786 |
< |
pixnumber( /* compute pixel index (brushed) */ |
786 |
> |
pixnumber( /* compute pixel index (screen door) */ |
787 |
|
int x, |
788 |
|
int y, |
789 |
|
int xres, |
790 |
|
int yres |
791 |
|
) |
792 |
|
{ |
793 |
< |
x -= y; |
794 |
< |
while (x < 0) |
795 |
< |
x += xres; |
796 |
< |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
793 |
> |
unsigned nbits = 0; |
794 |
> |
bitmask_t coord[2]; |
795 |
> |
|
796 |
> |
if (xres < yres) xres = yres; |
797 |
> |
while (xres > 0) { |
798 |
> |
xres >>= 1; |
799 |
> |
++nbits; |
800 |
> |
} |
801 |
> |
coord[0] = x; coord[1] = y; |
802 |
> |
return ((int)hilbert_c2i(2, nbits, coord)); |
803 |
|
} |