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