1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.13 |
static const char RCSid[] = "$Id: xshowtrace.c,v 2.12 2005/07/30 22:02:29 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Display an image and watch the rays get traced. |
6 |
|
|
* |
7 |
|
|
* 9/21/90 Greg Ward |
8 |
|
|
*/ |
9 |
|
|
|
10 |
schorsch |
2.9 |
#include <X11/Xlib.h> |
11 |
|
|
|
12 |
greg |
1.1 |
#include "standard.h" |
13 |
schorsch |
2.9 |
#include "paths.h" |
14 |
greg |
1.1 |
#include "view.h" |
15 |
|
|
|
16 |
|
|
#define MAXDEPTH 32 /* ridiculous ray tree depth */ |
17 |
|
|
|
18 |
greg |
2.13 |
#define sscanvec(s,v) (sscanf(s,FVFORMAT,v,v+1,v+2)==3) |
19 |
greg |
2.2 |
|
20 |
greg |
2.11 |
char rtcom[64] = "rtrace -h- -otp -fa -x 1"; |
21 |
greg |
2.5 |
char xicom[] = "ximage -c 256"; |
22 |
greg |
1.1 |
|
23 |
|
|
VIEW ourview = STDVIEW; /* view for picture */ |
24 |
greg |
2.2 |
RESOLU ourres; /* picture resolution */ |
25 |
greg |
1.1 |
|
26 |
|
|
char *progname; /* program name */ |
27 |
|
|
|
28 |
greg |
1.5 |
char *picture; /* picture name */ |
29 |
|
|
|
30 |
greg |
1.1 |
FILE *pin; /* input stream */ |
31 |
|
|
|
32 |
|
|
Display *theDisplay = NULL; /* connection to server */ |
33 |
|
|
|
34 |
|
|
struct node { /* ray tree node */ |
35 |
greg |
2.2 |
int ipt[2]; |
36 |
greg |
1.1 |
struct node *sister; |
37 |
|
|
struct node *daughter; |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
#define newnode() (struct node *)calloc(1, sizeof(struct node)) |
41 |
|
|
|
42 |
greg |
1.2 |
int slow = 0; /* slow trace? */ |
43 |
greg |
1.1 |
|
44 |
schorsch |
2.10 |
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 |
greg |
1.2 |
|
51 |
schorsch |
2.10 |
|
52 |
|
|
int |
53 |
|
|
main( /* takes both the octree and the image */ |
54 |
|
|
int argc, |
55 |
|
|
char *argv[] |
56 |
|
|
) |
57 |
greg |
1.1 |
{ |
58 |
|
|
int i; |
59 |
schorsch |
2.9 |
char combuf[PATH_MAX]; |
60 |
greg |
1.1 |
|
61 |
|
|
progname = argv[0]; |
62 |
greg |
1.2 |
for (i = 1; i < argc-2; i++) |
63 |
|
|
if (!strcmp(argv[i], "-s")) |
64 |
|
|
slow++; |
65 |
greg |
2.11 |
else if (!strcmp(argv[i], "-T")) |
66 |
|
|
strcat(rtcom, " -oTp"); |
67 |
greg |
1.2 |
else |
68 |
|
|
break; |
69 |
|
|
if (i > argc-2) { |
70 |
greg |
2.12 |
fprintf(stderr, "Usage: %s [-s][-T] [rtrace args] octree picture\n", |
71 |
greg |
1.5 |
progname); |
72 |
greg |
1.1 |
exit(1); |
73 |
|
|
} |
74 |
greg |
1.5 |
picture = argv[argc-1]; |
75 |
greg |
1.1 |
/* get the viewing parameters */ |
76 |
greg |
2.2 |
if (viewfile(picture, &ourview, &ourres) <= 0 || |
77 |
greg |
1.1 |
setview(&ourview) != NULL) { |
78 |
|
|
fprintf(stderr, "%s: cannot get view from \"%s\"\n", |
79 |
greg |
1.5 |
progname, picture); |
80 |
greg |
1.1 |
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 |
greg |
1.5 |
progname); |
87 |
greg |
1.1 |
exit(1); |
88 |
|
|
} |
89 |
|
|
/* build input command */ |
90 |
schorsch |
2.9 |
sprintf(combuf, "%s \"%s\" | %s", xicom, picture, rtcom); |
91 |
greg |
1.2 |
for ( ; i < argc-1; i++) { |
92 |
greg |
1.1 |
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 |
greg |
2.3 |
/* close pipe and exit */ |
101 |
|
|
pclose(pin); |
102 |
greg |
1.1 |
exit(0); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
|
106 |
schorsch |
2.10 |
void |
107 |
|
|
mainloop(void) /* get and process input */ |
108 |
greg |
1.1 |
{ |
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 |
greg |
1.2 |
if (!slow) |
140 |
|
|
XFlush(theDisplay); |
141 |
greg |
1.1 |
} |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
|
146 |
schorsch |
2.10 |
static void |
147 |
|
|
freetree( /* free a trace tree */ |
148 |
|
|
struct node *tp |
149 |
|
|
) |
150 |
greg |
1.1 |
{ |
151 |
greg |
2.8 |
register struct node *kid, *k2; |
152 |
greg |
1.1 |
|
153 |
greg |
2.8 |
for (kid = tp->daughter; kid != NULL; kid = k2) { |
154 |
|
|
k2 = kid->sister; |
155 |
greg |
1.1 |
freetree(kid); |
156 |
greg |
2.8 |
} |
157 |
greg |
2.7 |
free((void *)tp); |
158 |
greg |
1.1 |
} |
159 |
|
|
|
160 |
|
|
|
161 |
schorsch |
2.10 |
static void |
162 |
|
|
tracerays( /* trace a ray tree */ |
163 |
|
|
struct node *tp |
164 |
|
|
) |
165 |
greg |
1.1 |
{ |
166 |
|
|
register struct node *kid; |
167 |
|
|
|
168 |
|
|
for (kid = tp->daughter; kid != NULL; kid = kid->sister) { |
169 |
greg |
1.2 |
vector(tp->ipt, kid->ipt); |
170 |
greg |
1.1 |
tracerays(kid); |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
|
175 |
schorsch |
2.10 |
static int |
176 |
|
|
strtoipt( /* convert string x y z to image point */ |
177 |
|
|
int ipt[2], |
178 |
|
|
char *str |
179 |
|
|
) |
180 |
greg |
1.1 |
{ |
181 |
greg |
2.2 |
FVECT im_pt, pt; |
182 |
greg |
1.1 |
|
183 |
greg |
2.2 |
if (!sscanvec(str, pt)) |
184 |
greg |
1.1 |
return(-1); |
185 |
greg |
2.2 |
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 |
greg |
1.1 |
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 |
schorsch |
2.10 |
static void |
204 |
|
|
setvec( /* set up vector drawing for pick */ |
205 |
|
|
int ipt[2] |
206 |
|
|
) |
207 |
greg |
1.1 |
{ |
208 |
greg |
2.4 |
extern Window xfindwind(); |
209 |
greg |
1.1 |
XWindowAttributes wa; |
210 |
|
|
XColor xc; |
211 |
|
|
XGCValues gcv; |
212 |
greg |
1.4 |
int rx, ry, wx, wy; |
213 |
|
|
Window rw, cw; |
214 |
|
|
unsigned int pm; |
215 |
greg |
1.1 |
/* compute pointer location */ |
216 |
greg |
2.6 |
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 |
greg |
1.1 |
} |
226 |
greg |
1.5 |
XQueryPointer(theDisplay, gwind, &rw, &cw, &rx, &ry, &wx, &wy, &pm); |
227 |
greg |
2.2 |
xoff = wx - ipt[0]; |
228 |
|
|
yoff = wy - ipt[1]; |
229 |
greg |
1.1 |
/* 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 |
schorsch |
2.10 |
static void |
246 |
|
|
vector( /* draw a vector */ |
247 |
|
|
int ip1[2], |
248 |
|
|
int ip2[2] |
249 |
|
|
) |
250 |
greg |
1.1 |
{ |
251 |
greg |
2.2 |
if (ip2[0] == -1 && ip2[1] == -1) |
252 |
|
|
return; /* null vector */ |
253 |
greg |
1.1 |
XDrawLine(theDisplay, gwind, vecGC, |
254 |
greg |
2.2 |
ip1[0]+xoff, ip1[1]+yoff, |
255 |
|
|
ip2[0]+xoff, ip2[1]+yoff); |
256 |
greg |
1.2 |
if (slow) { |
257 |
|
|
XFlush(theDisplay); |
258 |
|
|
sleep(1); |
259 |
|
|
} |
260 |
greg |
1.1 |
} |