--- ray/src/rt/rpict.c 1992/09/21 12:07:52 2.14 +++ ray/src/rt/rpict.c 1996/01/23 16:27:31 2.42 @@ -1,4 +1,4 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -12,11 +12,21 @@ static char SCCSid[] = "$SunId$ LBL"; #include "ray.h" +#include + +#ifndef NIX #ifdef BSD #include #include +#else +#include +#include +#include #endif +#endif +extern time_t time(); + #include #include "view.h" @@ -27,6 +37,12 @@ static char SCCSid[] = "$SunId$ LBL"; #include "paths.h" +#define RFTEMPLATE "rfXXXXXX" + +#ifndef SIGCONT +#define SIGCONT SIGIO +#endif + int dimlist[MAXDIM]; /* sampling dimensions */ int ndims = 0; /* number of sampling dimensions */ int samplendx; /* sample index number */ @@ -45,12 +61,19 @@ double shadthresh = .05; /* shadow threshold */ double shadcert = .5; /* shadow certainty */ int directrelay = 1; /* number of source relays */ int vspretest = 512; /* virtual source pretest density */ -int directinvis = 0; /* sources invisible? */ +int directvis = 1; /* sources visible? */ double srcsizerat = .25; /* maximum ratio source size/dist. */ +COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ +double salbedo = 0.; /* global scattering albedo */ +double seccg = 0.; /* global scattering eccentricity */ +double ssampdist = 0.; /* scatter sampling distance */ + double specthresh = .15; /* specular sampling threshold */ double specjitter = 1.; /* specular sampling jitter */ +int backvis = 1; /* back face visibility */ + int maxdepth = 6; /* maximum recursion depth */ double minweight = 5e-3; /* minimum ray weight */ @@ -63,108 +86,113 @@ int ambounce = 0; /* ambient bounces */ char *amblist[128]; /* ambient include/exclude list */ int ambincl = -1; /* include == 1, exclude == 0 */ +#ifdef MSDOS +int ralrm = 60; /* seconds between reports */ +#else int ralrm = 0; /* seconds between reports */ +#endif double pctdone = 0.0; /* percentage done */ -long tlastrept = 0L; /* time at last report */ +time_t tlastrept = 0L; /* time at last report */ -extern long time(); -extern long tstart; /* starting time */ +extern time_t tstart; /* starting time */ -extern long nrays; /* number of rays traced */ +extern unsigned long nrays; /* number of rays traced */ #define MAXDIV 16 /* maximum sample size */ #define pixjitter() (.5+dstrpix*(.5-frandom())) -#define RFTEMPLATE "rfXXXXXX" -#define HFTEMPLATE TEMPLATE - -static char *hfname = NULL; /* header file name */ -static FILE *hfp = NULL; /* header file pointer */ - static int hres, vres; /* resolution for this frame */ extern char *mktemp(); double pixvalue(); +#ifdef NIX +#define file_exists(f) (access(f,F_OK)==0) +#else +#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); +} +#endif + quit(code) /* quit program */ int code; { if (code) /* report status */ report(); - if (hfname != NULL) { /* delete header file */ - if (hfp != NULL) - fclose(hfp); - unlink(hfname); - } + headclean(); /* delete header file */ + pfclean(); /* clean up persist files */ exit(code); } -#ifdef BSD +#ifndef NIX report() /* report progress */ { + double u, s; +#ifdef BSD + char hostname[257]; struct rusage rubuf; - double t; +#else + struct tms tbuf; + struct utsname nambuf; + double period; +#define hostname nambuf.nodename +#endif + tlastrept = time((time_t *)NULL); +#ifdef BSD getrusage(RUSAGE_SELF, &rubuf); - t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; - t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; + u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; + s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; getrusage(RUSAGE_CHILDREN, &rubuf); - t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; - t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; + u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; + s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; + gethostname(hostname, sizeof(hostname)); +#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; + uname(&nambuf); +#endif - sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", - nrays, pctdone, t/3600.0); + sprintf(errmsg, + "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", + nrays, pctdone, u/3600., s/3600., + (tlastrept-tstart)/3600., hostname); eputs(errmsg); - tlastrept = time((long *)0); +#ifndef BSD + signal(SIGCONT, report); +#undef hostname +#endif } #else report() /* report progress */ { - tlastrept = time((long *)0); - sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", + tlastrept = time((time_t *)NULL); + sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", nrays, pctdone, (tlastrept-tstart)/3600.0); eputs(errmsg); - signal(SIGALRM, report); } #endif -openheader() /* save standard output to header file */ -{ - hfname = mktemp(HFTEMPLATE); - if (freopen(hfname, "w", stdout) == NULL) { - sprintf(errmsg, "cannot open header file \"%s\"", hfname); - error(SYSTEM, errmsg); - } -} - - -closeheader() /* done with header output */ -{ - if (hfname == NULL) - return; - if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL) - error(SYSTEM, "error reopening header file"); -} - - -dupheader() /* repeat header on standard output */ -{ - register int c; - - if (fseek(hfp, 0L, 0) < 0) - error(SYSTEM, "seek error on header file"); - while ((c = getc(hfp)) != EOF) - putchar(c); -} - - rpict(seq, pout, zout, prvr) /* generate image(s) */ int seq; char *pout, *zout, *prvr; @@ -180,13 +208,12 @@ char *pout, *zout, *prvr; * sequenced file naming. */ { - extern char *rindex(), *strncpy(), *strcat(); + extern char *rindex(), *strncpy(), *strcat(), *strcpy(); char fbuf[128], fbuf2[128]; + int npicts; register char *cp; RESOLU rs; double pa; - /* finished writing header */ - closeheader(); /* check sampling */ if (psample < 1) psample = 1; @@ -210,33 +237,55 @@ char *pout, *zout, *prvr; error(USER, "unexpected EOF on view input"); prvr = fbuf; /* mark for renaming */ } - if (pout != NULL) { + if (pout != NULL & prvr != NULL) { sprintf(fbuf, pout, seq); - if (prvr != NULL && !strcmp(prvr, fbuf)) { /* rename */ - fbuf2[0] = '\0'; - if ((cp = rindex(fbuf, '/')) != NULL) - strncpy(fbuf2, fbuf, cp-fbuf+1); - strcat(fbuf2, RFTEMPLATE); + if (!strcmp(prvr, fbuf)) { /* rename */ + strcpy(fbuf2, fbuf); + for (cp = fbuf2; *cp; cp++) + ; + while (cp > fbuf2 && !ISDIRSEP(cp[-1])) + cp--; + strcpy(cp, RFTEMPLATE); prvr = mktemp(fbuf2); - if (rename(fbuf, prvr) < 0 && errno != ENOENT) { - sprintf(errmsg, + if (rename(fbuf, prvr) < 0) + if (errno == ENOENT) { /* ghost file */ + sprintf(errmsg, + "new output file \"%s\"", + fbuf); + error(WARNING, errmsg); + prvr = NULL; + } else { /* serious error */ + sprintf(errmsg, "cannot rename \"%s\" to \"%s\"", fbuf, prvr); - error(SYSTEM, errmsg); - } + error(SYSTEM, errmsg); + } } } - /* render sequence */ + npicts = 0; /* render sequence */ do { if (seq && nextview(stdin) == EOF) break; + pctdone = 0.0; if (pout != NULL) { sprintf(fbuf, pout, seq); + if (file_exists(fbuf)) { + if (prvr != NULL || !strcmp(fbuf, pout)) { + sprintf(errmsg, + "output file \"%s\" exists", + fbuf); + error(USER, errmsg); + } + continue; /* don't clobber */ + } if (freopen(fbuf, "w", stdout) == NULL) { sprintf(errmsg, "cannot open output file \"%s\"", fbuf); error(SYSTEM, errmsg); } +#ifdef MSDOS + setmode(fileno(stdout), O_BINARY); +#endif dupheader(); } hres = hresolu; vres = vresolu; pa = pixaspect; @@ -276,7 +325,11 @@ char *pout, *zout, *prvr; cp = NULL; render(cp, prvr); prvr = NULL; + npicts++; } while (seq++); + /* check that we did something */ + if (npicts == 0) + error(WARNING, "no output produced"); } @@ -322,12 +375,15 @@ char *zfile, *oldfile; sampdens[i] = hstep; } else sampdens = NULL; - /* open z file */ + /* open z-file */ if (zfile != NULL) { if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { - sprintf(errmsg, "cannot open z file \"%s\"", zfile); + sprintf(errmsg, "cannot open z-file \"%s\"", zfile); error(SYSTEM, errmsg); } +#ifdef MSDOS + setmode(zfd, O_BINARY); +#endif for (i = 0; i <= psample; i++) { zbar[i] = (float *)malloc(hres*sizeof(float)); if (zbar[i] == NULL) @@ -342,16 +398,18 @@ char *zfile, *oldfile; fprtresolu(hres, vres, stdout); /* recover file and compute first */ i = salvage(oldfile); + if (i >= vres) + goto alldone; if (zfd != -1 && i > 0 && lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) - error(SYSTEM, "z file seek error in render"); + error(SYSTEM, "z-file seek error in render"); pctdone = 100.0*i/vres; if (ralrm > 0) /* report init stats */ report(); #ifndef BSD else #endif - signal(SIGALRM, report); + signal(SIGCONT, report); ypos = vres-1 - i; fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); /* compute scanlines */ @@ -374,7 +432,7 @@ char *zfile, *oldfile; fillscanbar(scanbar, zbar, hres, ypos, ystep); /* write it out */ #ifndef BSD - signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ + signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ #endif for (i = ystep; i > 0; i--) { if (zfd != -1 && write(zfd, (char *)zbar[i], @@ -388,27 +446,28 @@ char *zfile, *oldfile; goto writerr; /* record progress */ pctdone = 100.0*(vres-1-ypos)/vres; - if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) + if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) report(); #ifndef BSD else - signal(SIGALRM, report); + signal(SIGCONT, report); #endif } /* clean up */ - signal(SIGALRM, SIG_IGN); - if (zfd != -1) { - if (write(zfd, (char *)zbar[0], hres*sizeof(float)) + signal(SIGCONT, SIG_IGN); + if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) < hres*sizeof(float)) - goto writerr; + goto writerr; + fwritescan(scanbar[0], hres, stdout); + if (fflush(stdout) == EOF) + goto writerr; +alldone: + if (zfd != -1) { if (close(zfd) == -1) goto writerr; for (i = 0; i <= psample; i++) free((char *)zbar[i]); } - fwritescan(scanbar[0], hres, stdout); - if (fflush(stdout) == EOF) - goto writerr; for (i = 0; i <= psample; i++) free((char *)scanbar[i]); if (sampdens != NULL) @@ -416,6 +475,7 @@ char *zfile, *oldfile; pctdone = 100.0; if (ralrm > 0) report(); + signal(SIGCONT, SIG_DFL); return; writerr: error(SYSTEM, "write error in render"); @@ -450,7 +510,7 @@ int xres, y, xstep; b = fillsample(scanline, zline, 0, y, i, 0, b/2); else b = fillsample(scanline+i-xstep, - zline ? zline+i-xstep : NULL, + zline ? zline+i-xstep : (float *)NULL, i-xstep, y, xstep, 0, b/2); if (sd) *sd++ = nc & 1 ? bl : b; bl = b; @@ -478,7 +538,7 @@ int xres, y, ysize; zline[ysize] = zbar[ysize][i]; } - b = fillsample(vline, zbar[0] ? zline : NULL, + b = fillsample(vline, zbar[0] ? zline : (float *)NULL, i, y, 0, ysize, b/2); for (j = 1; j < ysize; j++) @@ -498,7 +558,6 @@ int x, y; int xlen, ylen; int b; { - extern double fabs(); double ratio; double z; COLOR ctmp; @@ -537,7 +596,8 @@ int b; /* recurse */ ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); - ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, + ncut += fillsample(colline+(len>>1), + zline ? zline+(len>>1) : (float *)NULL, x+(xlen>>1), y+(ylen>>1), xlen-(xlen>>1), ylen-(ylen>>1), b/2); @@ -552,8 +612,8 @@ int x, y; /* pixel position */ { static RAY thisray; - if (viewray(thisray.rorg, thisray.rdir, &ourview, - (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) { + if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview, + (x+pixjitter())/hres, (y+pixjitter())/vres)) < -FTINY) { setcolor(col, 0.0, 0.0, 0.0); return(0.0); } @@ -579,21 +639,25 @@ char *oldfile; int x, y; if (oldfile == NULL) - return(0); - + goto gotzip; + if ((fp = fopen(oldfile, "r")) == NULL) { sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); error(WARNING, errmsg); - return(0); + goto gotzip; } +#ifdef MSDOS + setmode(fileno(fp), O_BINARY); +#endif /* discard header */ - getheader(fp, NULL); + getheader(fp, NULL, NULL); /* get picture size */ if (!fscnresolu(&x, &y, fp)) { - sprintf(errmsg, "bad recover file \"%s\"", oldfile); + sprintf(errmsg, "bad recover file \"%s\" - not removed", + oldfile); error(WARNING, errmsg); fclose(fp); - return(0); + goto gotzip; } if (x != hres || y != vres) { @@ -617,6 +681,10 @@ char *oldfile; fclose(fp); unlink(oldfile); return(y); +gotzip: + if (fflush(stdout) == EOF) + error(SYSTEM, "error writing picture header"); + return(0); writerr: sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); error(SYSTEM, errmsg);