ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/xshowtrace.c
Revision: 2.3
Committed: Fri Oct 30 09:54:19 1992 UTC (31 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +2 -1 lines
Log Message:
added pclose() call for nicer exit

File Contents

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