ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/xshowtrace.c
Revision: 2.9
Committed: Mon Nov 10 12:28:56 2003 UTC (20 years, 5 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.8: +6 -4 lines
Log Message:
Fixed size of command line buffers, and allow spaces in input file paths.

File Contents

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