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