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