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.8 by greg, Wed Jan 3 09:37:16 1990 UTC vs.
Revision 2.17 by greg, Fri Dec 23 17:23:05 1994 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1994 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 + #define NSTEPS          64              /* number steps in overlap prescan */
38 + #define MINSTEP         4               /* minimum worthwhile preview step */
39 +
40 + struct bound {int min,max;};
41 +
42 + VIEW    ourview = STDVIEW;              /* desired view */
43 + int     hresolu = 512;                  /* horizontal resolution */
44 + int     vresolu = 512;                  /* vertical resolution */
45 + double  pixaspect = 1.0;                /* pixel aspect ratio */
46 +
47   double  zeps = .02;                     /* allowed z epsilon */
48  
49   COLR    *ourpict;                       /* output picture */
# Line 28 | Line 51 | float  *ourzbuf;                       /* corresponding z-buffer */
51  
52   char    *progname;
53  
54 < VIEW    theirview = STDVIEW(512);       /* input view */
55 < int     gotview;                        /* got input view? */
54 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
55 > int     fillsamp = 0;                   /* sample separation (0 == inf) */
56 > extern int      backfill(), rcalfill(); /* fill functions */
57 > int     (*fillfunc)() = backfill;       /* selected fill function */
58 > COLR    backcolr = BLKCOLR;             /* background color */
59 > double  backz = 0.0;                    /* background z value */
60 > int     normdist = 1;                   /* normalized distance? */
61 > double  ourexp = -1;                    /* output picture exposure */
62  
63 + VIEW    theirview = STDVIEW;            /* input view */
64 + int     gotview;                        /* got input view? */
65 + int     wrongformat = 0;                /* input in another format? */
66 + RESOLU  tresolu;                        /* input resolution */
67 + double  theirexp;                       /* input picture exposure */
68   double  theirs2ours[4][4];              /* transformation matrix */
69 + int     hasmatrix = 0;                  /* has transformation matrix */
70  
71 + int     PDesc[3] = {-1,-1,-1};          /* rtrace process descriptor */
72 + #define childpid        (PDesc[2])
73 + unsigned short  queue[PACKSIZ][2];      /* pending pixels */
74 + int     packsiz;                        /* actual packet size */
75 + int     queuesiz;                       /* number of pixels pending */
76  
77 +
78   main(argc, argv)                        /* interpolate pictures */
79   int     argc;
80   char    *argv[];
81   {
82 < #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
82 > #define  check(ol,al)           if (argv[i][ol] || \
83 >                                badarg(argc-i-1,argv+i+1,al)) \
84 >                                goto badopt
85          int     gotvfile = 0;
86 +        char    *zfile = NULL;
87          char    *err;
88 <        int     i;
88 >        int     i, rval;
89  
90          progname = argv[0];
91  
92 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
92 >        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
93 >                rval = getviewopt(&ourview, argc-i, argv+i);
94 >                if (rval >= 0) {
95 >                        i += rval;
96 >                        continue;
97 >                }
98                  switch (argv[i][1]) {
99                  case 't':                               /* threshold */
100 <                        check(2,1);
100 >                        check(2,"f");
101                          zeps = atof(argv[++i]);
102                          break;
103 <                case 'x':                               /* x resolution */
104 <                        check(2,1);
105 <                        ourview.hresolu = atoi(argv[++i]);
103 >                case 'n':                               /* dist. normalized? */
104 >                        check(2,NULL);
105 >                        normdist = !normdist;
106                          break;
107 <                case 'y':                               /* y resolution */
59 <                        check(2,1);
60 <                        ourview.vresolu = atoi(argv[++i]);
61 <                        break;
62 <                case 'v':                               /* view */
107 >                case 'f':                               /* fill type */
108                          switch (argv[i][2]) {
109 <                        case 't':                               /* type */
110 <                                check(4,0);
111 <                                ourview.type = argv[i][3];
109 >                        case '0':                               /* none */
110 >                                check(3,NULL);
111 >                                fillo = 0;
112                                  break;
113 <                        case 'p':                               /* point */
114 <                                check(3,3);
115 <                                ourview.vp[0] = atof(argv[++i]);
71 <                                ourview.vp[1] = atof(argv[++i]);
72 <                                ourview.vp[2] = atof(argv[++i]);
113 >                        case 'f':                               /* foreground */
114 >                                check(3,NULL);
115 >                                fillo = F_FORE;
116                                  break;
117 <                        case 'd':                               /* direction */
118 <                                check(3,3);
119 <                                ourview.vdir[0] = atof(argv[++i]);
77 <                                ourview.vdir[1] = atof(argv[++i]);
78 <                                ourview.vdir[2] = atof(argv[++i]);
117 >                        case 'b':                               /* background */
118 >                                check(3,NULL);
119 >                                fillo = F_BACK;
120                                  break;
121 <                        case 'u':                               /* up */
122 <                                check(3,3);
123 <                                ourview.vup[0] = atof(argv[++i]);
83 <                                ourview.vup[1] = atof(argv[++i]);
84 <                                ourview.vup[2] = atof(argv[++i]);
121 >                        case 'a':                               /* all */
122 >                                check(3,NULL);
123 >                                fillo = F_FORE|F_BACK;
124                                  break;
125 <                        case 'h':                               /* horizontal */
126 <                                check(3,1);
127 <                                ourview.horiz = atof(argv[++i]);
125 >                        case 's':                               /* sample */
126 >                                check(3,"i");
127 >                                fillsamp = atoi(argv[++i]);
128                                  break;
129 <                        case 'v':                               /* vertical */
130 <                                check(3,1);
131 <                                ourview.vert = atof(argv[++i]);
129 >                        case 'c':                               /* color */
130 >                                check(3,"fff");
131 >                                fillfunc = backfill;
132 >                                setcolr(backcolr, atof(argv[i+1]),
133 >                                        atof(argv[i+2]), atof(argv[i+3]));
134 >                                i += 3;
135                                  break;
136 <                        case 'f':                               /* file */
137 <                                check(3,1);
138 <                                gotvfile = viewfile(argv[++i], &ourview);
139 <                                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 <                                }
136 >                        case 'z':                               /* z value */
137 >                                check(3,"f");
138 >                                fillfunc = backfill;
139 >                                backz = atof(argv[++i]);
140                                  break;
141 +                        case 'r':                               /* rtrace */
142 +                                check(3,"s");
143 +                                fillfunc = rcalfill;
144 +                                calstart(RTCOM, argv[++i]);
145 +                                break;
146                          default:
147                                  goto badopt;
148                          }
149                          break;
150 +                case 'z':                               /* z file */
151 +                        check(2,"s");
152 +                        zfile = argv[++i];
153 +                        break;
154 +                case 'x':                               /* x resolution */
155 +                        check(2,"i");
156 +                        hresolu = atoi(argv[++i]);
157 +                        break;
158 +                case 'y':                               /* y resolution */
159 +                        check(2,"i");
160 +                        vresolu = atoi(argv[++i]);
161 +                        break;
162 +                case 'p':                               /* pixel aspect */
163 +                        if (argv[i][2] != 'a')
164 +                                goto badopt;
165 +                        check(3,"f");
166 +                        pixaspect = atof(argv[++i]);
167 +                        break;
168 +                case 'v':                               /* view file */
169 +                        if (argv[i][2] != 'f')
170 +                                goto badopt;
171 +                        check(3,"s");
172 +                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
173 +                        if (gotvfile < 0)
174 +                                syserror(argv[i]);
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                  }
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);
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)) {
194 >        if ((err = setview(&ourview)) != NULL) {
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);
204 <                exit(1);
205 <        }
200 >        ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
201 >        ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
202 >        if (ourpict == NULL || ourzbuf == NULL)
203 >                syserror(progname);
204 >        bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
205 >                                                        /* new header */
206 >        newheader("RADIANCE", stdout);
207                                                          /* get input */
208          for ( ; i < argc; i += 2)
209                  addpicture(argv[i], argv[i+1]);
210                                                          /* fill in spaces */
211 <        fillpicture();
211 >        if (fillo&F_BACK)
212 >                backpicture(fillfunc, fillsamp);
213 >        else
214 >                fillpicture(fillfunc);
215 >                                                        /* close calculation */
216 >        caldone();
217 >                                                        /* aft clipping */
218 >        clipaft();
219                                                          /* add to header */
220          printargs(argc, argv, stdout);
221          if (gotvfile) {
222 <                printf(VIEWSTR);
222 >                fputs(VIEWSTR, stdout);
223                  fprintview(&ourview, stdout);
224 <                printf("\n");
224 >                putc('\n', stdout);
225          }
226 <        printf("\n");
227 <                                                        /* write output */
226 >        if (pixaspect < .99 || pixaspect > 1.01)
227 >                fputaspect(pixaspect, stdout);
228 >        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
229 >                fputexpos(ourexp, stdout);
230 >        fputformat(COLRFMT, stdout);
231 >        putc('\n', stdout);
232 >                                                        /* write picture */
233          writepicture();
234 +                                                        /* write z file */
235 +        if (zfile != NULL)
236 +                writedistance(zfile);
237  
238          exit(0);
239 + userr:
240 +        fprintf(stderr,
241 +        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
242 +                        progname);
243 +        exit(1);
244   #undef check
245   }
246  
# Line 155 | Line 248 | char   *argv[];
248   headline(s)                             /* process header string */
249   char    *s;
250   {
251 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
159 <        register char   **an;
251 >        char    fmt[32];
252  
253 <        printf("\t%s", s);
253 >        if (isheadid(s))
254 >                return;
255 >        if (formatval(fmt, s)) {
256 >                wrongformat = strcmp(fmt, COLRFMT);
257 >                return;
258 >        }
259 >        putc('\t', stdout);
260 >        fputs(s, stdout);
261  
262 <        for (an = altname; *an != NULL; an++)
263 <                if (!strncmp(*an, s, strlen(*an))) {
264 <                        if (sscanview(&theirview, s+strlen(*an)) == 0)
265 <                                gotview++;
266 <                        break;
267 <                }
262 >        if (isexpos(s)) {
263 >                theirexp *= exposval(s);
264 >                return;
265 >        }
266 >        if (isview(s) && sscanview(&theirview, s) > 0)
267 >                gotview++;
268   }
269  
270  
271 < addpicture(pfile, zfile)                /* add picture to output */
272 < char    *pfile, *zfile;
271 > addpicture(pfile, zspec)                /* add picture to output */
272 > char    *pfile, *zspec;
273   {
274 <        FILE    *pfp, *zfp;
274 >        FILE    *pfp;
275 >        int     zfd;
276          char    *err;
277          COLR    *scanin;
278 <        float   *zin, *zout;
279 <        int     *pout;
280 <        int     xres, yres;
278 >        float   *zin;
279 >        struct position *plast;
280 >        struct bound    *xlim, ylim;
281          int     y;
282 <                                        /* open input files */
283 <        if ((pfp = fopen(pfile, "r")) == NULL) {
284 <                perror(pfile);
285 <                exit(1);
286 <        }
187 <        if ((zfp = fopen(zfile, "r")) == NULL) {
188 <                perror(zfile);
189 <                exit(1);
190 <        }
191 <                                        /* get header and view */
192 <        printf("%s:\n", pfile);
282 >                                        /* open picture file */
283 >        if ((pfp = fopen(pfile, "r")) == NULL)
284 >                syserror(pfile);
285 >                                        /* get header with exposure and view */
286 >        theirexp = 1.0;
287          gotview = 0;
288 <        getheader(pfp, headline);
289 <        if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) {
290 <                fprintf(stderr, "%s: picture view error\n", pfile);
288 >        printf("%s:\n", pfile);
289 >        getheader(pfp, headline, NULL);
290 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
291 >                fprintf(stderr, "%s: picture format error\n", pfile);
292                  exit(1);
293          }
294 <        theirview.hresolu = xres;
295 <        theirview.vresolu = yres;
294 >        if (ourexp <= 0)
295 >                ourexp = theirexp;
296 >        else if (ABS(theirexp-ourexp) > .01*ourexp)
297 >                fprintf(stderr, "%s: different exposure (warning)\n", pfile);
298          if (err = setview(&theirview)) {
299                  fprintf(stderr, "%s: %s\n", pfile, err);
300                  exit(1);
301          }
302                                          /* compute transformation */
303 <        pixform(theirs2ours, &theirview, &ourview);
304 <                                        /* allocate scanlines */
305 <        scanin = (COLR *)malloc(xres*sizeof(COLR));
306 <        zin = (float *)malloc(xres*sizeof(float));
307 <        zout = (float *)calloc(xres, sizeof(float));
308 <        pout = (int *)calloc(xres, sizeof(int));
309 <        if (scanin == NULL || zin == NULL || zout == NULL || pout == NULL) {
310 <                perror(progname);
311 <                exit(1);
303 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
304 >                                        /* get z specification or file */
305 >        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
306 >        if (zin == NULL)
307 >                syserror(progname);
308 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
309 >                double  zvalue;
310 >                register int    x;
311 >                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
312 >                        syserror(zspec);
313 >                for (x = scanlen(&tresolu); x-- > 0; )
314 >                        zin[x] = zvalue;
315          }
316 <                                        /* load image */
317 <        for (y = yres-1; y >= 0; y--) {
318 <                if (freadcolrs(scanin, xres, pfp) < 0) {
316 >                                        /* compute transferrable perimeter */
317 >        xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound));
318 >        if (xlim == NULL)
319 >                syserror(progname);
320 >        if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */
321 >                free((char *)zin);
322 >                free((char *)xlim);
323 >                if (zfd != -1)
324 >                        close(zfd);
325 >                fclose(pfp);
326 >                return;
327 >        }
328 >                                        /* allocate scanlines */
329 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
330 >        plast = (struct position *)calloc(scanlen(&tresolu),
331 >                        sizeof(struct position));
332 >        if (scanin == NULL | plast == NULL)
333 >                syserror(progname);
334 >                                        /* skip to starting point */
335 >        for (y = 0; y < ylim.min; y++)
336 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
337                          fprintf(stderr, "%s: read error\n", pfile);
338                          exit(1);
339                  }
340 <                if (fread(zin, sizeof(float), xres, zfp) < xres) {
341 <                        fprintf(stderr, "%s: read error\n", zfile);
340 >        if (zfd != -1 && lseek(zfd,
341 >                        (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0)
342 >                syserror(zspec);
343 >                                        /* load image */
344 >        for (y = ylim.min; y <= ylim.max; y++) {
345 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
346 >                        fprintf(stderr, "%s: read error\n", pfile);
347                          exit(1);
348                  }
349 <                addscanline(y, scanin, zin, pout, zout);
349 >                if (zfd != -1 && read(zfd, (char *)zin,
350 >                                scanlen(&tresolu)*sizeof(float))
351 >                                < scanlen(&tresolu)*sizeof(float))
352 >                        syserror(zspec);
353 >                addscanline(xlim+y, y, scanin, zin, plast);
354          }
355                                          /* clean up */
356 +        free((char *)xlim);
357          free((char *)scanin);
358          free((char *)zin);
359 <        free((char *)pout);
232 <        free((char *)zout);
359 >        free((char *)plast);
360          fclose(pfp);
361 <        fclose(zfp);
361 >        if (zfd != -1)
362 >                close(zfd);
363   }
364  
365  
# Line 241 | Line 369 | register VIEW  *vw1, *vw2;
369   {
370          double  m4t[4][4];
371  
372 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
373 +                return(0);
374 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
375 +                return(0);
376          setident4(xfmat);
377 <        xfmat[0][0] = vw1->vhinc[0];
378 <        xfmat[0][1] = vw1->vhinc[1];
379 <        xfmat[0][2] = vw1->vhinc[2];
380 <        xfmat[1][0] = vw1->vvinc[0];
381 <        xfmat[1][1] = vw1->vvinc[1];
382 <        xfmat[1][2] = vw1->vvinc[2];
377 >        xfmat[0][0] = vw1->hvec[0];
378 >        xfmat[0][1] = vw1->hvec[1];
379 >        xfmat[0][2] = vw1->hvec[2];
380 >        xfmat[1][0] = vw1->vvec[0];
381 >        xfmat[1][1] = vw1->vvec[1];
382 >        xfmat[1][2] = vw1->vvec[2];
383          xfmat[2][0] = vw1->vdir[0];
384          xfmat[2][1] = vw1->vdir[1];
385          xfmat[2][2] = vw1->vdir[2];
# Line 255 | Line 387 | register VIEW  *vw1, *vw2;
387          xfmat[3][1] = vw1->vp[1];
388          xfmat[3][2] = vw1->vp[2];
389          setident4(m4t);
390 <        m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
391 <        m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
392 <        m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
393 <        m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
394 <        m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
395 <        m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
396 <        m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
397 <        m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
390 >        m4t[0][0] = vw2->hvec[0]/vw2->hn2;
391 >        m4t[1][0] = vw2->hvec[1]/vw2->hn2;
392 >        m4t[2][0] = vw2->hvec[2]/vw2->hn2;
393 >        m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
394 >        m4t[0][1] = vw2->vvec[0]/vw2->vn2;
395 >        m4t[1][1] = vw2->vvec[1]/vw2->vn2;
396 >        m4t[2][1] = vw2->vvec[2]/vw2->vn2;
397 >        m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
398          m4t[0][2] = vw2->vdir[0];
399          m4t[1][2] = vw2->vdir[1];
400          m4t[2][2] = vw2->vdir[2];
401          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
402          multmat4(xfmat, xfmat, m4t);
403 +        return(1);
404   }
405  
406  
407 < addscanline(y, pline, zline, lasty, lastyz)     /* add scanline to output */
407 > addscanline(xl, y, pline, zline, lasty) /* add scanline to output */
408 > struct bound    *xl;
409   int     y;
410   COLR    *pline;
411   float   *zline;
412 < int     *lasty;
279 < float   *lastyz;
412 > struct position *lasty;         /* input/output */
413   {
414 <        extern double   sqrt(), fabs();
415 <        double  pos[3];
283 <        int     lastx = 0;
284 <        double  lastxz = 0;
285 <        double  zt;
286 <        int     xpos, ypos;
414 >        FVECT   pos;
415 >        struct position lastx, newpos;
416          register int    x;
417  
418 <        for (x = theirview.hresolu-1; x >= 0; x--) {
419 <                pos[0] = x - .5*(theirview.hresolu-1);
420 <                pos[1] = y - .5*(theirview.vresolu-1);
418 >        lastx.z = 0;
419 >        for (x = xl->max; x >= xl->min; x--) {
420 >                pix2loc(pos, &tresolu, x, y);
421                  pos[2] = zline[x];
422 <                if (theirview.type == VT_PER) {
423 <                        /*
295 <                         * The following (single) statement can go
296 <                         * if z is along the view direction rather
297 <                         * than an eye ray.
298 <                         */
299 <                        pos[2] /= sqrt( 1.
300 <                                        + pos[0]*pos[0]*theirview.vhn2
301 <                                        + pos[1]*pos[1]*theirview.vvn2 );
302 <                        pos[0] *= pos[2];
303 <                        pos[1] *= pos[2];
304 <                }
305 <                multp3(pos, pos, theirs2ours);
306 <                if (pos[2] <= 0)
422 >                if (movepixel(pos) < 0) {
423 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
424                          continue;
308                if (ourview.type == VT_PER) {
309                        pos[0] /= pos[2];
310                        pos[1] /= pos[2];
425                  }
426 <                pos[0] += .5*ourview.hresolu;
427 <                pos[1] += .5*ourview.vresolu;
428 <                if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
315 <                        || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
316 <                        continue;
426 >                newpos.x = pos[0] * hresolu;
427 >                newpos.y = pos[1] * vresolu;
428 >                newpos.z = zline[x];
429                                          /* add pixel to our image */
430 <                zt = 2.*zeps*zline[x];
431 <                addpixel(xpos, ypos,
432 <                        (fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1,
433 <                        (fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1,
434 <                        pline[x], pos[2]);
435 <                lastx = xpos;
436 <                lasty[x] = ypos;
437 <                lastxz = lastyz[x] = zline[x];
430 >                if (pos[0] >= 0 && newpos.x < hresolu
431 >                                && pos[1] >= 0 && newpos.y < vresolu) {
432 >                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
433 >                        lasty[x].x = lastx.x = newpos.x;
434 >                        lasty[x].y = lastx.y = newpos.y;
435 >                        lasty[x].z = lastx.z = newpos.z;
436 >                } else
437 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
438          }
439   }
440  
441  
442 < addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */
443 < int     xstart, ystart;
332 < int     width, height;
442 > addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
443 > struct position *p0, *p1, *p2;
444   COLR    pix;
445   double  z;
446   {
447 <        register int    x, y;
448 <                                        /* make width and height positive */
449 <        if (width < 0) {
450 <                width = -width;
451 <                xstart = xstart-width+1;
452 <        } else if (width == 0)
453 <                width = 1;
454 <        if (height < 0) {
455 <                height = -height;
456 <                ystart = ystart-height+1;
457 <        } else if (height == 0)
458 <                height = 1;
459 <                                        /* fill pixel(s) within rectangle */
460 <        for (y = ystart; y < ystart+height; y++)
461 <                for (x = xstart; x < xstart+width; x++)
462 <                        if (zscan(y)[x] <= 0
463 <                                        || zscan(y)[x]-z > zeps*zscan(y)[x]) {
447 >        double  zt = 2.*zeps*p0->z;             /* threshold */
448 >        int     s1x, s1y, s2x, s2y;             /* step sizes */
449 >        int     l1, l2, c1, c2;                 /* side lengths and counters */
450 >        int     p1isy;                          /* p0p1 along y? */
451 >        int     x1, y1;                         /* p1 position */
452 >        register int    x, y;                   /* final position */
453 >
454 >                                        /* compute vector p0p1 */
455 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
456 >                s1x = p1->x - p0->x;
457 >                s1y = p1->y - p0->y;
458 >                l1 = ABS(s1x);
459 >                if (p1isy = (ABS(s1y) > l1))
460 >                        l1 = ABS(s1y);
461 >        } else {
462 >                l1 = s1x = s1y = 1;
463 >                p1isy = -1;
464 >        }
465 >                                        /* compute vector p0p2 */
466 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
467 >                s2x = p2->x - p0->x;
468 >                s2y = p2->y - p0->y;
469 >                if (p1isy == 1)
470 >                        l2 = ABS(s2x);
471 >                else {
472 >                        l2 = ABS(s2y);
473 >                        if (p1isy != 0 && ABS(s2x) > l2)
474 >                                l2 = ABS(s2x);
475 >                }
476 >        } else
477 >                l2 = s2x = s2y = 1;
478 >                                        /* fill the parallelogram */
479 >        for (c1 = l1; c1-- > 0; ) {
480 >                x1 = p0->x + c1*s1x/l1;
481 >                y1 = p0->y + c1*s1y/l1;
482 >                for (c2 = l2; c2-- > 0; ) {
483 >                        x = x1 + c2*s2x/l2;
484 >                        if (x < 0 || x >= hresolu)
485 >                                continue;
486 >                        y = y1 + c2*s2y/l2;
487 >                        if (y < 0 || y >= vresolu)
488 >                                continue;
489 >                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
490 >                                                > zeps*zscan(y)[x]) {
491                                  zscan(y)[x] = z;
492                                  copycolr(pscan(y)[x], pix);
493                          }
494 +                }
495 +        }
496   }
497  
498  
499 < fillpicture()                           /* fill in empty spaces */
499 > getperim(xl, yl, zline, zfd)            /* compute overlapping image area */
500 > register struct bound   *xl;
501 > struct bound    *yl;
502 > float   *zline;
503 > int     zfd;
504   {
505 +        int     step;
506 +        FVECT   pos;
507 +        register int    x, y;
508 +                                                /* set up step size */
509 +        if (scanlen(&tresolu) < numscans(&tresolu))
510 +                step = scanlen(&tresolu)/NSTEPS;
511 +        else
512 +                step = numscans(&tresolu)/NSTEPS;
513 +        if (step < MINSTEP) {                   /* not worth cropping? */
514 +                yl->min = 0;
515 +                yl->max = numscans(&tresolu) - 1;
516 +                x = scanlen(&tresolu) - 1;
517 +                for (y = numscans(&tresolu); y--; ) {
518 +                        xl[y].min = 0;
519 +                        xl[y].max = x;
520 +                }
521 +                return(1);
522 +        }
523 +        yl->min = 32000; yl->max = 0;           /* search for points on image */
524 +        for (y = step - 1; y < numscans(&tresolu); y += step) {
525 +                if (zfd != -1) {
526 +                        if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float),
527 +                                        0) < 0)
528 +                                syserror("lseek");
529 +                        if (read(zfd, (char *)zline,
530 +                                        scanlen(&tresolu)*sizeof(float))
531 +                                        < scanlen(&tresolu)*sizeof(float))
532 +                                syserror("read");
533 +                }
534 +                xl[y].min = 32000; xl[y].max = 0;               /* x max */
535 +                for (x = scanlen(&tresolu); (x -= step) > 0; ) {
536 +                        pix2loc(pos, &tresolu, x, y);
537 +                        pos[2] = zline[x];
538 +                        if (movepixel(pos) == 0 && pos[0] >= 0 &&
539 +                                        pos[0] < 1 && pos[1] >= 0 &&
540 +                                        pos[1] < 1) {
541 +                                xl[y].max = x + step - 1;
542 +                                xl[y].min = x - step + 1;       /* x min */
543 +                                if (xl[y].min < 0)
544 +                                        xl[y].min = 0;
545 +                                for (x = step - 1; x < xl[y].max; x += step) {
546 +                                        pix2loc(pos, &tresolu, x, y);
547 +                                        pos[2] = zline[x];
548 +                                        if (movepixel(pos) == 0 &&
549 +                                                        pos[0] >= 0 &&
550 +                                                        pos[0] < 1 &&
551 +                                                        pos[1] >= 0 &&
552 +                                                        pos[1] < 1) {
553 +                                                xl[y].min = x - step + 1;
554 +                                                break;
555 +                                        }
556 +                                }
557 +                                if (y < yl->min)                /* y limits */
558 +                                        yl->min = y - step + 1;
559 +                                yl->max = y + step - 1;
560 +                                break;
561 +                        }
562 +                }
563 +                                                        /* fill in between */
564 +                if (xl[y].min < xl[y-step].min)
565 +                        xl[y-1].min = xl[y].min;
566 +                else
567 +                        xl[y-1].min = xl[y-step].min;
568 +                if (xl[y].max > xl[y-step].max)
569 +                        xl[y-1].max = xl[y].max;
570 +                else
571 +                        xl[y-1].max = xl[y-step].max;
572 +                for (x = 2; x < step; x++)
573 +                        copystruct(xl+y-x, xl+y-1);
574 +        }
575 +        if (yl->max >= numscans(&tresolu))
576 +                yl->max = numscans(&tresolu) - 1;
577 +        for (x = numscans(&tresolu) - 1; x > y; x--)    /* fill bottom rows */
578 +                copystruct(xl+x, xl+y);
579 +        return(yl->max >= yl->min);
580 + }
581 +
582 +
583 + movepixel(pos)                          /* reposition image point */
584 + FVECT   pos;
585 + {
586 +        double  d0, d1;
587 +        FVECT   pt, direc;
588 +        
589 +        if (pos[2] <= 0)                /* empty pixel */
590 +                return(-1);
591 +        if (normdist && theirview.type == VT_PER) {     /* adjust distance */
592 +                d0 = pos[0] + theirview.hoff - .5;
593 +                d1 = pos[1] + theirview.voff - .5;
594 +                pos[2] /= sqrt(1. + d0*d0*theirview.hn2 + d1*d1*theirview.vn2);
595 +        }
596 +        if (hasmatrix) {
597 +                pos[0] += theirview.hoff - .5;
598 +                pos[1] += theirview.voff - .5;
599 +                if (theirview.type == VT_PER) {
600 +                        pos[0] *= pos[2];
601 +                        pos[1] *= pos[2];
602 +                }
603 +                multp3(pos, pos, theirs2ours);
604 +                if (pos[2] <= 0)
605 +                        return(-1);
606 +                if (ourview.type == VT_PER) {
607 +                        pos[0] /= pos[2];
608 +                        pos[1] /= pos[2];
609 +                }
610 +                pos[0] += .5 - ourview.hoff;
611 +                pos[1] += .5 - ourview.voff;
612 +                return(0);
613 +        }
614 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < -FTINY)
615 +                return(-1);
616 +        pt[0] += direc[0]*pos[2];
617 +        pt[1] += direc[1]*pos[2];
618 +        pt[2] += direc[2]*pos[2];
619 +        viewloc(pos, &ourview, pt);
620 +        if (pos[2] <= 0)
621 +                return(-1);
622 +        return(0);
623 + }
624 +
625 +
626 + backpicture(fill, samp)                 /* background fill algorithm */
627 + int     (*fill)();
628 + int     samp;
629 + {
630          int     *yback, xback;
631          int     y;
363        COLR    pfill;
632          register int    x, i;
633                                                          /* get back buffer */
634 <        yback = (int *)malloc(ourview.hresolu*sizeof(int));
635 <        if (yback == NULL) {
636 <                perror(progname);
637 <                return;
370 <        }
371 <        for (x = 0; x < ourview.hresolu; x++)
634 >        yback = (int *)malloc(hresolu*sizeof(int));
635 >        if (yback == NULL)
636 >                syserror(progname);
637 >        for (x = 0; x < hresolu; x++)
638                  yback[x] = -2;
639          /*
640           * Xback and yback are the pixel locations of suitable
# Line 377 | Line 643 | fillpicture()                          /* fill in empty spaces */
643           * that there is no suitable background in this direction.
644           */
645                                                          /* fill image */
646 <        for (y = 0; y < ourview.vresolu; y++) {
646 >        for (y = 0; y < vresolu; y++) {
647                  xback = -2;
648 <                for (x = 0; x < ourview.hresolu; x++)
648 >                for (x = 0; x < hresolu; x++)
649                          if (zscan(y)[x] <= 0) {         /* empty pixel */
650                                  /*
651                                   * First, find background from above or below.
652                                   * (farthest assigned pixel)
653                                   */
654                                  if (yback[x] == -2) {
655 <                                        for (i = y+1; i < ourview.vresolu; i++)
655 >                                        for (i = y+1; i < vresolu; i++)
656                                                  if (zscan(i)[x] > 0)
657                                                          break;
658 <                                        if (i < ourview.vresolu
658 >                                        if (i < vresolu
659                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
660                                                  yback[x] = i;
661                                          else
# Line 399 | Line 665 | fillpicture()                          /* fill in empty spaces */
665                                   * Next, find background from left or right.
666                                   */
667                                  if (xback == -2) {
668 <                                        for (i = x+1; x < ourview.hresolu; i++)
668 >                                        for (i = x+1; i < hresolu; i++)
669                                                  if (zscan(y)[i] > 0)
670                                                          break;
671 <                                        if (i < ourview.hresolu
671 >                                        if (i < hresolu
672                                  && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
673                                                  xback = i;
674                                          else
675                                                  xback = x-1;
676                                  }
677 +                                /*
678 +                                 * If we have no background for this pixel,
679 +                                 * use the given fill function.
680 +                                 */
681                                  if (xback < 0 && yback[x] < 0)
682 <                                        continue;       /* no background */
682 >                                        goto fillit;
683                                  /*
684                                   * Compare, and use the background that is
685                                   * farther, unless one of them is next to us.
686 +                                 * If the background is too distant, call
687 +                                 * the fill function.
688                                   */
689 <                                if (yback[x] < 0 || ABS(x-xback) <= 1
689 >                                if ( yback[x] < 0
690 >                                        || (xback >= 0 && ABS(x-xback) <= 1)
691                                          || ( ABS(y-yback[x]) > 1
692 <                                && zscan(yback[x])[x] < zscan(y)[xback] ))
692 >                                                && zscan(yback[x])[x]
693 >                                                < zscan(y)[xback] ) ) {
694 >                                        if (samp > 0 && ABS(x-xback) >= samp)
695 >                                                goto fillit;
696                                          copycolr(pscan(y)[x],pscan(y)[xback]);
697 <                                else
697 >                                        zscan(y)[x] = zscan(y)[xback];
698 >                                } else {
699 >                                        if (samp > 0 && ABS(y-yback[x]) > samp)
700 >                                                goto fillit;
701                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
702 +                                        zscan(y)[x] = zscan(yback[x])[x];
703 +                                }
704 +                                continue;
705 +                        fillit:
706 +                                (*fill)(x,y);
707 +                                if (fill == rcalfill) {         /* use it */
708 +                                        clearqueue();
709 +                                        xback = x;
710 +                                        yback[x] = y;
711 +                                }
712                          } else {                                /* full pixel */
713                                  yback[x] = -2;
714                                  xback = -2;
# Line 429 | Line 718 | fillpicture()                          /* fill in empty spaces */
718   }
719  
720  
721 + fillpicture(fill)               /* paint in empty pixels using fill */
722 + int     (*fill)();
723 + {
724 +        register int    x, y;
725 +
726 +        for (y = 0; y < vresolu; y++)
727 +                for (x = 0; x < hresolu; x++)
728 +                        if (zscan(y)[x] <= 0)
729 +                                (*fill)(x,y);
730 + }
731 +
732 +
733 + clipaft()                       /* perform aft clipping as indicated */
734 + {
735 +        register int    x, y;
736 +        double  tstdist;
737 +        double  yzn2, vx;
738 +
739 +        if (ourview.vaft <= FTINY)
740 +                return;
741 +        tstdist = ourview.vaft - ourview.vfore;
742 +        for (y = 0; y < vresolu; y++) {
743 +                if (ourview.type == VT_PER) {           /* adjust distance */
744 +                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
745 +                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
746 +                        tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2);
747 +                }
748 +                for (x = 0; x < hresolu; x++)
749 +                        if (zscan(y)[x] > tstdist) {
750 +                                if (ourview.type == VT_PER) {
751 +                                        vx = (x+.5)/hresolu + ourview.hoff - .5;
752 +                                        if (zscan(y)[x] <= (ourview.vaft -
753 +                                                        ourview.vfore) *
754 +                                                sqrt(vx*vx*ourview.hn2 + yzn2))
755 +                                                continue;
756 +                                }
757 +                                bzero(pscan(y)[x], sizeof(COLR));
758 +                                zscan(y)[x] = 0.0;
759 +                        }
760 +        }
761 + }
762 +
763 +
764   writepicture()                          /* write out picture */
765   {
766          int     y;
767  
768 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
769 <        for (y = ourview.vresolu-1; y >= 0; y--)
770 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) {
771 <                        perror(progname);
772 <                        exit(1);
768 >        fprtresolu(hresolu, vresolu, stdout);
769 >        for (y = vresolu-1; y >= 0; y--)
770 >                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
771 >                        syserror(progname);
772 > }
773 >
774 >
775 > writedistance(fname)                    /* write out z file */
776 > char    *fname;
777 > {
778 >        int     donorm = normdist && ourview.type == VT_PER;
779 >        int     fd;
780 >        int     y;
781 >        float   *zout;
782 >
783 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
784 >                syserror(fname);
785 >        if (donorm
786 >        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
787 >                syserror(progname);
788 >        for (y = vresolu-1; y >= 0; y--) {
789 >                if (donorm) {
790 >                        double  vx, yzn2;
791 >                        register int    x;
792 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
793 >                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
794 >                        for (x = 0; x < hresolu; x++) {
795 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
796 >                                zout[x] = zscan(y)[x]
797 >                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
798 >                        }
799 >                } else
800 >                        zout = zscan(y);
801 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
802 >                                < hresolu*sizeof(float))
803 >                        syserror(fname);
804 >        }
805 >        if (donorm)
806 >                free((char *)zout);
807 >        close(fd);
808 > }
809 >
810 >
811 > isfloat(s)                              /* see if string is floating number */
812 > register char   *s;
813 > {
814 >        for ( ; *s; s++)
815 >                if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
816 >                                && *s != 'e' && *s != 'E' && *s != '+')
817 >                        return(0);
818 >        return(1);
819 > }
820 >
821 >
822 > backfill(x, y)                          /* fill pixel with background */
823 > int     x, y;
824 > {
825 >        register BYTE   *dest = pscan(y)[x];
826 >
827 >        copycolr(dest, backcolr);
828 >        zscan(y)[x] = backz;
829 > }
830 >
831 >
832 > calstart(prog, args)                    /* start fill calculation */
833 > char    *prog, *args;
834 > {
835 >        char    combuf[512];
836 >        char    *argv[64];
837 >        int     rval;
838 >        register char   **wp, *cp;
839 >
840 >        if (childpid != -1) {
841 >                fprintf(stderr, "%s: too many calculations\n", progname);
842 >                exit(1);
843 >        }
844 >        strcpy(combuf, prog);
845 >        strcat(combuf, args);
846 >        cp = combuf;
847 >        wp = argv;
848 >        for ( ; ; ) {
849 >                while (isspace(*cp))    /* nullify spaces */
850 >                        *cp++ = '\0';
851 >                if (!*cp)               /* all done? */
852 >                        break;
853 >                *wp++ = cp;             /* add argument to list */
854 >                while (*++cp && !isspace(*cp))
855 >                        ;
856 >        }
857 >        *wp = NULL;
858 >                                                /* start process */
859 >        if ((rval = open_process(PDesc, argv)) < 0)
860 >                syserror(progname);
861 >        if (rval == 0) {
862 >                fprintf(stderr, "%s: command not found\n", argv[0]);
863 >                exit(1);
864 >        }
865 >        packsiz = rval/(6*sizeof(float)) - 1;
866 >        if (packsiz > PACKSIZ)
867 >                packsiz = PACKSIZ;
868 >        queuesiz = 0;
869 > }
870 >
871 >
872 > caldone()                               /* done with calculation */
873 > {
874 >        if (childpid == -1)
875 >                return;
876 >        clearqueue();
877 >        close_process(PDesc);
878 >        childpid = -1;
879 > }
880 >
881 >
882 > rcalfill(x, y)                          /* fill with ray-calculated pixel */
883 > int     x, y;
884 > {
885 >        if (queuesiz >= packsiz)        /* flush queue if needed */
886 >                clearqueue();
887 >                                        /* add position to queue */
888 >        queue[queuesiz][0] = x;
889 >        queue[queuesiz][1] = y;
890 >        queuesiz++;
891 > }
892 >
893 >
894 > clearqueue()                            /* process queue */
895 > {
896 >        FVECT   orig, dir;
897 >        float   fbuf[6*(PACKSIZ+1)];
898 >        register float  *fbp;
899 >        register int    i;
900 >
901 >        if (queuesiz == 0)
902 >                return;
903 >        fbp = fbuf;
904 >        for (i = 0; i < queuesiz; i++) {
905 >                viewray(orig, dir, &ourview,
906 >                                (queue[i][0]+.5)/hresolu,
907 >                                (queue[i][1]+.5)/vresolu);
908 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
909 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
910 >        }
911 >                                        /* mark end and get results */
912 >        bzero((char *)fbp, 6*sizeof(float));
913 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
914 >                        6*sizeof(float)*(queuesiz+1)) !=
915 >                        4*sizeof(float)*queuesiz) {
916 >                fprintf(stderr, "%s: error reading from rtrace process\n",
917 >                                progname);
918 >                exit(1);
919 >        }
920 >        fbp = fbuf;
921 >        for (i = 0; i < queuesiz; i++) {
922 >                if (ourexp > 0 && ourexp != 1.0) {
923 >                        fbp[0] *= ourexp;
924 >                        fbp[1] *= ourexp;
925 >                        fbp[2] *= ourexp;
926                  }
927 +                setcolr(pscan(queue[i][1])[queue[i][0]],
928 +                                fbp[0], fbp[1], fbp[2]);
929 +                zscan(queue[i][1])[queue[i][0]] = fbp[3];
930 +                fbp += 4;
931 +        }
932 +        queuesiz = 0;
933 + }
934 +
935 +
936 + syserror(s)                     /* report error and exit */
937 + char    *s;
938 + {
939 +        perror(s);
940 +        exit(1);
941   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines