ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/xshowtrace.c
Revision: 1.4
Committed: Fri Dec 21 17:24:45 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +3 -2 lines
Log Message:
minor compiler complaints

File Contents

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