ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 2.9
Committed: Fri Oct 2 16:24:22 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +0 -2 lines
Log Message:
Removed problematic math function declarations

File Contents

# User Rev Content
1 greg 1.34 /* Copyright (c) 1991 Regents of the University of California */
2    
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 1.21 #ifndef BSD
24     #define vfork fork
25     #endif
26    
27 greg 1.17 #define pscan(y) (ourpict+(y)*hresolu)
28     #define zscan(y) (ourzbuf+(y)*hresolu)
29 greg 1.1
30 greg 1.13 #define F_FORE 1 /* fill foreground */
31     #define F_BACK 2 /* fill background */
32 greg 1.11
33 greg 2.3 #define PACKSIZ 256 /* max. calculation packet size */
34 greg 1.13
35 greg 2.4 #define RTCOM "rtrace -h- -ovl -fff "
36 greg 1.13
37 greg 1.3 #define ABS(x) ((x)>0?(x):-(x))
38    
39 greg 1.15 struct position {int x,y; float z;};
40    
41 greg 1.17 VIEW ourview = STDVIEW; /* desired view */
42     int hresolu = 512; /* horizontal resolution */
43     int vresolu = 512; /* vertical resolution */
44 greg 1.18 double pixaspect = 1.0; /* pixel aspect ratio */
45 greg 1.1
46 greg 1.8 double zeps = .02; /* allowed z epsilon */
47 greg 1.1
48     COLR *ourpict; /* output picture */
49     float *ourzbuf; /* corresponding z-buffer */
50    
51     char *progname;
52    
53 greg 1.25 int fillo = F_FORE|F_BACK; /* selected fill options */
54 greg 1.26 int fillsamp = 0; /* sample separation (0 == inf) */
55 greg 1.14 extern int backfill(), rcalfill(); /* fill functions */
56 greg 1.25 int (*fillfunc)() = backfill; /* selected fill function */
57 greg 1.11 COLR backcolr = BLKCOLR; /* background color */
58 greg 1.13 double backz = 0.0; /* background z value */
59 greg 1.16 int normdist = 1; /* normalized distance? */
60     double ourexp = -1; /* output picture exposure */
61 greg 1.11
62 greg 1.17 VIEW theirview = STDVIEW; /* input view */
63 greg 1.16 int gotview; /* got input view? */
64 greg 1.33 int wrongformat = 0; /* input in another format? */
65 greg 1.34 RESOLU tresolu; /* input resolution */
66 greg 1.17 double theirexp; /* input picture exposure */
67 greg 1.5 double theirs2ours[4][4]; /* transformation matrix */
68 greg 1.30 int hasmatrix = 0; /* has transformation matrix */
69 greg 1.1
70 greg 2.3 int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */
71     #define childpid (PDesc[2])
72     unsigned short queue[PACKSIZ][2]; /* pending pixels */
73     int packsiz; /* actual packet size */
74 greg 1.13 int queuesiz; /* number of pixels pending */
75 greg 1.5
76 greg 1.13
77 greg 1.1 main(argc, argv) /* interpolate pictures */
78     int argc;
79     char *argv[];
80     {
81 greg 2.3 #define check(ol,al) if (argv[i][ol] || \
82     badarg(argc-i-1,argv+i+1,al)) \
83     goto badopt
84 greg 1.1 int gotvfile = 0;
85 greg 1.11 char *zfile = NULL;
86 greg 1.1 char *err;
87 greg 1.17 int i, rval;
88 greg 1.1
89     progname = argv[0];
90    
91 greg 1.17 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
92     rval = getviewopt(&ourview, argc-i, argv+i);
93     if (rval >= 0) {
94     i += rval;
95     continue;
96     }
97 greg 1.1 switch (argv[i][1]) {
98     case 't': /* threshold */
99 greg 2.3 check(2,"f");
100 greg 1.1 zeps = atof(argv[++i]);
101     break;
102 greg 1.11 case 'n': /* dist. normalized? */
103 greg 2.3 check(2,NULL);
104 greg 1.11 normdist = !normdist;
105 greg 1.10 break;
106 greg 1.11 case 'f': /* fill type */
107     switch (argv[i][2]) {
108     case '0': /* none */
109 greg 2.3 check(3,NULL);
110 greg 1.25 fillo = 0;
111 greg 1.11 break;
112     case 'f': /* foreground */
113 greg 2.3 check(3,NULL);
114 greg 1.25 fillo = F_FORE;
115 greg 1.11 break;
116     case 'b': /* background */
117 greg 2.3 check(3,NULL);
118 greg 1.25 fillo = F_BACK;
119 greg 1.11 break;
120     case 'a': /* all */
121 greg 2.3 check(3,NULL);
122 greg 1.25 fillo = F_FORE|F_BACK;
123 greg 1.11 break;
124 greg 1.25 case 's': /* sample */
125 greg 2.3 check(3,"i");
126 greg 1.26 fillsamp = atoi(argv[++i]);
127 greg 1.25 break;
128 greg 1.11 case 'c': /* color */
129 greg 2.3 check(3,"fff");
130 greg 1.25 fillfunc = backfill;
131 greg 1.11 setcolr(backcolr, atof(argv[i+1]),
132     atof(argv[i+2]), atof(argv[i+3]));
133     i += 3;
134     break;
135 greg 1.13 case 'z': /* z value */
136 greg 2.3 check(3,"f");
137 greg 1.25 fillfunc = backfill;
138 greg 1.13 backz = atof(argv[++i]);
139     break;
140     case 'r': /* rtrace */
141 greg 2.3 check(3,"s");
142 greg 1.25 fillfunc = rcalfill;
143 greg 1.13 calstart(RTCOM, argv[++i]);
144     break;
145 greg 1.11 default:
146     goto badopt;
147     }
148     break;
149     case 'z': /* z file */
150 greg 2.3 check(2,"s");
151 greg 1.11 zfile = argv[++i];
152     break;
153 greg 1.6 case 'x': /* x resolution */
154 greg 2.3 check(2,"i");
155 greg 1.17 hresolu = atoi(argv[++i]);
156 greg 1.6 break;
157     case 'y': /* y resolution */
158 greg 2.3 check(2,"i");
159 greg 1.17 vresolu = atoi(argv[++i]);
160 greg 1.6 break;
161 greg 1.18 case 'p': /* pixel aspect */
162 greg 2.6 if (argv[i][2] != 'a')
163     goto badopt;
164     check(3,"f");
165 greg 1.18 pixaspect = atof(argv[++i]);
166     break;
167 greg 1.17 case 'v': /* view file */
168     if (argv[i][2] != 'f')
169 greg 1.1 goto badopt;
170 greg 2.3 check(3,"s");
171 greg 1.19 gotvfile = viewfile(argv[++i], &ourview, 0, 0);
172 greg 1.17 if (gotvfile < 0) {
173     perror(argv[i]);
174     exit(1);
175     } else if (gotvfile == 0) {
176     fprintf(stderr, "%s: bad view file\n",
177     argv[i]);
178     exit(1);
179 greg 1.1 }
180     break;
181     default:
182     badopt:
183 greg 1.10 fprintf(stderr, "%s: command line error at '%s'\n",
184 greg 1.1 progname, argv[i]);
185 greg 1.10 goto userr;
186 greg 1.1 }
187 greg 1.17 }
188 greg 1.1 /* check arguments */
189 greg 1.13 if ((argc-i)%2)
190 greg 1.10 goto userr;
191 greg 2.3 if (fillsamp == 1)
192     fillo &= ~F_BACK;
193 greg 1.1 /* set view */
194     if (err = setview(&ourview)) {
195     fprintf(stderr, "%s: %s\n", progname, err);
196     exit(1);
197     }
198 greg 1.18 normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
199 greg 1.1 /* allocate frame */
200 greg 2.7 ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
201     ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
202 greg 1.15 if (ourpict == NULL || ourzbuf == NULL)
203     syserror();
204 greg 2.7 bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
205 greg 1.1 /* get input */
206     for ( ; i < argc; i += 2)
207     addpicture(argv[i], argv[i+1]);
208     /* fill in spaces */
209 greg 1.25 if (fillo&F_BACK)
210 greg 1.26 backpicture(fillfunc, fillsamp);
211 greg 1.12 else
212 greg 1.25 fillpicture(fillfunc);
213 greg 1.13 /* close calculation */
214 greg 1.14 caldone();
215 greg 1.1 /* add to header */
216     printargs(argc, argv, stdout);
217     if (gotvfile) {
218 greg 1.25 fputs(VIEWSTR, stdout);
219 greg 1.1 fprintview(&ourview, stdout);
220 greg 1.25 putc('\n', stdout);
221 greg 1.1 }
222 greg 1.18 if (pixaspect < .99 || pixaspect > 1.01)
223     fputaspect(pixaspect, stdout);
224     if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
225 greg 1.16 fputexpos(ourexp, stdout);
226 greg 1.33 fputformat(COLRFMT, stdout);
227 greg 1.25 putc('\n', stdout);
228 greg 1.11 /* write picture */
229 greg 1.1 writepicture();
230 greg 1.11 /* write z file */
231     if (zfile != NULL)
232     writedistance(zfile);
233 greg 1.1
234     exit(0);
235 greg 1.10 userr:
236     fprintf(stderr,
237 greg 1.17 "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
238 greg 1.10 progname);
239     exit(1);
240 greg 1.1 #undef check
241     }
242    
243    
244     headline(s) /* process header string */
245     char *s;
246     {
247 greg 1.33 char fmt[32];
248 greg 1.1
249 greg 1.33 if (isformat(s)) {
250     formatval(fmt, s);
251     wrongformat = strcmp(fmt, COLRFMT);
252     return;
253     }
254 greg 1.25 putc('\t', stdout);
255     fputs(s, stdout);
256 greg 1.1
257 greg 1.16 if (isexpos(s)) {
258     theirexp *= exposval(s);
259     return;
260     }
261 greg 2.5 if (isview(s) && sscanview(&theirview, s) > 0)
262     gotview++;
263 greg 1.1 }
264    
265    
266 greg 1.10 addpicture(pfile, zspec) /* add picture to output */
267     char *pfile, *zspec;
268 greg 1.1 {
269 greg 1.19 FILE *pfp;
270     int zfd;
271 greg 1.8 char *err;
272 greg 1.1 COLR *scanin;
273 greg 1.15 float *zin;
274     struct position *plast;
275 greg 1.1 int y;
276 greg 1.10 /* open picture file */
277 greg 1.1 if ((pfp = fopen(pfile, "r")) == NULL) {
278     perror(pfile);
279     exit(1);
280     }
281 greg 1.16 /* get header with exposure and view */
282     theirexp = 1.0;
283     gotview = 0;
284 greg 1.1 printf("%s:\n", pfile);
285 greg 1.33 getheader(pfp, headline, NULL);
286 greg 1.34 if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
287 greg 1.33 fprintf(stderr, "%s: picture format error\n", pfile);
288 greg 1.1 exit(1);
289     }
290 greg 1.16 if (ourexp <= 0)
291     ourexp = theirexp;
292     else if (ABS(theirexp-ourexp) > .01*ourexp)
293     fprintf(stderr, "%s: different exposure (warning)\n", pfile);
294 greg 1.1 if (err = setview(&theirview)) {
295     fprintf(stderr, "%s: %s\n", pfile, err);
296     exit(1);
297     }
298 greg 1.5 /* compute transformation */
299 greg 1.30 hasmatrix = pixform(theirs2ours, &theirview, &ourview);
300 greg 1.1 /* allocate scanlines */
301 greg 1.34 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 greg 1.15 if (scanin == NULL || zin == NULL || plast == NULL)
306     syserror();
307 greg 1.10 /* get z specification or file */
308 greg 1.19 if ((zfd = open(zspec, O_RDONLY)) == -1) {
309 greg 1.10 double zvalue;
310     register int x;
311     if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
312     perror(zspec);
313     exit(1);
314     }
315 greg 1.34 for (x = scanlen(&tresolu); x-- > 0; )
316 greg 1.10 zin[x] = zvalue;
317     }
318 greg 1.1 /* load image */
319 greg 1.34 for (y = 0; y < numscans(&tresolu); y++) {
320     if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
321 greg 1.1 fprintf(stderr, "%s: read error\n", pfile);
322     exit(1);
323     }
324 greg 1.34 if (zfd != -1 && read(zfd,(char *)zin,
325     scanlen(&tresolu)*sizeof(float))
326     < scanlen(&tresolu)*sizeof(float)) {
327 greg 1.10 fprintf(stderr, "%s: read error\n", zspec);
328 greg 1.1 exit(1);
329     }
330 greg 1.15 addscanline(y, scanin, zin, plast);
331 greg 1.1 }
332     /* clean up */
333     free((char *)scanin);
334     free((char *)zin);
335 greg 1.9 free((char *)plast);
336 greg 1.1 fclose(pfp);
337 greg 1.19 if (zfd != -1)
338     close(zfd);
339 greg 1.1 }
340    
341    
342 greg 1.5 pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */
343     register double xfmat[4][4];
344     register VIEW *vw1, *vw2;
345     {
346     double m4t[4][4];
347    
348 greg 1.30 if (vw1->type != VT_PER && vw1->type != VT_PAR)
349     return(0);
350     if (vw2->type != VT_PER && vw2->type != VT_PAR)
351     return(0);
352 greg 1.5 setident4(xfmat);
353 greg 1.17 xfmat[0][0] = vw1->hvec[0];
354     xfmat[0][1] = vw1->hvec[1];
355     xfmat[0][2] = vw1->hvec[2];
356     xfmat[1][0] = vw1->vvec[0];
357     xfmat[1][1] = vw1->vvec[1];
358     xfmat[1][2] = vw1->vvec[2];
359 greg 1.5 xfmat[2][0] = vw1->vdir[0];
360     xfmat[2][1] = vw1->vdir[1];
361     xfmat[2][2] = vw1->vdir[2];
362     xfmat[3][0] = vw1->vp[0];
363     xfmat[3][1] = vw1->vp[1];
364     xfmat[3][2] = vw1->vp[2];
365     setident4(m4t);
366 greg 1.17 m4t[0][0] = vw2->hvec[0]/vw2->hn2;
367     m4t[1][0] = vw2->hvec[1]/vw2->hn2;
368     m4t[2][0] = vw2->hvec[2]/vw2->hn2;
369     m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
370     m4t[0][1] = vw2->vvec[0]/vw2->vn2;
371     m4t[1][1] = vw2->vvec[1]/vw2->vn2;
372     m4t[2][1] = vw2->vvec[2]/vw2->vn2;
373     m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
374 greg 1.5 m4t[0][2] = vw2->vdir[0];
375     m4t[1][2] = vw2->vdir[1];
376     m4t[2][2] = vw2->vdir[2];
377     m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
378     multmat4(xfmat, xfmat, m4t);
379 greg 1.30 return(1);
380 greg 1.5 }
381    
382    
383 greg 1.15 addscanline(y, pline, zline, lasty) /* add scanline to output */
384 greg 1.1 int y;
385     COLR *pline;
386     float *zline;
387 greg 1.15 struct position *lasty; /* input/output */
388 greg 1.1 {
389 greg 1.30 FVECT pos;
390 greg 1.15 struct position lastx, newpos;
391 greg 1.1 register int x;
392    
393 greg 1.20 lastx.z = 0;
394 greg 1.34 for (x = scanlen(&tresolu); x-- > 0; ) {
395     pix2loc(pos, &tresolu, x, y);
396 greg 1.5 pos[2] = zline[x];
397 greg 1.30 if (movepixel(pos) < 0) {
398 greg 1.15 lasty[x].z = lastx.z = 0; /* mark invalid */
399 greg 1.1 continue;
400 greg 1.15 }
401 greg 1.17 newpos.x = pos[0] * hresolu;
402     newpos.y = pos[1] * vresolu;
403 greg 1.15 newpos.z = zline[x];
404 greg 1.8 /* add pixel to our image */
405 greg 1.17 if (pos[0] >= 0 && newpos.x < hresolu
406     && pos[1] >= 0 && newpos.y < vresolu) {
407 greg 1.15 addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
408     lasty[x].x = lastx.x = newpos.x;
409     lasty[x].y = lastx.y = newpos.y;
410     lasty[x].z = lastx.z = newpos.z;
411     } else
412     lasty[x].z = lastx.z = 0; /* mark invalid */
413 greg 1.1 }
414     }
415    
416    
417 greg 1.15 addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */
418     struct position *p0, *p1, *p2;
419 greg 1.8 COLR pix;
420     double z;
421     {
422 greg 1.15 double zt = 2.*zeps*p0->z; /* threshold */
423     int s1x, s1y, s2x, s2y; /* step sizes */
424     int l1, l2, c1, c2; /* side lengths and counters */
425     int p1isy; /* p0p1 along y? */
426     int x1, y1; /* p1 position */
427     register int x, y; /* final position */
428    
429     /* compute vector p0p1 */
430 greg 1.25 if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
431 greg 1.15 s1x = p1->x - p0->x;
432     s1y = p1->y - p0->y;
433     l1 = ABS(s1x);
434     if (p1isy = (ABS(s1y) > l1))
435     l1 = ABS(s1y);
436     } else {
437     l1 = s1x = s1y = 1;
438     p1isy = -1;
439     }
440     /* compute vector p0p2 */
441 greg 1.25 if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
442 greg 1.15 s2x = p2->x - p0->x;
443     s2y = p2->y - p0->y;
444     if (p1isy == 1)
445     l2 = ABS(s2x);
446     else {
447     l2 = ABS(s2y);
448     if (p1isy != 0 && ABS(s2x) > l2)
449     l2 = ABS(s2x);
450     }
451     } else
452     l2 = s2x = s2y = 1;
453     /* fill the parallelogram */
454     for (c1 = l1; c1-- > 0; ) {
455     x1 = p0->x + c1*s1x/l1;
456     y1 = p0->y + c1*s1y/l1;
457     for (c2 = l2; c2-- > 0; ) {
458     x = x1 + c2*s2x/l2;
459 greg 1.28 if (x < 0 || x >= hresolu)
460     continue;
461 greg 1.15 y = y1 + c2*s2y/l2;
462 greg 1.28 if (y < 0 || y >= vresolu)
463     continue;
464 greg 1.15 if (zscan(y)[x] <= 0 || zscan(y)[x]-z
465     > zeps*zscan(y)[x]) {
466 greg 1.8 zscan(y)[x] = z;
467     copycolr(pscan(y)[x], pix);
468     }
469 greg 1.15 }
470     }
471 greg 1.30 }
472    
473    
474     movepixel(pos) /* reposition image point */
475     FVECT pos;
476     {
477     FVECT pt, direc;
478    
479 greg 1.32 if (pos[2] <= 0) /* empty pixel */
480     return(-1);
481 greg 1.30 if (hasmatrix) {
482     pos[0] += theirview.hoff - .5;
483     pos[1] += theirview.voff - .5;
484     if (theirview.type == VT_PER) {
485     if (normdist) /* adjust for eye-ray distance */
486     pos[2] /= sqrt( 1.
487     + pos[0]*pos[0]*theirview.hn2
488     + pos[1]*pos[1]*theirview.vn2 );
489     pos[0] *= pos[2];
490     pos[1] *= pos[2];
491     }
492     multp3(pos, pos, theirs2ours);
493     if (pos[2] <= 0)
494     return(-1);
495     if (ourview.type == VT_PER) {
496     pos[0] /= pos[2];
497     pos[1] /= pos[2];
498     }
499     pos[0] += .5 - ourview.hoff;
500     pos[1] += .5 - ourview.voff;
501     return(0);
502     }
503     if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
504     return(-1);
505     pt[0] += direc[0]*pos[2];
506     pt[1] += direc[1]*pos[2];
507     pt[2] += direc[2]*pos[2];
508 greg 1.34 viewloc(pos, &ourview, pt);
509 greg 1.30 if (pos[2] <= 0)
510     return(-1);
511     return(0);
512 greg 1.8 }
513    
514    
515 greg 1.26 backpicture(fill, samp) /* background fill algorithm */
516 greg 1.25 int (*fill)();
517 greg 1.26 int samp;
518 greg 1.1 {
519 greg 1.2 int *yback, xback;
520 greg 1.1 int y;
521 greg 1.2 register int x, i;
522     /* get back buffer */
523 greg 1.17 yback = (int *)malloc(hresolu*sizeof(int));
524 greg 1.15 if (yback == NULL)
525     syserror();
526 greg 1.17 for (x = 0; x < hresolu; x++)
527 greg 1.4 yback[x] = -2;
528 greg 1.7 /*
529     * Xback and yback are the pixel locations of suitable
530     * background values in each direction.
531     * A value of -2 means unassigned, and -1 means
532     * that there is no suitable background in this direction.
533     */
534 greg 1.2 /* fill image */
535 greg 1.17 for (y = 0; y < vresolu; y++) {
536 greg 1.4 xback = -2;
537 greg 1.17 for (x = 0; x < hresolu; x++)
538 greg 1.8 if (zscan(y)[x] <= 0) { /* empty pixel */
539 greg 1.7 /*
540     * First, find background from above or below.
541     * (farthest assigned pixel)
542     */
543 greg 1.4 if (yback[x] == -2) {
544 greg 1.17 for (i = y+1; i < vresolu; i++)
545 greg 1.8 if (zscan(i)[x] > 0)
546 greg 1.4 break;
547 greg 1.17 if (i < vresolu
548 greg 1.2 && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
549 greg 1.4 yback[x] = i;
550     else
551     yback[x] = y-1;
552 greg 1.2 }
553 greg 1.7 /*
554     * Next, find background from left or right.
555     */
556 greg 1.4 if (xback == -2) {
557 greg 1.17 for (i = x+1; i < hresolu; i++)
558 greg 1.8 if (zscan(y)[i] > 0)
559 greg 1.4 break;
560 greg 1.17 if (i < hresolu
561 greg 1.4 && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
562     xback = i;
563     else
564     xback = x-1;
565     }
566 greg 1.7 /*
567 greg 1.26 * If we have no background for this pixel,
568     * use the given fill function.
569 greg 1.11 */
570 greg 1.27 if (xback < 0 && yback[x] < 0)
571     goto fillit;
572 greg 1.11 /*
573 greg 1.7 * Compare, and use the background that is
574     * farther, unless one of them is next to us.
575 greg 1.27 * If the background is too distant, call
576     * the fill function.
577 greg 1.7 */
578 greg 1.11 if ( yback[x] < 0
579     || (xback >= 0 && ABS(x-xback) <= 1)
580 greg 1.4 || ( ABS(y-yback[x]) > 1
581 greg 1.11 && zscan(yback[x])[x]
582 greg 1.13 < zscan(y)[xback] ) ) {
583 greg 1.27 if (samp > 0 && ABS(x-xback) >= samp)
584     goto fillit;
585 greg 1.4 copycolr(pscan(y)[x],pscan(y)[xback]);
586 greg 1.13 zscan(y)[x] = zscan(y)[xback];
587     } else {
588 greg 1.27 if (samp > 0 && ABS(y-yback[x]) > samp)
589     goto fillit;
590 greg 1.4 copycolr(pscan(y)[x],pscan(yback[x])[x]);
591 greg 1.13 zscan(y)[x] = zscan(yback[x])[x];
592 greg 1.27 }
593     continue;
594     fillit:
595     (*fill)(x,y);
596     if (fill == rcalfill) { /* use it */
597     clearqueue();
598     xback = x;
599     yback[x] = y;
600 greg 1.13 }
601 greg 1.4 } else { /* full pixel */
602     yback[x] = -2;
603     xback = -2;
604     }
605     }
606 greg 1.2 free((char *)yback);
607 greg 1.1 }
608    
609    
610 greg 1.25 fillpicture(fill) /* paint in empty pixels using fill */
611     int (*fill)();
612 greg 1.11 {
613     register int x, y;
614    
615 greg 1.17 for (y = 0; y < vresolu; y++)
616     for (x = 0; x < hresolu; x++)
617 greg 1.11 if (zscan(y)[x] <= 0)
618 greg 1.25 (*fill)(x,y);
619 greg 1.11 }
620    
621    
622 greg 1.1 writepicture() /* write out picture */
623     {
624     int y;
625    
626 greg 1.34 fprtresolu(hresolu, vresolu, stdout);
627 greg 1.17 for (y = vresolu-1; y >= 0; y--)
628     if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
629 greg 1.15 syserror();
630 greg 1.11 }
631    
632    
633     writedistance(fname) /* write out z file */
634     char *fname;
635     {
636     int donorm = normdist && ourview.type == VT_PER;
637 greg 1.19 int fd;
638 greg 1.11 int y;
639     float *zout;
640    
641 greg 1.19 if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
642 greg 1.11 perror(fname);
643     exit(1);
644     }
645     if (donorm
646 greg 1.17 && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
647 greg 1.15 syserror();
648 greg 1.17 for (y = vresolu-1; y >= 0; y--) {
649 greg 1.11 if (donorm) {
650     double vx, yzn2;
651     register int x;
652 greg 1.29 yzn2 = (y+.5)/vresolu + ourview.voff - .5;
653 greg 1.17 yzn2 = 1. + yzn2*yzn2*ourview.vn2;
654     for (x = 0; x < hresolu; x++) {
655 greg 1.29 vx = (x+.5)/hresolu + ourview.hoff - .5;
656 greg 1.11 zout[x] = zscan(y)[x]
657 greg 1.17 * sqrt(vx*vx*ourview.hn2 + yzn2);
658 greg 1.11 }
659     } else
660     zout = zscan(y);
661 greg 1.23 if (write(fd, (char *)zout, hresolu*sizeof(float))
662 greg 1.19 < hresolu*sizeof(float)) {
663 greg 1.11 perror(fname);
664     exit(1);
665     }
666     }
667     if (donorm)
668     free((char *)zout);
669 greg 1.19 close(fd);
670 greg 1.10 }
671    
672    
673     isfloat(s) /* see if string is floating number */
674     register char *s;
675     {
676     for ( ; *s; s++)
677     if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
678     && *s != 'e' && *s != 'E' && *s != '+')
679     return(0);
680     return(1);
681 greg 1.12 }
682    
683    
684     backfill(x, y) /* fill pixel with background */
685     int x, y;
686     {
687     register BYTE *dest = pscan(y)[x];
688    
689     copycolr(dest, backcolr);
690 greg 1.13 zscan(y)[x] = backz;
691     }
692    
693    
694 greg 2.3 calstart(prog, args) /* start fill calculation */
695 greg 1.13 char *prog, *args;
696     {
697     char combuf[512];
698 greg 2.3 char *argv[64];
699     int rval;
700     register char **wp, *cp;
701 greg 1.13
702 greg 1.14 if (childpid != -1) {
703     fprintf(stderr, "%s: too many calculations\n", progname);
704     exit(1);
705     }
706 greg 2.3 strcpy(combuf, prog);
707     strcat(combuf, args);
708     cp = combuf;
709     wp = argv;
710     for ( ; ; ) {
711     while (isspace(*cp)) cp++;
712     if (!*cp) break;
713     *wp++ = cp;
714     while (!isspace(*cp))
715     if (!*cp++) goto done;
716     *cp++ = '\0';
717     }
718     done:
719     *wp = NULL;
720     /* start process */
721     if ((rval = open_process(PDesc, argv)) < 0)
722 greg 1.14 syserror();
723 greg 2.3 if (rval == 0) {
724     fprintf(stderr, "%s: command not found\n", argv[0]);
725     exit(1);
726 greg 1.13 }
727 greg 2.3 packsiz = rval/(6*sizeof(float)) - 1;
728     if (packsiz > PACKSIZ)
729     packsiz = PACKSIZ;
730 greg 1.13 queuesiz = 0;
731     }
732    
733    
734 greg 2.3 caldone() /* done with calculation */
735 greg 1.13 {
736 greg 1.14 if (childpid == -1)
737     return;
738 greg 1.25 clearqueue();
739 greg 2.3 close_process(PDesc);
740 greg 1.14 childpid = -1;
741 greg 1.13 }
742    
743    
744 greg 1.14 rcalfill(x, y) /* fill with ray-calculated pixel */
745 greg 1.13 int x, y;
746     {
747 greg 2.3 if (queuesiz >= packsiz) /* flush queue if needed */
748 greg 1.25 clearqueue();
749 greg 1.22 /* add position to queue */
750 greg 1.13 queue[queuesiz][0] = x;
751     queue[queuesiz][1] = y;
752     queuesiz++;
753     }
754    
755    
756 greg 1.25 clearqueue() /* process queue */
757 greg 1.13 {
758 greg 1.22 FVECT orig, dir;
759 greg 2.3 float fbuf[6*(PACKSIZ+1)];
760     register float *fbp;
761 greg 1.13 register int i;
762    
763 greg 2.3 fbp = fbuf;
764 greg 1.13 for (i = 0; i < queuesiz; i++) {
765 greg 1.22 viewray(orig, dir, &ourview,
766     (queue[i][0]+.5)/hresolu,
767     (queue[i][1]+.5)/vresolu);
768 greg 2.3 *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
769     *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
770 greg 1.22 }
771 greg 2.3 /* mark end and get results */
772     bzero((char *)fbp, 6*sizeof(float));
773     if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
774     6*sizeof(float)*(queuesiz+1)) !=
775     4*sizeof(float)*queuesiz) {
776     fprintf(stderr, "%s: error reading from rtrace process\n",
777     progname);
778     exit(1);
779     }
780     fbp = fbuf;
781 greg 1.22 for (i = 0; i < queuesiz; i++) {
782 greg 1.16 if (ourexp > 0 && ourexp != 1.0) {
783 greg 2.3 fbp[0] *= ourexp;
784     fbp[1] *= ourexp;
785     fbp[2] *= ourexp;
786 greg 1.14 }
787 greg 1.13 setcolr(pscan(queue[i][1])[queue[i][0]],
788 greg 2.3 fbp[0], fbp[1], fbp[2]);
789     zscan(queue[i][1])[queue[i][0]] = fbp[3];
790     fbp += 4;
791 greg 1.13 }
792     queuesiz = 0;
793 greg 1.14 }
794    
795    
796     syserror() /* report error and exit */
797     {
798     perror(progname);
799     exit(1);
800 greg 1.1 }