10 |
|
|
11 |
|
#include "standard.h" |
12 |
|
|
13 |
< |
#include <sys/fcntl.h> |
13 |
> |
#include <fcntl.h> |
14 |
|
|
15 |
|
#include "view.h" |
16 |
|
|
28 |
|
|
29 |
|
#define PACKSIZ 42 /* calculation packet size */ |
30 |
|
|
31 |
< |
#define RTCOM "rtrace -h -ovl -fff -x %d %s" |
31 |
> |
#define RTCOM "rtrace -h -ovl -fff %s" |
32 |
|
|
33 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
34 |
|
|
46 |
|
|
47 |
|
char *progname; |
48 |
|
|
49 |
< |
int fill = F_FORE|F_BACK; /* selected fill algorithm */ |
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 (*deffill)() = backfill; /* selected fill function */ |
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? */ |
60 |
|
int thresolu, tvresolu; /* input resolution */ |
61 |
|
double theirexp; /* input picture exposure */ |
62 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
63 |
+ |
int hasmatrix = 0; /* has transformation matrix */ |
64 |
|
|
65 |
|
int childpid = -1; /* id of fill process */ |
66 |
|
FILE *psend, *precv; /* pipes to/from fill calculation */ |
100 |
|
switch (argv[i][2]) { |
101 |
|
case '0': /* none */ |
102 |
|
check(3,0); |
103 |
< |
fill = 0; |
103 |
> |
fillo = 0; |
104 |
|
break; |
105 |
|
case 'f': /* foreground */ |
106 |
|
check(3,0); |
107 |
< |
fill = F_FORE; |
107 |
> |
fillo = F_FORE; |
108 |
|
break; |
109 |
|
case 'b': /* background */ |
110 |
|
check(3,0); |
111 |
< |
fill = F_BACK; |
111 |
> |
fillo = F_BACK; |
112 |
|
break; |
113 |
|
case 'a': /* all */ |
114 |
|
check(3,0); |
115 |
< |
fill = F_FORE|F_BACK; |
115 |
> |
fillo = F_FORE|F_BACK; |
116 |
|
break; |
117 |
+ |
case 's': /* sample */ |
118 |
+ |
check(3,1); |
119 |
+ |
fillsamp = atoi(argv[++i]); |
120 |
+ |
break; |
121 |
|
case 'c': /* color */ |
122 |
|
check(3,3); |
123 |
< |
deffill = backfill; |
123 |
> |
fillfunc = backfill; |
124 |
|
setcolr(backcolr, atof(argv[i+1]), |
125 |
|
atof(argv[i+2]), atof(argv[i+3])); |
126 |
|
i += 3; |
127 |
|
break; |
128 |
|
case 'z': /* z value */ |
129 |
|
check(3,1); |
130 |
< |
deffill = backfill; |
130 |
> |
fillfunc = backfill; |
131 |
|
backz = atof(argv[++i]); |
132 |
|
break; |
133 |
|
case 'r': /* rtrace */ |
134 |
|
check(3,1); |
135 |
< |
deffill = rcalfill; |
135 |
> |
fillfunc = rcalfill; |
136 |
|
calstart(RTCOM, argv[++i]); |
137 |
|
break; |
138 |
|
default: |
194 |
|
for ( ; i < argc; i += 2) |
195 |
|
addpicture(argv[i], argv[i+1]); |
196 |
|
/* fill in spaces */ |
197 |
< |
if (fill&F_BACK) |
198 |
< |
backpicture(); |
197 |
> |
if (fillo&F_BACK) |
198 |
> |
backpicture(fillfunc, fillsamp); |
199 |
|
else |
200 |
< |
fillpicture(); |
200 |
> |
fillpicture(fillfunc); |
201 |
|
/* close calculation */ |
202 |
|
caldone(); |
203 |
|
/* add to header */ |
204 |
|
printargs(argc, argv, stdout); |
205 |
|
if (gotvfile) { |
206 |
< |
printf(VIEWSTR); |
206 |
> |
fputs(VIEWSTR, stdout); |
207 |
|
fprintview(&ourview, stdout); |
208 |
< |
printf("\n"); |
208 |
> |
putc('\n', stdout); |
209 |
|
} |
210 |
|
if (pixaspect < .99 || pixaspect > 1.01) |
211 |
|
fputaspect(pixaspect, stdout); |
212 |
|
if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005)) |
213 |
|
fputexpos(ourexp, stdout); |
214 |
< |
printf("\n"); |
214 |
> |
putc('\n', stdout); |
215 |
|
/* write picture */ |
216 |
|
writepicture(); |
217 |
|
/* write z file */ |
231 |
|
headline(s) /* process header string */ |
232 |
|
char *s; |
233 |
|
{ |
234 |
< |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
234 |
> |
static char *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL}; |
235 |
|
register char **an; |
236 |
|
|
237 |
< |
printf("\t%s", s); |
237 |
> |
putc('\t', stdout); |
238 |
> |
fputs(s, stdout); |
239 |
|
|
240 |
|
if (isexpos(s)) { |
241 |
|
theirexp *= exposval(s); |
285 |
|
exit(1); |
286 |
|
} |
287 |
|
/* compute transformation */ |
288 |
< |
pixform(theirs2ours, &theirview, &ourview); |
288 |
> |
hasmatrix = pixform(theirs2ours, &theirview, &ourview); |
289 |
|
/* allocate scanlines */ |
290 |
|
scanin = (COLR *)malloc(thresolu*sizeof(COLR)); |
291 |
|
zin = (float *)malloc(thresolu*sizeof(float)); |
332 |
|
{ |
333 |
|
double m4t[4][4]; |
334 |
|
|
335 |
+ |
if (vw1->type != VT_PER && vw1->type != VT_PAR) |
336 |
+ |
return(0); |
337 |
+ |
if (vw2->type != VT_PER && vw2->type != VT_PAR) |
338 |
+ |
return(0); |
339 |
|
setident4(xfmat); |
340 |
|
xfmat[0][0] = vw1->hvec[0]; |
341 |
|
xfmat[0][1] = vw1->hvec[1]; |
363 |
|
m4t[2][2] = vw2->vdir[2]; |
364 |
|
m4t[3][2] = -DOT(vw2->vp,vw2->vdir); |
365 |
|
multmat4(xfmat, xfmat, m4t); |
366 |
+ |
return(1); |
367 |
|
} |
368 |
|
|
369 |
|
|
374 |
|
struct position *lasty; /* input/output */ |
375 |
|
{ |
376 |
|
extern double sqrt(); |
377 |
< |
double pos[3]; |
377 |
> |
FVECT pos; |
378 |
|
struct position lastx, newpos; |
379 |
|
register int x; |
380 |
|
|
381 |
|
lastx.z = 0; |
382 |
|
for (x = thresolu-1; x >= 0; x--) { |
383 |
< |
pos[0] = (x+.5)/thresolu + theirview.hoff - .5; |
384 |
< |
pos[1] = (y+.5)/tvresolu + theirview.voff - .5; |
383 |
> |
pos[0] = (x+.5)/thresolu; |
384 |
> |
pos[1] = (y+.5)/tvresolu; |
385 |
|
pos[2] = zline[x]; |
386 |
< |
if (theirview.type == VT_PER) { |
375 |
< |
if (normdist) /* adjust for eye-ray distance */ |
376 |
< |
pos[2] /= sqrt( 1. |
377 |
< |
+ pos[0]*pos[0]*theirview.hn2 |
378 |
< |
+ pos[1]*pos[1]*theirview.vn2 ); |
379 |
< |
pos[0] *= pos[2]; |
380 |
< |
pos[1] *= pos[2]; |
381 |
< |
} |
382 |
< |
multp3(pos, pos, theirs2ours); |
383 |
< |
if (pos[2] <= 0) { |
386 |
> |
if (movepixel(pos) < 0) { |
387 |
|
lasty[x].z = lastx.z = 0; /* mark invalid */ |
388 |
|
continue; |
389 |
|
} |
387 |
– |
if (ourview.type == VT_PER) { |
388 |
– |
pos[0] /= pos[2]; |
389 |
– |
pos[1] /= pos[2]; |
390 |
– |
} |
391 |
– |
pos[0] += .5 - ourview.hoff; |
392 |
– |
pos[1] += .5 - ourview.voff; |
390 |
|
newpos.x = pos[0] * hresolu; |
391 |
|
newpos.y = pos[1] * vresolu; |
392 |
|
newpos.z = zline[x]; |
416 |
|
register int x, y; /* final position */ |
417 |
|
|
418 |
|
/* compute vector p0p1 */ |
419 |
< |
if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { |
419 |
> |
if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) { |
420 |
|
s1x = p1->x - p0->x; |
421 |
|
s1y = p1->y - p0->y; |
422 |
|
l1 = ABS(s1x); |
427 |
|
p1isy = -1; |
428 |
|
} |
429 |
|
/* compute vector p0p2 */ |
430 |
< |
if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { |
430 |
> |
if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) { |
431 |
|
s2x = p2->x - p0->x; |
432 |
|
s2y = p2->y - p0->y; |
433 |
|
if (p1isy == 1) |
445 |
|
y1 = p0->y + c1*s1y/l1; |
446 |
|
for (c2 = l2; c2-- > 0; ) { |
447 |
|
x = x1 + c2*s2x/l2; |
448 |
+ |
if (x < 0 || x >= hresolu) |
449 |
+ |
continue; |
450 |
|
y = y1 + c2*s2y/l2; |
451 |
+ |
if (y < 0 || y >= vresolu) |
452 |
+ |
continue; |
453 |
|
if (zscan(y)[x] <= 0 || zscan(y)[x]-z |
454 |
|
> zeps*zscan(y)[x]) { |
455 |
|
zscan(y)[x] = z; |
460 |
|
} |
461 |
|
|
462 |
|
|
463 |
< |
backpicture() /* background fill algorithm */ |
463 |
> |
movepixel(pos) /* reposition image point */ |
464 |
> |
FVECT pos; |
465 |
|
{ |
466 |
+ |
FVECT pt, direc; |
467 |
+ |
|
468 |
+ |
if (pos[2] <= 0) /* empty pixel */ |
469 |
+ |
return(-1); |
470 |
+ |
if (hasmatrix) { |
471 |
+ |
pos[0] += theirview.hoff - .5; |
472 |
+ |
pos[1] += theirview.voff - .5; |
473 |
+ |
if (theirview.type == VT_PER) { |
474 |
+ |
if (normdist) /* adjust for eye-ray distance */ |
475 |
+ |
pos[2] /= sqrt( 1. |
476 |
+ |
+ pos[0]*pos[0]*theirview.hn2 |
477 |
+ |
+ pos[1]*pos[1]*theirview.vn2 ); |
478 |
+ |
pos[0] *= pos[2]; |
479 |
+ |
pos[1] *= pos[2]; |
480 |
+ |
} |
481 |
+ |
multp3(pos, pos, theirs2ours); |
482 |
+ |
if (pos[2] <= 0) |
483 |
+ |
return(-1); |
484 |
+ |
if (ourview.type == VT_PER) { |
485 |
+ |
pos[0] /= pos[2]; |
486 |
+ |
pos[1] /= pos[2]; |
487 |
+ |
} |
488 |
+ |
pos[0] += .5 - ourview.hoff; |
489 |
+ |
pos[1] += .5 - ourview.voff; |
490 |
+ |
return(0); |
491 |
+ |
} |
492 |
+ |
if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0) |
493 |
+ |
return(-1); |
494 |
+ |
pt[0] += direc[0]*pos[2]; |
495 |
+ |
pt[1] += direc[1]*pos[2]; |
496 |
+ |
pt[2] += direc[2]*pos[2]; |
497 |
+ |
viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt); |
498 |
+ |
if (pos[2] <= 0) |
499 |
+ |
return(-1); |
500 |
+ |
return(0); |
501 |
+ |
} |
502 |
+ |
|
503 |
+ |
|
504 |
+ |
backpicture(fill, samp) /* background fill algorithm */ |
505 |
+ |
int (*fill)(); |
506 |
+ |
int samp; |
507 |
+ |
{ |
508 |
|
int *yback, xback; |
509 |
|
int y; |
466 |
– |
COLR pfill; |
510 |
|
register int x, i; |
511 |
|
/* get back buffer */ |
512 |
|
yback = (int *)malloc(hresolu*sizeof(int)); |
553 |
|
xback = x-1; |
554 |
|
} |
555 |
|
/* |
556 |
< |
* Check to see if we have no background for |
557 |
< |
* this pixel. If not, use background color. |
556 |
> |
* If we have no background for this pixel, |
557 |
> |
* use the given fill function. |
558 |
|
*/ |
559 |
< |
if (xback < 0 && yback[x] < 0) { |
560 |
< |
(*deffill)(x,y); |
518 |
< |
continue; |
519 |
< |
} |
559 |
> |
if (xback < 0 && yback[x] < 0) |
560 |
> |
goto fillit; |
561 |
|
/* |
562 |
|
* Compare, and use the background that is |
563 |
|
* farther, unless one of them is next to us. |
564 |
+ |
* If the background is too distant, call |
565 |
+ |
* the fill function. |
566 |
|
*/ |
567 |
|
if ( yback[x] < 0 |
568 |
|
|| (xback >= 0 && ABS(x-xback) <= 1) |
569 |
|
|| ( ABS(y-yback[x]) > 1 |
570 |
|
&& zscan(yback[x])[x] |
571 |
|
< zscan(y)[xback] ) ) { |
572 |
+ |
if (samp > 0 && ABS(x-xback) >= samp) |
573 |
+ |
goto fillit; |
574 |
|
copycolr(pscan(y)[x],pscan(y)[xback]); |
575 |
|
zscan(y)[x] = zscan(y)[xback]; |
576 |
|
} else { |
577 |
+ |
if (samp > 0 && ABS(y-yback[x]) > samp) |
578 |
+ |
goto fillit; |
579 |
|
copycolr(pscan(y)[x],pscan(yback[x])[x]); |
580 |
|
zscan(y)[x] = zscan(yback[x])[x]; |
581 |
|
} |
582 |
+ |
continue; |
583 |
+ |
fillit: |
584 |
+ |
(*fill)(x,y); |
585 |
+ |
if (fill == rcalfill) { /* use it */ |
586 |
+ |
clearqueue(); |
587 |
+ |
xback = x; |
588 |
+ |
yback[x] = y; |
589 |
+ |
} |
590 |
|
} else { /* full pixel */ |
591 |
|
yback[x] = -2; |
592 |
|
xback = -2; |
596 |
|
} |
597 |
|
|
598 |
|
|
599 |
< |
fillpicture() /* paint in empty pixels with default */ |
599 |
> |
fillpicture(fill) /* paint in empty pixels using fill */ |
600 |
> |
int (*fill)(); |
601 |
|
{ |
602 |
|
register int x, y; |
603 |
|
|
604 |
|
for (y = 0; y < vresolu; y++) |
605 |
|
for (x = 0; x < hresolu; x++) |
606 |
|
if (zscan(y)[x] <= 0) |
607 |
< |
(*deffill)(x,y); |
607 |
> |
(*fill)(x,y); |
608 |
|
} |
609 |
|
|
610 |
|
|
639 |
|
if (donorm) { |
640 |
|
double vx, yzn2; |
641 |
|
register int x; |
642 |
< |
yzn2 = y - .5*(vresolu-1); |
642 |
> |
yzn2 = (y+.5)/vresolu + ourview.voff - .5; |
643 |
|
yzn2 = 1. + yzn2*yzn2*ourview.vn2; |
644 |
|
for (x = 0; x < hresolu; x++) { |
645 |
< |
vx = x - .5*(hresolu-1); |
645 |
> |
vx = (x+.5)/hresolu + ourview.hoff - .5; |
646 |
|
zout[x] = zscan(y)[x] |
647 |
|
* sqrt(vx*vx*ourview.hn2 + yzn2); |
648 |
|
} |
691 |
|
fprintf(stderr, "%s: too many calculations\n", progname); |
692 |
|
exit(1); |
693 |
|
} |
694 |
< |
sprintf(combuf, prog, PACKSIZ, args); |
694 |
> |
sprintf(combuf, prog, args); |
695 |
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
696 |
|
syserror(); |
697 |
|
if ((childpid = vfork()) == 0) { /* fork calculation */ |
727 |
|
|
728 |
|
if (childpid == -1) |
729 |
|
return; |
730 |
< |
clearqueue(1); |
730 |
> |
clearqueue(); |
731 |
> |
fclose(psend); |
732 |
> |
fclose(precv); |
733 |
|
while ((pid = wait(0)) != -1 && pid != childpid) |
734 |
|
; |
735 |
|
childpid = -1; |
740 |
|
int x, y; |
741 |
|
{ |
742 |
|
if (queuesiz >= PACKSIZ) /* flush queue if needed */ |
743 |
< |
clearqueue(0); |
743 |
> |
clearqueue(); |
744 |
|
/* add position to queue */ |
745 |
|
queue[queuesiz][0] = x; |
746 |
|
queue[queuesiz][1] = y; |
748 |
|
} |
749 |
|
|
750 |
|
|
751 |
< |
clearqueue(done) /* process queue */ |
694 |
< |
int done; |
751 |
> |
clearqueue() /* process queue */ |
752 |
|
{ |
753 |
|
FVECT orig, dir; |
754 |
|
float fbuf[6]; |
762 |
|
fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2]; |
763 |
|
fwrite((char *)fbuf, sizeof(float), 6, psend); |
764 |
|
} |
765 |
< |
if ((done ? fclose(psend) : fflush(psend)) == EOF) |
765 |
> |
/* flush output and get results */ |
766 |
> |
fbuf[3] = fbuf[4] = fbuf[5] = 0.0; /* mark */ |
767 |
> |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
768 |
> |
if (fflush(psend) == EOF) |
769 |
|
syserror(); |
770 |
|
for (i = 0; i < queuesiz; i++) { |
771 |
|
if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) { |
782 |
|
fbuf[0], fbuf[1], fbuf[2]); |
783 |
|
zscan(queue[i][1])[queue[i][0]] = fbuf[3]; |
784 |
|
} |
725 |
– |
if (done) |
726 |
– |
fclose(precv); |
785 |
|
queuesiz = 0; |
786 |
|
} |
787 |
|
|