--- ray/src/hd/rholo2.c 1998/01/10 16:37:10 3.11 +++ ray/src/hd/rholo2.c 1998/11/25 17:20:25 3.18 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -13,38 +13,69 @@ static char SCCSid[] = "$SunId$ SGI"; #include "random.h" +VIEWPOINT myeye; /* target view position */ + + packrays(rod, p) /* pack ray origins and directions */ register float *rod; register PACKET *p; { - static FVECT ro, rd; + float packdc2[RPACKSIZ]; + int iterleft = 3*p->nr + 9; + BYTE rpos[2][2]; + FVECT ro, rd, rp1; GCOORD gc[2]; - int ila[2], hsh; - double d, sl[4]; - register int i; + double d, dc2, md2, td2, dc2worst = FHUGE; + int i; + register int ii; if (!hdbcoord(gc, hdlist[p->hd], p->bi)) error(CONSISTENCY, "bad beam index in packrays"); - ila[0] = p->hd; ila[1] = p->bi; - hsh = ilhash(ila,2) + p->nc; - for (i = 0; i < p->nr; i++) { - multisamp(sl, 4, urand(hsh+i)); - p->ra[i].r[0][0] = sl[0] * 256.; - p->ra[i].r[0][1] = sl[1] * 256.; - p->ra[i].r[1][0] = sl[2] * 256.; - p->ra[i].r[1][1] = sl[3] * 256.; - d = hdray(ro, rd, hdlist[p->hd], gc, p->ra[i].r); - if (!vdef(OBSTRUCTIONS)) - d *= frandom(); /* random offset */ + td2 = myeye.rng + FTINY; td2 *= td2; + for (i = 0, md2 = 0.; i < p->nr || (md2 > td2 && iterleft--); ) { + rpos[0][0] = frandom() * 256.; + rpos[0][1] = frandom() * 256.; + rpos[1][0] = frandom() * 256.; + rpos[1][1] = frandom() * 256.; + d = hdray(ro, rd, hdlist[p->hd], gc, rpos); + if (myeye.rng > FTINY) { /* check eyepoint */ + register int nexti; + + VSUM(rp1, ro, rd, d); + dc2 = dist2line(myeye.vpt, ro, rp1) / p->nr; + if (i == p->nr) { /* packet full */ + if (dc2 >= dc2worst) /* quick check */ + continue; + nexti = 0; /* find worst */ + for (ii = i; --ii; ) + if (packdc2[ii] > packdc2[nexti]) + nexti = ii; + if (dc2 >= (dc2worst = packdc2[nexti])) + continue; /* worse than worst */ + md2 -= dc2worst; + } else + nexti = i++; + md2 += packdc2[nexti] = dc2; /* new distance */ + ii = nexti; /* put it here */ + } else + ii = i++; if (p->offset != NULL) { + if (!vdef(OBSTRUCTIONS)) + d *= frandom(); /* random offset */ VSUM(ro, ro, rd, d); /* advance ray */ - p->offset[i] = d; + p->offset[ii] = d; } - VCOPY(rod, ro); - rod += 3; - VCOPY(rod, rd); - rod += 3; + p->ra[ii].r[0][0] = rpos[0][0]; + p->ra[ii].r[0][1] = rpos[0][1]; + p->ra[ii].r[1][0] = rpos[1][0]; + p->ra[ii].r[1][1] = rpos[1][1]; + VCOPY(rod+6*ii, ro); + VCOPY(rod+6*ii+3, rd); } +#ifdef DEBUG + fprintf(stderr, "%f mean distance for target %f (%d iterations left)\n", + sqrt(md2), myeye.rng, iterleft); +#endif } @@ -115,7 +146,7 @@ new_rtrace() /* restart rtrace calculation */ getradfile() /* run rad and get needed variables */ { - static short mvar[] = {OCTREE,-1}; + static short mvar[] = {OCTREE,EYESEP,-1}; static char tf1[] = TEMPLATE; char tf2[64]; char combuf[256]; @@ -147,12 +178,10 @@ getradfile() /* run rad and get needed variables */ strcpy(pippt, "> /dev/null"); /* nothing to match */ else sprintf(cp, ")[ \t]*=' > %s", tf2); - if (system(combuf)) { - unlink(tf2); /* clean up */ - unlink(tf1); - error(WARNING, "error executing rad command"); - return(-1); - } +#ifdef DEBUG + wputs(combuf); wputs("\n"); +#endif + system(combuf); /* ignore exit code */ if (pippt == NULL) { loadvars(tf2); /* load variables */ unlink(tf2);