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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines