| 1 |
#ifndef lint |
| 2 |
static char SCCSid[] = "$SunId$ LBL"; |
| 3 |
#endif |
| 4 |
|
| 5 |
/* |
| 6 |
* Display an image and watch the rays get traced. |
| 7 |
* |
| 8 |
* 9/21/90 Greg Ward |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "standard.h" |
| 12 |
#include "view.h" |
| 13 |
#include <X11/Xlib.h> |
| 14 |
|
| 15 |
#define MAXDEPTH 32 /* ridiculous ray tree depth */ |
| 16 |
|
| 17 |
char rtcom[] = "rtrace -h- -otp -fa -x 1"; |
| 18 |
char xicom[] = "ximage"; |
| 19 |
|
| 20 |
VIEW ourview = STDVIEW; /* view for picture */ |
| 21 |
int xres, yres; |
| 22 |
|
| 23 |
char *progname; /* program name */ |
| 24 |
|
| 25 |
char *picture; /* picture name */ |
| 26 |
|
| 27 |
FILE *pin; /* input stream */ |
| 28 |
|
| 29 |
Display *theDisplay = NULL; /* connection to server */ |
| 30 |
|
| 31 |
struct node { /* ray tree node */ |
| 32 |
double ipt[2]; |
| 33 |
struct node *sister; |
| 34 |
struct node *daughter; |
| 35 |
}; |
| 36 |
|
| 37 |
#define newnode() (struct node *)calloc(1, sizeof(struct node)) |
| 38 |
|
| 39 |
int slow = 0; /* slow trace? */ |
| 40 |
|
| 41 |
|
| 42 |
main(argc, argv) /* takes both the octree and the image */ |
| 43 |
int argc; |
| 44 |
char *argv[]; |
| 45 |
{ |
| 46 |
int i; |
| 47 |
char combuf[256]; |
| 48 |
|
| 49 |
progname = argv[0]; |
| 50 |
for (i = 1; i < argc-2; i++) |
| 51 |
if (!strcmp(argv[i], "-s")) |
| 52 |
slow++; |
| 53 |
else |
| 54 |
break; |
| 55 |
if (i > argc-2) { |
| 56 |
fprintf(stderr, "Usage: %s [-s] [rtrace args] octree picture\n", |
| 57 |
progname); |
| 58 |
exit(1); |
| 59 |
} |
| 60 |
picture = argv[argc-1]; |
| 61 |
/* get the viewing parameters */ |
| 62 |
if (viewfile(picture, &ourview, &xres, &yres) <= 0 || |
| 63 |
setview(&ourview) != NULL) { |
| 64 |
fprintf(stderr, "%s: cannot get view from \"%s\"\n", |
| 65 |
progname, picture); |
| 66 |
exit(1); |
| 67 |
} |
| 68 |
/* open the display */ |
| 69 |
if ((theDisplay = XOpenDisplay(NULL)) == NULL) { |
| 70 |
fprintf(stderr, |
| 71 |
"%s: cannot open display; DISPLAY variable set?\n", |
| 72 |
progname); |
| 73 |
exit(1); |
| 74 |
} |
| 75 |
/* build input command */ |
| 76 |
sprintf(combuf, "%s %s | %s", xicom, picture, rtcom); |
| 77 |
for ( ; i < argc-1; i++) { |
| 78 |
strcat(combuf, " "); |
| 79 |
strcat(combuf, argv[i]); |
| 80 |
} |
| 81 |
/* start the damn thing */ |
| 82 |
if ((pin = popen(combuf, "r")) == NULL) |
| 83 |
exit(1); |
| 84 |
/* loop on input */ |
| 85 |
mainloop(); |
| 86 |
|
| 87 |
exit(0); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
mainloop() /* get and process input */ |
| 92 |
{ |
| 93 |
static struct node *sis[MAXDEPTH]; |
| 94 |
register struct node *newp; |
| 95 |
char buf[128]; |
| 96 |
int level; |
| 97 |
register int i; |
| 98 |
|
| 99 |
level = 0; |
| 100 |
while (fgets(buf, sizeof(buf), pin) != NULL) { |
| 101 |
if ((newp = newnode()) == NULL) { |
| 102 |
fprintf(stderr, "%s: memory error\n", progname); |
| 103 |
return; |
| 104 |
} |
| 105 |
for (i = 0; buf[i] == '\t'; i++) |
| 106 |
; |
| 107 |
if (strtoipt(newp->ipt, buf+i) < 0) { |
| 108 |
fprintf(stderr, "%s: bad read\n", progname); |
| 109 |
return; |
| 110 |
} |
| 111 |
newp->sister = sis[i]; |
| 112 |
sis[i] = newp; |
| 113 |
if (i < level) { |
| 114 |
newp->daughter = sis[level]; |
| 115 |
sis[level] = NULL; |
| 116 |
} |
| 117 |
level = i; |
| 118 |
if (i == 0) { |
| 119 |
setvec(sis[0]->ipt); |
| 120 |
tracerays(sis[0]); |
| 121 |
freetree(sis[0]); |
| 122 |
sis[0] = NULL; |
| 123 |
if (!slow) |
| 124 |
XFlush(theDisplay); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
freetree(tp) /* free a trace tree */ |
| 131 |
struct node *tp; |
| 132 |
{ |
| 133 |
register struct node *kid; |
| 134 |
|
| 135 |
for (kid = tp->daughter; kid != NULL; kid = kid->sister) |
| 136 |
freetree(kid); |
| 137 |
free((char *)tp); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
tracerays(tp) /* trace a ray tree */ |
| 142 |
struct node *tp; |
| 143 |
{ |
| 144 |
register struct node *kid; |
| 145 |
|
| 146 |
for (kid = tp->daughter; kid != NULL; kid = kid->sister) { |
| 147 |
vector(tp->ipt, kid->ipt); |
| 148 |
tracerays(kid); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
strtoipt(ipt, str) /* convert string x y z to image point */ |
| 154 |
double ipt[2]; |
| 155 |
char *str; |
| 156 |
{ |
| 157 |
FVECT pt; |
| 158 |
|
| 159 |
if (sscanf(str, "%lf %lf %lf", &pt[0], &pt[1], &pt[2]) != 3) |
| 160 |
return(-1); |
| 161 |
viewpixel(&ipt[0], &ipt[1], NULL, &ourview, pt); |
| 162 |
return(0); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
#define rwind RootWindow(theDisplay,ourScreen) |
| 167 |
#define ourScreen DefaultScreen(theDisplay) |
| 168 |
|
| 169 |
GC vecGC = 0; |
| 170 |
Window gwind = 0; |
| 171 |
int xoff, yoff; |
| 172 |
|
| 173 |
|
| 174 |
setvec(ipt) /* set up vector drawing for pick */ |
| 175 |
double ipt[2]; |
| 176 |
{ |
| 177 |
XWindowAttributes wa; |
| 178 |
XColor xc; |
| 179 |
XGCValues gcv; |
| 180 |
int rx, ry, wx, wy; |
| 181 |
Window rw, cw; |
| 182 |
unsigned int pm; |
| 183 |
/* compute pointer location */ |
| 184 |
if (gwind == 0 && |
| 185 |
(gwind = xfindwind(theDisplay, rwind, picture, 2)) == 0) { |
| 186 |
fprintf(stderr, "%s: cannot find display window!\n", progname); |
| 187 |
exit(1); |
| 188 |
} |
| 189 |
XQueryPointer(theDisplay, gwind, &rw, &cw, &rx, &ry, &wx, &wy, &pm); |
| 190 |
xoff = wx - ipt[0]*xres; |
| 191 |
yoff = wy - (1.-ipt[1])*yres; |
| 192 |
/* set graphics context */ |
| 193 |
if (vecGC == 0) { |
| 194 |
XGetWindowAttributes(theDisplay, gwind, &wa); |
| 195 |
xc.red = 65535; xc.green = 0; xc.blue = 0; |
| 196 |
xc.flags = DoRed|DoGreen|DoBlue; |
| 197 |
if (XAllocColor(theDisplay, wa.colormap, &xc)) { |
| 198 |
gcv.foreground = xc.pixel; |
| 199 |
vecGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv); |
| 200 |
} else { |
| 201 |
gcv.function = GXinvert; |
| 202 |
vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
vector(ip1, ip2) /* draw a vector */ |
| 209 |
double ip1[2], ip2[2]; |
| 210 |
{ |
| 211 |
XDrawLine(theDisplay, gwind, vecGC, |
| 212 |
(int)(ip1[0]*xres)+xoff, (int)((1.-ip1[1])*yres)+yoff, |
| 213 |
(int)(ip2[0]*xres)+xoff, (int)((1.-ip2[1])*yres)+yoff); |
| 214 |
if (slow) { |
| 215 |
XFlush(theDisplay); |
| 216 |
sleep(1); |
| 217 |
} |
| 218 |
} |