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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines