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