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