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