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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines