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 |
VIEW ourview = STDVIEW; /* view for picture */ |
28 |
int xres, yres; /* picture resolution */ |
29 |
|
30 |
char *progname; /* program name */ |
31 |
|
32 |
Display *theDisplay = NULL; /* connection to server */ |
33 |
|
34 |
#define rwind RootWindow(theDisplay,ourScreen) |
35 |
#define ourScreen DefaultScreen(theDisplay) |
36 |
|
37 |
GC vecGC; |
38 |
Window gwind; |
39 |
Cursor pickcursor; |
40 |
|
41 |
|
42 |
main(argc, argv) |
43 |
int argc; |
44 |
char *argv[]; |
45 |
{ |
46 |
char *windowname; |
47 |
FILE *fp; |
48 |
|
49 |
progname = argv[0]; |
50 |
if (argc > 2 && !strcmp(argv[1], "-n")) { |
51 |
windowname = argv[2]; |
52 |
argv += 2; |
53 |
argc -= 2; |
54 |
} else |
55 |
windowname = argv[1]; |
56 |
if (argc < 2 || argc > 3) { |
57 |
fprintf(stderr, |
58 |
"Usage: %s [-n windowname] picture [glaresrc]\n", |
59 |
progname); |
60 |
exit(1); |
61 |
} |
62 |
init(argv[1], windowname); |
63 |
if (argc < 3) |
64 |
fp = stdin; |
65 |
else if ((fp = fopen(argv[2], "r")) == NULL) { |
66 |
fprintf(stderr, "%s: cannot open \"%s\"\n", |
67 |
progname, argv[2]); |
68 |
exit(1); |
69 |
} |
70 |
circle_sources(fp); |
71 |
exit(0); |
72 |
} |
73 |
|
74 |
|
75 |
init(pname, wname) /* get view and find window */ |
76 |
char *pname, *wname; |
77 |
{ |
78 |
extern Window xfindwind(); |
79 |
XWindowAttributes wa; |
80 |
XColor xc; |
81 |
XGCValues gcv; |
82 |
/* get the viewing parameters */ |
83 |
if (viewfile(pname, &ourview, &xres, &yres) <= 0 || |
84 |
setview(&ourview) != NULL) { |
85 |
fprintf(stderr, "%s: cannot get view from \"%s\"\n", |
86 |
progname, pname); |
87 |
exit(1); |
88 |
} |
89 |
/* open the display */ |
90 |
if ((theDisplay = XOpenDisplay(NULL)) == NULL) { |
91 |
fprintf(stderr, |
92 |
"%s: cannot open display; DISPLAY variable set?\n", |
93 |
progname); |
94 |
exit(1); |
95 |
} |
96 |
/* find our window */ |
97 |
gwind = xfindwind(theDisplay, rwind, wname, 2); |
98 |
if (gwind == None) { |
99 |
if (wname != pname) { |
100 |
fprintf(stderr, "%s: cannot find \"%s\" window\n", |
101 |
progname, wname); |
102 |
exit(2); |
103 |
} |
104 |
/* start ximage */ |
105 |
if (vfork() == 0) { |
106 |
execlp(XIM, XIM, pname, 0); |
107 |
perror(XIM); |
108 |
fprintf(stderr, "%s: cannot start %s\n", |
109 |
progname, XIM); |
110 |
kill(getppid(), SIGPIPE); |
111 |
_exit(1); |
112 |
} |
113 |
do |
114 |
sleep(8); |
115 |
while ((gwind=xfindwind(theDisplay,rwind,pname,2)) == None); |
116 |
} else |
117 |
XMapRaised(theDisplay, gwind); |
118 |
do { |
119 |
XGetWindowAttributes(theDisplay, gwind, &wa); |
120 |
sleep(4); |
121 |
} while (wa.map_state != IsViewable); |
122 |
if (wa.width != xres || wa.height != yres) { |
123 |
fprintf(stderr, |
124 |
"%s: warning -- window seems to be the wrong size!\n", |
125 |
progname); |
126 |
xres = wa.width; |
127 |
yres = wa.height; |
128 |
} |
129 |
/* set graphics context */ |
130 |
xc.red = 65535; xc.green = 0; xc.blue = 0; |
131 |
xc.flags = DoRed|DoGreen|DoBlue; |
132 |
if (XAllocColor(theDisplay, wa.colormap, &xc)) { |
133 |
gcv.foreground = xc.pixel; |
134 |
vecGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv); |
135 |
} else { |
136 |
gcv.function = GXinvert; |
137 |
vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv); |
138 |
} |
139 |
} |
140 |
|
141 |
|
142 |
circle_sources(fp) /* circle sources listed in fp */ |
143 |
FILE *fp; |
144 |
{ |
145 |
char linbuf[256]; |
146 |
int reading = 0; |
147 |
FVECT dir; |
148 |
double dom; |
149 |
|
150 |
while (fgets(linbuf, sizeof(linbuf), fp) != NULL) |
151 |
if (reading) { |
152 |
if (!strncmp(linbuf, "END", 3)) { |
153 |
XFlush(theDisplay); |
154 |
return; |
155 |
} |
156 |
if (sscanf(linbuf, "%lf %lf %lf %lf", |
157 |
&dir[0], &dir[1], &dir[2], |
158 |
&dom) != 4) |
159 |
break; |
160 |
circle(dir, dom); |
161 |
} else if (!strcmp(linbuf, "BEGIN glare source\n")) |
162 |
reading++; |
163 |
|
164 |
fprintf(stderr, "%s: error reading glare sources\n", progname); |
165 |
exit(1); |
166 |
} |
167 |
|
168 |
|
169 |
circle(dir, dom) /* indicate a solid angle on image */ |
170 |
FVECT dir; |
171 |
double dom; |
172 |
{ |
173 |
FVECT start, cur; |
174 |
XPoint pt[NSEG+1]; |
175 |
double px, py, pz; |
176 |
register int i; |
177 |
|
178 |
fcross(cur, dir, ourview.vup); |
179 |
if (normalize(cur) == 0.0) |
180 |
goto fail; |
181 |
spinvector(start, dir, cur, acos(1.-dom/(2.*PI))); |
182 |
for (i = 0; i <= NSEG; i++) { |
183 |
spinvector(cur, start, dir, 2.*PI*i/NSEG); |
184 |
cur[0] += ourview.vp[0]; |
185 |
cur[1] += ourview.vp[1]; |
186 |
cur[2] += ourview.vp[2]; |
187 |
viewpixel(&px, &py, &pz, &ourview, cur); |
188 |
if (pz <= 0.0) |
189 |
goto fail; |
190 |
pt[i].x = px*xres; |
191 |
pt[i].y = yres-1 - (int)(py*yres); |
192 |
} |
193 |
XDrawLines(theDisplay, gwind, vecGC, pt, NSEG+1, CoordModeOrigin); |
194 |
return; |
195 |
fail: |
196 |
fprintf(stderr, "%s: cannot draw source at (%f,%f,%f)\n", |
197 |
progname, dir[0], dir[1], dir[2]); |
198 |
} |