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