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 */ |
119 |
|
|
120 |
|
int hres, vres; /* resolution for this frame */ |
121 |
|
|
122 |
+ |
extern void sskip_ray(RAY *r, double h, double v); |
123 |
+ |
|
124 |
|
static VIEW lastview; /* the previous view input */ |
125 |
|
|
126 |
|
static void report(int); |
138 |
|
|
139 |
|
|
140 |
|
|
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 |
– |
|
141 |
|
void |
142 |
< |
quit(code) /* quit program */ |
154 |
< |
int code; |
142 |
> |
quit(int code) /* quit program */ |
143 |
|
{ |
144 |
|
if (code) /* report status */ |
145 |
|
report(0); |
155 |
|
static void |
156 |
|
report(int dummy) /* report progress */ |
157 |
|
{ |
158 |
< |
double u, s; |
158 |
> |
char bcStat [128]; |
159 |
> |
double u, s; |
160 |
|
#ifdef BSD |
161 |
< |
struct rusage rubuf; |
161 |
> |
struct rusage rubuf; |
162 |
|
#else |
163 |
< |
struct tms tbuf; |
164 |
< |
double period; |
163 |
> |
double period = 1.0 / 60.0; |
164 |
> |
struct tms tbuf; |
165 |
|
#endif |
166 |
|
|
167 |
|
tlastrept = time((time_t *)NULL); |
168 |
|
#ifdef BSD |
169 |
|
getrusage(RUSAGE_SELF, &rubuf); |
170 |
< |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
171 |
< |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
170 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; |
171 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; |
172 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
173 |
< |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
174 |
< |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
173 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; |
174 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; |
175 |
|
#else |
176 |
|
times(&tbuf); |
177 |
|
#ifdef _SC_CLK_TCK |
178 |
|
period = 1.0 / sysconf(_SC_CLK_TCK); |
190 |
– |
#else |
191 |
– |
period = 1.0 / 60.0; |
179 |
|
#endif |
180 |
|
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
181 |
|
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
182 |
|
#endif |
183 |
|
|
184 |
+ |
/* PMAP: Get photon map bias compensation statistics */ |
185 |
+ |
pmapBiasCompReport(bcStat); |
186 |
+ |
|
187 |
|
sprintf(errmsg, |
188 |
< |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
189 |
< |
nrays, pctdone, u/3600., s/3600., |
190 |
< |
(tlastrept-tstart)/3600., myhostname()); |
188 |
> |
"%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n", |
189 |
> |
nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.), |
190 |
> |
(tlastrept-tstart)*(1./3600.), myhostname(), getpid()); |
191 |
|
eputs(errmsg); |
192 |
|
#ifdef SIGCONT |
193 |
|
signal(SIGCONT, report); |
197 |
|
static void |
198 |
|
report(int dummy) /* report progress */ |
199 |
|
{ |
200 |
+ |
char bcStat [128]; |
201 |
+ |
|
202 |
|
tlastrept = time((time_t *)NULL); |
203 |
< |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
204 |
< |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
203 |
> |
|
204 |
> |
/* PMAP: Get photon map bias compensation statistics */ |
205 |
> |
pmapBiasCompReport(bcStat); |
206 |
> |
|
207 |
> |
sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n", |
208 |
> |
nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0); |
209 |
|
eputs(errmsg); |
210 |
|
} |
211 |
|
#endif |
212 |
|
|
213 |
|
|
214 |
< |
extern void |
214 |
> |
void |
215 |
|
rpict( /* generate image(s) */ |
216 |
|
int seq, |
217 |
|
char *pout, |
291 |
|
break; |
292 |
|
pctdone = 0.0; |
293 |
|
if (pout != NULL) { |
294 |
+ |
int myfd; |
295 |
+ |
close(1); /* reassign stdout */ |
296 |
|
sprintf(fbuf, pout, seq); |
297 |
< |
if (file_exists(fbuf)) { |
298 |
< |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
297 |
> |
tryagain: |
298 |
> |
errno = 0; /* exclusive open */ |
299 |
> |
if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { |
300 |
> |
if ((errno != EEXIST) | (prvr != NULL) || |
301 |
> |
!strcmp(fbuf, pout)) { |
302 |
|
sprintf(errmsg, |
303 |
< |
"output file \"%s\" exists", |
303 |
> |
"cannot open output file \"%s\"", |
304 |
|
fbuf); |
305 |
< |
error(USER, errmsg); |
305 |
> |
error(SYSTEM, errmsg); |
306 |
|
} |
307 |
|
setview(&ourview); |
308 |
|
continue; /* don't clobber */ |
309 |
|
} |
310 |
< |
if (freopen(fbuf, "w", stdout) == NULL) { |
311 |
< |
sprintf(errmsg, |
312 |
< |
"cannot open output file \"%s\"", fbuf); |
312 |
< |
error(SYSTEM, errmsg); |
310 |
> |
if (myfd != 1) { |
311 |
> |
unlink(fbuf); |
312 |
> |
goto tryagain; /* leave it open */ |
313 |
|
} |
314 |
< |
SET_FILE_BINARY(stdout); |
314 |
> |
SET_FD_BINARY(1); |
315 |
|
dupheader(); |
316 |
|
} |
317 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
344 |
|
if ((pa < .99) | (pa > 1.01)) |
345 |
|
fputaspect(pa, stdout); |
346 |
|
fputnow(stdout); |
347 |
< |
fputformat(COLRFMT, stdout); |
348 |
< |
putchar('\n'); |
347 |
> |
if (out_prims == xyzprims) { |
348 |
> |
fputformat(CIEFMT, stdout); |
349 |
> |
} else { |
350 |
> |
fputprims(out_prims, stdout); |
351 |
> |
fputformat(COLRFMT, stdout); |
352 |
> |
} |
353 |
> |
putchar('\n'); /* close header */ |
354 |
|
if (zout != NULL) |
355 |
|
sprintf(cp=fbuf, zout, seq); |
356 |
|
else |
386 |
|
char *oldfile |
387 |
|
) |
388 |
|
{ |
389 |
+ |
const int srcdrawing = /* manually draw tiny light sources? */ |
390 |
+ |
(directvis && dblur <= FTINY && (mblur <= FTINY) | !lastview.type); |
391 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
392 |
|
float *zbar[MAXDIV+1]; /* z values */ |
393 |
|
char *sampdens; /* previous sample density */ |
454 |
|
signal(SIGCONT, report); |
455 |
|
#endif |
456 |
|
ypos = vres-1 - i; /* initialize sampling */ |
457 |
< |
if (directvis) |
457 |
> |
if (srcdrawing) |
458 |
|
init_drawsources(psample); |
459 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
460 |
|
/* compute scanlines */ |
475 |
|
hres, ypos, hstep); |
476 |
|
/* fill bar */ |
477 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
478 |
< |
if (directvis) /* add bitty sources */ |
479 |
< |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
478 |
> |
if (srcdrawing) /* add bitty sources */ |
479 |
> |
drawsources((COLORV **)scanbar, out_prims, zbar, 0, hres, ypos, ystep); |
480 |
|
/* write it out */ |
481 |
|
#ifdef SIGCONT |
482 |
|
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
672 |
|
{ |
673 |
|
RAY thisray; |
674 |
|
FVECT lorg, ldir; |
675 |
< |
double hpos, vpos, vdist, lmax; |
675 |
> |
double hpos, vpos, lmax; |
676 |
|
int i; |
677 |
|
/* compute view ray */ |
678 |
|
setcolor(col, 0.0, 0.0, 0.0); |
681 |
|
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
682 |
|
&ourview, hpos, vpos)) < -FTINY) |
683 |
|
return(0.0); |
677 |
– |
|
678 |
– |
vdist = ourview.vdist; |
684 |
|
/* set pixel index */ |
685 |
|
samplendx = pixnumber(x,y,hres,vres); |
686 |
|
/* optional motion blur */ |
695 |
|
} |
696 |
|
if (normalize(thisray.rdir) == 0.0) |
697 |
|
return(0.0); |
693 |
– |
vdist = (1.-d)*vdist + d*lastview.vdist; |
698 |
|
} |
699 |
< |
/* optional depth-of-field */ |
700 |
< |
if (dblur > FTINY) { |
701 |
< |
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 |
< |
} |
699 |
> |
/* depth-of-field */ |
700 |
> |
if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur)) |
701 |
> |
return(0.0); |
702 |
|
|
703 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
704 |
< |
|
704 |
> |
#ifdef SSKIPOPT |
705 |
> |
sskip_ray(&thisray, hpos, vpos); /* source skip hack */ |
706 |
> |
#endif |
707 |
|
rayvalue(&thisray); /* trace ray */ |
708 |
+ |
/* -> color */ |
709 |
+ |
scolor_out(col, out_prims, thisray.rcol); |
710 |
|
|
711 |
< |
copycolor(col, thisray.rcol); /* return color */ |
739 |
< |
|
740 |
< |
return(thisray.rt); /* return distance */ |
711 |
> |
return(raydistance(&thisray)); /* return distance */ |
712 |
|
} |
713 |
|
|
714 |
|
|
773 |
|
} |
774 |
|
|
775 |
|
static int |
776 |
< |
pixnumber( /* compute pixel index (brushed) */ |
776 |
> |
pixnumber( /* compute pixel index (screen door) */ |
777 |
|
int x, |
778 |
|
int y, |
779 |
|
int xres, |
780 |
|
int yres |
781 |
|
) |
782 |
|
{ |
783 |
< |
x -= y; |
784 |
< |
while (x < 0) |
785 |
< |
x += xres; |
786 |
< |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
783 |
> |
unsigned nbits = 0; |
784 |
> |
bitmask_t coord[2]; |
785 |
> |
|
786 |
> |
if (xres < yres) xres = yres; |
787 |
> |
while (xres > 0) { |
788 |
> |
xres >>= 1; |
789 |
> |
++nbits; |
790 |
> |
} |
791 |
> |
coord[0] = x; coord[1] = y; |
792 |
> |
return ((int)hilbert_c2i(2, nbits, coord)); |
793 |
|
} |