ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/xshowtrace.c
Revision: 2.8
Committed: Fri Jun 27 06:53:22 2003 UTC (20 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +4 -2 lines
Log Message:
Broke standard.h into rtio.h, rterror.h, rtmath.h, and rtmisc.h

File Contents

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