--- ray/src/px/xshowtrace.c 1990/12/21 17:24:45 1.4 +++ ray/src/px/xshowtrace.c 2004/03/28 20:33:14 2.10 @@ -1,33 +1,42 @@ #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: xshowtrace.c,v 2.10 2004/03/28 20:33:14 schorsch Exp $"; #endif - /* * Display an image and watch the rays get traced. * * 9/21/90 Greg Ward */ +#include + #include "standard.h" +#include "paths.h" #include "view.h" -#include #define MAXDEPTH 32 /* ridiculous ray tree depth */ -char rtcom[] = "rtrace -h -otp -fa -x 1"; -char xicom[] = "x11image -f"; +#ifdef SMLFLT +#define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3) +#else +#define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3) +#endif +char rtcom[] = "rtrace -h- -otp -fa -x 1"; +char xicom[] = "ximage -c 256"; + VIEW ourview = STDVIEW; /* view for picture */ -int xres, yres; +RESOLU ourres; /* picture resolution */ char *progname; /* program name */ +char *picture; /* picture name */ + FILE *pin; /* input stream */ Display *theDisplay = NULL; /* connection to server */ struct node { /* ray tree node */ - double ipt[2]; + int ipt[2]; struct node *sister; struct node *daughter; }; @@ -36,13 +45,22 @@ struct node { /* ray tree node */ int slow = 0; /* slow trace? */ +void mainloop(void); +static void freetree(struct node *tp); +static void tracerays(struct node *tp); +static int strtoipt(int ipt[2], char *str); +static void setvec(int ipt[2]); +static void vector(int ip1[2], int ip2[2]); -main(argc, argv) /* takes both the octree and the image */ -int argc; -char *argv[]; + +int +main( /* takes both the octree and the image */ + int argc, + char *argv[] +) { int i; - char combuf[256]; + char combuf[PATH_MAX]; progname = argv[0]; for (i = 1; i < argc-2; i++) @@ -52,25 +70,26 @@ char *argv[]; break; if (i > argc-2) { fprintf(stderr, "Usage: %s [-s] [rtrace args] octree picture\n", - argv[0]); + progname); exit(1); } + picture = argv[argc-1]; /* get the viewing parameters */ - if (viewfile(argv[argc-1], &ourview, &xres, &yres) <= 0 || + if (viewfile(picture, &ourview, &ourres) <= 0 || setview(&ourview) != NULL) { fprintf(stderr, "%s: cannot get view from \"%s\"\n", - argv[0], argv[argc-1]); + progname, picture); exit(1); } /* open the display */ if ((theDisplay = XOpenDisplay(NULL)) == NULL) { fprintf(stderr, "%s: cannot open display; DISPLAY variable set?\n", - argv[0]); + progname); exit(1); } /* build input command */ - sprintf(combuf, "%s %s | %s", xicom, argv[argc-1], rtcom); + sprintf(combuf, "%s \"%s\" | %s", xicom, picture, rtcom); for ( ; i < argc-1; i++) { strcat(combuf, " "); strcat(combuf, argv[i]); @@ -80,12 +99,14 @@ char *argv[]; exit(1); /* loop on input */ mainloop(); - + /* close pipe and exit */ + pclose(pin); exit(0); } -mainloop() /* get and process input */ +void +mainloop(void) /* get and process input */ { static struct node *sis[MAXDEPTH]; register struct node *newp; @@ -124,19 +145,25 @@ mainloop() /* get and process input */ } -freetree(tp) /* free a trace tree */ -struct node *tp; +static void +freetree( /* free a trace tree */ + struct node *tp +) { - register struct node *kid; + register struct node *kid, *k2; - for (kid = tp->daughter; kid != NULL; kid = kid->sister) + for (kid = tp->daughter; kid != NULL; kid = k2) { + k2 = kid->sister; freetree(kid); - free((char *)tp); + } + free((void *)tp); } -tracerays(tp) /* trace a ray tree */ -struct node *tp; +static void +tracerays( /* trace a ray tree */ + struct node *tp +) { register struct node *kid; @@ -147,15 +174,22 @@ struct node *tp; } -strtoipt(ipt, str) /* convert string x y z to image point */ -double ipt[2]; -char *str; +static int +strtoipt( /* convert string x y z to image point */ + int ipt[2], + char *str +) { - FVECT pt; + FVECT im_pt, pt; - if (sscanf(str, "%lf %lf %lf", &pt[0], &pt[1], &pt[2]) != 3) + if (!sscanvec(str, pt)) return(-1); - viewpixel(&ipt[0], &ipt[1], NULL, &ourview, pt); + if (DOT(pt,pt) <= FTINY) /* origin is really infinity */ + ipt[0] = ipt[1] = -1; /* null vector */ + else { + viewloc(im_pt, &ourview, pt); + loc2pix(ipt, &ourres, im_pt[0], im_pt[1]); + } return(0); } @@ -168,9 +202,12 @@ Window gwind = 0; int xoff, yoff; -setvec(ipt) /* set up vector drawing for pick */ -double ipt[2]; +static void +setvec( /* set up vector drawing for pick */ + int ipt[2] +) { + extern Window xfindwind(); XWindowAttributes wa; XColor xc; XGCValues gcv; @@ -179,13 +216,18 @@ double ipt[2]; unsigned int pm; /* compute pointer location */ if (gwind == 0) { - XQueryPointer(theDisplay, rwind, &rw, &gwind, - &rx, &ry, &wx, &wy, &pm); + register char *wn; + for (wn = picture; *wn; wn++); + while (wn > picture && wn[-1] != '/') wn--; + if ((gwind = xfindwind(theDisplay, rwind, wn, 4)) == 0) { + fprintf(stderr, "%s: cannot find display window!\n", + progname); + exit(1); + } } - XQueryPointer(theDisplay, gwind, &rw, &cw, - &rx, &ry, &wx, &wy, &pm); - xoff = wx - ipt[0]*xres; - yoff = wy - (1.-ipt[1])*yres; + XQueryPointer(theDisplay, gwind, &rw, &cw, &rx, &ry, &wx, &wy, &pm); + xoff = wx - ipt[0]; + yoff = wy - ipt[1]; /* set graphics context */ if (vecGC == 0) { XGetWindowAttributes(theDisplay, gwind, &wa); @@ -202,12 +244,17 @@ double ipt[2]; } -vector(ip1, ip2) /* draw a vector */ -double ip1[2], ip2[2]; +static void +vector( /* draw a vector */ + int ip1[2], + int ip2[2] +) { + if (ip2[0] == -1 && ip2[1] == -1) + return; /* null vector */ XDrawLine(theDisplay, gwind, vecGC, - (int)(ip1[0]*xres)+xoff, (int)((1.-ip1[1])*yres)+yoff, - (int)(ip2[0]*xres)+xoff, (int)((1.-ip2[1])*yres)+yoff); + ip1[0]+xoff, ip1[1]+yoff, + ip2[0]+xoff, ip2[1]+yoff); if (slow) { XFlush(theDisplay); sleep(1);