ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/xglaresrc.c
Revision: 1.10
Committed: Mon Nov 11 15:09:45 1991 UTC (32 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +24 -15 lines
Log Message:
changed call to viewpixel() -> viewloc()

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Circle sources in a displayed image.
9 *
10 * 18 Mar 1991 Greg Ward
11 */
12
13 #include "standard.h"
14 #include "view.h"
15 #include "resolu.h"
16 #include <signal.h>
17 #include <X11/Xlib.h>
18 #include <X11/Xutil.h>
19
20 #ifndef BSD
21 #define vfork fork
22 #endif
23
24 #define XIM "ximage"
25
26 #define NSEG 30 /* number of segments per circle */
27
28 #define FONTNAME "8x13" /* text font we'll use */
29
30 float col[3] = {1.,0.,0.}; /* color */
31
32 VIEW ourview = STDVIEW; /* view for picture */
33 RESOLU pres; /* picture resolution */
34
35 char *progname; /* program name */
36
37 Display *theDisplay = NULL; /* connection to server */
38
39 #define rwind RootWindow(theDisplay,ourScreen)
40 #define ourScreen DefaultScreen(theDisplay)
41
42 GC vecGC, strGC;
43 Window gwind;
44
45
46 main(argc, argv)
47 int argc;
48 char *argv[];
49 {
50 extern double atof();
51 char *windowname = NULL;
52 FILE *fp;
53
54 progname = *argv++; argc--;
55 while (argc > 0 && argv[0][0] == '-') {
56 switch (argv[0][1]) {
57 case 'n':
58 windowname = *++argv;
59 argc--;
60 break;
61 case 'c':
62 col[0] = atof(*++argv);
63 col[1] = atof(*++argv);
64 col[2] = atof(*++argv);
65 argc -= 3;
66 break;
67 }
68 argv++; argc--;
69 }
70 if (argc < 1 || argc > 2) {
71 fprintf(stderr,
72 "Usage: %s [-n windowname][-c color] picture [glaresrc]\n",
73 progname);
74 exit(1);
75 }
76 init(argv[0], windowname);
77 if (argc < 2)
78 fp = stdin;
79 else if ((fp = fopen(argv[1], "r")) == NULL) {
80 fprintf(stderr, "%s: cannot open \"%s\"\n", progname, argv[1]);
81 exit(1);
82 }
83 circle_sources(fp);
84 exit(0);
85 }
86
87
88 init(pname, wname) /* get view and find window */
89 char *pname, *wname;
90 {
91 extern Window xfindwind();
92 XWindowAttributes wa;
93 XColor xc;
94 XGCValues gcv;
95 /* get the viewing parameters */
96 if (viewfile(pname, &ourview, &pres) <= 0 ||
97 setview(&ourview) != NULL) {
98 fprintf(stderr, "%s: cannot get view from \"%s\"\n",
99 progname, pname);
100 exit(1);
101 }
102 /* open the display */
103 if ((theDisplay = XOpenDisplay(NULL)) == NULL) {
104 fprintf(stderr,
105 "%s: cannot open display; DISPLAY variable set?\n",
106 progname);
107 exit(1);
108 }
109 /* find our window */
110 if (wname == NULL)
111 wname = pname;
112 gwind = xfindwind(theDisplay, rwind, wname, 2);
113 if (gwind == None) {
114 if (wname != pname) {
115 fprintf(stderr, "%s: cannot find \"%s\" window\n",
116 progname, wname);
117 exit(2);
118 }
119 /* start ximage */
120 if (vfork() == 0) {
121 execlp(XIM, XIM, pname, 0);
122 perror(XIM);
123 fprintf(stderr, "%s: cannot start %s\n",
124 progname, XIM);
125 kill(getppid(), SIGPIPE);
126 _exit(1);
127 }
128 do
129 sleep(8);
130 while ((gwind=xfindwind(theDisplay,rwind,pname,2)) == None);
131 } else
132 XMapRaised(theDisplay, gwind);
133 do {
134 XGetWindowAttributes(theDisplay, gwind, &wa);
135 sleep(6);
136 } while (wa.map_state != IsViewable);
137 if (wa.width != scanlen(&pres) || wa.height != numscans(&pres)) {
138 fprintf(stderr,
139 "%s: warning -- window seems to be the wrong size!\n",
140 progname);
141 if (pres.or & YMAJOR) {
142 pres.xr = wa.width;
143 pres.yr = wa.height;
144 } else {
145 pres.xr = wa.height;
146 pres.yr = wa.width;
147 }
148 }
149 /* set graphics context */
150 gcv.font = XLoadFont(theDisplay, FONTNAME);
151 if (gcv.font == 0) {
152 fprintf(stderr, "%s: cannot load font \"%s\"\n",
153 progname, FONTNAME);
154 exit(1);
155 }
156 xc.red = col[0] >= 1.0 ? 65535 : (unsigned)(65536*col[0]);
157 xc.green = col[1] >= 1.0 ? 65535 : (unsigned)(65536*col[1]);
158 xc.blue = col[2] >= 1.0 ? 65535 : (unsigned)(65536*col[2]);
159 xc.flags = DoRed|DoGreen|DoBlue;
160 gcv.background = xc.green >= 32768 ?
161 BlackPixel(theDisplay,DefaultScreen(theDisplay)) :
162 WhitePixel(theDisplay,DefaultScreen(theDisplay)) ;
163 if (XAllocColor(theDisplay, wa.colormap, &xc)) {
164 gcv.foreground = xc.pixel;
165 vecGC = XCreateGC(theDisplay,gwind,
166 GCForeground|GCBackground|GCFont,&gcv);
167 strGC = vecGC;
168 } else {
169 gcv.function = GXinvert;
170 vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv);
171 gcv.foreground = xc.green < 32768 ?
172 BlackPixel(theDisplay,DefaultScreen(theDisplay)) :
173 WhitePixel(theDisplay,DefaultScreen(theDisplay)) ;
174 strGC = XCreateGC(theDisplay,gwind,
175 GCForeground|GCBackground|GCFont,&gcv);
176 }
177 }
178
179
180 circle_sources(fp) /* circle sources listed in fp */
181 FILE *fp;
182 {
183 char linbuf[256];
184 int reading = 0;
185 FVECT dir;
186 double dom, lum;
187
188 while (fgets(linbuf, sizeof(linbuf), fp) != NULL)
189 if (reading) {
190 if (!strncmp(linbuf, "END", 3)) {
191 XFlush(theDisplay);
192 return;
193 }
194 if (sscanf(linbuf, "%lf %lf %lf %lf %lf",
195 &dir[0], &dir[1], &dir[2],
196 &dom, &lum) != 5)
197 break;
198 circle(dir, dom);
199 value(dir, lum);
200 } else if (!strcmp(linbuf, "BEGIN glare source\n"))
201 reading++;
202
203 fprintf(stderr, "%s: error reading glare sources\n", progname);
204 exit(1);
205 }
206
207
208 circle(dir, dom) /* indicate a solid angle on image */
209 FVECT dir;
210 double dom;
211 {
212 FVECT start, cur;
213 XPoint pt[NSEG+1];
214 FVECT pp;
215 int ip[2];
216 register int i;
217
218 fcross(cur, dir, ourview.vup);
219 if (normalize(cur) == 0.0)
220 goto fail;
221 spinvector(start, dir, cur, acos(1.-dom/(2.*PI)));
222 for (i = 0; i <= NSEG; i++) {
223 spinvector(cur, start, dir, 2.*PI*i/NSEG);
224 cur[0] += ourview.vp[0];
225 cur[1] += ourview.vp[1];
226 cur[2] += ourview.vp[2];
227 viewloc(pp, &ourview, cur);
228 if (pp[2] <= 0.0)
229 goto fail;
230 loc2pix(ip, &pres, pp[0], pp[1]);
231 pt[i].x = ip[0];
232 pt[i].y = ip[1];
233 }
234 XDrawLines(theDisplay, gwind, vecGC, pt, NSEG+1, CoordModeOrigin);
235 return;
236 fail:
237 fprintf(stderr, "%s: cannot draw source at (%f,%f,%f)\n",
238 progname, dir[0], dir[1], dir[2]);
239 }
240
241
242 value(dir, v) /* print value on image */
243 FVECT dir;
244 double v;
245 {
246 FVECT pos;
247 FVECT pp;
248 int ip[2];
249 char buf[32];
250
251 pos[0] = ourview.vp[0] + dir[0];
252 pos[1] = ourview.vp[1] + dir[1];
253 pos[2] = ourview.vp[2] + dir[2];
254 viewloc(pp, &ourview, pos);
255 if (pp[2] <= 0.0)
256 return;
257 loc2pix(ip, &pres, pp[0], pp[1]);
258 sprintf(buf, "%.0f", v);
259 XDrawImageString(theDisplay, gwind, strGC,
260 ip[0], ip[1], buf, strlen(buf));
261 }