--- ray/src/px/pinterp.c 1989/12/10 17:34:13 1.1 +++ ray/src/px/pinterp.c 1990/01/02 17:22:07 1.7 @@ -17,6 +17,8 @@ static char SCCSid[] = "$SunId$ LBL"; #define pscan(y) (ourpict+(y)*ourview.hresolu) #define zscan(y) (ourzbuf+(y)*ourview.hresolu) +#define ABS(x) ((x)>0?(x):-(x)) + VIEW ourview = STDVIEW(512); /* desired view */ double zeps = 0.001; /* allowed z epsilon */ @@ -29,7 +31,9 @@ char *progname; VIEW theirview = STDVIEW(512); /* input view */ int gotview; /* got input view? */ +double theirs2ours[4][4]; /* transformation matrix */ + main(argc, argv) /* interpolate pictures */ int argc; char *argv[]; @@ -47,6 +51,14 @@ char *argv[]; check(2,1); zeps = atof(argv[++i]); break; + case 'x': /* x resolution */ + check(2,1); + ourview.hresolu = atoi(argv[++i]); + break; + case 'y': /* y resolution */ + check(2,1); + ourview.vresolu = atoi(argv[++i]); + break; case 'v': /* view */ switch (argv[i][2]) { case 't': /* type */ @@ -189,6 +201,8 @@ char *pfile, *zfile; fprintf(stderr, "%s: %s\n", pfile, err); exit(1); } + /* compute transformation */ + pixform(theirs2ours, &theirview, &ourview); /* allocate scanlines */ scanin = (COLR *)malloc(xres*sizeof(COLR)); zin = (float *)malloc(xres*sizeof(float)); @@ -216,32 +230,82 @@ char *pfile, *zfile; } +pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */ +register double xfmat[4][4]; +register VIEW *vw1, *vw2; +{ + double m4t[4][4]; + + setident4(xfmat); + xfmat[0][0] = vw1->vhinc[0]; + xfmat[0][1] = vw1->vhinc[1]; + xfmat[0][2] = vw1->vhinc[2]; + xfmat[1][0] = vw1->vvinc[0]; + xfmat[1][1] = vw1->vvinc[1]; + xfmat[1][2] = vw1->vvinc[2]; + xfmat[2][0] = vw1->vdir[0]; + xfmat[2][1] = vw1->vdir[1]; + xfmat[2][2] = vw1->vdir[2]; + xfmat[3][0] = vw1->vp[0]; + xfmat[3][1] = vw1->vp[1]; + xfmat[3][2] = vw1->vp[2]; + setident4(m4t); + m4t[0][0] = vw2->vhinc[0]/vw2->vhn2; + m4t[1][0] = vw2->vhinc[1]/vw2->vhn2; + m4t[2][0] = vw2->vhinc[2]/vw2->vhn2; + m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2; + m4t[0][1] = vw2->vvinc[0]/vw2->vvn2; + m4t[1][1] = vw2->vvinc[1]/vw2->vvn2; + m4t[2][1] = vw2->vvinc[2]/vw2->vvn2; + m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2; + m4t[0][2] = vw2->vdir[0]; + m4t[1][2] = vw2->vdir[1]; + m4t[2][2] = vw2->vdir[2]; + m4t[3][2] = -DOT(vw2->vp,vw2->vdir); + multmat4(xfmat, xfmat, m4t); +} + + addscanline(y, pline, zline) /* add scanline to output */ int y; COLR *pline; float *zline; { - FVECT p, dir; - double xnew, ynew, znew; + extern double sqrt(); + double pos[3]; register int x; register int xpos, ypos; for (x = 0; x < theirview.hresolu; x++) { - rayview(p, dir, &theirview, x+.5, y+.5); - p[0] += zline[x]*dir[0]; - p[1] += zline[x]*dir[1]; - p[2] += zline[x]*dir[2]; - pixelview(&xnew, &ynew, &znew, &ourview, p); - if (znew <= 0.0 || xnew < 0 || xnew > ourview.hresolu - || ynew < 0 || ynew > ourview.vresolu) + pos[0] = x - .5*(theirview.hresolu-1); + pos[1] = y - .5*(theirview.vresolu-1); + pos[2] = zline[x]; + if (theirview.type == VT_PER) { + pos[2] /= sqrt( 1. + + pos[0]*pos[0]*theirview.vhn2 + + pos[1]*pos[1]*theirview.vvn2 ); + pos[0] *= pos[2]; + pos[1] *= pos[2]; + } + multp3(pos, pos, theirs2ours); + if (pos[2] <= 0.0) continue; - /* check current value at position */ - xpos = xnew; - ypos = ynew; + 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 || pos[0] >= ourview.hresolu + || pos[1] < 0 || pos[1] >= ourview.vresolu) + continue; + /* check current value at pos */ + xpos = pos[0]; + ypos = pos[1]; if (zscan(ypos)[xpos] <= 0.0 - || zscan(ypos)[xpos] - znew + || zscan(ypos)[xpos] - pos[2] > zeps*zscan(ypos)[xpos]) { - zscan(ypos)[xpos] = znew; + zscan(ypos)[xpos] = pos[2]; copycolr(pscan(ypos)[xpos], pline[x]); } } @@ -250,31 +314,74 @@ float *zline; fillpicture() /* fill in empty spaces */ { + int *yback, xback; int y; - COLR cfill; - register int x, xblank; - + COLR pfill; + register int x, i; + /* get back buffer */ + yback = (int *)malloc(ourview.hresolu*sizeof(int)); + if (yback == NULL) { + perror(progname); + return; + } + for (x = 0; x < ourview.hresolu; x++) + yback[x] = -2; + /* + * Xback and yback are the pixel locations of suitable + * background values in each direction. + * A value of -2 means unassigned, and -1 means + * that there is no suitable background in this direction. + */ + /* fill image */ for (y = 0; y < ourview.vresolu; y++) { - xblank = -1; + xback = -2; for (x = 0; x < ourview.hresolu; x++) - if (zscan(y)[x] <= 0.0) { - if (xblank < 0) - xblank = x; - } else if (xblank >= 0) { - if (xblank == 0 || zscan(y)[xblank-1] < zscan(y)[x]) - copycolr(cfill, pscan(y)[x]); + if (zscan(y)[x] <= 0.0) { /* empty pixel */ + /* + * First, find background from above or below. + * (farthest assigned pixel) + */ + if (yback[x] == -2) { + for (i = y+1; i < ourview.vresolu; i++) + if (zscan(i)[x] > 0.0) + break; + if (i < ourview.vresolu + && (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) + yback[x] = i; + else + yback[x] = y-1; + } + /* + * Next, find background from left or right. + */ + if (xback == -2) { + for (i = x+1; x < ourview.hresolu; i++) + if (zscan(y)[i] > 0.0) + break; + if (i < ourview.hresolu + && (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) + xback = i; + else + xback = x-1; + } + if (xback < 0 && yback[x] < 0) + continue; /* no background */ + /* + * Compare, and use the background that is + * farther, unless one of them is next to us. + */ + if (yback[x] < 0 || ABS(x-xback) <= 1 + || ( ABS(y-yback[x]) > 1 + && zscan(yback[x])[x] < zscan(y)[xback] )) + copycolr(pscan(y)[x],pscan(y)[xback]); else - copycolr(cfill, pscan(y)[xblank-1]); - for ( ; xblank < x; xblank++) - copycolr(pscan(y)[xblank], cfill); - xblank = -1; + copycolr(pscan(y)[x],pscan(yback[x])[x]); + } else { /* full pixel */ + yback[x] = -2; + xback = -2; } - if (xblank > 0) { - copycolr(cfill, pscan(y)[xblank-1]); - for ( ; xblank < ourview.hresolu; xblank++) - copycolr(pscan(y)[xblank], cfill); - } } + free((char *)yback); }