9 |
|
|
10 |
|
#include <sys/types.h> |
11 |
|
|
12 |
< |
#ifndef NON_POSIX |
13 |
< |
#ifdef BSD |
14 |
< |
#include <sys/time.h> |
15 |
< |
#include <sys/resource.h> |
12 |
> |
#include "platform.h" |
13 |
> |
#ifdef NON_POSIX |
14 |
> |
#ifdef MINGW |
15 |
> |
#include <sys/time.h> |
16 |
> |
#endif |
17 |
|
#else |
18 |
< |
#include <sys/times.h> |
19 |
< |
#include <unistd.h> |
18 |
> |
#ifdef BSD |
19 |
> |
#include <sys/time.h> |
20 |
> |
#include <sys/resource.h> |
21 |
> |
#else |
22 |
> |
#include <sys/times.h> |
23 |
> |
#include <unistd.h> |
24 |
> |
#endif |
25 |
|
#endif |
20 |
– |
#endif |
26 |
|
|
27 |
|
#include <time.h> |
28 |
|
#include <signal.h> |
29 |
|
|
25 |
– |
#include "platform.h" |
30 |
|
#include "ray.h" |
31 |
|
#include "paths.h" |
32 |
|
#include "ambient.h" |
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 |
|
|
34 |
– |
|
40 |
|
#define RFTEMPLATE "rfXXXXXX" |
41 |
|
|
42 |
|
#ifndef SIGCONT |
52 |
|
int ndims = 0; /* number of sampling dimensions */ |
53 |
|
int samplendx; /* sample index number */ |
54 |
|
|
50 |
– |
//extern void ambnotify(); |
55 |
|
void (*addobjnotify[])() = {ambnotify, NULL}; |
56 |
|
|
57 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
71 |
|
|
72 |
|
int do_irrad = 0; /* compute irradiance? */ |
73 |
|
|
74 |
+ |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
75 |
+ |
|
76 |
|
double dstrsrc = 0.0; /* square source distribution */ |
77 |
|
double shadthresh = .05; /* shadow threshold */ |
78 |
|
double shadcert = .5; /* shadow certainty */ |
92 |
|
int backvis = 1; /* back face visibility */ |
93 |
|
|
94 |
|
int maxdepth = 7; /* maximum recursion depth */ |
95 |
< |
double minweight = 4e-3; /* minimum ray weight */ |
95 |
> |
double minweight = 1e-3; /* minimum ray weight */ |
96 |
|
|
97 |
|
char *ambfile = NULL; /* ambient file name */ |
98 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
119 |
|
|
120 |
|
static VIEW lastview; /* the previous view input */ |
121 |
|
|
116 |
– |
//extern char *mktemp(); /* XXX should be in stdlib.h or unistd.h */ |
117 |
– |
|
118 |
– |
//double pixvalue(); |
119 |
– |
|
122 |
|
static void report(int); |
123 |
|
static int nextview(FILE *fp); |
124 |
|
static void render(char *zfile, char *oldfile); |
134 |
|
|
135 |
|
|
136 |
|
|
135 |
– |
#ifdef RHAS_STAT |
136 |
– |
#include <sys/types.h> |
137 |
– |
#include <sys/stat.h> |
138 |
– |
int |
139 |
– |
file_exists(fname) /* ordinary file exists? */ |
140 |
– |
char *fname; |
141 |
– |
{ |
142 |
– |
struct stat sbuf; |
143 |
– |
if (stat(fname, &sbuf) < 0) return(0); |
144 |
– |
return((sbuf.st_mode & S_IFREG) != 0); |
145 |
– |
} |
146 |
– |
#else |
147 |
– |
#define file_exists(f) (access(f,F_OK)==0) |
148 |
– |
#endif |
149 |
– |
|
150 |
– |
|
137 |
|
void |
138 |
|
quit(code) /* quit program */ |
139 |
|
int code; |
152 |
|
static void |
153 |
|
report(int dummy) /* report progress */ |
154 |
|
{ |
155 |
< |
double u, s; |
155 |
> |
char bcStat [128]; |
156 |
> |
double u, s; |
157 |
|
#ifdef BSD |
158 |
< |
struct rusage rubuf; |
158 |
> |
struct rusage rubuf; |
159 |
|
#else |
160 |
< |
struct tms tbuf; |
161 |
< |
double period; |
160 |
> |
double period = 1.0 / 60.0; |
161 |
> |
struct tms tbuf; |
162 |
|
#endif |
163 |
|
|
164 |
|
tlastrept = time((time_t *)NULL); |
165 |
|
#ifdef BSD |
166 |
|
getrusage(RUSAGE_SELF, &rubuf); |
167 |
< |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
168 |
< |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
167 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; |
168 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; |
169 |
|
getrusage(RUSAGE_CHILDREN, &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 |
|
#else |
173 |
|
times(&tbuf); |
174 |
|
#ifdef _SC_CLK_TCK |
175 |
|
period = 1.0 / sysconf(_SC_CLK_TCK); |
189 |
– |
#else |
190 |
– |
period = 1.0 / 60.0; |
176 |
|
#endif |
177 |
|
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
178 |
|
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
179 |
|
#endif |
180 |
|
|
181 |
+ |
/* PMAP: Get photon map bias compensation statistics */ |
182 |
+ |
pmapBiasCompReport(bcStat); |
183 |
+ |
|
184 |
|
sprintf(errmsg, |
185 |
< |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
186 |
< |
nrays, pctdone, u/3600., s/3600., |
187 |
< |
(tlastrept-tstart)/3600., myhostname()); |
185 |
> |
"%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n", |
186 |
> |
nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.), |
187 |
> |
(tlastrept-tstart)*(1./3600.), myhostname(), getpid()); |
188 |
|
eputs(errmsg); |
189 |
|
#ifdef SIGCONT |
190 |
|
signal(SIGCONT, report); |
194 |
|
static void |
195 |
|
report(int dummy) /* report progress */ |
196 |
|
{ |
197 |
+ |
char bcStat [128]; |
198 |
+ |
|
199 |
|
tlastrept = time((time_t *)NULL); |
200 |
< |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
201 |
< |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
200 |
> |
|
201 |
> |
/* PMAP: Get photon map bias compensation statistics */ |
202 |
> |
pmapBiasCompReport(bcStat); |
203 |
> |
|
204 |
> |
sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n", |
205 |
> |
nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0); |
206 |
|
eputs(errmsg); |
207 |
|
} |
208 |
|
#endif |
209 |
|
|
210 |
|
|
211 |
< |
extern void |
211 |
> |
void |
212 |
|
rpict( /* generate image(s) */ |
213 |
|
int seq, |
214 |
|
char *pout, |
229 |
|
{ |
230 |
|
char fbuf[128], fbuf2[128]; |
231 |
|
int npicts; |
232 |
< |
register char *cp; |
232 |
> |
char *cp; |
233 |
|
RESOLU rs; |
234 |
|
double pa; |
235 |
|
/* check sampling */ |
245 |
|
if (seq <= 0) |
246 |
|
seq = 0; |
247 |
|
else if (prvr != NULL && isint(prvr)) { |
248 |
< |
register int rn; /* skip to specified view */ |
248 |
> |
int rn; /* skip to specified view */ |
249 |
|
if ((rn = atoi(prvr)) < seq) |
250 |
|
error(USER, "recover frame less than start frame"); |
251 |
|
if (pout == NULL) |
288 |
|
break; |
289 |
|
pctdone = 0.0; |
290 |
|
if (pout != NULL) { |
291 |
+ |
int myfd; |
292 |
+ |
close(1); /* reassign stdout */ |
293 |
|
sprintf(fbuf, pout, seq); |
294 |
< |
if (file_exists(fbuf)) { |
295 |
< |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
294 |
> |
tryagain: |
295 |
> |
errno = 0; /* exclusive open */ |
296 |
> |
if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { |
297 |
> |
if ((errno != EEXIST) | (prvr != NULL) || |
298 |
> |
!strcmp(fbuf, pout)) { |
299 |
|
sprintf(errmsg, |
300 |
< |
"output file \"%s\" exists", |
300 |
> |
"cannot open output file \"%s\"", |
301 |
|
fbuf); |
302 |
< |
error(USER, errmsg); |
302 |
> |
error(SYSTEM, errmsg); |
303 |
|
} |
304 |
|
setview(&ourview); |
305 |
|
continue; /* don't clobber */ |
306 |
|
} |
307 |
< |
if (freopen(fbuf, "w", stdout) == NULL) { |
308 |
< |
sprintf(errmsg, |
309 |
< |
"cannot open output file \"%s\"", fbuf); |
311 |
< |
error(SYSTEM, errmsg); |
307 |
> |
if (myfd != 1) { |
308 |
> |
unlink(fbuf); |
309 |
> |
goto tryagain; /* leave it open */ |
310 |
|
} |
311 |
< |
SET_FILE_BINARY(stdout); |
311 |
> |
SET_FD_BINARY(1); |
312 |
|
dupheader(); |
313 |
|
} |
314 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
338 |
|
fputs(VIEWSTR, stdout); |
339 |
|
fprintview(&ourview, stdout); |
340 |
|
putchar('\n'); |
341 |
< |
if (pa < .99 || pa > 1.01) |
341 |
> |
if ((pa < .99) | (pa > 1.01)) |
342 |
|
fputaspect(pa, stdout); |
343 |
|
fputnow(stdout); |
344 |
|
fputformat(COLRFMT, stdout); |
387 |
|
int zfd; |
388 |
|
COLOR *colptr; |
389 |
|
float *zptr; |
390 |
< |
register int i; |
390 |
> |
int i; |
391 |
|
/* check for empty image */ |
392 |
< |
if (hres <= 0 || vres <= 0) { |
392 |
> |
if ((hres <= 0) | (vres <= 0)) { |
393 |
|
error(WARNING, "empty output picture"); |
394 |
|
fprtresolu(0, 0, stdout); |
395 |
|
return; |
433 |
|
i = salvage(oldfile); |
434 |
|
if (i >= vres) |
435 |
|
goto alldone; |
436 |
< |
if (zfd != -1 && i > 0 && |
436 |
> |
if ((zfd != -1) & (i > 0) && |
437 |
|
lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0) |
438 |
|
error(SYSTEM, "z-file seek error in render"); |
439 |
|
pctdone = 100.0*i/vres; |
527 |
|
|
528 |
|
static void |
529 |
|
fillscanline( /* fill scan at y */ |
530 |
< |
register COLOR *scanline, |
531 |
< |
register float *zline, |
532 |
< |
register char *sd, |
530 |
> |
COLOR *scanline, |
531 |
> |
float *zline, |
532 |
> |
char *sd, |
533 |
|
int xres, |
534 |
|
int y, |
535 |
|
int xstep |
538 |
|
static int nc = 0; /* number of calls */ |
539 |
|
int bl = xstep, b = xstep; |
540 |
|
double z; |
541 |
< |
register int i; |
541 |
> |
int i; |
542 |
|
|
543 |
|
z = pixvalue(scanline[0], 0, y); |
544 |
|
if (zline) zline[0] = z; |
566 |
|
|
567 |
|
static void |
568 |
|
fillscanbar( /* fill interior */ |
569 |
< |
register COLOR *scanbar[], |
570 |
< |
register float *zbar[], |
569 |
> |
COLOR *scanbar[], |
570 |
> |
float *zbar[], |
571 |
|
int xres, |
572 |
|
int y, |
573 |
|
int ysize |
576 |
|
COLOR vline[MAXDIV+1]; |
577 |
|
float zline[MAXDIV+1]; |
578 |
|
int b = ysize; |
579 |
< |
register int i, j; |
579 |
> |
int i, j; |
580 |
|
|
581 |
|
for (i = 0; i < xres; i++) { |
582 |
|
copycolor(vline[0], scanbar[0][i]); |
599 |
|
|
600 |
|
static int |
601 |
|
fillsample( /* fill interior points */ |
602 |
< |
register COLOR *colline, |
603 |
< |
register float *zline, |
602 |
> |
COLOR *colline, |
603 |
> |
float *zline, |
604 |
|
int x, |
605 |
|
int y, |
606 |
|
int xlen, |
612 |
|
double z; |
613 |
|
COLOR ctmp; |
614 |
|
int ncut; |
615 |
< |
register int len; |
615 |
> |
int len; |
616 |
|
|
617 |
|
if (xlen > 0) /* x or y length is zero */ |
618 |
|
len = xlen; |
660 |
|
int y |
661 |
|
) |
662 |
|
{ |
663 |
+ |
extern void SDsquare2disk(double ds[2], double seedx, double seedy); |
664 |
|
RAY thisray; |
665 |
|
FVECT lorg, ldir; |
666 |
|
double hpos, vpos, vdist, lmax; |
667 |
< |
register int i; |
667 |
> |
int i; |
668 |
|
/* compute view ray */ |
669 |
+ |
setcolor(col, 0.0, 0.0, 0.0); |
670 |
|
hpos = (x+pixjitter())/hres; |
671 |
|
vpos = (y+pixjitter())/vres; |
672 |
|
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
673 |
< |
&ourview, hpos, vpos)) < -FTINY) { |
674 |
< |
setcolor(col, 0.0, 0.0, 0.0); |
673 |
> |
&ourview, hpos, vpos)) < -FTINY) |
674 |
|
return(0.0); |
676 |
– |
} |
677 |
– |
vdist = ourview.vdist; |
675 |
|
|
676 |
< |
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
677 |
< |
|
676 |
> |
vdist = ourview.vdist; |
677 |
> |
/* set pixel index */ |
678 |
> |
samplendx = pixnumber(x,y,hres,vres); |
679 |
|
/* optional motion blur */ |
680 |
|
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
681 |
|
&lastview, hpos, vpos)) >= -FTINY) { |
682 |
< |
register double d = mblur*(.5-urand(281+samplendx)); |
682 |
> |
double d = mblur*(.5-urand(281+samplendx)); |
683 |
|
|
684 |
|
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
685 |
|
for (i = 3; i--; ) { |
691 |
|
vdist = (1.-d)*vdist + d*lastview.vdist; |
692 |
|
} |
693 |
|
/* optional depth-of-field */ |
694 |
< |
if (dblur > FTINY && vdist > FTINY) { |
694 |
> |
if (dblur > FTINY) { |
695 |
|
double vc, df[2]; |
696 |
< |
df[0] = PI/4.*dblur*(.5 - frandom())/sqrt(ourview.hn2); |
697 |
< |
df[1] = PI/4.*dblur*(.5 - frandom())/sqrt(ourview.vn2); |
698 |
< |
for (i = 3; i--; ) { |
699 |
< |
vc = thisray.rorg[i] + vdist*thisray.rdir[i]; |
700 |
< |
thisray.rorg[i] += df[0]*ourview.hvec[i] + |
701 |
< |
df[1]*ourview.vvec[i] ; |
702 |
< |
thisray.rdir[i] = vc - thisray.rorg[i]; |
696 |
> |
/* random point on disk */ |
697 |
> |
SDsquare2disk(df, frandom(), frandom()); |
698 |
> |
df[0] *= .5*dblur; |
699 |
> |
df[1] *= .5*dblur; |
700 |
> |
if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) { |
701 |
> |
double adj = 1.0; |
702 |
> |
if (ourview.type == VT_PER) |
703 |
> |
adj /= DOT(thisray.rdir, ourview.vdir); |
704 |
> |
df[0] /= sqrt(ourview.hn2); |
705 |
> |
df[1] /= sqrt(ourview.vn2); |
706 |
> |
for (i = 3; i--; ) { |
707 |
> |
vc = ourview.vp[i] + adj*vdist*thisray.rdir[i]; |
708 |
> |
thisray.rorg[i] += df[0]*ourview.hvec[i] + |
709 |
> |
df[1]*ourview.vvec[i] ; |
710 |
> |
thisray.rdir[i] = vc - thisray.rorg[i]; |
711 |
> |
} |
712 |
> |
} else { /* non-standard view case */ |
713 |
> |
double dfd = PI/4.*dblur*(.5 - frandom()); |
714 |
> |
if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) { |
715 |
> |
if (ourview.type != VT_CYL) |
716 |
> |
df[0] /= sqrt(ourview.hn2); |
717 |
> |
df[1] /= sqrt(ourview.vn2); |
718 |
> |
} |
719 |
> |
for (i = 3; i--; ) { |
720 |
> |
vc = ourview.vp[i] + vdist*thisray.rdir[i]; |
721 |
> |
thisray.rorg[i] += df[0]*ourview.hvec[i] + |
722 |
> |
df[1]*ourview.vvec[i] + |
723 |
> |
dfd*ourview.vdir[i] ; |
724 |
> |
thisray.rdir[i] = vc - thisray.rorg[i]; |
725 |
> |
} |
726 |
|
} |
727 |
|
if (normalize(thisray.rdir) == 0.0) |
728 |
|
return(0.0); |
729 |
|
} |
730 |
|
|
731 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
731 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
732 |
|
|
733 |
|
rayvalue(&thisray); /* trace ray */ |
734 |
|
|
735 |
|
copycolor(col, thisray.rcol); /* return color */ |
736 |
|
|
737 |
< |
return(thisray.rt); /* return distance */ |
737 |
> |
return(raydistance(&thisray)); /* return distance */ |
738 |
|
} |
739 |
|
|
740 |
|
|
767 |
|
goto gotzip; |
768 |
|
} |
769 |
|
|
770 |
< |
if (x != hres || y != vres) { |
770 |
> |
if ((x != hres) | (y != vres)) { |
771 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
772 |
|
oldfile); |
773 |
|
error(USER, errmsg); |
799 |
|
} |
800 |
|
|
801 |
|
static int |
802 |
< |
pixnumber( /* compute pixel index (brushed) */ |
803 |
< |
register int x, |
804 |
< |
register int y, |
802 |
> |
pixnumber( /* compute pixel index (screen door) */ |
803 |
> |
int x, |
804 |
> |
int y, |
805 |
|
int xres, |
806 |
|
int yres |
807 |
|
) |
808 |
|
{ |
809 |
< |
x -= y; |
810 |
< |
while (x < 0) |
811 |
< |
x += xres; |
812 |
< |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
809 |
> |
unsigned nbits = 0; |
810 |
> |
bitmask_t coord[2]; |
811 |
> |
|
812 |
> |
if (xres < yres) xres = yres; |
813 |
> |
while (xres > 0) { |
814 |
> |
xres >>= 1; |
815 |
> |
++nbits; |
816 |
> |
} |
817 |
> |
coord[0] = x; coord[1] = y; |
818 |
> |
return ((int)hilbert_c2i(2, nbits, coord)); |
819 |
|
} |