| 1 | greg | 1.7 | /* Copyright (c) 1991 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 | greg | 1.1 | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | *  Display an image and watch the rays get traced. | 
| 9 |  |  | * | 
| 10 |  |  | *      9/21/90 Greg Ward | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 |  |  | #include "standard.h" | 
| 14 |  |  | #include "view.h" | 
| 15 |  |  | #include <X11/Xlib.h> | 
| 16 |  |  |  | 
| 17 |  |  | #define MAXDEPTH        32              /* ridiculous ray tree depth */ | 
| 18 |  |  |  | 
| 19 | greg | 1.6 | char    rtcom[] = "rtrace -h- -otp -fa -x 1"; | 
| 20 |  |  | char    xicom[] = "ximage"; | 
| 21 | greg | 1.1 |  | 
| 22 |  |  | VIEW    ourview = STDVIEW;              /* view for picture */ | 
| 23 |  |  | int     xres, yres; | 
| 24 |  |  |  | 
| 25 |  |  | char    *progname;                      /* program name */ | 
| 26 |  |  |  | 
| 27 | greg | 1.5 | char    *picture;                       /* picture name */ | 
| 28 |  |  |  | 
| 29 | greg | 1.1 | FILE    *pin;                           /* input stream */ | 
| 30 |  |  |  | 
| 31 |  |  | Display *theDisplay = NULL;             /* connection to server */ | 
| 32 |  |  |  | 
| 33 |  |  | struct node {                           /* ray tree node */ | 
| 34 | greg | 1.7 | FVECT   ipt; | 
| 35 | greg | 1.1 | struct node     *sister; | 
| 36 |  |  | struct node     *daughter; | 
| 37 |  |  | }; | 
| 38 |  |  |  | 
| 39 |  |  | #define newnode()       (struct node *)calloc(1, sizeof(struct node)) | 
| 40 |  |  |  | 
| 41 | greg | 1.2 | int     slow = 0;               /* slow trace? */ | 
| 42 | greg | 1.1 |  | 
| 43 | greg | 1.2 |  | 
| 44 | greg | 1.1 | main(argc, argv)                /* takes both the octree and the image */ | 
| 45 |  |  | int     argc; | 
| 46 |  |  | char    *argv[]; | 
| 47 |  |  | { | 
| 48 |  |  | int     i; | 
| 49 |  |  | char    combuf[256]; | 
| 50 |  |  |  | 
| 51 |  |  | progname = argv[0]; | 
| 52 | greg | 1.2 | for (i = 1; i < argc-2; i++) | 
| 53 |  |  | if (!strcmp(argv[i], "-s")) | 
| 54 |  |  | slow++; | 
| 55 |  |  | else | 
| 56 |  |  | break; | 
| 57 |  |  | if (i > argc-2) { | 
| 58 |  |  | fprintf(stderr, "Usage: %s [-s] [rtrace args] octree picture\n", | 
| 59 | greg | 1.5 | progname); | 
| 60 | greg | 1.1 | exit(1); | 
| 61 |  |  | } | 
| 62 | greg | 1.5 | picture = argv[argc-1]; | 
| 63 | greg | 1.1 | /* get the viewing parameters */ | 
| 64 | greg | 1.5 | if (viewfile(picture, &ourview, &xres, &yres) <= 0 || | 
| 65 | greg | 1.1 | setview(&ourview) != NULL) { | 
| 66 |  |  | fprintf(stderr, "%s: cannot get view from \"%s\"\n", | 
| 67 | greg | 1.5 | progname, picture); | 
| 68 | greg | 1.1 | exit(1); | 
| 69 |  |  | } | 
| 70 |  |  | /* open the display */ | 
| 71 |  |  | if ((theDisplay = XOpenDisplay(NULL)) == NULL) { | 
| 72 |  |  | fprintf(stderr, | 
| 73 |  |  | "%s: cannot open display; DISPLAY variable set?\n", | 
| 74 | greg | 1.5 | progname); | 
| 75 | greg | 1.1 | exit(1); | 
| 76 |  |  | } | 
| 77 |  |  | /* build input command */ | 
| 78 | greg | 1.5 | sprintf(combuf, "%s %s | %s", xicom, picture, rtcom); | 
| 79 | greg | 1.2 | for ( ; i < argc-1; i++) { | 
| 80 | greg | 1.1 | strcat(combuf, " "); | 
| 81 |  |  | strcat(combuf, argv[i]); | 
| 82 |  |  | } | 
| 83 |  |  | /* start the damn thing */ | 
| 84 |  |  | if ((pin = popen(combuf, "r")) == NULL) | 
| 85 |  |  | exit(1); | 
| 86 |  |  | /* loop on input */ | 
| 87 |  |  | mainloop(); | 
| 88 |  |  |  | 
| 89 |  |  | exit(0); | 
| 90 |  |  | } | 
| 91 |  |  |  | 
| 92 |  |  |  | 
| 93 |  |  | mainloop()                              /* get and process input */ | 
| 94 |  |  | { | 
| 95 |  |  | static struct node      *sis[MAXDEPTH]; | 
| 96 |  |  | register struct node    *newp; | 
| 97 |  |  | char    buf[128]; | 
| 98 |  |  | int     level; | 
| 99 |  |  | register int    i; | 
| 100 |  |  |  | 
| 101 |  |  | level = 0; | 
| 102 |  |  | while (fgets(buf, sizeof(buf), pin) != NULL) { | 
| 103 |  |  | if ((newp = newnode()) == NULL) { | 
| 104 |  |  | fprintf(stderr, "%s: memory error\n", progname); | 
| 105 |  |  | return; | 
| 106 |  |  | } | 
| 107 |  |  | for (i = 0; buf[i] == '\t'; i++) | 
| 108 |  |  | ; | 
| 109 |  |  | if (strtoipt(newp->ipt, buf+i) < 0) { | 
| 110 |  |  | fprintf(stderr, "%s: bad read\n", progname); | 
| 111 |  |  | return; | 
| 112 |  |  | } | 
| 113 |  |  | newp->sister = sis[i]; | 
| 114 |  |  | sis[i] = newp; | 
| 115 |  |  | if (i < level) { | 
| 116 |  |  | newp->daughter = sis[level]; | 
| 117 |  |  | sis[level] = NULL; | 
| 118 |  |  | } | 
| 119 |  |  | level = i; | 
| 120 |  |  | if (i == 0) { | 
| 121 |  |  | setvec(sis[0]->ipt); | 
| 122 |  |  | tracerays(sis[0]); | 
| 123 |  |  | freetree(sis[0]); | 
| 124 |  |  | sis[0] = NULL; | 
| 125 | greg | 1.2 | if (!slow) | 
| 126 |  |  | XFlush(theDisplay); | 
| 127 | greg | 1.1 | } | 
| 128 |  |  | } | 
| 129 |  |  | } | 
| 130 |  |  |  | 
| 131 |  |  |  | 
| 132 |  |  | freetree(tp)                            /* free a trace tree */ | 
| 133 |  |  | struct node     *tp; | 
| 134 |  |  | { | 
| 135 |  |  | register struct node    *kid; | 
| 136 |  |  |  | 
| 137 |  |  | for (kid = tp->daughter; kid != NULL; kid = kid->sister) | 
| 138 |  |  | freetree(kid); | 
| 139 |  |  | free((char *)tp); | 
| 140 |  |  | } | 
| 141 |  |  |  | 
| 142 |  |  |  | 
| 143 |  |  | tracerays(tp)                           /* trace a ray tree */ | 
| 144 |  |  | struct node     *tp; | 
| 145 |  |  | { | 
| 146 |  |  | register struct node    *kid; | 
| 147 |  |  |  | 
| 148 |  |  | for (kid = tp->daughter; kid != NULL; kid = kid->sister) { | 
| 149 | greg | 1.2 | vector(tp->ipt, kid->ipt); | 
| 150 | greg | 1.1 | tracerays(kid); | 
| 151 |  |  | } | 
| 152 |  |  | } | 
| 153 |  |  |  | 
| 154 |  |  |  | 
| 155 |  |  | strtoipt(ipt, str)              /* convert string x y z to image point */ | 
| 156 | greg | 1.7 | FVECT   ipt; | 
| 157 | greg | 1.1 | char    *str; | 
| 158 |  |  | { | 
| 159 |  |  | FVECT   pt; | 
| 160 |  |  |  | 
| 161 |  |  | if (sscanf(str, "%lf %lf %lf", &pt[0], &pt[1], &pt[2]) != 3) | 
| 162 |  |  | return(-1); | 
| 163 | greg | 1.7 | viewloc(ipt, &ourview, pt); | 
| 164 | greg | 1.1 | return(0); | 
| 165 |  |  | } | 
| 166 |  |  |  | 
| 167 |  |  |  | 
| 168 |  |  | #define rwind           RootWindow(theDisplay,ourScreen) | 
| 169 |  |  | #define ourScreen       DefaultScreen(theDisplay) | 
| 170 |  |  |  | 
| 171 |  |  | GC      vecGC = 0; | 
| 172 |  |  | Window  gwind = 0; | 
| 173 |  |  | int     xoff, yoff; | 
| 174 |  |  |  | 
| 175 |  |  |  | 
| 176 |  |  | setvec(ipt)                     /* set up vector drawing for pick */ | 
| 177 |  |  | double  ipt[2]; | 
| 178 |  |  | { | 
| 179 |  |  | XWindowAttributes       wa; | 
| 180 |  |  | XColor  xc; | 
| 181 |  |  | XGCValues       gcv; | 
| 182 | greg | 1.4 | int     rx, ry, wx, wy; | 
| 183 |  |  | Window  rw, cw; | 
| 184 |  |  | unsigned int    pm; | 
| 185 | greg | 1.1 | /* compute pointer location */ | 
| 186 | greg | 1.5 | if (gwind == 0 && | 
| 187 |  |  | (gwind = xfindwind(theDisplay, rwind, picture, 2)) == 0) { | 
| 188 |  |  | fprintf(stderr, "%s: cannot find display window!\n", progname); | 
| 189 |  |  | exit(1); | 
| 190 | greg | 1.1 | } | 
| 191 | greg | 1.5 | XQueryPointer(theDisplay, gwind, &rw, &cw, &rx, &ry, &wx, &wy, &pm); | 
| 192 | greg | 1.1 | xoff = wx - ipt[0]*xres; | 
| 193 |  |  | yoff = wy - (1.-ipt[1])*yres; | 
| 194 |  |  | /* set graphics context */ | 
| 195 |  |  | if (vecGC == 0) { | 
| 196 |  |  | XGetWindowAttributes(theDisplay, gwind, &wa); | 
| 197 |  |  | xc.red = 65535; xc.green = 0; xc.blue = 0; | 
| 198 |  |  | xc.flags = DoRed|DoGreen|DoBlue; | 
| 199 |  |  | if (XAllocColor(theDisplay, wa.colormap, &xc)) { | 
| 200 |  |  | gcv.foreground = xc.pixel; | 
| 201 |  |  | vecGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv); | 
| 202 |  |  | } else { | 
| 203 |  |  | gcv.function = GXinvert; | 
| 204 |  |  | vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv); | 
| 205 |  |  | } | 
| 206 |  |  | } | 
| 207 |  |  | } | 
| 208 |  |  |  | 
| 209 |  |  |  | 
| 210 |  |  | vector(ip1, ip2)                /* draw a vector */ | 
| 211 |  |  | double  ip1[2], ip2[2]; | 
| 212 |  |  | { | 
| 213 |  |  | XDrawLine(theDisplay, gwind, vecGC, | 
| 214 |  |  | (int)(ip1[0]*xres)+xoff, (int)((1.-ip1[1])*yres)+yoff, | 
| 215 |  |  | (int)(ip2[0]*xres)+xoff, (int)((1.-ip2[1])*yres)+yoff); | 
| 216 | greg | 1.2 | if (slow) { | 
| 217 |  |  | XFlush(theDisplay); | 
| 218 |  |  | sleep(1); | 
| 219 |  |  | } | 
| 220 | greg | 1.1 | } |