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