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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines