ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/xshowtrace.c
Revision: 2.2
Committed: Tue Apr 21 17:24:51 1992 UTC (32 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +27 -13 lines
Log Message:
fixed bugs resulting from changes to image.c

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    
96     exit(0);
97     }
98    
99    
100     mainloop() /* get and process input */
101     {
102     static struct node *sis[MAXDEPTH];
103     register struct node *newp;
104     char buf[128];
105     int level;
106     register int i;
107    
108     level = 0;
109     while (fgets(buf, sizeof(buf), pin) != NULL) {
110     if ((newp = newnode()) == NULL) {
111     fprintf(stderr, "%s: memory error\n", progname);
112     return;
113     }
114     for (i = 0; buf[i] == '\t'; i++)
115     ;
116     if (strtoipt(newp->ipt, buf+i) < 0) {
117     fprintf(stderr, "%s: bad read\n", progname);
118     return;
119     }
120     newp->sister = sis[i];
121     sis[i] = newp;
122     if (i < level) {
123     newp->daughter = sis[level];
124     sis[level] = NULL;
125     }
126     level = i;
127     if (i == 0) {
128     setvec(sis[0]->ipt);
129     tracerays(sis[0]);
130     freetree(sis[0]);
131     sis[0] = NULL;
132 greg 1.2 if (!slow)
133     XFlush(theDisplay);
134 greg 1.1 }
135     }
136     }
137    
138    
139     freetree(tp) /* free a trace tree */
140     struct node *tp;
141     {
142     register struct node *kid;
143    
144     for (kid = tp->daughter; kid != NULL; kid = kid->sister)
145     freetree(kid);
146     free((char *)tp);
147     }
148    
149    
150     tracerays(tp) /* trace a ray tree */
151     struct node *tp;
152     {
153     register struct node *kid;
154    
155     for (kid = tp->daughter; kid != NULL; kid = kid->sister) {
156 greg 1.2 vector(tp->ipt, kid->ipt);
157 greg 1.1 tracerays(kid);
158     }
159     }
160    
161    
162     strtoipt(ipt, str) /* convert string x y z to image point */
163 greg 2.2 int ipt[2];
164 greg 1.1 char *str;
165     {
166 greg 2.2 FVECT im_pt, pt;
167 greg 1.1
168 greg 2.2 if (!sscanvec(str, pt))
169 greg 1.1 return(-1);
170 greg 2.2 if (DOT(pt,pt) <= FTINY) /* origin is really infinity */
171     ipt[0] = ipt[1] = -1; /* null vector */
172     else {
173     viewloc(im_pt, &ourview, pt);
174     loc2pix(ipt, &ourres, im_pt[0], im_pt[1]);
175     }
176 greg 1.1 return(0);
177     }
178    
179    
180     #define rwind RootWindow(theDisplay,ourScreen)
181     #define ourScreen DefaultScreen(theDisplay)
182    
183     GC vecGC = 0;
184     Window gwind = 0;
185     int xoff, yoff;
186    
187    
188     setvec(ipt) /* set up vector drawing for pick */
189 greg 2.2 int ipt[2];
190 greg 1.1 {
191     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 1.5 if (gwind == 0 &&
199     (gwind = xfindwind(theDisplay, rwind, picture, 2)) == 0) {
200     fprintf(stderr, "%s: cannot find display window!\n", progname);
201     exit(1);
202 greg 1.1 }
203 greg 1.5 XQueryPointer(theDisplay, gwind, &rw, &cw, &rx, &ry, &wx, &wy, &pm);
204 greg 2.2 xoff = wx - ipt[0];
205     yoff = wy - ipt[1];
206 greg 1.1 /* set graphics context */
207     if (vecGC == 0) {
208     XGetWindowAttributes(theDisplay, gwind, &wa);
209     xc.red = 65535; xc.green = 0; xc.blue = 0;
210     xc.flags = DoRed|DoGreen|DoBlue;
211     if (XAllocColor(theDisplay, wa.colormap, &xc)) {
212     gcv.foreground = xc.pixel;
213     vecGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv);
214     } else {
215     gcv.function = GXinvert;
216     vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv);
217     }
218     }
219     }
220    
221    
222     vector(ip1, ip2) /* draw a vector */
223 greg 2.2 int ip1[2], ip2[2];
224 greg 1.1 {
225 greg 2.2 if (ip2[0] == -1 && ip2[1] == -1)
226     return; /* null vector */
227 greg 1.1 XDrawLine(theDisplay, gwind, vecGC,
228 greg 2.2 ip1[0]+xoff, ip1[1]+yoff,
229     ip2[0]+xoff, ip2[1]+yoff);
230 greg 1.2 if (slow) {
231     XFlush(theDisplay);
232     sleep(1);
233     }
234 greg 1.1 }