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