--- ray/src/px/pinterp.c 1990/01/04 16:52:05 1.13 +++ ray/src/px/pinterp.c 1990/01/05 11:34:27 1.15 @@ -26,6 +26,8 @@ static char SCCSid[] = "$SunId$ LBL"; #define ABS(x) ((x)>0?(x):-(x)) +struct position {int x,y; float z;}; + VIEW ourview = STDVIEW(512); /* desired view */ double zeps = .02; /* allowed z epsilon */ @@ -39,7 +41,7 @@ VIEW theirview = STDVIEW(512); /* input view */ int gotview; /* got input view? */ int fill = F_FORE|F_BACK; /* selected fill algorithm */ -extern int backfill(), calfill(); /* fill functions */ +extern int backfill(), rcalfill(); /* fill functions */ int (*deffill)() = backfill; /* selected fill function */ COLR backcolr = BLKCOLR; /* background color */ double backz = 0.0; /* background z value */ @@ -47,8 +49,8 @@ double backz = 0.0; /* background z value */ double theirs2ours[4][4]; /* transformation matrix */ int normdist = 1; /* normalized distance? */ +int childpid = -1; /* id of fill process */ FILE *psend, *precv; /* pipes to/from fill calculation */ -int childpid; /* child's process id */ int queue[PACKSIZ][2]; /* pending pixels */ int queuesiz; /* number of pixels pending */ @@ -108,7 +110,7 @@ char *argv[]; break; case 'r': /* rtrace */ check(3,1); - deffill = calfill; + deffill = rcalfill; calstart(RTCOM, argv[++i]); break; default: @@ -192,10 +194,8 @@ char *argv[]; /* allocate frame */ ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR)); ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); - if (ourpict == NULL || ourzbuf == NULL) { - perror(progname); - exit(1); - } + if (ourpict == NULL || ourzbuf == NULL) + syserror(); /* get input */ for ( ; i < argc; i += 2) addpicture(argv[i], argv[i+1]); @@ -205,8 +205,7 @@ char *argv[]; else fillpicture(); /* close calculation */ - if (deffill == calfill) - caldone(); + caldone(); /* add to header */ printargs(argc, argv, stdout); if (gotvfile) { @@ -255,8 +254,8 @@ char *pfile, *zspec; FILE *pfp, *zfp; char *err; COLR *scanin; - float *zin, *zlast; - int *plast; + float *zin; + struct position *plast; int y; /* open picture file */ if ((pfp = fopen(pfile, "r")) == NULL) { @@ -281,12 +280,10 @@ char *pfile, *zspec; /* allocate scanlines */ scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); zin = (float *)malloc(theirview.hresolu*sizeof(float)); - plast = (int *)calloc(theirview.hresolu, sizeof(int)); - zlast = (float *)calloc(theirview.hresolu, sizeof(float)); - if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { - perror(progname); - exit(1); - } + plast = (struct position *)calloc(theirview.hresolu, + sizeof(struct position)); + if (scanin == NULL || zin == NULL || plast == NULL) + syserror(); /* get z specification or file */ if ((zfp = fopen(zspec, "r")) == NULL) { double zvalue; @@ -310,13 +307,12 @@ char *pfile, *zspec; fprintf(stderr, "%s: read error\n", zspec); exit(1); } - addscanline(y, scanin, zin, plast, zlast); + addscanline(y, scanin, zin, plast); } /* clean up */ free((char *)scanin); free((char *)zin); free((char *)plast); - free((char *)zlast); fclose(pfp); if (zfp != NULL) fclose(zfp); @@ -359,19 +355,15 @@ register VIEW *vw1, *vw2; } -addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ +addscanline(y, pline, zline, lasty) /* add scanline to output */ int y; COLR *pline; float *zline; -int *lasty; /* input/output */ -float *lastyz; /* input/output */ +struct position *lasty; /* input/output */ { extern double sqrt(); double pos[3]; - int lastx = 0; - double lastxz = 0; - double zt; - int xpos, ypos; + struct position lastx, newpos; register int x; for (x = theirview.hresolu-1; x >= 0; x--) { @@ -387,58 +379,82 @@ float *lastyz; /* input/output */ pos[1] *= pos[2]; } multp3(pos, pos, theirs2ours); - if (pos[2] <= 0) + if (pos[2] <= 0) { + lasty[x].z = lastx.z = 0; /* mark invalid */ continue; + } if (ourview.type == VT_PER) { pos[0] /= pos[2]; pos[1] /= pos[2]; } pos[0] += .5*ourview.hresolu; pos[1] += .5*ourview.vresolu; - if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu - || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) - continue; + newpos.x = pos[0]; + newpos.y = pos[1]; + newpos.z = zline[x]; /* add pixel to our image */ - zt = 2.*zeps*zline[x]; - addpixel(xpos, ypos, - (fill&F_FORE && ABS(zline[x]-lastxz) <= zt) - ? lastx - xpos : 1, - (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt) - ? lasty[x] - ypos : 1, - pline[x], pos[2]); - lastx = xpos; - lasty[x] = ypos; - lastxz = lastyz[x] = zline[x]; + if (pos[0] >= 0 && newpos.x < ourview.hresolu + && pos[1] >= 0 && newpos.y < ourview.vresolu) { + addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); + lasty[x].x = lastx.x = newpos.x; + lasty[x].y = lastx.y = newpos.y; + lasty[x].z = lastx.z = newpos.z; + } else + lasty[x].z = lastx.z = 0; /* mark invalid */ } } -addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ -int xstart, ystart; -int width, height; +addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */ +struct position *p0, *p1, *p2; COLR pix; double z; { - register int x, y; - /* make width and height positive */ - if (width < 0) { - width = -width; - xstart = xstart-width+1; - } else if (width == 0) - width = 1; - if (height < 0) { - height = -height; - ystart = ystart-height+1; - } else if (height == 0) - height = 1; - /* fill pixel(s) within rectangle */ - for (y = ystart; y < ystart+height; y++) - for (x = xstart; x < xstart+width; x++) - if (zscan(y)[x] <= 0 - || zscan(y)[x]-z > zeps*zscan(y)[x]) { + double zt = 2.*zeps*p0->z; /* threshold */ + int s1x, s1y, s2x, s2y; /* step sizes */ + int l1, l2, c1, c2; /* side lengths and counters */ + int p1isy; /* p0p1 along y? */ + int x1, y1; /* p1 position */ + register int x, y; /* final position */ + + /* compute vector p0p1 */ + if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { + s1x = p1->x - p0->x; + s1y = p1->y - p0->y; + l1 = ABS(s1x); + if (p1isy = (ABS(s1y) > l1)) + l1 = ABS(s1y); + } else { + l1 = s1x = s1y = 1; + p1isy = -1; + } + /* compute vector p0p2 */ + if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { + s2x = p2->x - p0->x; + s2y = p2->y - p0->y; + if (p1isy == 1) + l2 = ABS(s2x); + else { + l2 = ABS(s2y); + if (p1isy != 0 && ABS(s2x) > l2) + l2 = ABS(s2x); + } + } else + l2 = s2x = s2y = 1; + /* fill the parallelogram */ + for (c1 = l1; c1-- > 0; ) { + x1 = p0->x + c1*s1x/l1; + y1 = p0->y + c1*s1y/l1; + for (c2 = l2; c2-- > 0; ) { + x = x1 + c2*s2x/l2; + y = y1 + c2*s2y/l2; + if (zscan(y)[x] <= 0 || zscan(y)[x]-z + > zeps*zscan(y)[x]) { zscan(y)[x] = z; copycolr(pscan(y)[x], pix); } + } + } } @@ -450,10 +466,8 @@ backpicture() /* background fill algorithm */ register int x, i; /* get back buffer */ yback = (int *)malloc(ourview.hresolu*sizeof(int)); - if (yback == NULL) { - perror(progname); - return; - } + if (yback == NULL) + syserror(); for (x = 0; x < ourview.hresolu; x++) yback[x] = -2; /* @@ -543,10 +557,8 @@ writepicture() /* write out picture */ fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); for (y = ourview.vresolu-1; y >= 0; y--) - if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) { - perror(progname); - exit(1); - } + if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) + syserror(); } @@ -564,10 +576,8 @@ char *fname; exit(1); } if (donorm - && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) { - perror(progname); - exit(1); - } + && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) + syserror(); for (y = ourview.vresolu-1; y >= 0; y--) { if (donorm) { double vx, yzn2; @@ -620,9 +630,13 @@ char *prog, *args; char combuf[512]; int p0[2], p1[2]; + if (childpid != -1) { + fprintf(stderr, "%s: too many calculations\n", progname); + exit(1); + } sprintf(combuf, prog, PACKSIZ, args); if (pipe(p0) < 0 || pipe(p1) < 0) - goto syserr; + syserror(); if ((childpid = vfork()) == 0) { /* fork calculation */ close(p0[1]); close(p1[0]); @@ -638,19 +652,15 @@ char *prog, *args; perror("/bin/sh"); _exit(127); } - if (childpid < 0) - goto syserr; + if (childpid == -1) + syserror(); close(p0[0]); close(p1[1]); if ((psend = fdopen(p0[1], "w")) == NULL) - goto syserr; + syserror(); if ((precv = fdopen(p1[0], "r")) == NULL) - goto syserr; + syserror(); queuesiz = 0; - return; -syserr: - perror(progname); - exit(1); } @@ -658,29 +668,35 @@ caldone() /* done with calculation */ { int pid; - fclose(psend); + if (childpid == -1) + return; + if (fclose(psend) == EOF) + syserror(); clearqueue(); fclose(precv); while ((pid = wait(0)) != -1 && pid != childpid) ; + childpid = -1; } -calfill(x, y) /* fill with calculated pixel */ +rcalfill(x, y) /* fill with ray-calculated pixel */ int x, y; { FVECT orig, dir; float outbuf[6]; if (queuesiz >= PACKSIZ) { /* flush queue */ - fflush(psend); + if (fflush(psend) == EOF) + syserror(); clearqueue(); } /* send new ray */ rayview(orig, dir, &ourview, x+.5, y+.5); outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; - fwrite(outbuf, sizeof(float), 6, psend); + if (fwrite(outbuf, sizeof(float), 6, psend) < 6) + syserror(); /* remember it */ queue[queuesiz][0] = x; queue[queuesiz][1] = y; @@ -694,10 +710,21 @@ clearqueue() /* get results from queue */ register int i; for (i = 0; i < queuesiz; i++) { - fread(inbuf, sizeof(float), 4, precv); + if (fread(inbuf, sizeof(float), 4, precv) < 4) { + fprintf(stderr, "%s: read error in clearqueue\n", + progname); + exit(1); + } setcolr(pscan(queue[i][1])[queue[i][0]], inbuf[0], inbuf[1], inbuf[2]); zscan(queue[i][1])[queue[i][0]] = inbuf[3]; } queuesiz = 0; +} + + +syserror() /* report error and exit */ +{ + perror(progname); + exit(1); }