ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 2.31
Committed: Tue Oct 27 09:08:26 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.30: +5 -3 lines
Log Message:
changed getheader() to listen to return value of passed function

File Contents

# User Rev Content
1 greg 2.24 /* Copyright (c) 1995 Regents of the University of California */
2 greg 1.34
3 greg 1.1 #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Interpolate and extrapolate pictures with different view parameters.
9     *
10     * Greg Ward 09Dec89
11     */
12    
13     #include "standard.h"
14    
15 greg 2.3 #include <ctype.h>
16    
17 greg 1.1 #include "view.h"
18    
19     #include "color.h"
20    
21 greg 1.34 #include "resolu.h"
22    
23 greg 2.21 #define LOG2 0.69314718055994530942
24    
25 greg 1.17 #define pscan(y) (ourpict+(y)*hresolu)
26 greg 2.19 #define sscan(y) (ourspict+(y)*hresolu)
27     #define wscan(y) (ourweigh+(y)*hresolu)
28 greg 1.17 #define zscan(y) (ourzbuf+(y)*hresolu)
29 greg 2.24 #define bscan(y) (ourbpict+(y)*hresolu)
30 greg 2.19 #define averaging (ourweigh != NULL)
31 greg 2.24 #define blurring (ourbpict != NULL)
32 greg 2.22 #define usematrix (hasmatrix & !averaging)
33     #define zisnorm (!usematrix | ourview.type != VT_PER)
34 greg 1.1
35 greg 2.20 #define MAXWT 1000. /* maximum pixel weight (averaging) */
36 greg 2.19
37 greg 1.13 #define F_FORE 1 /* fill foreground */
38     #define F_BACK 2 /* fill background */
39 greg 1.11
40 greg 2.3 #define PACKSIZ 256 /* max. calculation packet size */
41 greg 1.13
42 gregl 2.30 #define RTCOM "rtrace -h- -ovl -fff -ld- -i- -I- "
43 greg 1.13
44 greg 1.3 #define ABS(x) ((x)>0?(x):-(x))
45    
46 greg 1.15 struct position {int x,y; float z;};
47    
48 greg 2.16 #define NSTEPS 64 /* number steps in overlap prescan */
49     #define MINSTEP 4 /* minimum worthwhile preview step */
50    
51     struct bound {int min,max;};
52    
53 greg 1.17 VIEW ourview = STDVIEW; /* desired view */
54     int hresolu = 512; /* horizontal resolution */
55     int vresolu = 512; /* vertical resolution */
56 greg 1.18 double pixaspect = 1.0; /* pixel aspect ratio */
57 greg 1.1
58 greg 1.8 double zeps = .02; /* allowed z epsilon */
59 greg 1.1
60 greg 2.19 COLR *ourpict; /* output picture (COLR's) */
61 greg 2.20 COLOR *ourspict; /* output pixel sums (averaging) */
62     float *ourweigh = NULL; /* output pixel weights (averaging) */
63 greg 1.1 float *ourzbuf; /* corresponding z-buffer */
64 greg 2.25 COLOR *ourbpict = NULL; /* blurred picture (view averaging) */
65 greg 1.1
66 greg 2.24 VIEW avgview; /* average view for -B option */
67     int nvavg; /* number of views averaged */
68    
69 greg 1.1 char *progname;
70    
71 greg 1.25 int fillo = F_FORE|F_BACK; /* selected fill options */
72 greg 1.26 int fillsamp = 0; /* sample separation (0 == inf) */
73 greg 1.14 extern int backfill(), rcalfill(); /* fill functions */
74 greg 1.25 int (*fillfunc)() = backfill; /* selected fill function */
75 greg 1.11 COLR backcolr = BLKCOLR; /* background color */
76 greg 2.19 COLOR backcolor = BLKCOLOR; /* background color (float) */
77 greg 1.13 double backz = 0.0; /* background z value */
78 greg 2.22 int normdist = 1; /* i/o normalized distance? */
79 greg 2.27 char ourfmt[LPICFMT+1] = PICFMT; /* original picture format */
80 greg 2.21 double ourexp = -1; /* original picture exposure */
81     int expadj = 0; /* exposure adjustment (f-stops) */
82     double rexpadj = 1; /* real exposure adjustment */
83 greg 1.11
84 greg 2.24 VIEW theirview; /* input view */
85 greg 1.16 int gotview; /* got input view? */
86 greg 1.33 int wrongformat = 0; /* input in another format? */
87 greg 1.34 RESOLU tresolu; /* input resolution */
88 greg 1.17 double theirexp; /* input picture exposure */
89 greg 2.19 MAT4 theirs2ours; /* transformation matrix */
90 greg 1.30 int hasmatrix = 0; /* has transformation matrix */
91 greg 1.1
92 greg 2.3 int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */
93     #define childpid (PDesc[2])
94     unsigned short queue[PACKSIZ][2]; /* pending pixels */
95     int packsiz; /* actual packet size */
96 greg 2.26 int queuesiz = 0; /* number of pixels pending */
97 greg 1.5
98 greg 2.19 extern double movepixel();
99 greg 1.13
100 greg 2.19
101 greg 1.1 main(argc, argv) /* interpolate pictures */
102     int argc;
103     char *argv[];
104     {
105 greg 2.24 #define check(ol,al) if (argv[an][ol] || \
106     badarg(argc-an-1,argv+an+1,al)) \
107 greg 2.3 goto badopt
108 greg 1.1 int gotvfile = 0;
109 greg 2.19 int doavg = -1;
110 greg 2.24 int doblur = 0;
111 greg 1.11 char *zfile = NULL;
112 greg 2.21 char *expcomp = NULL;
113 greg 2.24 int i, an, rval;
114 greg 1.1
115     progname = argv[0];
116    
117 greg 2.24 for (an = 1; an < argc && argv[an][0] == '-'; an++) {
118     rval = getviewopt(&ourview, argc-an, argv+an);
119 greg 1.17 if (rval >= 0) {
120 greg 2.24 an += rval;
121 greg 1.17 continue;
122     }
123 greg 2.24 switch (argv[an][1]) {
124 greg 2.21 case 'e': /* exposure */
125     check(2,"f");
126 greg 2.24 expcomp = argv[++an];
127 greg 2.21 break;
128 greg 1.1 case 't': /* threshold */
129 greg 2.3 check(2,"f");
130 greg 2.24 zeps = atof(argv[++an]);
131 greg 1.1 break;
132 greg 2.19 case 'a': /* average */
133     check(2,NULL);
134     doavg = 1;
135     break;
136 greg 2.24 case 'B': /* blur views */
137     check(2,NULL);
138     doblur = 1;
139     break;
140 greg 2.19 case 'q': /* quick (no avg.) */
141     check(2,NULL);
142     doavg = 0;
143     break;
144 greg 1.11 case 'n': /* dist. normalized? */
145 greg 2.3 check(2,NULL);
146 greg 1.11 normdist = !normdist;
147 greg 1.10 break;
148 greg 1.11 case 'f': /* fill type */
149 greg 2.24 switch (argv[an][2]) {
150 greg 1.11 case '0': /* none */
151 greg 2.3 check(3,NULL);
152 greg 1.25 fillo = 0;
153 greg 1.11 break;
154     case 'f': /* foreground */
155 greg 2.3 check(3,NULL);
156 greg 1.25 fillo = F_FORE;
157 greg 1.11 break;
158     case 'b': /* background */
159 greg 2.3 check(3,NULL);
160 greg 1.25 fillo = F_BACK;
161 greg 1.11 break;
162     case 'a': /* all */
163 greg 2.3 check(3,NULL);
164 greg 1.25 fillo = F_FORE|F_BACK;
165 greg 1.11 break;
166 greg 1.25 case 's': /* sample */
167 greg 2.3 check(3,"i");
168 greg 2.24 fillsamp = atoi(argv[++an]);
169 greg 1.25 break;
170 greg 1.11 case 'c': /* color */
171 greg 2.3 check(3,"fff");
172 greg 1.25 fillfunc = backfill;
173 greg 2.24 setcolor(backcolor, atof(argv[an+1]),
174     atof(argv[an+2]), atof(argv[an+3]));
175 greg 2.19 setcolr(backcolr, colval(backcolor,RED),
176     colval(backcolor,GRN),
177     colval(backcolor,BLU));
178 greg 2.24 an += 3;
179 greg 1.11 break;
180 greg 1.13 case 'z': /* z value */
181 greg 2.3 check(3,"f");
182 greg 1.25 fillfunc = backfill;
183 greg 2.24 backz = atof(argv[++an]);
184 greg 1.13 break;
185     case 'r': /* rtrace */
186 greg 2.3 check(3,"s");
187 greg 1.25 fillfunc = rcalfill;
188 greg 2.24 calstart(RTCOM, argv[++an]);
189 greg 1.13 break;
190 greg 1.11 default:
191     goto badopt;
192     }
193     break;
194     case 'z': /* z file */
195 greg 2.3 check(2,"s");
196 greg 2.24 zfile = argv[++an];
197 greg 1.11 break;
198 greg 1.6 case 'x': /* x resolution */
199 greg 2.3 check(2,"i");
200 greg 2.24 hresolu = atoi(argv[++an]);
201 greg 1.6 break;
202     case 'y': /* y resolution */
203 greg 2.3 check(2,"i");
204 greg 2.24 vresolu = atoi(argv[++an]);
205 greg 1.6 break;
206 greg 1.18 case 'p': /* pixel aspect */
207 greg 2.24 if (argv[an][2] != 'a')
208 greg 2.6 goto badopt;
209     check(3,"f");
210 greg 2.24 pixaspect = atof(argv[++an]);
211 greg 1.18 break;
212 greg 1.17 case 'v': /* view file */
213 greg 2.24 if (argv[an][2] != 'f')
214 greg 1.1 goto badopt;
215 greg 2.3 check(3,"s");
216 greg 2.24 gotvfile = viewfile(argv[++an], &ourview, 0, 0);
217 greg 2.10 if (gotvfile < 0)
218 greg 2.24 syserror(argv[an]);
219 greg 2.10 else if (gotvfile == 0) {
220 greg 1.17 fprintf(stderr, "%s: bad view file\n",
221 greg 2.24 argv[an]);
222 greg 1.17 exit(1);
223 greg 1.1 }
224     break;
225     default:
226     badopt:
227 greg 1.10 fprintf(stderr, "%s: command line error at '%s'\n",
228 greg 2.24 progname, argv[an]);
229 greg 1.10 goto userr;
230 greg 1.1 }
231 greg 1.17 }
232 greg 1.1 /* check arguments */
233 greg 2.24 if ((argc-an)%2)
234 greg 1.10 goto userr;
235 greg 2.3 if (fillsamp == 1)
236     fillo &= ~F_BACK;
237 greg 2.20 if (doavg < 0)
238 greg 2.24 doavg = (argc-an) > 2;
239 greg 2.21 if (expcomp != NULL)
240     if (expcomp[0] == '+' | expcomp[0] == '-') {
241     expadj = atof(expcomp) + (expcomp[0]=='+' ? .5 : -.5);
242 greg 2.24 if (doavg | doblur)
243 greg 2.21 rexpadj = pow(2.0, atof(expcomp));
244     else
245     rexpadj = pow(2.0, (double)expadj);
246     } else {
247     if (!isflt(expcomp))
248     goto userr;
249     rexpadj = atof(expcomp);
250     expadj = log(rexpadj)/LOG2 + (rexpadj>1 ? .5 : -.5);
251 greg 2.24 if (!(doavg | doblur))
252 greg 2.21 rexpadj = pow(2.0, (double)expadj);
253     }
254 greg 1.1 /* set view */
255 greg 2.28 if (nextview(doblur ? stdin : (FILE *)NULL) == EOF) {
256 greg 2.24 fprintf(stderr, "%s: no view on standard input!\n",
257     progname);
258 greg 1.1 exit(1);
259     }
260 greg 1.18 normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
261 greg 1.1 /* allocate frame */
262 greg 2.19 if (doavg) {
263     ourspict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR));
264     ourweigh = (float *)bmalloc(hresolu*vresolu*sizeof(float));
265     if (ourspict == NULL | ourweigh == NULL)
266     syserror(progname);
267     } else {
268     ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
269     if (ourpict == NULL)
270     syserror(progname);
271     }
272 greg 2.24 if (doblur) {
273     ourbpict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR));
274     if (ourbpict == NULL)
275     syserror(progname);
276     }
277 greg 2.7 ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
278 greg 2.19 if (ourzbuf == NULL)
279 greg 2.10 syserror(progname);
280 greg 2.13 /* new header */
281     newheader("RADIANCE", stdout);
282 greg 2.24 /* run pictures */
283     do {
284     bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
285     for (i = an; i < argc; i += 2)
286     addpicture(argv[i], argv[i+1]);
287     if (fillo&F_BACK) /* fill in spaces */
288     backpicture(fillfunc, fillsamp);
289     else
290     fillpicture(fillfunc);
291     /* aft clipping */
292     clipaft();
293     } while (addblur() && nextview(stdin) != EOF);
294 greg 1.13 /* close calculation */
295 greg 1.14 caldone();
296 greg 1.1 /* add to header */
297     printargs(argc, argv, stdout);
298 greg 2.24 compavgview();
299     if (doblur | gotvfile) {
300 greg 1.25 fputs(VIEWSTR, stdout);
301 greg 2.24 fprintview(&avgview, stdout);
302 greg 1.25 putc('\n', stdout);
303 greg 1.1 }
304 greg 2.21 if (pixaspect < .99 | pixaspect > 1.01)
305 greg 1.18 fputaspect(pixaspect, stdout);
306 greg 2.21 if (ourexp > 0)
307     ourexp *= rexpadj;
308     else
309     ourexp = rexpadj;
310     if (ourexp < .995 | ourexp > 1.005)
311 greg 1.16 fputexpos(ourexp, stdout);
312 greg 2.27 if (strcmp(ourfmt, PICFMT)) /* print format if known */
313     fputformat(ourfmt, stdout);
314 greg 1.25 putc('\n', stdout);
315 greg 1.11 /* write picture */
316 greg 1.1 writepicture();
317 greg 1.11 /* write z file */
318     if (zfile != NULL)
319     writedistance(zfile);
320 greg 1.1
321     exit(0);
322 greg 1.10 userr:
323     fprintf(stderr,
324 greg 2.24 "Usage: %s [view opts][-t eps][-z zout][-e spec][-B][-a|-q][-fT][-n] pfile zspec ..\n",
325 greg 1.10 progname);
326     exit(1);
327 greg 1.1 #undef check
328     }
329    
330    
331 gwlarson 2.31 int
332 greg 1.1 headline(s) /* process header string */
333     char *s;
334     {
335 greg 1.33 char fmt[32];
336 greg 1.1
337 greg 2.13 if (isheadid(s))
338 gwlarson 2.31 return(0);
339 greg 2.13 if (formatval(fmt, s)) {
340 greg 2.27 if (globmatch(ourfmt, fmt)) {
341     wrongformat = 0;
342     strcpy(ourfmt, fmt);
343     } else
344     wrongformat = 1;
345 gwlarson 2.31 return(0);
346 greg 1.33 }
347 greg 2.24 if (nvavg < 2) {
348     putc('\t', stdout);
349     fputs(s, stdout);
350     }
351 greg 1.16 if (isexpos(s)) {
352     theirexp *= exposval(s);
353 gwlarson 2.31 return(0);
354 greg 1.16 }
355 greg 2.5 if (isview(s) && sscanview(&theirview, s) > 0)
356     gotview++;
357 gwlarson 2.31 return(0);
358 greg 1.1 }
359    
360    
361 greg 2.24 nextview(fp) /* get and set next view */
362     FILE *fp;
363     {
364     char linebuf[256];
365     char *err;
366     register int i;
367    
368     if (fp != NULL) {
369     do /* get new view */
370     if (fgets(linebuf, sizeof(linebuf), fp) == NULL)
371     return(EOF);
372     while (!isview(linebuf) || !sscanview(&ourview, linebuf));
373     }
374     /* set new view */
375     if ((err = setview(&ourview)) != NULL) {
376     fprintf(stderr, "%s: %s\n", progname, err);
377     exit(1);
378     }
379     if (!nvavg) { /* first view */
380     copystruct(&avgview, &ourview);
381     return(nvavg++);
382     }
383     /* add to average view */
384     for (i = 0; i < 3; i++) {
385     avgview.vp[i] += ourview.vp[i];
386     avgview.vdir[i] += ourview.vdir[i];
387     avgview.vup[i] += ourview.vup[i];
388     }
389     avgview.horiz += ourview.horiz;
390     avgview.vert += ourview.vert;
391     avgview.hoff += ourview.hoff;
392     avgview.voff += ourview.voff;
393     avgview.vfore += ourview.vfore;
394     avgview.vaft += ourview.vaft;
395     return(nvavg++);
396     }
397    
398    
399     compavgview() /* compute average view */
400     {
401     register int i;
402     double f;
403    
404     if (nvavg < 2)
405     return;
406     f = 1.0/nvavg;
407     for (i = 0; i < 3; i++) {
408     avgview.vp[i] *= f;
409     avgview.vdir[i] *= f;
410     avgview.vup[i] *= f;
411     }
412     avgview.horiz *= f;
413     avgview.vert *= f;
414     avgview.hoff *= f;
415     avgview.voff *= f;
416     avgview.vfore *= f;
417     avgview.vaft *= f;
418     if (setview(&avgview) != NULL) /* in case of emergency... */
419     copystruct(&avgview, &ourview);
420     pixaspect = viewaspect(&avgview) * hresolu / vresolu;
421     }
422    
423    
424 greg 1.10 addpicture(pfile, zspec) /* add picture to output */
425     char *pfile, *zspec;
426 greg 1.1 {
427 greg 1.19 FILE *pfp;
428     int zfd;
429 greg 1.8 char *err;
430 greg 1.1 COLR *scanin;
431 greg 1.15 float *zin;
432     struct position *plast;
433 greg 2.16 struct bound *xlim, ylim;
434 greg 1.1 int y;
435 greg 1.10 /* open picture file */
436 greg 2.10 if ((pfp = fopen(pfile, "r")) == NULL)
437     syserror(pfile);
438 greg 1.16 /* get header with exposure and view */
439     theirexp = 1.0;
440 greg 2.24 copystruct(&theirview, &stdview);
441 greg 1.16 gotview = 0;
442 greg 2.24 if (nvavg < 2)
443     printf("%s:\n", pfile);
444 greg 1.33 getheader(pfp, headline, NULL);
445 greg 1.34 if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
446 greg 1.33 fprintf(stderr, "%s: picture format error\n", pfile);
447 greg 1.1 exit(1);
448     }
449 greg 1.16 if (ourexp <= 0)
450     ourexp = theirexp;
451     else if (ABS(theirexp-ourexp) > .01*ourexp)
452     fprintf(stderr, "%s: different exposure (warning)\n", pfile);
453 greg 1.1 if (err = setview(&theirview)) {
454     fprintf(stderr, "%s: %s\n", pfile, err);
455     exit(1);
456     }
457 greg 1.5 /* compute transformation */
458 greg 1.30 hasmatrix = pixform(theirs2ours, &theirview, &ourview);
459 greg 2.16 /* get z specification or file */
460 greg 1.34 zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
461 greg 2.16 if (zin == NULL)
462 greg 2.10 syserror(progname);
463 greg 1.19 if ((zfd = open(zspec, O_RDONLY)) == -1) {
464 greg 1.10 double zvalue;
465     register int x;
466 greg 2.19 if (!isflt(zspec) || (zvalue = atof(zspec)) <= 0.0)
467 greg 2.10 syserror(zspec);
468 greg 1.34 for (x = scanlen(&tresolu); x-- > 0; )
469 greg 1.10 zin[x] = zvalue;
470     }
471 greg 2.16 /* compute transferrable perimeter */
472     xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound));
473     if (xlim == NULL)
474     syserror(progname);
475     if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */
476     free((char *)zin);
477     free((char *)xlim);
478     if (zfd != -1)
479     close(zfd);
480     fclose(pfp);
481     return;
482     }
483     /* allocate scanlines */
484     scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
485     plast = (struct position *)calloc(scanlen(&tresolu),
486     sizeof(struct position));
487     if (scanin == NULL | plast == NULL)
488     syserror(progname);
489     /* skip to starting point */
490     for (y = 0; y < ylim.min; y++)
491     if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
492     fprintf(stderr, "%s: read error\n", pfile);
493     exit(1);
494     }
495     if (zfd != -1 && lseek(zfd,
496     (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0)
497     syserror(zspec);
498 greg 1.1 /* load image */
499 greg 2.16 for (y = ylim.min; y <= ylim.max; y++) {
500 greg 1.34 if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
501 greg 1.1 fprintf(stderr, "%s: read error\n", pfile);
502     exit(1);
503     }
504 greg 2.16 if (zfd != -1 && read(zfd, (char *)zin,
505 greg 1.34 scanlen(&tresolu)*sizeof(float))
506 greg 2.16 < scanlen(&tresolu)*sizeof(float))
507     syserror(zspec);
508     addscanline(xlim+y, y, scanin, zin, plast);
509 greg 1.1 }
510     /* clean up */
511 greg 2.16 free((char *)xlim);
512 greg 1.1 free((char *)scanin);
513     free((char *)zin);
514 greg 1.9 free((char *)plast);
515 greg 1.1 fclose(pfp);
516 greg 1.19 if (zfd != -1)
517     close(zfd);
518 greg 1.1 }
519    
520    
521 greg 1.5 pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */
522 greg 2.19 register MAT4 xfmat;
523 greg 1.5 register VIEW *vw1, *vw2;
524     {
525     double m4t[4][4];
526    
527 greg 2.25 if (vw1->type != VT_PER & vw1->type != VT_PAR)
528 greg 1.30 return(0);
529 greg 2.25 if (vw2->type != VT_PER & vw2->type != VT_PAR)
530 greg 1.30 return(0);
531 greg 1.5 setident4(xfmat);
532 greg 1.17 xfmat[0][0] = vw1->hvec[0];
533     xfmat[0][1] = vw1->hvec[1];
534     xfmat[0][2] = vw1->hvec[2];
535     xfmat[1][0] = vw1->vvec[0];
536     xfmat[1][1] = vw1->vvec[1];
537     xfmat[1][2] = vw1->vvec[2];
538 greg 1.5 xfmat[2][0] = vw1->vdir[0];
539     xfmat[2][1] = vw1->vdir[1];
540     xfmat[2][2] = vw1->vdir[2];
541     xfmat[3][0] = vw1->vp[0];
542     xfmat[3][1] = vw1->vp[1];
543     xfmat[3][2] = vw1->vp[2];
544     setident4(m4t);
545 greg 1.17 m4t[0][0] = vw2->hvec[0]/vw2->hn2;
546     m4t[1][0] = vw2->hvec[1]/vw2->hn2;
547     m4t[2][0] = vw2->hvec[2]/vw2->hn2;
548     m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
549     m4t[0][1] = vw2->vvec[0]/vw2->vn2;
550     m4t[1][1] = vw2->vvec[1]/vw2->vn2;
551     m4t[2][1] = vw2->vvec[2]/vw2->vn2;
552     m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
553 greg 1.5 m4t[0][2] = vw2->vdir[0];
554     m4t[1][2] = vw2->vdir[1];
555     m4t[2][2] = vw2->vdir[2];
556     m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
557     multmat4(xfmat, xfmat, m4t);
558 greg 1.30 return(1);
559 greg 1.5 }
560    
561    
562 greg 2.16 addscanline(xl, y, pline, zline, lasty) /* add scanline to output */
563     struct bound *xl;
564 greg 1.1 int y;
565     COLR *pline;
566     float *zline;
567 greg 1.15 struct position *lasty; /* input/output */
568 greg 1.1 {
569 greg 1.30 FVECT pos;
570 greg 1.15 struct position lastx, newpos;
571 greg 2.19 double wt;
572 greg 1.1 register int x;
573    
574 greg 1.20 lastx.z = 0;
575 greg 2.16 for (x = xl->max; x >= xl->min; x--) {
576 greg 1.34 pix2loc(pos, &tresolu, x, y);
577 greg 1.5 pos[2] = zline[x];
578 greg 2.19 if ((wt = movepixel(pos)) <= FTINY) {
579 greg 1.15 lasty[x].z = lastx.z = 0; /* mark invalid */
580 greg 1.1 continue;
581 greg 1.15 }
582 greg 2.19 /* add pixel to our image */
583 greg 1.17 newpos.x = pos[0] * hresolu;
584     newpos.y = pos[1] * vresolu;
585 greg 1.15 newpos.z = zline[x];
586 greg 2.19 addpixel(&newpos, &lastx, &lasty[x], pline[x], wt, pos[2]);
587     lasty[x].x = lastx.x = newpos.x;
588     lasty[x].y = lastx.y = newpos.y;
589     lasty[x].z = lastx.z = newpos.z;
590 greg 1.1 }
591     }
592    
593    
594 greg 2.19 addpixel(p0, p1, p2, pix, w, z) /* fill in pixel parallelogram */
595 greg 1.15 struct position *p0, *p1, *p2;
596 greg 1.8 COLR pix;
597 greg 2.19 double w;
598 greg 1.8 double z;
599     {
600 greg 1.15 double zt = 2.*zeps*p0->z; /* threshold */
601 greg 2.19 COLOR pval; /* converted+weighted pixel */
602 greg 1.15 int s1x, s1y, s2x, s2y; /* step sizes */
603     int l1, l2, c1, c2; /* side lengths and counters */
604     int p1isy; /* p0p1 along y? */
605     int x1, y1; /* p1 position */
606     register int x, y; /* final position */
607    
608     /* compute vector p0p1 */
609 greg 1.25 if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
610 greg 1.15 s1x = p1->x - p0->x;
611     s1y = p1->y - p0->y;
612     l1 = ABS(s1x);
613     if (p1isy = (ABS(s1y) > l1))
614     l1 = ABS(s1y);
615 greg 2.20 else if (l1 < 1)
616 greg 2.19 l1 = 1;
617 greg 1.15 } else {
618     l1 = s1x = s1y = 1;
619     p1isy = -1;
620     }
621     /* compute vector p0p2 */
622 greg 1.25 if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
623 greg 1.15 s2x = p2->x - p0->x;
624     s2y = p2->y - p0->y;
625     if (p1isy == 1)
626     l2 = ABS(s2x);
627     else {
628     l2 = ABS(s2y);
629     if (p1isy != 0 && ABS(s2x) > l2)
630     l2 = ABS(s2x);
631     }
632 greg 2.19 if (l2 < 1)
633     l2 = 1;
634 greg 1.15 } else
635     l2 = s2x = s2y = 1;
636     /* fill the parallelogram */
637 greg 2.19 if (averaging) {
638     colr_color(pval, pix);
639     scalecolor(pval, w);
640     }
641 greg 1.15 for (c1 = l1; c1-- > 0; ) {
642     x1 = p0->x + c1*s1x/l1;
643     y1 = p0->y + c1*s1y/l1;
644     for (c2 = l2; c2-- > 0; ) {
645     x = x1 + c2*s2x/l2;
646 greg 2.25 if (x < 0 | x >= hresolu)
647 greg 1.28 continue;
648 greg 1.15 y = y1 + c2*s2y/l2;
649 greg 2.25 if (y < 0 | y >= vresolu)
650 greg 1.28 continue;
651 greg 2.19 if (averaging) {
652     if (zscan(y)[x] <= 0 || zscan(y)[x]-z
653 greg 1.15 > zeps*zscan(y)[x]) {
654 greg 2.19 copycolor(sscan(y)[x], pval);
655     wscan(y)[x] = w;
656     zscan(y)[x] = z;
657     } else if (z-zscan(y)[x] <= zeps*zscan(y)[x]) {
658     addcolor(sscan(y)[x], pval);
659     wscan(y)[x] += w;
660     }
661     } else if (zscan(y)[x] <= 0 || zscan(y)[x]-z
662     > zeps*zscan(y)[x]) {
663     copycolr(pscan(y)[x], pix);
664 greg 1.8 zscan(y)[x] = z;
665     }
666 greg 1.15 }
667     }
668 greg 2.16 }
669    
670    
671 greg 2.19 double
672 greg 2.18 movepixel(pos) /* reposition image point */
673 greg 2.20 register FVECT pos;
674 greg 2.18 {
675 greg 2.19 FVECT pt, tdir, odir;
676 greg 2.20 double d;
677 greg 2.19 register int i;
678 greg 2.18
679     if (pos[2] <= 0) /* empty pixel */
680 greg 2.19 return(0);
681 greg 2.22 if (usematrix) {
682 greg 2.18 pos[0] += theirview.hoff - .5;
683     pos[1] += theirview.voff - .5;
684 greg 2.25 if (normdist & theirview.type == VT_PER)
685 greg 2.23 d = sqrt(1. + pos[0]*pos[0]*theirview.hn2
686     + pos[1]*pos[1]*theirview.vn2);
687     else
688     d = 1.;
689     pos[2] += d*theirview.vfore;
690 greg 2.18 if (theirview.type == VT_PER) {
691 greg 2.23 pos[2] /= d;
692 greg 2.18 pos[0] *= pos[2];
693     pos[1] *= pos[2];
694     }
695     multp3(pos, pos, theirs2ours);
696 greg 2.23 if (pos[2] <= ourview.vfore)
697 greg 2.19 return(0);
698 greg 2.18 if (ourview.type == VT_PER) {
699     pos[0] /= pos[2];
700     pos[1] /= pos[2];
701     }
702     pos[0] += .5 - ourview.hoff;
703     pos[1] += .5 - ourview.voff;
704 greg 2.23 pos[2] -= ourview.vfore;
705 greg 2.19 } else {
706     if (viewray(pt, tdir, &theirview, pos[0], pos[1]) < -FTINY)
707     return(0);
708 greg 2.22 if (!normdist & theirview.type == VT_PER) /* adjust */
709 greg 2.20 pos[2] *= sqrt(1. + pos[0]*pos[0]*theirview.hn2
710     + pos[1]*pos[1]*theirview.vn2);
711 greg 2.19 pt[0] += tdir[0]*pos[2];
712     pt[1] += tdir[1]*pos[2];
713     pt[2] += tdir[2]*pos[2];
714     viewloc(pos, &ourview, pt);
715     if (pos[2] <= 0)
716     return(0);
717     }
718 greg 2.25 if (pos[0] < 0 | pos[0] >= 1-FTINY | pos[1] < 0 | pos[1] >= 1-FTINY)
719 greg 2.18 return(0);
720 greg 2.19 if (!averaging)
721     return(1);
722     if (ourview.type == VT_PAR) /* compute our direction */
723     VCOPY(odir, ourview.vdir);
724     else
725     for (i = 0; i < 3; i++)
726     odir[i] = (pt[i] - ourview.vp[i])/pos[2];
727 greg 2.20 d = DOT(odir,tdir); /* compute pixel weight */
728     if (d >= 1.-1./MAXWT/MAXWT)
729 greg 2.19 return(MAXWT); /* clip to maximum weight */
730 greg 2.20 return(1./sqrt(1.-d));
731 greg 2.18 }
732    
733    
734 greg 2.16 getperim(xl, yl, zline, zfd) /* compute overlapping image area */
735     register struct bound *xl;
736     struct bound *yl;
737     float *zline;
738     int zfd;
739     {
740     int step;
741     FVECT pos;
742     register int x, y;
743     /* set up step size */
744     if (scanlen(&tresolu) < numscans(&tresolu))
745     step = scanlen(&tresolu)/NSTEPS;
746     else
747     step = numscans(&tresolu)/NSTEPS;
748     if (step < MINSTEP) { /* not worth cropping? */
749     yl->min = 0;
750     yl->max = numscans(&tresolu) - 1;
751     x = scanlen(&tresolu) - 1;
752     for (y = numscans(&tresolu); y--; ) {
753     xl[y].min = 0;
754     xl[y].max = x;
755     }
756     return(1);
757     }
758     yl->min = 32000; yl->max = 0; /* search for points on image */
759     for (y = step - 1; y < numscans(&tresolu); y += step) {
760     if (zfd != -1) {
761     if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float),
762     0) < 0)
763     syserror("lseek");
764     if (read(zfd, (char *)zline,
765     scanlen(&tresolu)*sizeof(float))
766     < scanlen(&tresolu)*sizeof(float))
767     syserror("read");
768     }
769     xl[y].min = 32000; xl[y].max = 0; /* x max */
770     for (x = scanlen(&tresolu); (x -= step) > 0; ) {
771     pix2loc(pos, &tresolu, x, y);
772     pos[2] = zline[x];
773 greg 2.19 if (movepixel(pos) > FTINY) {
774 greg 2.16 xl[y].max = x + step - 1;
775     xl[y].min = x - step + 1; /* x min */
776     if (xl[y].min < 0)
777     xl[y].min = 0;
778     for (x = step - 1; x < xl[y].max; x += step) {
779     pix2loc(pos, &tresolu, x, y);
780     pos[2] = zline[x];
781 greg 2.19 if (movepixel(pos) > FTINY) {
782 greg 2.16 xl[y].min = x - step + 1;
783     break;
784     }
785     }
786     if (y < yl->min) /* y limits */
787     yl->min = y - step + 1;
788     yl->max = y + step - 1;
789     break;
790     }
791     }
792     /* fill in between */
793 greg 2.18 if (y < step) {
794 greg 2.16 xl[y-1].min = xl[y].min;
795     xl[y-1].max = xl[y].max;
796 greg 2.18 } else {
797     if (xl[y].min < xl[y-step].min)
798     xl[y-1].min = xl[y].min;
799     else
800     xl[y-1].min = xl[y-step].min;
801     if (xl[y].max > xl[y-step].max)
802     xl[y-1].max = xl[y].max;
803     else
804     xl[y-1].max = xl[y-step].max;
805     }
806 greg 2.16 for (x = 2; x < step; x++)
807     copystruct(xl+y-x, xl+y-1);
808     }
809     if (yl->max >= numscans(&tresolu))
810     yl->max = numscans(&tresolu) - 1;
811 greg 2.20 y -= step;
812 greg 2.16 for (x = numscans(&tresolu) - 1; x > y; x--) /* fill bottom rows */
813     copystruct(xl+x, xl+y);
814     return(yl->max >= yl->min);
815 greg 1.8 }
816    
817    
818 greg 1.26 backpicture(fill, samp) /* background fill algorithm */
819 greg 1.25 int (*fill)();
820 greg 1.26 int samp;
821 greg 1.1 {
822 greg 1.2 int *yback, xback;
823 greg 1.1 int y;
824 greg 1.2 register int x, i;
825     /* get back buffer */
826 greg 1.17 yback = (int *)malloc(hresolu*sizeof(int));
827 greg 1.15 if (yback == NULL)
828 greg 2.10 syserror(progname);
829 greg 1.17 for (x = 0; x < hresolu; x++)
830 greg 1.4 yback[x] = -2;
831 greg 1.7 /*
832     * Xback and yback are the pixel locations of suitable
833     * background values in each direction.
834     * A value of -2 means unassigned, and -1 means
835     * that there is no suitable background in this direction.
836     */
837 greg 1.2 /* fill image */
838 greg 1.17 for (y = 0; y < vresolu; y++) {
839 greg 1.4 xback = -2;
840 greg 1.17 for (x = 0; x < hresolu; x++)
841 greg 1.8 if (zscan(y)[x] <= 0) { /* empty pixel */
842 greg 1.7 /*
843     * First, find background from above or below.
844     * (farthest assigned pixel)
845     */
846 greg 1.4 if (yback[x] == -2) {
847 greg 1.17 for (i = y+1; i < vresolu; i++)
848 greg 1.8 if (zscan(i)[x] > 0)
849 greg 1.4 break;
850 greg 1.17 if (i < vresolu
851 greg 1.2 && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
852 greg 1.4 yback[x] = i;
853     else
854     yback[x] = y-1;
855 greg 1.2 }
856 greg 1.7 /*
857     * Next, find background from left or right.
858     */
859 greg 1.4 if (xback == -2) {
860 greg 1.17 for (i = x+1; i < hresolu; i++)
861 greg 1.8 if (zscan(y)[i] > 0)
862 greg 1.4 break;
863 greg 1.17 if (i < hresolu
864 greg 1.4 && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
865     xback = i;
866     else
867     xback = x-1;
868     }
869 greg 1.7 /*
870 greg 1.26 * If we have no background for this pixel,
871     * use the given fill function.
872 greg 1.11 */
873 greg 1.27 if (xback < 0 && yback[x] < 0)
874     goto fillit;
875 greg 1.11 /*
876 greg 1.7 * Compare, and use the background that is
877     * farther, unless one of them is next to us.
878 greg 1.27 * If the background is too distant, call
879     * the fill function.
880 greg 1.7 */
881 greg 1.11 if ( yback[x] < 0
882     || (xback >= 0 && ABS(x-xback) <= 1)
883 greg 1.4 || ( ABS(y-yback[x]) > 1
884 greg 1.11 && zscan(yback[x])[x]
885 greg 1.13 < zscan(y)[xback] ) ) {
886 greg 1.27 if (samp > 0 && ABS(x-xback) >= samp)
887     goto fillit;
888 greg 2.19 if (averaging) {
889     copycolor(sscan(y)[x],
890     sscan(y)[xback]);
891     wscan(y)[x] = wscan(y)[xback];
892     } else
893     copycolr(pscan(y)[x],
894     pscan(y)[xback]);
895 greg 1.13 zscan(y)[x] = zscan(y)[xback];
896     } else {
897 greg 1.27 if (samp > 0 && ABS(y-yback[x]) > samp)
898     goto fillit;
899 greg 2.19 if (averaging) {
900     copycolor(sscan(y)[x],
901     sscan(yback[x])[x]);
902     wscan(y)[x] =
903     wscan(yback[x])[x];
904     } else
905     copycolr(pscan(y)[x],
906     pscan(yback[x])[x]);
907 greg 1.13 zscan(y)[x] = zscan(yback[x])[x];
908 greg 1.27 }
909     continue;
910     fillit:
911     (*fill)(x,y);
912     if (fill == rcalfill) { /* use it */
913     clearqueue();
914     xback = x;
915     yback[x] = y;
916 greg 1.13 }
917 greg 1.4 } else { /* full pixel */
918     yback[x] = -2;
919     xback = -2;
920     }
921     }
922 greg 1.2 free((char *)yback);
923 greg 1.1 }
924    
925    
926 greg 1.25 fillpicture(fill) /* paint in empty pixels using fill */
927     int (*fill)();
928 greg 1.11 {
929     register int x, y;
930    
931 greg 1.17 for (y = 0; y < vresolu; y++)
932     for (x = 0; x < hresolu; x++)
933 greg 1.11 if (zscan(y)[x] <= 0)
934 greg 1.25 (*fill)(x,y);
935 greg 2.26 if (fill == rcalfill)
936     clearqueue();
937 greg 2.15 }
938    
939    
940     clipaft() /* perform aft clipping as indicated */
941     {
942     register int x, y;
943 greg 2.22 int adjtest = ourview.type == VT_PER & zisnorm;
944 greg 2.15 double tstdist;
945     double yzn2, vx;
946    
947     if (ourview.vaft <= FTINY)
948 greg 2.24 return(0);
949 greg 2.17 tstdist = ourview.vaft - ourview.vfore;
950 greg 2.15 for (y = 0; y < vresolu; y++) {
951 greg 2.22 if (adjtest) { /* adjust test */
952 greg 2.15 yzn2 = (y+.5)/vresolu + ourview.voff - .5;
953     yzn2 = 1. + yzn2*yzn2*ourview.vn2;
954 greg 2.17 tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2);
955 greg 2.15 }
956     for (x = 0; x < hresolu; x++)
957     if (zscan(y)[x] > tstdist) {
958 greg 2.22 if (adjtest) {
959 greg 2.15 vx = (x+.5)/hresolu + ourview.hoff - .5;
960 greg 2.17 if (zscan(y)[x] <= (ourview.vaft -
961     ourview.vfore) *
962 greg 2.15 sqrt(vx*vx*ourview.hn2 + yzn2))
963     continue;
964     }
965 greg 2.19 if (averaging)
966     bzero(sscan(y)[x], sizeof(COLOR));
967     else
968     bzero(pscan(y)[x], sizeof(COLR));
969 greg 2.15 zscan(y)[x] = 0.0;
970     }
971     }
972 greg 2.24 return(1);
973 greg 1.11 }
974    
975    
976 greg 2.24 addblur() /* add to blurred picture */
977     {
978     COLOR cval;
979     double d;
980     register int i;
981    
982     if (!blurring)
983     return(0);
984     i = hresolu*vresolu;
985     if (nvavg < 2)
986     if (averaging)
987     while (i--) {
988     copycolor(ourbpict[i], ourspict[i]);
989     d = 1.0/ourweigh[i];
990     scalecolor(ourbpict[i], d);
991     }
992     else
993     while (i--)
994     colr_color(ourbpict[i], ourpict[i]);
995     else
996     if (averaging)
997     while (i--) {
998     copycolor(cval, ourspict[i]);
999     d = 1.0/ourweigh[i];
1000     scalecolor(cval, d);
1001     addcolor(ourbpict[i], cval);
1002     }
1003     else
1004     while (i--) {
1005     colr_color(cval, ourpict[i]);
1006     addcolor(ourbpict[i], cval);
1007     }
1008     /* print view */
1009     printf("VIEW%d:", nvavg);
1010     fprintview(&ourview, stdout);
1011     putchar('\n');
1012     return(1);
1013     }
1014    
1015    
1016 greg 2.21 writepicture() /* write out picture (alters buffer) */
1017 greg 1.1 {
1018     int y;
1019 greg 2.19 register int x;
1020     double d;
1021 greg 1.1
1022 greg 1.34 fprtresolu(hresolu, vresolu, stdout);
1023 greg 1.17 for (y = vresolu-1; y >= 0; y--)
1024 greg 2.24 if (blurring) {
1025     for (x = 0; x < hresolu; x++) { /* compute avg. */
1026     d = rexpadj/nvavg;
1027     scalecolor(bscan(y)[x], d);
1028     }
1029     if (fwritescan(bscan(y), hresolu, stdout) < 0)
1030     syserror(progname);
1031     } else if (averaging) {
1032 greg 2.19 for (x = 0; x < hresolu; x++) { /* average pixels */
1033 greg 2.21 d = rexpadj/wscan(y)[x];
1034 greg 2.19 scalecolor(sscan(y)[x], d);
1035     }
1036     if (fwritescan(sscan(y), hresolu, stdout) < 0)
1037     syserror(progname);
1038 greg 2.21 } else {
1039     if (expadj)
1040     shiftcolrs(pscan(y), hresolu, expadj);
1041     if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
1042     syserror(progname);
1043     }
1044 greg 1.11 }
1045    
1046    
1047 greg 2.22 writedistance(fname) /* write out z file (alters buffer) */
1048 greg 1.11 char *fname;
1049     {
1050 greg 2.22 int donorm = normdist & !zisnorm ? 1 :
1051     ourview.type == VT_PER & !normdist & zisnorm ? -1 : 0;
1052 greg 1.19 int fd;
1053 greg 1.11 int y;
1054    
1055 greg 2.10 if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
1056     syserror(fname);
1057 greg 1.17 for (y = vresolu-1; y >= 0; y--) {
1058 greg 1.11 if (donorm) {
1059 greg 2.22 double vx, yzn2, d;
1060 greg 1.11 register int x;
1061 greg 1.29 yzn2 = (y+.5)/vresolu + ourview.voff - .5;
1062 greg 1.17 yzn2 = 1. + yzn2*yzn2*ourview.vn2;
1063     for (x = 0; x < hresolu; x++) {
1064 greg 1.29 vx = (x+.5)/hresolu + ourview.hoff - .5;
1065 greg 2.22 d = sqrt(vx*vx*ourview.hn2 + yzn2);
1066     if (donorm > 0)
1067     zscan(y)[x] *= d;
1068     else
1069     zscan(y)[x] /= d;
1070 greg 1.11 }
1071 greg 2.22 }
1072     if (write(fd, (char *)zscan(y), hresolu*sizeof(float))
1073 greg 2.10 < hresolu*sizeof(float))
1074     syserror(fname);
1075 greg 1.11 }
1076 greg 1.19 close(fd);
1077 greg 1.10 }
1078    
1079    
1080 greg 1.12 backfill(x, y) /* fill pixel with background */
1081     int x, y;
1082     {
1083 greg 2.19 if (averaging) {
1084     copycolor(sscan(y)[x], backcolor);
1085     wscan(y)[x] = 1;
1086     } else
1087     copycolr(pscan(y)[x], backcolr);
1088 greg 1.13 zscan(y)[x] = backz;
1089     }
1090    
1091    
1092 greg 2.3 calstart(prog, args) /* start fill calculation */
1093 greg 1.13 char *prog, *args;
1094     {
1095     char combuf[512];
1096 greg 2.3 char *argv[64];
1097     int rval;
1098     register char **wp, *cp;
1099 greg 1.13
1100 greg 1.14 if (childpid != -1) {
1101     fprintf(stderr, "%s: too many calculations\n", progname);
1102     exit(1);
1103     }
1104 greg 2.3 strcpy(combuf, prog);
1105     strcat(combuf, args);
1106     cp = combuf;
1107     wp = argv;
1108     for ( ; ; ) {
1109 greg 2.11 while (isspace(*cp)) /* nullify spaces */
1110     *cp++ = '\0';
1111     if (!*cp) /* all done? */
1112     break;
1113     *wp++ = cp; /* add argument to list */
1114     while (*++cp && !isspace(*cp))
1115     ;
1116 greg 2.3 }
1117     *wp = NULL;
1118     /* start process */
1119     if ((rval = open_process(PDesc, argv)) < 0)
1120 greg 2.10 syserror(progname);
1121 greg 2.3 if (rval == 0) {
1122     fprintf(stderr, "%s: command not found\n", argv[0]);
1123     exit(1);
1124 greg 1.13 }
1125 greg 2.3 packsiz = rval/(6*sizeof(float)) - 1;
1126     if (packsiz > PACKSIZ)
1127     packsiz = PACKSIZ;
1128 greg 1.13 queuesiz = 0;
1129     }
1130    
1131    
1132 greg 2.3 caldone() /* done with calculation */
1133 greg 1.13 {
1134 greg 1.14 if (childpid == -1)
1135     return;
1136 greg 1.25 clearqueue();
1137 greg 2.3 close_process(PDesc);
1138 greg 1.14 childpid = -1;
1139 greg 1.13 }
1140    
1141    
1142 greg 1.14 rcalfill(x, y) /* fill with ray-calculated pixel */
1143 greg 1.13 int x, y;
1144     {
1145 greg 2.3 if (queuesiz >= packsiz) /* flush queue if needed */
1146 greg 1.25 clearqueue();
1147 greg 1.22 /* add position to queue */
1148 greg 1.13 queue[queuesiz][0] = x;
1149     queue[queuesiz][1] = y;
1150     queuesiz++;
1151     }
1152    
1153    
1154 greg 1.25 clearqueue() /* process queue */
1155 greg 1.13 {
1156 greg 1.22 FVECT orig, dir;
1157 greg 2.3 float fbuf[6*(PACKSIZ+1)];
1158     register float *fbp;
1159 greg 1.13 register int i;
1160 greg 2.22 double vx, vy;
1161 greg 1.13
1162 greg 2.10 if (queuesiz == 0)
1163     return;
1164 greg 2.3 fbp = fbuf;
1165 greg 1.13 for (i = 0; i < queuesiz; i++) {
1166 greg 1.22 viewray(orig, dir, &ourview,
1167     (queue[i][0]+.5)/hresolu,
1168     (queue[i][1]+.5)/vresolu);
1169 greg 2.3 *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
1170     *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
1171 greg 1.22 }
1172 greg 2.3 /* mark end and get results */
1173     bzero((char *)fbp, 6*sizeof(float));
1174 gregl 2.29 if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*(queuesiz+1),
1175 greg 2.3 6*sizeof(float)*(queuesiz+1)) !=
1176 gregl 2.29 4*sizeof(float)*(queuesiz+1)) {
1177 greg 2.3 fprintf(stderr, "%s: error reading from rtrace process\n",
1178     progname);
1179     exit(1);
1180     }
1181     fbp = fbuf;
1182 greg 1.22 for (i = 0; i < queuesiz; i++) {
1183 greg 1.16 if (ourexp > 0 && ourexp != 1.0) {
1184 greg 2.3 fbp[0] *= ourexp;
1185     fbp[1] *= ourexp;
1186     fbp[2] *= ourexp;
1187 greg 1.14 }
1188 greg 2.19 if (averaging) {
1189     setcolor(sscan(queue[i][1])[queue[i][0]],
1190     fbp[0], fbp[1], fbp[2]);
1191 greg 2.26 wscan(queue[i][1])[queue[i][0]] = 1;
1192 greg 2.19 } else
1193     setcolr(pscan(queue[i][1])[queue[i][0]],
1194     fbp[0], fbp[1], fbp[2]);
1195 greg 2.22 if (zisnorm)
1196     zscan(queue[i][1])[queue[i][0]] = fbp[3];
1197     else {
1198     vx = (queue[i][0]+.5)/hresolu + ourview.hoff - .5;
1199     vy = (queue[i][1]+.5)/vresolu + ourview.voff - .5;
1200     zscan(queue[i][1])[queue[i][0]] = fbp[3] / sqrt(1. +
1201     vx*vx*ourview.hn2 + vy*vy*ourview.vn2);
1202     }
1203 greg 2.3 fbp += 4;
1204 greg 1.13 }
1205     queuesiz = 0;
1206 greg 1.14 }
1207    
1208    
1209 greg 2.10 syserror(s) /* report error and exit */
1210     char *s;
1211 greg 1.14 {
1212 greg 2.10 perror(s);
1213 greg 1.14 exit(1);
1214 greg 1.1 }