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