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