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.14 by greg, Thu Jan 4 17:47:37 1990 UTC vs.
Revision 2.11 by greg, Fri Apr 2 09:10:48 1993 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1993 Regents of the University of California */
2 +
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
5   #endif
# Line 10 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include "standard.h"
14  
15 + #include <ctype.h>
16 +
17   #include "view.h"
18  
19   #include "color.h"
20  
21 < #define pscan(y)        (ourpict+(y)*ourview.hresolu)
18 < #define zscan(y)        (ourzbuf+(y)*ourview.hresolu)
21 > #include "resolu.h"
22  
23 + #define pscan(y)        (ourpict+(y)*hresolu)
24 + #define zscan(y)        (ourzbuf+(y)*hresolu)
25 +
26   #define F_FORE          1               /* fill foreground */
27   #define F_BACK          2               /* fill background */
28  
29 < #define PACKSIZ         42              /* calculation packet size */
29 > #define PACKSIZ         256             /* max. calculation packet size */
30  
31 < #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
31 > #define RTCOM           "rtrace -h- -ovl -fff "
32  
33   #define ABS(x)          ((x)>0?(x):-(x))
34  
35 < VIEW    ourview = STDVIEW(512);         /* desired view */
35 > struct position {int x,y; float z;};
36  
37 + VIEW    ourview = STDVIEW;              /* desired view */
38 + int     hresolu = 512;                  /* horizontal resolution */
39 + int     vresolu = 512;                  /* vertical resolution */
40 + double  pixaspect = 1.0;                /* pixel aspect ratio */
41 +
42   double  zeps = .02;                     /* allowed z epsilon */
43  
44   COLR    *ourpict;                       /* output picture */
# Line 35 | Line 46 | float  *ourzbuf;                       /* corresponding z-buffer */
46  
47   char    *progname;
48  
49 < VIEW    theirview = STDVIEW(512);       /* input view */
50 < int     gotview;                        /* got input view? */
40 <
41 < int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
49 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
50 > int     fillsamp = 0;                   /* sample separation (0 == inf) */
51   extern int      backfill(), rcalfill(); /* fill functions */
52 < int     (*deffill)() = backfill;        /* selected fill function */
52 > int     (*fillfunc)() = backfill;       /* selected fill function */
53   COLR    backcolr = BLKCOLR;             /* background color */
54   double  backz = 0.0;                    /* background z value */
55 + int     normdist = 1;                   /* normalized distance? */
56 + double  ourexp = -1;                    /* output picture exposure */
57  
58 + VIEW    theirview = STDVIEW;            /* input view */
59 + int     gotview;                        /* got input view? */
60 + int     wrongformat = 0;                /* input in another format? */
61 + RESOLU  tresolu;                        /* input resolution */
62 + double  theirexp;                       /* input picture exposure */
63   double  theirs2ours[4][4];              /* transformation matrix */
64 < int     normdist = 1;                   /* normalized distance? */
64 > int     hasmatrix = 0;                  /* has transformation matrix */
65  
66 < int     childpid = -1;                  /* id of fill process */
67 < FILE    *psend, *precv;                 /* pipes to/from fill calculation */
68 < int     queue[PACKSIZ][2];              /* pending pixels */
66 > int     PDesc[3] = {-1,-1,-1};          /* rtrace process descriptor */
67 > #define childpid        (PDesc[2])
68 > unsigned short  queue[PACKSIZ][2];      /* pending pixels */
69 > int     packsiz;                        /* actual packet size */
70   int     queuesiz;                       /* number of pixels pending */
71  
72  
# Line 57 | Line 74 | main(argc, argv)                       /* interpolate pictures */
74   int     argc;
75   char    *argv[];
76   {
77 < #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
78 <        extern double   atof();
77 > #define  check(ol,al)           if (argv[i][ol] || \
78 >                                badarg(argc-i-1,argv+i+1,al)) \
79 >                                goto badopt
80          int     gotvfile = 0;
81          char    *zfile = NULL;
82          char    *err;
83 <        int     i;
83 >        int     i, rval;
84  
85          progname = argv[0];
86  
87 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
87 >        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
88 >                rval = getviewopt(&ourview, argc-i, argv+i);
89 >                if (rval >= 0) {
90 >                        i += rval;
91 >                        continue;
92 >                }
93                  switch (argv[i][1]) {
94                  case 't':                               /* threshold */
95 <                        check(2,1);
95 >                        check(2,"f");
96                          zeps = atof(argv[++i]);
97                          break;
98                  case 'n':                               /* dist. normalized? */
99 <                        check(2,0);
99 >                        check(2,NULL);
100                          normdist = !normdist;
101                          break;
102                  case 'f':                               /* fill type */
103                          switch (argv[i][2]) {
104                          case '0':                               /* none */
105 <                                check(3,0);
106 <                                fill = 0;
105 >                                check(3,NULL);
106 >                                fillo = 0;
107                                  break;
108                          case 'f':                               /* foreground */
109 <                                check(3,0);
110 <                                fill = F_FORE;
109 >                                check(3,NULL);
110 >                                fillo = F_FORE;
111                                  break;
112                          case 'b':                               /* background */
113 <                                check(3,0);
114 <                                fill = F_BACK;
113 >                                check(3,NULL);
114 >                                fillo = F_BACK;
115                                  break;
116                          case 'a':                               /* all */
117 <                                check(3,0);
118 <                                fill = F_FORE|F_BACK;
117 >                                check(3,NULL);
118 >                                fillo = F_FORE|F_BACK;
119                                  break;
120 +                        case 's':                               /* sample */
121 +                                check(3,"i");
122 +                                fillsamp = atoi(argv[++i]);
123 +                                break;
124                          case 'c':                               /* color */
125 <                                check(3,3);
126 <                                deffill = backfill;
125 >                                check(3,"fff");
126 >                                fillfunc = backfill;
127                                  setcolr(backcolr, atof(argv[i+1]),
128                                          atof(argv[i+2]), atof(argv[i+3]));
129                                  i += 3;
130                                  break;
131                          case 'z':                               /* z value */
132 <                                check(3,1);
133 <                                deffill = backfill;
132 >                                check(3,"f");
133 >                                fillfunc = backfill;
134                                  backz = atof(argv[++i]);
135                                  break;
136                          case 'r':                               /* rtrace */
137 <                                check(3,1);
138 <                                deffill = rcalfill;
137 >                                check(3,"s");
138 >                                fillfunc = rcalfill;
139                                  calstart(RTCOM, argv[++i]);
140                                  break;
141                          default:
# Line 116 | Line 143 | char   *argv[];
143                          }
144                          break;
145                  case 'z':                               /* z file */
146 <                        check(2,1);
146 >                        check(2,"s");
147                          zfile = argv[++i];
148                          break;
149                  case 'x':                               /* x resolution */
150 <                        check(2,1);
151 <                        ourview.hresolu = atoi(argv[++i]);
150 >                        check(2,"i");
151 >                        hresolu = atoi(argv[++i]);
152                          break;
153                  case 'y':                               /* y resolution */
154 <                        check(2,1);
155 <                        ourview.vresolu = atoi(argv[++i]);
154 >                        check(2,"i");
155 >                        vresolu = atoi(argv[++i]);
156                          break;
157 <                case 'v':                               /* view */
158 <                        switch (argv[i][2]) {
132 <                        case 't':                               /* type */
133 <                                check(4,0);
134 <                                ourview.type = argv[i][3];
135 <                                break;
136 <                        case 'p':                               /* point */
137 <                                check(3,3);
138 <                                ourview.vp[0] = atof(argv[++i]);
139 <                                ourview.vp[1] = atof(argv[++i]);
140 <                                ourview.vp[2] = atof(argv[++i]);
141 <                                break;
142 <                        case 'd':                               /* direction */
143 <                                check(3,3);
144 <                                ourview.vdir[0] = atof(argv[++i]);
145 <                                ourview.vdir[1] = atof(argv[++i]);
146 <                                ourview.vdir[2] = atof(argv[++i]);
147 <                                break;
148 <                        case 'u':                               /* up */
149 <                                check(3,3);
150 <                                ourview.vup[0] = atof(argv[++i]);
151 <                                ourview.vup[1] = atof(argv[++i]);
152 <                                ourview.vup[2] = atof(argv[++i]);
153 <                                break;
154 <                        case 'h':                               /* horizontal */
155 <                                check(3,1);
156 <                                ourview.horiz = atof(argv[++i]);
157 <                                break;
158 <                        case 'v':                               /* vertical */
159 <                                check(3,1);
160 <                                ourview.vert = atof(argv[++i]);
161 <                                break;
162 <                        case 'f':                               /* file */
163 <                                check(3,1);
164 <                                gotvfile = viewfile(argv[++i], &ourview);
165 <                                if (gotvfile < 0) {
166 <                                        perror(argv[i]);
167 <                                        exit(1);
168 <                                } else if (gotvfile == 0) {
169 <                                        fprintf(stderr, "%s: bad view file\n",
170 <                                                        argv[i]);
171 <                                        exit(1);
172 <                                }
173 <                                break;
174 <                        default:
157 >                case 'p':                               /* pixel aspect */
158 >                        if (argv[i][2] != 'a')
159                                  goto badopt;
160 +                        check(3,"f");
161 +                        pixaspect = atof(argv[++i]);
162 +                        break;
163 +                case 'v':                               /* view file */
164 +                        if (argv[i][2] != 'f')
165 +                                goto badopt;
166 +                        check(3,"s");
167 +                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
168 +                        if (gotvfile < 0)
169 +                                syserror(argv[i]);
170 +                        else if (gotvfile == 0) {
171 +                                fprintf(stderr, "%s: bad view file\n",
172 +                                                argv[i]);
173 +                                exit(1);
174                          }
175                          break;
176                  default:
# Line 181 | Line 179 | char   *argv[];
179                                          progname, argv[i]);
180                          goto userr;
181                  }
182 +        }
183                                                  /* check arguments */
184          if ((argc-i)%2)
185                  goto userr;
186 +        if (fillsamp == 1)
187 +                fillo &= ~F_BACK;
188                                                  /* set view */
189          if (err = setview(&ourview)) {
190                  fprintf(stderr, "%s: %s\n", progname, err);
191                  exit(1);
192          }
193 +        normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
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);
199 <                exit(1);
198 <        }
195 >        ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
196 >        ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
197 >        if (ourpict == NULL || ourzbuf == NULL)
198 >                syserror(progname);
199 >        bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
200                                                          /* get input */
201          for ( ; i < argc; i += 2)
202                  addpicture(argv[i], argv[i+1]);
203                                                          /* fill in spaces */
204 <        if (fill&F_BACK)
205 <                backpicture();
204 >        if (fillo&F_BACK)
205 >                backpicture(fillfunc, fillsamp);
206          else
207 <                fillpicture();
207 >                fillpicture(fillfunc);
208                                                          /* close calculation */
209          caldone();
210                                                          /* add to header */
211          printargs(argc, argv, stdout);
212          if (gotvfile) {
213 <                printf(VIEWSTR);
213 >                fputs(VIEWSTR, stdout);
214                  fprintview(&ourview, stdout);
215 <                printf("\n");
215 >                putc('\n', stdout);
216          }
217 <        printf("\n");
217 >        if (pixaspect < .99 || pixaspect > 1.01)
218 >                fputaspect(pixaspect, stdout);
219 >        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
220 >                fputexpos(ourexp, stdout);
221 >        fputformat(COLRFMT, stdout);
222 >        putc('\n', stdout);
223                                                          /* write picture */
224          writepicture();
225                                                          /* write z file */
# Line 223 | Line 229 | char   *argv[];
229          exit(0);
230   userr:
231          fprintf(stderr,
232 <        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
232 >        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
233                          progname);
234          exit(1);
235   #undef check
# Line 233 | Line 239 | userr:
239   headline(s)                             /* process header string */
240   char    *s;
241   {
242 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
237 <        register char   **an;
242 >        char    fmt[32];
243  
244 <        printf("\t%s", s);
244 >        if (isformat(s)) {
245 >                formatval(fmt, s);
246 >                wrongformat = strcmp(fmt, COLRFMT);
247 >                return;
248 >        }
249 >        putc('\t', stdout);
250 >        fputs(s, stdout);
251  
252 <        for (an = altname; *an != NULL; an++)
253 <                if (!strncmp(*an, s, strlen(*an))) {
254 <                        if (sscanview(&theirview, s+strlen(*an)) == 0)
255 <                                gotview++;
256 <                        break;
257 <                }
252 >        if (isexpos(s)) {
253 >                theirexp *= exposval(s);
254 >                return;
255 >        }
256 >        if (isview(s) && sscanview(&theirview, s) > 0)
257 >                gotview++;
258   }
259  
260  
261   addpicture(pfile, zspec)                /* add picture to output */
262   char    *pfile, *zspec;
263   {
264 <        extern double   atof();
265 <        FILE    *pfp, *zfp;
264 >        FILE    *pfp;
265 >        int     zfd;
266          char    *err;
267          COLR    *scanin;
268 <        float   *zin, *zlast;
269 <        int     *plast;
268 >        float   *zin;
269 >        struct position *plast;
270          int     y;
271                                          /* open picture file */
272 <        if ((pfp = fopen(pfile, "r")) == NULL) {
273 <                perror(pfile);
274 <                exit(1);
275 <        }
265 <                                        /* get header and view */
266 <        printf("%s:\n", pfile);
272 >        if ((pfp = fopen(pfile, "r")) == NULL)
273 >                syserror(pfile);
274 >                                        /* get header with exposure and view */
275 >        theirexp = 1.0;
276          gotview = 0;
277 <        getheader(pfp, headline);
278 <        if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
279 <                        != (YMAJOR|YDECR)) {
280 <                fprintf(stderr, "%s: picture view error\n", pfile);
277 >        printf("%s:\n", pfile);
278 >        getheader(pfp, headline, NULL);
279 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
280 >                fprintf(stderr, "%s: picture format error\n", pfile);
281                  exit(1);
282          }
283 +        if (ourexp <= 0)
284 +                ourexp = theirexp;
285 +        else if (ABS(theirexp-ourexp) > .01*ourexp)
286 +                fprintf(stderr, "%s: different exposure (warning)\n", pfile);
287          if (err = setview(&theirview)) {
288                  fprintf(stderr, "%s: %s\n", pfile, err);
289                  exit(1);
290          }
291                                          /* compute transformation */
292 <        pixform(theirs2ours, &theirview, &ourview);
292 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
293                                          /* allocate scanlines */
294 <        scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
295 <        zin = (float *)malloc(theirview.hresolu*sizeof(float));
296 <        plast = (int *)calloc(theirview.hresolu, sizeof(int));
297 <        zlast = (float *)calloc(theirview.hresolu, sizeof(float));
298 <        if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) {
299 <                perror(progname);
287 <                exit(1);
288 <        }
294 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
295 >        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
296 >        plast = (struct position *)calloc(scanlen(&tresolu),
297 >                        sizeof(struct position));
298 >        if (scanin == NULL || zin == NULL || plast == NULL)
299 >                syserror(progname);
300                                          /* get z specification or file */
301 <        if ((zfp = fopen(zspec, "r")) == NULL) {
301 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
302                  double  zvalue;
303                  register int    x;
304 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
305 <                        perror(zspec);
304 >                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
305 >                        syserror(zspec);
306                          exit(1);
307 <                }
297 <                for (x = 0; x < theirview.hresolu; x++)
307 >                for (x = scanlen(&tresolu); x-- > 0; )
308                          zin[x] = zvalue;
309          }
310                                          /* load image */
311 <        for (y = theirview.vresolu-1; y >= 0; y--) {
312 <                if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
311 >        for (y = 0; y < numscans(&tresolu); y++) {
312 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
313                          fprintf(stderr, "%s: read error\n", pfile);
314                          exit(1);
315                  }
316 <                if (zfp != NULL
317 <                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
318 <                                < theirview.hresolu) {
316 >                if (zfd != -1 && read(zfd,(char *)zin,
317 >                                scanlen(&tresolu)*sizeof(float))
318 >                                < scanlen(&tresolu)*sizeof(float)) {
319                          fprintf(stderr, "%s: read error\n", zspec);
320                          exit(1);
321                  }
322 <                addscanline(y, scanin, zin, plast, zlast);
322 >                addscanline(y, scanin, zin, plast);
323          }
324                                          /* clean up */
325          free((char *)scanin);
326          free((char *)zin);
327          free((char *)plast);
318        free((char *)zlast);
328          fclose(pfp);
329 <        if (zfp != NULL)
330 <                fclose(zfp);
329 >        if (zfd != -1)
330 >                close(zfd);
331   }
332  
333  
# Line 328 | Line 337 | register VIEW  *vw1, *vw2;
337   {
338          double  m4t[4][4];
339  
340 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
341 +                return(0);
342 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
343 +                return(0);
344          setident4(xfmat);
345 <        xfmat[0][0] = vw1->vhinc[0];
346 <        xfmat[0][1] = vw1->vhinc[1];
347 <        xfmat[0][2] = vw1->vhinc[2];
348 <        xfmat[1][0] = vw1->vvinc[0];
349 <        xfmat[1][1] = vw1->vvinc[1];
350 <        xfmat[1][2] = vw1->vvinc[2];
345 >        xfmat[0][0] = vw1->hvec[0];
346 >        xfmat[0][1] = vw1->hvec[1];
347 >        xfmat[0][2] = vw1->hvec[2];
348 >        xfmat[1][0] = vw1->vvec[0];
349 >        xfmat[1][1] = vw1->vvec[1];
350 >        xfmat[1][2] = vw1->vvec[2];
351          xfmat[2][0] = vw1->vdir[0];
352          xfmat[2][1] = vw1->vdir[1];
353          xfmat[2][2] = vw1->vdir[2];
# Line 342 | Line 355 | register VIEW  *vw1, *vw2;
355          xfmat[3][1] = vw1->vp[1];
356          xfmat[3][2] = vw1->vp[2];
357          setident4(m4t);
358 <        m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
359 <        m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
360 <        m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
361 <        m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
362 <        m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
363 <        m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
364 <        m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
365 <        m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
358 >        m4t[0][0] = vw2->hvec[0]/vw2->hn2;
359 >        m4t[1][0] = vw2->hvec[1]/vw2->hn2;
360 >        m4t[2][0] = vw2->hvec[2]/vw2->hn2;
361 >        m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
362 >        m4t[0][1] = vw2->vvec[0]/vw2->vn2;
363 >        m4t[1][1] = vw2->vvec[1]/vw2->vn2;
364 >        m4t[2][1] = vw2->vvec[2]/vw2->vn2;
365 >        m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
366          m4t[0][2] = vw2->vdir[0];
367          m4t[1][2] = vw2->vdir[1];
368          m4t[2][2] = vw2->vdir[2];
369          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
370          multmat4(xfmat, xfmat, m4t);
371 +        return(1);
372   }
373  
374  
375 < addscanline(y, pline, zline, lasty, lastyz)     /* add scanline to output */
375 > addscanline(y, pline, zline, lasty)     /* add scanline to output */
376   int     y;
377   COLR    *pline;
378   float   *zline;
379 < int     *lasty;                 /* input/output */
366 < float   *lastyz;                /* input/output */
379 > struct position *lasty;         /* input/output */
380   {
381 <        extern double   sqrt();
382 <        double  pos[3];
370 <        int     lastx = 0;
371 <        double  lastxz = 0;
372 <        double  zt;
373 <        int     xpos, ypos;
381 >        FVECT   pos;
382 >        struct position lastx, newpos;
383          register int    x;
384  
385 <        for (x = theirview.hresolu-1; x >= 0; x--) {
386 <                pos[0] = x - .5*(theirview.hresolu-1);
387 <                pos[1] = y - .5*(theirview.vresolu-1);
385 >        lastx.z = 0;
386 >        for (x = scanlen(&tresolu); x-- > 0; ) {
387 >                pix2loc(pos, &tresolu, x, y);
388                  pos[2] = zline[x];
389 <                if (theirview.type == VT_PER) {
390 <                        if (normdist)   /* adjust for eye-ray distance */
382 <                                pos[2] /= sqrt( 1.
383 <                                        + pos[0]*pos[0]*theirview.vhn2
384 <                                        + pos[1]*pos[1]*theirview.vvn2 );
385 <                        pos[0] *= pos[2];
386 <                        pos[1] *= pos[2];
387 <                }
388 <                multp3(pos, pos, theirs2ours);
389 <                if (pos[2] <= 0)
389 >                if (movepixel(pos) < 0) {
390 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
391                          continue;
391                if (ourview.type == VT_PER) {
392                        pos[0] /= pos[2];
393                        pos[1] /= pos[2];
392                  }
393 <                pos[0] += .5*ourview.hresolu;
394 <                pos[1] += .5*ourview.vresolu;
395 <                if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
398 <                        || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
399 <                        continue;
393 >                newpos.x = pos[0] * hresolu;
394 >                newpos.y = pos[1] * vresolu;
395 >                newpos.z = zline[x];
396                                          /* add pixel to our image */
397 <                zt = 2.*zeps*zline[x];
398 <                addpixel(xpos, ypos,
399 <                        (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
400 <                                ? lastx - xpos : 1,
401 <                        (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
402 <                                ? lasty[x] - ypos : 1,
403 <                        pline[x], pos[2]);
404 <                lastx = xpos;
409 <                lasty[x] = ypos;
410 <                lastxz = lastyz[x] = zline[x];
397 >                if (pos[0] >= 0 && newpos.x < hresolu
398 >                                && pos[1] >= 0 && newpos.y < vresolu) {
399 >                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
400 >                        lasty[x].x = lastx.x = newpos.x;
401 >                        lasty[x].y = lastx.y = newpos.y;
402 >                        lasty[x].z = lastx.z = newpos.z;
403 >                } else
404 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
405          }
406   }
407  
408  
409 < addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */
410 < int     xstart, ystart;
417 < int     width, height;
409 > addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
410 > struct position *p0, *p1, *p2;
411   COLR    pix;
412   double  z;
413   {
414 <        register int    x, y;
415 <                                        /* make width and height positive */
416 <        if (width < 0) {
417 <                width = -width;
418 <                xstart = xstart-width+1;
419 <        } else if (width == 0)
420 <                width = 1;
421 <        if (height < 0) {
422 <                height = -height;
423 <                ystart = ystart-height+1;
424 <        } else if (height == 0)
425 <                height = 1;
426 <                                        /* fill pixel(s) within rectangle */
427 <        for (y = ystart; y < ystart+height; y++)
428 <                for (x = xstart; x < xstart+width; x++)
429 <                        if (zscan(y)[x] <= 0
430 <                                        || zscan(y)[x]-z > zeps*zscan(y)[x]) {
414 >        double  zt = 2.*zeps*p0->z;             /* threshold */
415 >        int     s1x, s1y, s2x, s2y;             /* step sizes */
416 >        int     l1, l2, c1, c2;                 /* side lengths and counters */
417 >        int     p1isy;                          /* p0p1 along y? */
418 >        int     x1, y1;                         /* p1 position */
419 >        register int    x, y;                   /* final position */
420 >
421 >                                        /* compute vector p0p1 */
422 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
423 >                s1x = p1->x - p0->x;
424 >                s1y = p1->y - p0->y;
425 >                l1 = ABS(s1x);
426 >                if (p1isy = (ABS(s1y) > l1))
427 >                        l1 = ABS(s1y);
428 >        } else {
429 >                l1 = s1x = s1y = 1;
430 >                p1isy = -1;
431 >        }
432 >                                        /* compute vector p0p2 */
433 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
434 >                s2x = p2->x - p0->x;
435 >                s2y = p2->y - p0->y;
436 >                if (p1isy == 1)
437 >                        l2 = ABS(s2x);
438 >                else {
439 >                        l2 = ABS(s2y);
440 >                        if (p1isy != 0 && ABS(s2x) > l2)
441 >                                l2 = ABS(s2x);
442 >                }
443 >        } else
444 >                l2 = s2x = s2y = 1;
445 >                                        /* fill the parallelogram */
446 >        for (c1 = l1; c1-- > 0; ) {
447 >                x1 = p0->x + c1*s1x/l1;
448 >                y1 = p0->y + c1*s1y/l1;
449 >                for (c2 = l2; c2-- > 0; ) {
450 >                        x = x1 + c2*s2x/l2;
451 >                        if (x < 0 || x >= hresolu)
452 >                                continue;
453 >                        y = y1 + c2*s2y/l2;
454 >                        if (y < 0 || y >= vresolu)
455 >                                continue;
456 >                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
457 >                                                > zeps*zscan(y)[x]) {
458                                  zscan(y)[x] = z;
459                                  copycolr(pscan(y)[x], pix);
460                          }
461 +                }
462 +        }
463   }
464  
465  
466 < backpicture()                           /* background fill algorithm */
466 > movepixel(pos)                          /* reposition image point */
467 > FVECT   pos;
468   {
469 +        FVECT   pt, direc;
470 +        
471 +        if (pos[2] <= 0)                /* empty pixel */
472 +                return(-1);
473 +        if (hasmatrix) {
474 +                pos[0] += theirview.hoff - .5;
475 +                pos[1] += theirview.voff - .5;
476 +                if (theirview.type == VT_PER) {
477 +                        if (normdist)   /* adjust for eye-ray distance */
478 +                                pos[2] /= sqrt( 1.
479 +                                        + pos[0]*pos[0]*theirview.hn2
480 +                                        + pos[1]*pos[1]*theirview.vn2 );
481 +                        pos[0] *= pos[2];
482 +                        pos[1] *= pos[2];
483 +                }
484 +                multp3(pos, pos, theirs2ours);
485 +                if (pos[2] <= 0)
486 +                        return(-1);
487 +                if (ourview.type == VT_PER) {
488 +                        pos[0] /= pos[2];
489 +                        pos[1] /= pos[2];
490 +                }
491 +                pos[0] += .5 - ourview.hoff;
492 +                pos[1] += .5 - ourview.voff;
493 +                return(0);
494 +        }
495 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
496 +                return(-1);
497 +        pt[0] += direc[0]*pos[2];
498 +        pt[1] += direc[1]*pos[2];
499 +        pt[2] += direc[2]*pos[2];
500 +        viewloc(pos, &ourview, pt);
501 +        if (pos[2] <= 0)
502 +                return(-1);
503 +        return(0);
504 + }
505 +
506 +
507 + backpicture(fill, samp)                 /* background fill algorithm */
508 + int     (*fill)();
509 + int     samp;
510 + {
511          int     *yback, xback;
512          int     y;
448        COLR    pfill;
513          register int    x, i;
514                                                          /* get back buffer */
515 <        yback = (int *)malloc(ourview.hresolu*sizeof(int));
516 <        if (yback == NULL) {
517 <                perror(progname);
518 <                return;
455 <        }
456 <        for (x = 0; x < ourview.hresolu; x++)
515 >        yback = (int *)malloc(hresolu*sizeof(int));
516 >        if (yback == NULL)
517 >                syserror(progname);
518 >        for (x = 0; x < hresolu; x++)
519                  yback[x] = -2;
520          /*
521           * Xback and yback are the pixel locations of suitable
# Line 462 | Line 524 | backpicture()                          /* background fill algorithm */
524           * that there is no suitable background in this direction.
525           */
526                                                          /* fill image */
527 <        for (y = 0; y < ourview.vresolu; y++) {
527 >        for (y = 0; y < vresolu; y++) {
528                  xback = -2;
529 <                for (x = 0; x < ourview.hresolu; x++)
529 >                for (x = 0; x < hresolu; x++)
530                          if (zscan(y)[x] <= 0) {         /* empty pixel */
531                                  /*
532                                   * First, find background from above or below.
533                                   * (farthest assigned pixel)
534                                   */
535                                  if (yback[x] == -2) {
536 <                                        for (i = y+1; i < ourview.vresolu; i++)
536 >                                        for (i = y+1; i < vresolu; i++)
537                                                  if (zscan(i)[x] > 0)
538                                                          break;
539 <                                        if (i < ourview.vresolu
539 >                                        if (i < vresolu
540                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
541                                                  yback[x] = i;
542                                          else
# Line 484 | Line 546 | backpicture()                          /* background fill algorithm */
546                                   * Next, find background from left or right.
547                                   */
548                                  if (xback == -2) {
549 <                                        for (i = x+1; i < ourview.hresolu; i++)
549 >                                        for (i = x+1; i < hresolu; i++)
550                                                  if (zscan(y)[i] > 0)
551                                                          break;
552 <                                        if (i < ourview.hresolu
552 >                                        if (i < hresolu
553                                  && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
554                                                  xback = i;
555                                          else
556                                                  xback = x-1;
557                                  }
558                                  /*
559 <                                 * Check to see if we have no background for
560 <                                 * this pixel.  If not, use background color.
559 >                                 * If we have no background for this pixel,
560 >                                 * use the given fill function.
561                                   */
562 <                                if (xback < 0 && yback[x] < 0) {
563 <                                        (*deffill)(x,y);
502 <                                        continue;
503 <                                }
562 >                                if (xback < 0 && yback[x] < 0)
563 >                                        goto fillit;
564                                  /*
565                                   * Compare, and use the background that is
566                                   * farther, unless one of them is next to us.
567 +                                 * If the background is too distant, call
568 +                                 * the fill function.
569                                   */
570                                  if ( yback[x] < 0
571                                          || (xback >= 0 && ABS(x-xback) <= 1)
572                                          || ( ABS(y-yback[x]) > 1
573                                                  && zscan(yback[x])[x]
574                                                  < zscan(y)[xback] ) ) {
575 +                                        if (samp > 0 && ABS(x-xback) >= samp)
576 +                                                goto fillit;
577                                          copycolr(pscan(y)[x],pscan(y)[xback]);
578                                          zscan(y)[x] = zscan(y)[xback];
579                                  } else {
580 +                                        if (samp > 0 && ABS(y-yback[x]) > samp)
581 +                                                goto fillit;
582                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
583                                          zscan(y)[x] = zscan(yback[x])[x];
584                                  }
585 +                                continue;
586 +                        fillit:
587 +                                (*fill)(x,y);
588 +                                if (fill == rcalfill) {         /* use it */
589 +                                        clearqueue();
590 +                                        xback = x;
591 +                                        yback[x] = y;
592 +                                }
593                          } else {                                /* full pixel */
594                                  yback[x] = -2;
595                                  xback = -2;
# Line 525 | Line 599 | backpicture()                          /* background fill algorithm */
599   }
600  
601  
602 < fillpicture()                           /* paint in empty pixels with default */
602 > fillpicture(fill)               /* paint in empty pixels using fill */
603 > int     (*fill)();
604   {
605          register int    x, y;
606  
607 <        for (y = 0; y < ourview.vresolu; y++)
608 <                for (x = 0; x < ourview.hresolu; x++)
607 >        for (y = 0; y < vresolu; y++)
608 >                for (x = 0; x < hresolu; x++)
609                          if (zscan(y)[x] <= 0)
610 <                                (*deffill)(x,y);
610 >                                (*fill)(x,y);
611   }
612  
613  
# Line 540 | Line 615 | writepicture()                         /* write out picture */
615   {
616          int     y;
617  
618 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
619 <        for (y = ourview.vresolu-1; y >= 0; y--)
620 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) {
621 <                        perror(progname);
547 <                        exit(1);
548 <                }
618 >        fprtresolu(hresolu, vresolu, stdout);
619 >        for (y = vresolu-1; y >= 0; y--)
620 >                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
621 >                        syserror(progname);
622   }
623  
624  
625   writedistance(fname)                    /* write out z file */
626   char    *fname;
627   {
555        extern double   sqrt();
628          int     donorm = normdist && ourview.type == VT_PER;
629 <        FILE    *fp;
629 >        int     fd;
630          int     y;
631          float   *zout;
632  
633 <        if ((fp = fopen(fname, "w")) == NULL) {
634 <                perror(fname);
563 <                exit(1);
564 <        }
633 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
634 >                syserror(fname);
635          if (donorm
636 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
637 <                perror(progname);
638 <                exit(1);
569 <        }
570 <        for (y = ourview.vresolu-1; y >= 0; y--) {
636 >        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
637 >                syserror(progname);
638 >        for (y = vresolu-1; y >= 0; y--) {
639                  if (donorm) {
640                          double  vx, yzn2;
641                          register int    x;
642 <                        yzn2 = y - .5*(ourview.vresolu-1);
643 <                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
644 <                        for (x = 0; x < ourview.hresolu; x++) {
645 <                                vx = x - .5*(ourview.hresolu-1);
642 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
643 >                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
644 >                        for (x = 0; x < hresolu; x++) {
645 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
646                                  zout[x] = zscan(y)[x]
647 <                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
647 >                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
648                          }
649                  } else
650                          zout = zscan(y);
651 <                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
652 <                                < ourview.hresolu) {
653 <                        perror(fname);
586 <                        exit(1);
587 <                }
651 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
652 >                                < hresolu*sizeof(float))
653 >                        syserror(fname);
654          }
655          if (donorm)
656                  free((char *)zout);
657 <        fclose(fp);
657 >        close(fd);
658   }
659  
660  
# Line 613 | Line 679 | int    x, y;
679   }
680  
681  
682 < calstart(prog, args)                    /* start fill calculation */
682 > calstart(prog, args)                    /* start fill calculation */
683   char    *prog, *args;
684   {
685          char    combuf[512];
686 <        int     p0[2], p1[2];
686 >        char    *argv[64];
687 >        int     rval;
688 >        register char   **wp, *cp;
689  
690          if (childpid != -1) {
691                  fprintf(stderr, "%s: too many calculations\n", progname);
692                  exit(1);
693          }
694 <        sprintf(combuf, prog, PACKSIZ, args);
695 <        if (pipe(p0) < 0 || pipe(p1) < 0)
696 <                syserror();
697 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
698 <                close(p0[1]);
699 <                close(p1[0]);
700 <                if (p0[0] != 0) {
701 <                        dup2(p0[0], 0);
702 <                        close(p0[0]);
703 <                }
704 <                if (p1[1] != 1) {
705 <                        dup2(p1[1], 1);
638 <                        close(p1[1]);
639 <                }
640 <                execl("/bin/sh", "sh", "-c", combuf, 0);
641 <                perror("/bin/sh");
642 <                _exit(127);
694 >        strcpy(combuf, prog);
695 >        strcat(combuf, args);
696 >        cp = combuf;
697 >        wp = argv;
698 >        for ( ; ; ) {
699 >                while (isspace(*cp))    /* nullify spaces */
700 >                        *cp++ = '\0';
701 >                if (!*cp)               /* all done? */
702 >                        break;
703 >                *wp++ = cp;             /* add argument to list */
704 >                while (*++cp && !isspace(*cp))
705 >                        ;
706          }
707 <        if (childpid == -1)
708 <                syserror();
709 <        close(p0[0]);
710 <        close(p1[1]);
711 <        if ((psend = fdopen(p0[1], "w")) == NULL)
712 <                syserror();
713 <        if ((precv = fdopen(p1[0], "r")) == NULL)
714 <                syserror();
707 >        *wp = NULL;
708 >                                                /* start process */
709 >        if ((rval = open_process(PDesc, argv)) < 0)
710 >                syserror(progname);
711 >        if (rval == 0) {
712 >                fprintf(stderr, "%s: command not found\n", argv[0]);
713 >                exit(1);
714 >        }
715 >        packsiz = rval/(6*sizeof(float)) - 1;
716 >        if (packsiz > PACKSIZ)
717 >                packsiz = PACKSIZ;
718          queuesiz = 0;
719   }
720  
721  
722 < caldone()                               /* done with calculation */
722 > caldone()                               /* done with calculation */
723   {
658        int     pid;
659
724          if (childpid == -1)
725                  return;
662        if (fclose(psend) == EOF)
663                syserror();
726          clearqueue();
727 <        fclose(precv);
666 <        while ((pid = wait(0)) != -1 && pid != childpid)
667 <                ;
727 >        close_process(PDesc);
728          childpid = -1;
729   }
730  
# Line 672 | Line 732 | caldone()                              /* done with calculation */
732   rcalfill(x, y)                          /* fill with ray-calculated pixel */
733   int     x, y;
734   {
735 <        FVECT   orig, dir;
676 <        float   outbuf[6];
677 <
678 <        if (queuesiz >= PACKSIZ) {      /* flush queue */
679 <                if (fflush(psend) == EOF)
680 <                        syserror();
735 >        if (queuesiz >= packsiz)        /* flush queue if needed */
736                  clearqueue();
737 <        }
683 <                                        /* send new ray */
684 <        rayview(orig, dir, &ourview, x+.5, y+.5);
685 <        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
686 <        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
687 <        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
688 <                syserror();
689 <                                        /* remember it */
737 >                                        /* add position to queue */
738          queue[queuesiz][0] = x;
739          queue[queuesiz][1] = y;
740          queuesiz++;
741   }
742  
743  
744 < clearqueue()                            /* get results from queue */
744 > clearqueue()                            /* process queue */
745   {
746 <        float   inbuf[4];
746 >        FVECT   orig, dir;
747 >        float   fbuf[6*(PACKSIZ+1)];
748 >        register float  *fbp;
749          register int    i;
750  
751 +        if (queuesiz == 0)
752 +                return;
753 +        fbp = fbuf;
754          for (i = 0; i < queuesiz; i++) {
755 <                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
756 <                        fprintf(stderr, "%s: read error in clearqueue\n",
757 <                                        progname);
758 <                        exit(1);
755 >                viewray(orig, dir, &ourview,
756 >                                (queue[i][0]+.5)/hresolu,
757 >                                (queue[i][1]+.5)/vresolu);
758 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
759 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
760 >        }
761 >                                        /* mark end and get results */
762 >        bzero((char *)fbp, 6*sizeof(float));
763 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
764 >                        6*sizeof(float)*(queuesiz+1)) !=
765 >                        4*sizeof(float)*queuesiz) {
766 >                fprintf(stderr, "%s: error reading from rtrace process\n",
767 >                                progname);
768 >                exit(1);
769 >        }
770 >        fbp = fbuf;
771 >        for (i = 0; i < queuesiz; i++) {
772 >                if (ourexp > 0 && ourexp != 1.0) {
773 >                        fbp[0] *= ourexp;
774 >                        fbp[1] *= ourexp;
775 >                        fbp[2] *= ourexp;
776                  }
777                  setcolr(pscan(queue[i][1])[queue[i][0]],
778 <                                inbuf[0], inbuf[1], inbuf[2]);
779 <                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
778 >                                fbp[0], fbp[1], fbp[2]);
779 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
780 >                fbp += 4;
781          }
782          queuesiz = 0;
783   }
784  
785  
786 < syserror()                      /* report error and exit */
786 > syserror(s)                     /* report error and exit */
787 > char    *s;
788   {
789 <        perror(progname);
789 >        perror(s);
790          exit(1);
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines