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