--- ray/src/rt/rpict.c 1992/07/10 14:58:19 2.8 +++ ray/src/rt/rpict.c 1993/08/11 09:46:15 2.29 @@ -18,7 +18,6 @@ static char SCCSid[] = "$SunId$ LBL"; #endif #include -#include #include "view.h" @@ -26,6 +25,14 @@ static char SCCSid[] = "$SunId$ LBL"; #include "random.h" +#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 */ @@ -33,28 +40,28 @@ int samplendx; /* sample index number */ VIEW ourview = STDVIEW; /* view parameters */ int hresolu = 512; /* horizontal resolution */ int vresolu = 512; /* vertical resolution */ -double pixaspect = 1.0; /* pixel aspect ratio */ +double pixaspect = 1.0; /* pixel aspect ratio */ int psample = 4; /* pixel sample size */ -double maxdiff = .05; /* max. difference for interpolation */ -double dstrpix = 0.67; /* square pixel distribution */ +double maxdiff = .05; /* max. difference for interpolation */ +double dstrpix = 0.67; /* square pixel distribution */ -double dstrsrc = 0.0; /* square source distribution */ -double shadthresh = .05; /* shadow threshold */ -double shadcert = .5; /* shadow certainty */ +double dstrsrc = 0.0; /* square source distribution */ +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? */ -double srcsizerat = .25; /* maximum ratio source size/dist. */ +int directvis = 1; /* sources visible? */ +double srcsizerat = .25; /* maximum ratio source size/dist. */ -double specthresh = .15; /* specular sampling threshold */ -double specjitter = 1.; /* specular sampling jitter */ +double specthresh = .15; /* specular sampling threshold */ +double specjitter = 1.; /* specular sampling jitter */ int maxdepth = 6; /* maximum recursion depth */ -double minweight = 5e-3; /* minimum ray weight */ +double minweight = 5e-3; /* minimum ray weight */ COLOR ambval = BLKCOLOR; /* ambient value */ -double ambacc = 0.2; /* ambient accuracy */ +double ambacc = 0.2; /* ambient accuracy */ int ambres = 32; /* ambient resolution */ int ambdiv = 128; /* ambient divisions */ int ambssamp = 0; /* ambient super-samples */ @@ -62,43 +69,54 @@ 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 */ +double pctdone = 0.0; /* percentage done */ long tlastrept = 0L; /* time at last report */ extern long time(); extern long 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 MAXDIV 16 /* maximum sample size */ -#define pixjitter() (.5+dstrpix*(.5-frandom())) +#define pixjitter() (.5+dstrpix*(.5-frandom())) -#define HFTEMPLATE "/tmp/hfXXXXXX" - -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(); +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); } @@ -107,7 +125,7 @@ int code; report() /* report progress */ { struct rusage rubuf; - double t; + double t; getrusage(RUSAGE_SELF, &rubuf); t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; @@ -116,7 +134,7 @@ report() /* report progress */ t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; - sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", + sprintf(errmsg, "%lu rays, %4.2f%% done after %5.4f CPU hours\n", nrays, pctdone, t/3600.0); eputs(errmsg); tlastrept = time((long *)0); @@ -125,44 +143,14 @@ report() /* report progress */ report() /* report progress */ { tlastrept = time((long *)0); - sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", + sprintf(errmsg, "%lu rays, %4.2f%% done after %5.4f hours\n", nrays, pctdone, (tlastrept-tstart)/3600.0); eputs(errmsg); - signal(SIGALRM, report); + signal(SIGCONT, 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; @@ -178,12 +166,12 @@ char *pout, *zout, *prvr; * sequenced file naming. */ { - extern char *rindex(), *strncpy(), *strcat(); - char fbuf[128], fbuf2[128], *zf; - RESOLU rs; - double pa; - /* finished writing header */ - closeheader(); + extern char *rindex(), *strncpy(), *strcat(), *strcpy(); + char fbuf[128], fbuf2[128]; + int npicts; + register char *cp; + RESOLU rs; + double pa; /* check sampling */ if (psample < 1) psample = 1; @@ -197,7 +185,7 @@ char *pout, *zout, *prvr; if (seq <= 0) seq = 0; else if (prvr != NULL && isint(prvr)) { - int rn; /* skip to specified view */ + register int rn; /* skip to specified view */ if ((rn = atoi(prvr)) < seq) error(USER, "recover frame less than start frame"); if (pout == NULL) @@ -207,33 +195,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 (!strcmp(prvr, fbuf)) { /* rename recover file */ - fbuf2[0] = '\0'; - if ((prvr = rindex(fbuf, '/')) != NULL) - strncpy(fbuf2, fbuf, prvr-fbuf+1); - strcat(fbuf2, "rfXXXXXX"); + 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; @@ -244,18 +254,19 @@ char *pout, *zout, *prvr; "cannot recover view parameters from \"%s\"", prvr); error(WARNING, errmsg); } else { - char *err; - if ((err = setview(&ourview)) != NULL) - error(USER, err); pa = 0.0; hres = scanlen(&rs); vres = numscans(&rs); } + if ((cp = setview(&ourview)) != NULL) + error(USER, cp); normaspect(viewaspect(&ourview), &pa, &hres, &vres); if (seq) { if (ralrm > 0) { - sprintf(errmsg, "starting frame %d\n", seq); - eputs(errmsg); + fprintf(stderr, "FRAME %d:", seq); + fprintview(&ourview, stderr); + putc('\n', stderr); + fflush(stderr); } printf("FRAME=%d\n", seq); } @@ -267,26 +278,27 @@ char *pout, *zout, *prvr; fputformat(COLRFMT, stdout); putchar('\n'); if (zout != NULL) - sprintf(zf=fbuf, zout, seq); + sprintf(cp=fbuf, zout, seq); else - zf = NULL; - render(zf, prvr); + cp = NULL; + render(cp, prvr); prvr = NULL; + npicts++; } while (seq++); + /* check that we did something */ + if (npicts == 0) + error(WARNING, "no output produced"); } nextview(fp) /* get next view from fp */ FILE *fp; { - char linebuf[256], *err; + char linebuf[256]; while (fgets(linebuf, sizeof(linebuf), fp) != NULL) - if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) { - if ((err = setview(&ourview)) != NULL) - error(USER, err); + if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) return(0); - } return(EOF); } @@ -321,12 +333,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) @@ -343,14 +358,14 @@ char *zfile, *oldfile; i = salvage(oldfile); 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 +#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 */ @@ -372,8 +387,8 @@ char *zfile, *oldfile; /* fill bar */ fillscanbar(scanbar, zbar, hres, ypos, ystep); /* write it out */ -#ifndef BSD - signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ +#ifndef BSD + signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ #endif for (i = ystep; i > 0; i--) { if (zfd != -1 && write(zfd, (char *)zbar[i], @@ -389,13 +404,13 @@ char *zfile, *oldfile; pctdone = 100.0*(vres-1-ypos)/vres; if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) report(); -#ifndef BSD +#ifndef BSD else - signal(SIGALRM, report); + signal(SIGCONT, report); #endif } /* clean up */ - signal(SIGALRM, SIG_IGN); + signal(SIGCONT, SIG_IGN); if (zfd != -1) { if (write(zfd, (char *)zbar[0], hres*sizeof(float)) < hres*sizeof(float)) @@ -413,6 +428,9 @@ char *zfile, *oldfile; if (sampdens != NULL) free(sampdens); pctdone = 100.0; + if (ralrm > 0) + report(); + signal(SIGCONT, SIG_DFL); return; writerr: error(SYSTEM, "write error in render"); @@ -422,14 +440,14 @@ memerr: fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ -register COLOR *scanline; -register float *zline; +register COLOR *scanline; +register float *zline; register char *sd; int xres, y, xstep; { static int nc = 0; /* number of calls */ int bl = xstep, b = xstep; - double z; + double z; register int i; z = pixvalue(scanline[0], 0, y); @@ -457,8 +475,8 @@ int xres, y, xstep; fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ -register COLOR *scanbar[]; -register float *zbar[]; +register COLOR *scanbar[]; +register float *zbar[]; int xres, y, ysize; { COLOR vline[MAXDIV+1]; @@ -488,16 +506,15 @@ int xres, y, ysize; int -fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ -register COLOR *colline; -register float *zline; +fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ +register COLOR *colline; +register float *zline; int x, y; int xlen, ylen; int b; { - extern double fabs(); - double ratio; - double z; + double ratio; + double z; COLOR ctmp; int ncut; register int len; @@ -583,11 +600,15 @@ char *oldfile; error(WARNING, errmsg); return(0); } +#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); @@ -615,7 +636,8 @@ char *oldfile; unlink(oldfile); return(y); writerr: - error(SYSTEM, "write error in salvage"); + sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); + error(SYSTEM, errmsg); }