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.13 by greg, Thu Jan 4 16:52:05 1990 UTC vs.
Revision 2.13 by greg, Sun Feb 27 10:16:57 1994 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines