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 2.6 by greg, Tue May 26 15:23:09 1992 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines