--- ray/src/rt/rpict.c 2009/05/11 21:46:31 2.81 +++ ray/src/rt/rpict.c 2023/01/24 21:54:49 2.98 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rpict.c,v 2.81 2009/05/11 21:46:31 greg Exp $"; +static const char RCSid[] = "$Id: rpict.c,v 2.98 2023/01/24 21:54:49 greg Exp $"; #endif /* * rpict.c - routines and variables for picture generation. @@ -33,9 +33,10 @@ static const char RCSid[] = "$Id: rpict.c,v 2.81 2009/ #include "view.h" #include "random.h" #include "paths.h" -#include "rtmisc.h" /* myhostname() */ +#include "hilbert.h" +#include "pmapbias.h" +#include "pmapdiag.h" - #define RFTEMPLATE "rfXXXXXX" #ifndef SIGCONT @@ -51,7 +52,6 @@ int dimlist[MAXDIM]; /* sampling dimensions */ int ndims = 0; /* number of sampling dimensions */ int samplendx; /* sample index number */ -//extern void ambnotify(); void (*addobjnotify[])() = {ambnotify, NULL}; VIEW ourview = STDVIEW; /* view parameters */ @@ -92,7 +92,7 @@ double specjitter = 1.; /* specular sampling jitter * int backvis = 1; /* back face visibility */ int maxdepth = 7; /* maximum recursion depth */ -double minweight = 4e-3; /* minimum ray weight */ +double minweight = 1e-4; /* minimum ray weight */ char *ambfile = NULL; /* ambient file name */ COLOR ambval = BLKCOLOR; /* ambient value */ @@ -119,10 +119,6 @@ int hres, vres; /* resolution for this frame */ static VIEW lastview; /* the previous view input */ -//extern char *mktemp(); /* XXX should be in stdlib.h or unistd.h */ - -//double pixvalue(); - static void report(int); static int nextview(FILE *fp); static void render(char *zfile, char *oldfile); @@ -138,25 +134,8 @@ static int pixnumber(int x, int y, int xres, int y -#ifdef RHAS_STAT -#include -#include -int -file_exists(fname) /* ordinary file exists? */ -char *fname; -{ - struct stat sbuf; - if (stat(fname, &sbuf) < 0) return(0); - return((sbuf.st_mode & S_IFREG) != 0); -} -#else -#define file_exists(f) (access(f,F_OK)==0) -#endif - - void -quit(code) /* quit program */ -int code; +quit(int code) /* quit program */ { if (code) /* report status */ report(0); @@ -172,37 +151,39 @@ int code; static void report(int dummy) /* report progress */ { - double u, s; + char bcStat [128]; + double u, s; #ifdef BSD - struct rusage rubuf; + struct rusage rubuf; #else - struct tms tbuf; - double period; + double period = 1.0 / 60.0; + struct tms tbuf; #endif tlastrept = time((time_t *)NULL); #ifdef BSD getrusage(RUSAGE_SELF, &rubuf); - u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; - s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; + u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; + s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; getrusage(RUSAGE_CHILDREN, &rubuf); - u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; - s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; + u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; + s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; #else times(&tbuf); #ifdef _SC_CLK_TCK period = 1.0 / sysconf(_SC_CLK_TCK); -#else - period = 1.0 / 60.0; #endif u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; #endif + /* PMAP: Get photon map bias compensation statistics */ + pmapBiasCompReport(bcStat); + sprintf(errmsg, - "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", - nrays, pctdone, u/3600., s/3600., - (tlastrept-tstart)/3600., myhostname()); + "%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n", + nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.), + (tlastrept-tstart)*(1./3600.), myhostname(), getpid()); eputs(errmsg); #ifdef SIGCONT signal(SIGCONT, report); @@ -212,15 +193,21 @@ report(int dummy) /* report progress */ static void report(int dummy) /* report progress */ { + char bcStat [128]; + tlastrept = time((time_t *)NULL); - sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", - nrays, pctdone, (tlastrept-tstart)/3600.0); + + /* PMAP: Get photon map bias compensation statistics */ + pmapBiasCompReport(bcStat); + + sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n", + nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0); eputs(errmsg); } #endif -extern void +void rpict( /* generate image(s) */ int seq, char *pout, @@ -241,7 +228,7 @@ rpict( /* generate image(s) */ { char fbuf[128], fbuf2[128]; int npicts; - register char *cp; + char *cp; RESOLU rs; double pa; /* check sampling */ @@ -257,7 +244,7 @@ rpict( /* generate image(s) */ if (seq <= 0) seq = 0; else if (prvr != NULL && isint(prvr)) { - register int rn; /* skip to specified view */ + int rn; /* skip to specified view */ if ((rn = atoi(prvr)) < seq) error(USER, "recover frame less than start frame"); if (pout == NULL) @@ -300,23 +287,27 @@ rpict( /* generate image(s) */ break; pctdone = 0.0; if (pout != NULL) { + int myfd; + close(1); /* reassign stdout */ sprintf(fbuf, pout, seq); - if (file_exists(fbuf)) { - if (prvr != NULL || !strcmp(fbuf, pout)) { + tryagain: + errno = 0; /* exclusive open */ + if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { + if ((errno != EEXIST) | (prvr != NULL) || + !strcmp(fbuf, pout)) { sprintf(errmsg, - "output file \"%s\" exists", + "cannot open output file \"%s\"", fbuf); - error(USER, errmsg); + error(SYSTEM, errmsg); } setview(&ourview); continue; /* don't clobber */ } - if (freopen(fbuf, "w", stdout) == NULL) { - sprintf(errmsg, - "cannot open output file \"%s\"", fbuf); - error(SYSTEM, errmsg); + if (myfd != 1) { + unlink(fbuf); + goto tryagain; /* leave it open */ } - SET_FILE_BINARY(stdout); + SET_FD_BINARY(1); dupheader(); } hres = hresolu; vres = vresolu; pa = pixaspect; @@ -346,9 +337,10 @@ rpict( /* generate image(s) */ fputs(VIEWSTR, stdout); fprintview(&ourview, stdout); putchar('\n'); - if (pa < .99 || pa > 1.01) + if ((pa < .99) | (pa > 1.01)) fputaspect(pa, stdout); fputnow(stdout); + fputprims(stdprims, stdout); fputformat(COLRFMT, stdout); putchar('\n'); if (zout != NULL) @@ -395,9 +387,9 @@ render( /* render the scene */ int zfd; COLOR *colptr; float *zptr; - register int i; + int i; /* check for empty image */ - if (hres <= 0 || vres <= 0) { + if ((hres <= 0) | (vres <= 0)) { error(WARNING, "empty output picture"); fprtresolu(0, 0, stdout); return; @@ -441,7 +433,7 @@ render( /* render the scene */ i = salvage(oldfile); if (i >= vres) goto alldone; - if (zfd != -1 && i > 0 && + if ((zfd != -1) & (i > 0) && lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0) error(SYSTEM, "z-file seek error in render"); pctdone = 100.0*i/vres; @@ -535,9 +527,9 @@ memerr: static void fillscanline( /* fill scan at y */ - register COLOR *scanline, - register float *zline, - register char *sd, + COLOR *scanline, + float *zline, + char *sd, int xres, int y, int xstep @@ -546,7 +538,7 @@ fillscanline( /* fill scan at y */ static int nc = 0; /* number of calls */ int bl = xstep, b = xstep; double z; - register int i; + int i; z = pixvalue(scanline[0], 0, y); if (zline) zline[0] = z; @@ -574,8 +566,8 @@ fillscanline( /* fill scan at y */ static void fillscanbar( /* fill interior */ - register COLOR *scanbar[], - register float *zbar[], + COLOR *scanbar[], + float *zbar[], int xres, int y, int ysize @@ -584,7 +576,7 @@ fillscanbar( /* fill interior */ COLOR vline[MAXDIV+1]; float zline[MAXDIV+1]; int b = ysize; - register int i, j; + int i, j; for (i = 0; i < xres; i++) { copycolor(vline[0], scanbar[0][i]); @@ -607,8 +599,8 @@ fillscanbar( /* fill interior */ static int fillsample( /* fill interior points */ - register COLOR *colline, - register float *zline, + COLOR *colline, + float *zline, int x, int y, int xlen, @@ -620,7 +612,7 @@ fillsample( /* fill interior points */ double z; COLOR ctmp; int ncut; - register int len; + int len; if (xlen > 0) /* x or y length is zero */ len = xlen; @@ -670,23 +662,21 @@ pixvalue( /* compute pixel value */ { RAY thisray; FVECT lorg, ldir; - double hpos, vpos, vdist, lmax; - register int i; + double hpos, vpos, lmax; + int i; /* compute view ray */ + setcolor(col, 0.0, 0.0, 0.0); hpos = (x+pixjitter())/hres; vpos = (y+pixjitter())/vres; if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, - &ourview, hpos, vpos)) < -FTINY) { - setcolor(col, 0.0, 0.0, 0.0); + &ourview, hpos, vpos)) < -FTINY) return(0.0); - } - vdist = ourview.vdist; /* set pixel index */ samplendx = pixnumber(x,y,hres,vres); /* optional motion blur */ if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, &lastview, hpos, vpos)) >= -FTINY) { - register double d = mblur*(.5-urand(281+samplendx)); + double d = mblur*(.5-urand(281+samplendx)); thisray.rmax = (1.-d)*thisray.rmax + d*lmax; for (i = 3; i--; ) { @@ -695,43 +685,10 @@ pixvalue( /* compute pixel value */ } if (normalize(thisray.rdir) == 0.0) return(0.0); - vdist = (1.-d)*vdist + d*lastview.vdist; } - /* optional depth-of-field */ - if (dblur > FTINY && vdist > FTINY) { - double vc, dfh, dfv; - /* square/circle conv. */ - dfh = vc = frandom(); - dfv = frandom(); - dfh *= .5*dblur*sqrt(1. - .5*dfv*dfv); - dfv *= .5*dblur*sqrt(1. - .5*vc*vc); - if (ourview.type == VT_PER || ourview.type == VT_PAR) { - dfh /= sqrt(ourview.hn2); - dfv /= sqrt(ourview.vn2); - for (i = 3; i--; ) { - vc = thisray.rorg[i] + vdist*thisray.rdir[i]; - thisray.rorg[i] += dfh*ourview.hvec[i] + - dfv*ourview.vvec[i] ; - thisray.rdir[i] = vc - thisray.rorg[i]; - } - } else { /* non-standard view case */ - double dfd = PI/4.*dblur*(.5 - frandom()); - if (ourview.type != VT_ANG && ourview.type != VT_PLS) { - if (ourview.type != VT_CYL) - dfh /= sqrt(ourview.hn2); - dfv /= sqrt(ourview.vn2); - } - for (i = 3; i--; ) { - vc = thisray.rorg[i] + vdist*thisray.rdir[i]; - thisray.rorg[i] += dfh*ourview.hvec[i] + - dfv*ourview.vvec[i] + - dfd*ourview.vdir[i] ; - thisray.rdir[i] = vc - thisray.rorg[i]; - } - } - if (normalize(thisray.rdir) == 0.0) - return(0.0); - } + /* depth-of-field */ + if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur)) + return(0.0); rayorigin(&thisray, PRIMARY, NULL, NULL); @@ -739,7 +696,7 @@ pixvalue( /* compute pixel value */ copycolor(col, thisray.rcol); /* return color */ - return(thisray.rt); /* return distance */ + return(raydistance(&thisray)); /* return distance */ } @@ -772,7 +729,7 @@ salvage( /* salvage scanlines from killed program */ goto gotzip; } - if (x != hres || y != vres) { + if ((x != hres) | (y != vres)) { sprintf(errmsg, "resolution mismatch in recover file \"%s\"", oldfile); error(USER, errmsg); @@ -804,15 +761,21 @@ writerr: } static int -pixnumber( /* compute pixel index (brushed) */ - register int x, - register int y, +pixnumber( /* compute pixel index (screen door) */ + int x, + int y, int xres, int yres ) { - x -= y; - while (x < 0) - x += xres; - return((((x>>2)*yres + y) << 2) + (x & 3)); + unsigned nbits = 0; + bitmask_t coord[2]; + + if (xres < yres) xres = yres; + while (xres > 0) { + xres >>= 1; + ++nbits; + } + coord[0] = x; coord[1] = y; + return ((int)hilbert_c2i(2, nbits, coord)); }