--- ray/src/util/vwrays.c 2005/09/19 04:26:09 3.12 +++ ray/src/util/vwrays.c 2022/04/09 17:18:08 3.24 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: vwrays.c,v 3.12 2005/09/19 04:26:09 greg Exp $"; +static const char RCSid[] = "$Id: vwrays.c,v 3.24 2022/04/09 17:18:08 greg Exp $"; #endif /* * Compute rays corresponding to a given picture or view. @@ -27,10 +27,16 @@ double pa = 1.; double pj = 0.; +double pd = 0.; + int zfd = -1; int fromstdin = 0; +int unbuffered = 0; + +int repeatcnt = 1; + char *progname; @@ -42,7 +48,7 @@ main( { char *err; int rval, getdim = 0; - register int i; + int i; progname = argv[0]; if (argc < 2) @@ -56,9 +62,11 @@ main( break; case 'f': /* float */ putr = putf; + SET_FILE_BINARY(stdout); break; case 'd': /* double */ putr = putd; + SET_FILE_BINARY(stdout); break; default: goto userr; @@ -71,7 +79,7 @@ main( fprintf(stderr, "%s: no view in file\n", argv[i]); - exit(1); + return(1); } break; } @@ -88,7 +96,7 @@ main( if (rs.xr <= 0) { fprintf(stderr, "%s: bad x resolution\n", progname); - exit(1); + return(1); } break; case 'y': /* y resolution */ @@ -96,20 +104,28 @@ main( if (rs.yr <= 0) { fprintf(stderr, "%s: bad y resolution\n", progname); - exit(1); + return(1); } break; + case 'c': /* repeat count */ + repeatcnt = atoi(argv[++i]); + break; case 'p': /* pixel aspect or jitter */ if (argv[i][2] == 'a') pa = atof(argv[++i]); else if (argv[i][2] == 'j') - pj= atof(argv[++i]); + pj = atof(argv[++i]); + else if (argv[i][2] == 'd') + pd = atof(argv[++i]); else goto userr; break; case 'i': /* get pixels from stdin */ fromstdin = 1; break; + case 'u': /* unbuffered output */ + unbuffered = 1; + break; default: goto userr; } @@ -119,39 +135,35 @@ main( rval = viewfile(argv[i], &vw, &rs); if (rval <= 0) { fprintf(stderr, "%s: no view in picture\n", argv[i]); - exit(1); + return(1); } - if (i+1 < argc) { - zfd = open(argv[i+1], O_RDONLY); - if (zfd < 0) { - fprintf(stderr, - "%s: cannot open depth buffer\n", - argv[i+1]); - exit(1); - } + if (!getdim & (i+1 < argc)) { + zfd = open_float_depth(argv[i+1], (long)rs.xr*rs.yr); + if (zfd < 0) + return(1); } } if ((err = setview(&vw)) != NULL) { fprintf(stderr, "%s: %s\n", progname, err); - exit(1); + return(1); } if (i == argc) normaspect(viewaspect(&vw), &pa, &rs.xr, &rs.yr); if (getdim) { - printf("-x %d -y %d -ld%c\n", rs.xr, rs.yr, - vw.vaft > FTINY ? '+' : '-'); - exit(0); + printf("-x %d -y %d%s\n", rs.xr, rs.yr, + (vw.vaft > FTINY) ? " -ld+" : ""); + return(0); } if (fromstdin) pix2rays(stdin); else putrays(); - exit(0); + return(0); userr: fprintf(stderr, - "Usage: %s [ -i -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n", + "Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n", progname); - exit(1); + return(1); } @@ -178,19 +190,18 @@ pix2rays( RREAL loc[2]; int pp[2]; double d; - register int i; + int i, c; while (fscanf(fp, "%lf %lf", &px, &py) == 2) { px += .5; py += .5; - if (px < 0 || px >= rs.xr || - py < 0 || py >= rs.yr) { - fprintf(stderr, - "%s: (x,y) pair (%.0f,%.0f) out of range\n", - progname, px, py); - exit(1); - } loc[0] = px/rs.xr; loc[1] = py/rs.yr; if (zfd >= 0) { + if ((loc[0] < 0) | (loc[0] >= 1) | + (loc[1] < 0) | (loc[1] >= 1)) { + fprintf(stderr, "%s: input pixel outside image\n", + progname); + exit(1); + } loc2pix(pp, &rs, loc[0], loc[1]); if (lseek(zfd, (pp[1]*scanlen(&rs)+pp[0])*sizeof(float), @@ -202,20 +213,27 @@ pix2rays( exit(1); } } - jitterloc(loc); - d = viewray(rorg, rdir, &vw, loc[0], loc[1]); - if (d < -FTINY) - rorg[0] = rorg[1] = rorg[2] = - rdir[0] = rdir[1] = rdir[2] = 0.; - else if (zfd >= 0) - for (i = 0; i < 3; i++) { - rorg[i] += rdir[i]*zval; - rdir[i] = -rdir[i]; + for (c = repeatcnt; c-- > 0; ) { + jitterloc(loc); + d = viewray(rorg, rdir, &vw, loc[0], loc[1]); + if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd)) + rorg[0] = rorg[1] = rorg[2] = + rdir[0] = rdir[1] = rdir[2] = 0.; + else if (zfd >= 0) + for (i = 0; i < 3; i++) { + rorg[i] += rdir[i]*zval; + rdir[i] = -rdir[i]; + } + else if (d > FTINY) { + rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; } - else if (d > FTINY) { - rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; + (*putr)(rorg, rdir); + if (c) { + loc[0] = px/rs.xr; loc[1] = py/rs.yr; + } } - (*putr)(rorg, rdir); + if (unbuffered) + fflush(stdout); } if (!feof(fp)) { fprintf(stderr, "%s: expected px py on input\n", progname); @@ -232,7 +250,7 @@ putrays(void) float *zbuf = NULL; int sc; double d; - register int si, i; + int si, i, c; if (zfd >= 0) { zbuf = (float *)malloc(scanlen(&rs)*sizeof(float)); @@ -251,21 +269,23 @@ putrays(void) } } for (si = 0; si < scanlen(&rs); si++) { + for (c = repeatcnt; c-- > 0; ) { pix2loc(loc, &rs, si, sc); jitterloc(loc); d = viewray(rorg, rdir, &vw, loc[0], loc[1]); - if (d < -FTINY) + if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd)) rorg[0] = rorg[1] = rorg[2] = rdir[0] = rdir[1] = rdir[2] = 0.; else if (zfd >= 0) for (i = 0; i < 3; i++) { - rorg[i] += rdir[i]*zbuf[si]; - rdir[i] = -rdir[i]; + rdir[i] = -rdir[i]*zbuf[si]; + rorg[i] -= rdir[i]; } else if (d > FTINY) { rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; } (*putr)(rorg, rdir); + } } } if (zfd >= 0) @@ -295,7 +315,7 @@ putf( /* put out ray in float format */ v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; - fwrite(v, sizeof(float), 6, stdout); + putbinary(v, sizeof(float), 6, stdout); } @@ -309,5 +329,5 @@ putd( /* put out ray in double format */ v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; - fwrite(v, sizeof(double), 6, stdout); + putbinary(v, sizeof(double), 6, stdout); }