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