ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
(Generate patch)

Comparing ray/src/px/pinterp.c (file contents):
Revision 1.12 by greg, Thu Jan 4 14:25:56 1990 UTC vs.
Revision 1.15 by greg, Fri Jan 5 11:34:27 1990 UTC

# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17   #define pscan(y)        (ourpict+(y)*ourview.hresolu)
18   #define zscan(y)        (ourzbuf+(y)*ourview.hresolu)
19  
20 < #define F_FORE  1                       /* fill foreground */
21 < #define F_BACK  2                       /* fill background */
20 > #define F_FORE          1               /* fill foreground */
21 > #define F_BACK          2               /* fill background */
22  
23 + #define PACKSIZ         42              /* calculation packet size */
24 +
25 + #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
26 +
27   #define ABS(x)          ((x)>0?(x):-(x))
28  
29 + struct position {int x,y; float z;};
30 +
31   VIEW    ourview = STDVIEW(512);         /* desired view */
32  
33   double  zeps = .02;                     /* allowed z epsilon */
# Line 35 | Line 41 | VIEW   theirview = STDVIEW(512);       /* input view */
41   int     gotview;                        /* got input view? */
42  
43   int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
44 < extern int      backfill();             /* fill functions */
44 > extern int      backfill(), rcalfill(); /* fill functions */
45   int     (*deffill)() = backfill;        /* selected fill function */
46   COLR    backcolr = BLKCOLR;             /* background color */
47 + double  backz = 0.0;                    /* background z value */
48  
49   double  theirs2ours[4][4];              /* transformation matrix */
50   int     normdist = 1;                   /* normalized distance? */
51  
52 + int     childpid = -1;                  /* id of fill process */
53 + FILE    *psend, *precv;                 /* pipes to/from fill calculation */
54 + int     queue[PACKSIZ][2];              /* pending pixels */
55 + int     queuesiz;                       /* number of pixels pending */
56  
57 +
58   main(argc, argv)                        /* interpolate pictures */
59   int     argc;
60   char    *argv[];
# Line 91 | Line 103 | char   *argv[];
103                                          atof(argv[i+2]), atof(argv[i+3]));
104                                  i += 3;
105                                  break;
106 +                        case 'z':                               /* z value */
107 +                                check(3,1);
108 +                                deffill = backfill;
109 +                                backz = atof(argv[++i]);
110 +                                break;
111 +                        case 'r':                               /* rtrace */
112 +                                check(3,1);
113 +                                deffill = rcalfill;
114 +                                calstart(RTCOM, argv[++i]);
115 +                                break;
116                          default:
117                                  goto badopt;
118                          }
# Line 162 | Line 184 | char   *argv[];
184                          goto userr;
185                  }
186                                                  /* check arguments */
187 <        if (argc-i < 2 || (argc-i)%2)
187 >        if ((argc-i)%2)
188                  goto userr;
189                                                  /* set view */
190          if (err = setview(&ourview)) {
# Line 172 | Line 194 | char   *argv[];
194                                                  /* allocate frame */
195          ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
196          ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
197 <        if (ourpict == NULL || ourzbuf == NULL) {
198 <                perror(progname);
177 <                exit(1);
178 <        }
197 >        if (ourpict == NULL || ourzbuf == NULL)
198 >                syserror();
199                                                          /* get input */
200          for ( ; i < argc; i += 2)
201                  addpicture(argv[i], argv[i+1]);
# Line 184 | Line 204 | char   *argv[];
204                  backpicture();
205          else
206                  fillpicture();
207 +                                                        /* close calculation */
208 +        caldone();
209                                                          /* add to header */
210          printargs(argc, argv, stdout);
211          if (gotvfile) {
# Line 232 | Line 254 | char   *pfile, *zspec;
254          FILE    *pfp, *zfp;
255          char    *err;
256          COLR    *scanin;
257 <        float   *zin, *zlast;
258 <        int     *plast;
257 >        float   *zin;
258 >        struct position *plast;
259          int     y;
260                                          /* open picture file */
261          if ((pfp = fopen(pfile, "r")) == NULL) {
# Line 258 | Line 280 | char   *pfile, *zspec;
280                                          /* allocate scanlines */
281          scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
282          zin = (float *)malloc(theirview.hresolu*sizeof(float));
283 <        plast = (int *)calloc(theirview.hresolu, sizeof(int));
284 <        zlast = (float *)calloc(theirview.hresolu, sizeof(float));
285 <        if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) {
286 <                perror(progname);
265 <                exit(1);
266 <        }
283 >        plast = (struct position *)calloc(theirview.hresolu,
284 >                                                sizeof(struct position));
285 >        if (scanin == NULL || zin == NULL || plast == NULL)
286 >                syserror();
287                                          /* get z specification or file */
288          if ((zfp = fopen(zspec, "r")) == NULL) {
289                  double  zvalue;
# Line 287 | Line 307 | char   *pfile, *zspec;
307                          fprintf(stderr, "%s: read error\n", zspec);
308                          exit(1);
309                  }
310 <                addscanline(y, scanin, zin, plast, zlast);
310 >                addscanline(y, scanin, zin, plast);
311          }
312                                          /* clean up */
313          free((char *)scanin);
314          free((char *)zin);
315          free((char *)plast);
296        free((char *)zlast);
316          fclose(pfp);
317          if (zfp != NULL)
318                  fclose(zfp);
# Line 336 | Line 355 | register VIEW  *vw1, *vw2;
355   }
356  
357  
358 < addscanline(y, pline, zline, lasty, lastyz)     /* add scanline to output */
358 > addscanline(y, pline, zline, lasty)     /* add scanline to output */
359   int     y;
360   COLR    *pline;
361   float   *zline;
362 < int     *lasty;                 /* input/output */
344 < float   *lastyz;                /* input/output */
362 > struct position *lasty;         /* input/output */
363   {
364          extern double   sqrt();
365          double  pos[3];
366 <        int     lastx = 0;
349 <        double  lastxz = 0;
350 <        double  zt;
351 <        int     xpos, ypos;
366 >        struct position lastx, newpos;
367          register int    x;
368  
369          for (x = theirview.hresolu-1; x >= 0; x--) {
# Line 364 | Line 379 | float  *lastyz;                /* input/output */
379                          pos[1] *= pos[2];
380                  }
381                  multp3(pos, pos, theirs2ours);
382 <                if (pos[2] <= 0)
382 >                if (pos[2] <= 0) {
383 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
384                          continue;
385 +                }
386                  if (ourview.type == VT_PER) {
387                          pos[0] /= pos[2];
388                          pos[1] /= pos[2];
389                  }
390                  pos[0] += .5*ourview.hresolu;
391                  pos[1] += .5*ourview.vresolu;
392 <                if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
393 <                        || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
394 <                        continue;
392 >                newpos.x = pos[0];
393 >                newpos.y = pos[1];
394 >                newpos.z = zline[x];
395                                          /* add pixel to our image */
396 <                zt = 2.*zeps*zline[x];
397 <                addpixel(xpos, ypos,
398 <                        (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
399 <                                ? lastx - xpos : 1,
400 <                        (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
401 <                                ? lasty[x] - ypos : 1,
402 <                        pline[x], pos[2]);
403 <                lastx = xpos;
387 <                lasty[x] = ypos;
388 <                lastxz = lastyz[x] = zline[x];
396 >                if (pos[0] >= 0 && newpos.x < ourview.hresolu
397 >                                && pos[1] >= 0 && newpos.y < ourview.vresolu) {
398 >                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
399 >                        lasty[x].x = lastx.x = newpos.x;
400 >                        lasty[x].y = lastx.y = newpos.y;
401 >                        lasty[x].z = lastx.z = newpos.z;
402 >                } else
403 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
404          }
405   }
406  
407  
408 < addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */
409 < int     xstart, ystart;
395 < int     width, height;
408 > addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
409 > struct position *p0, *p1, *p2;
410   COLR    pix;
411   double  z;
412   {
413 <        register int    x, y;
414 <                                        /* make width and height positive */
415 <        if (width < 0) {
416 <                width = -width;
417 <                xstart = xstart-width+1;
418 <        } else if (width == 0)
419 <                width = 1;
420 <        if (height < 0) {
421 <                height = -height;
422 <                ystart = ystart-height+1;
423 <        } else if (height == 0)
424 <                height = 1;
425 <                                        /* fill pixel(s) within rectangle */
426 <        for (y = ystart; y < ystart+height; y++)
427 <                for (x = xstart; x < xstart+width; x++)
428 <                        if (zscan(y)[x] <= 0
429 <                                        || zscan(y)[x]-z > zeps*zscan(y)[x]) {
413 >        double  zt = 2.*zeps*p0->z;             /* threshold */
414 >        int     s1x, s1y, s2x, s2y;             /* step sizes */
415 >        int     l1, l2, c1, c2;                 /* side lengths and counters */
416 >        int     p1isy;                          /* p0p1 along y? */
417 >        int     x1, y1;                         /* p1 position */
418 >        register int    x, y;                   /* final position */
419 >
420 >                                        /* compute vector p0p1 */
421 >        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
422 >                s1x = p1->x - p0->x;
423 >                s1y = p1->y - p0->y;
424 >                l1 = ABS(s1x);
425 >                if (p1isy = (ABS(s1y) > l1))
426 >                        l1 = ABS(s1y);
427 >        } else {
428 >                l1 = s1x = s1y = 1;
429 >                p1isy = -1;
430 >        }
431 >                                        /* compute vector p0p2 */
432 >        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
433 >                s2x = p2->x - p0->x;
434 >                s2y = p2->y - p0->y;
435 >                if (p1isy == 1)
436 >                        l2 = ABS(s2x);
437 >                else {
438 >                        l2 = ABS(s2y);
439 >                        if (p1isy != 0 && ABS(s2x) > l2)
440 >                                l2 = ABS(s2x);
441 >                }
442 >        } else
443 >                l2 = s2x = s2y = 1;
444 >                                        /* fill the parallelogram */
445 >        for (c1 = l1; c1-- > 0; ) {
446 >                x1 = p0->x + c1*s1x/l1;
447 >                y1 = p0->y + c1*s1y/l1;
448 >                for (c2 = l2; c2-- > 0; ) {
449 >                        x = x1 + c2*s2x/l2;
450 >                        y = y1 + c2*s2y/l2;
451 >                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
452 >                                                > zeps*zscan(y)[x]) {
453                                  zscan(y)[x] = z;
454                                  copycolr(pscan(y)[x], pix);
455                          }
456 +                }
457 +        }
458   }
459  
460  
# Line 427 | Line 466 | backpicture()                          /* background fill algorithm */
466          register int    x, i;
467                                                          /* get back buffer */
468          yback = (int *)malloc(ourview.hresolu*sizeof(int));
469 <        if (yback == NULL) {
470 <                perror(progname);
432 <                return;
433 <        }
469 >        if (yback == NULL)
470 >                syserror();
471          for (x = 0; x < ourview.hresolu; x++)
472                  yback[x] = -2;
473          /*
# Line 487 | Line 524 | backpicture()                          /* background fill algorithm */
524                                          || (xback >= 0 && ABS(x-xback) <= 1)
525                                          || ( ABS(y-yback[x]) > 1
526                                                  && zscan(yback[x])[x]
527 <                                                < zscan(y)[xback] ) )
527 >                                                < zscan(y)[xback] ) ) {
528                                          copycolr(pscan(y)[x],pscan(y)[xback]);
529 <                                else
529 >                                        zscan(y)[x] = zscan(y)[xback];
530 >                                } else {
531                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
532 +                                        zscan(y)[x] = zscan(yback[x])[x];
533 +                                }
534                          } else {                                /* full pixel */
535                                  yback[x] = -2;
536                                  xback = -2;
# Line 517 | Line 557 | writepicture()                         /* write out picture */
557  
558          fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
559          for (y = ourview.vresolu-1; y >= 0; y--)
560 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) {
561 <                        perror(progname);
522 <                        exit(1);
523 <                }
560 >                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0)
561 >                        syserror();
562   }
563  
564  
# Line 538 | Line 576 | char   *fname;
576                  exit(1);
577          }
578          if (donorm
579 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
580 <                perror(progname);
543 <                exit(1);
544 <        }
579 >        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL)
580 >                syserror();
581          for (y = ourview.vresolu-1; y >= 0; y--) {
582                  if (donorm) {
583                          double  vx, yzn2;
# Line 584 | Line 620 | int    x, y;
620          register BYTE   *dest = pscan(y)[x];
621  
622          copycolr(dest, backcolr);
623 +        zscan(y)[x] = backz;
624 + }
625 +
626 +
627 + calstart(prog, args)                    /* start fill calculation */
628 + char    *prog, *args;
629 + {
630 +        char    combuf[512];
631 +        int     p0[2], p1[2];
632 +
633 +        if (childpid != -1) {
634 +                fprintf(stderr, "%s: too many calculations\n", progname);
635 +                exit(1);
636 +        }
637 +        sprintf(combuf, prog, PACKSIZ, args);
638 +        if (pipe(p0) < 0 || pipe(p1) < 0)
639 +                syserror();
640 +        if ((childpid = vfork()) == 0) {        /* fork calculation */
641 +                close(p0[1]);
642 +                close(p1[0]);
643 +                if (p0[0] != 0) {
644 +                        dup2(p0[0], 0);
645 +                        close(p0[0]);
646 +                }
647 +                if (p1[1] != 1) {
648 +                        dup2(p1[1], 1);
649 +                        close(p1[1]);
650 +                }
651 +                execl("/bin/sh", "sh", "-c", combuf, 0);
652 +                perror("/bin/sh");
653 +                _exit(127);
654 +        }
655 +        if (childpid == -1)
656 +                syserror();
657 +        close(p0[0]);
658 +        close(p1[1]);
659 +        if ((psend = fdopen(p0[1], "w")) == NULL)
660 +                syserror();
661 +        if ((precv = fdopen(p1[0], "r")) == NULL)
662 +                syserror();
663 +        queuesiz = 0;
664 + }
665 +
666 +
667 + caldone()                               /* done with calculation */
668 + {
669 +        int     pid;
670 +
671 +        if (childpid == -1)
672 +                return;
673 +        if (fclose(psend) == EOF)
674 +                syserror();
675 +        clearqueue();
676 +        fclose(precv);
677 +        while ((pid = wait(0)) != -1 && pid != childpid)
678 +                ;
679 +        childpid = -1;
680 + }
681 +
682 +
683 + rcalfill(x, y)                          /* fill with ray-calculated pixel */
684 + int     x, y;
685 + {
686 +        FVECT   orig, dir;
687 +        float   outbuf[6];
688 +
689 +        if (queuesiz >= PACKSIZ) {      /* flush queue */
690 +                if (fflush(psend) == EOF)
691 +                        syserror();
692 +                clearqueue();
693 +        }
694 +                                        /* send new ray */
695 +        rayview(orig, dir, &ourview, x+.5, y+.5);
696 +        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
697 +        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
698 +        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
699 +                syserror();
700 +                                        /* remember it */
701 +        queue[queuesiz][0] = x;
702 +        queue[queuesiz][1] = y;
703 +        queuesiz++;
704 + }
705 +
706 +
707 + clearqueue()                            /* get results from queue */
708 + {
709 +        float   inbuf[4];
710 +        register int    i;
711 +
712 +        for (i = 0; i < queuesiz; i++) {
713 +                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
714 +                        fprintf(stderr, "%s: read error in clearqueue\n",
715 +                                        progname);
716 +                        exit(1);
717 +                }
718 +                setcolr(pscan(queue[i][1])[queue[i][0]],
719 +                                inbuf[0], inbuf[1], inbuf[2]);
720 +                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
721 +        }
722 +        queuesiz = 0;
723 + }
724 +
725 +
726 + syserror()                      /* report error and exit */
727 + {
728 +        perror(progname);
729 +        exit(1);
730   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines