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