--- ray/src/hd/rholo2.c 1998/12/01 15:47:05 3.19 +++ ray/src/hd/rholo2.c 2003/07/21 22:30:18 3.26 @@ -1,9 +1,6 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rholo2.c,v 3.26 2003/07/21 22:30:18 schorsch Exp $"; #endif - /* * Rtrace support routines for holodeck rendering */ @@ -19,67 +16,130 @@ struct gclim { HOLO *hp; /* holodeck pointer */ GCOORD gc; /* grid cell */ FVECT egp; /* eye grid point */ - double erg[2]; /* eye range in wall grid coords */ + double erg2; /* mean square eye grid range */ double gmin[2], gmax[2]; /* grid coordinate limits */ }; /* a grid coordinate range */ static -initeyelim(gcl, hd, gc) /* initialize grid coordinate limits */ +initeyelim(gcl, hp, gc) /* initialize grid coordinate limits */ register struct gclim *gcl; -int hd; +register HOLO *hp; GCOORD *gc; { - register FLOAT *v; + register RREAL *v; register int i; - gcl->hp = hdlist[hd]; - copystruct(&gcl->gc, gc); - hdgrid(gcl->egp, gcl->hp, myeye.vpt); - for (i = 0; i < 2; i++) { - v = gcl->hp->wg[((gcl->gc.w>>1)+i+1)%3]; - gcl->erg[i] = myeye.rng * VLEN(v); - gcl->gmin[i] = FHUGE; gcl->gmax[i] = -FHUGE; + if (hp != NULL) { + hdgrid(gcl->egp, gcl->hp = hp, myeye.vpt); + gcl->erg2 = 0; + for (i = 0, v = hp->wg[0]; i < 3; i++, v += 3) + gcl->erg2 += DOT(v,v); + gcl->erg2 *= (1./3.) * myeye.rng*myeye.rng; } + if (gc != NULL) + gcl->gc = *gc; + gcl->gmin[0] = gcl->gmin[1] = FHUGE; + gcl->gmax[0] = gcl->gmax[1] = -FHUGE; } static -groweyelim(gcl, gp) /* grow grid limits about eye point */ +groweyelim(gcl, gc, r0, r1, tight) /* grow grid limits about eye point */ register struct gclim *gcl; -FVECT gp; +GCOORD *gc; +double r0, r1; +int tight; { - FVECT ab; - double l2, d, mult, wg; - register int i, g; - + FVECT gp, ab; + double ab2, od, cfact; + double sqcoef[3], ctcoef[3], licoef[3], cnst; + int gw, gi[2]; + double wallpos, a, b, c, d, e, f; + double root[2], yex; + int n, i, j, nex; + /* point/view cone */ + i = gc->w>>1; + gp[i] = gc->w&1 ? gcl->hp->grid[i] : 0; + gp[hdwg0[gc->w]] = gc->i[0] + r0; + gp[hdwg1[gc->w]] = gc->i[1] + r1; VSUB(ab, gcl->egp, gp); - l2 = DOT(ab,ab); - if (l2 <= gcl->erg[0]*gcl->erg[1]) { + ab2 = DOT(ab, ab); + gw = gcl->gc.w>>1; + if ((i==gw ? ab[gw]*ab[gw] : ab2) <= gcl->erg2 + FTINY) { gcl->gmin[0] = gcl->gmin[1] = -FHUGE; gcl->gmax[0] = gcl->gmax[1] = FHUGE; - return; + return; /* too close (to wall) */ } - mult = gp[g = gcl->gc.w>>1]; - if (gcl->gc.w&1) - mult -= gcl->hp->grid[g]; - if (ab[g]*ab[g] > gcl->erg[0]*gcl->erg[1]) - mult /= -ab[g]; - else if (fabs(ab[hdwg0[gcl->gc.w]]) > fabs(ab[hdwg1[gcl->gc.w]])) - mult = (gcl->gc.i[0] + .5 - gp[hdwg0[gcl->gc.w]]) / - ab[hdwg0[gcl->gc.w]]; - else - mult = (gcl->gc.i[1] + .5 - gp[hdwg1[gcl->gc.w]]) / - ab[hdwg1[gcl->gc.w]]; + ab2 = 1./ab2; /* 1/norm2(ab) */ + od = DOT(gp, ab); /* origin dot direction */ + cfact = 1./(1. - ab2*gcl->erg2); /* tan^2 + 1 of cone angle */ + for (i = 0; i < 3; i++) { /* compute cone equation */ + sqcoef[i] = ab[i]*ab[i]*cfact*ab2 - 1.; + ctcoef[i] = 2.*ab[i]*ab[(i+1)%3]*cfact*ab2; + licoef[i] = 2.*(gp[i] - ab[i]*cfact*od*ab2); + } + cnst = cfact*od*od*ab2 - DOT(gp,gp); + /* + * CONE: sqcoef[0]*x*x + sqcoef[1]*y*y + sqcoef[2]*z*z + * + ctcoef[0]*x*y + ctcoef[1]*y*z + ctcoef[2]*z*x + * + licoef[0]*x + licoef[1]*y + licoef[2]*z + cnst == 0 + */ + /* equation for conic section in plane */ + gi[0] = hdwg0[gcl->gc.w]; + gi[1] = hdwg1[gcl->gc.w]; + wallpos = gcl->gc.w&1 ? gcl->hp->grid[gw] : 0; + a = sqcoef[gi[0]]; /* x2 */ + b = ctcoef[gi[0]]; /* xy */ + c = sqcoef[gi[1]]; /* y2 */ + d = ctcoef[gw]*wallpos + licoef[gi[0]]; /* x */ + e = ctcoef[gi[1]]*wallpos + licoef[gi[1]]; /* y */ + f = wallpos*(wallpos*sqcoef[gw] + licoef[gw]) + cnst; for (i = 0; i < 2; i++) { - g = ((gcl->gc.w>>1)+i+1)%3; - wg = gp[g] + mult*ab[g]; - d = mult*gcl->erg[i]; - if (d < 0.) d = -d; - if (wg - d < gcl->gmin[i]) - gcl->gmin[i] = wg - d; - if (wg + d > gcl->gmax[i]) - gcl->gmax[i] = wg + d; + if (i) { /* swap x and y coefficients */ + register double t; + t = a; a = c; c = t; + t = d; d = e; e = t; + } + nex = 0; /* check global extrema */ + n = quadratic(root, a*(4.*a*c-b*b), 2.*a*(2.*c*d-b*e), + d*(c*d-b*e) + f*b*b); + while (n-- > 0) { + if (gc->w>>1 == gi[i] && + (gc->w&1) ^ root[n] < gp[gc->w>>1]) { + if (gc->w&1) + gcl->gmin[i] = -FHUGE; + else + gcl->gmax[i] = FHUGE; + nex++; + continue; /* hyperbolic */ + } + if (tight) { + yex = (-2.*a*root[n] - d)/b; + if (yex < gcl->gc.i[1-i] || + yex > gcl->gc.i[1-i]+1) + continue; /* outside cell */ + } + if (root[n] < gcl->gmin[i]) + gcl->gmin[i] = root[n]; + if (root[n] > gcl->gmax[i]) + gcl->gmax[i] = root[n]; + nex++; + } + /* check local extrema */ + for (j = nex < 2 ? 2 : 0; j--; ) { + yex = gcl->gc.i[1-i] + j; + n = quadratic(root, a, b*yex+d, yex*(yex*c+e)+f); + while (n-- > 0) { + if (gc->w>>1 == gi[i] && + (gc->w&1) ^ root[n] < gp[gc->w>>1]) + continue; + if (root[n] < gcl->gmin[i]) + gcl->gmin[i] = root[n]; + if (root[n] > gcl->gmax[i]) + gcl->gmax[i] = root[n]; + } + } } } @@ -97,13 +157,14 @@ register struct gclim *gcl; gcl->gmin[i] = gcl->gc.i[i]; if (gcl->gmax[i] > gcl->gc.i[i]+1) gcl->gmax[i] = gcl->gc.i[i]+1; - if ((incell &= gcl->gmax[i] > gcl->gmin[i])) { + if (gcl->gmax[i] > gcl->gmin[i]) { rrng[i][0] = 256.*(gcl->gmin[i] - gcl->gc.i[i]) + (1.-FTINY); rrng[i][1] = 256.*(gcl->gmax[i] - gcl->gc.i[i]) + (1.-FTINY) - rrng[i][0]; - incell &= rrng[i][1] > 0; - } + } else + rrng[i][0] = rrng[i][1] = 0; + incell &= rrng[i][1] > 0; } return(incell); } @@ -113,9 +174,8 @@ packrays(rod, p) /* pack ray origins and directions * register float *rod; register PACKET *p; { -#define gp ro -#ifdef DEBUG - double dist2sum = 0.; +#if 0 + double dist2sum = 0.; FVECT vt; #endif int nretries = p->nr + 2; @@ -130,37 +190,32 @@ register PACKET *p; if (!hdbcoord(gc, hdlist[p->hd], p->bi)) error(CONSISTENCY, "bad beam index in packrays"); if ((useyelim = myeye.rng > FTINY)) { - initeyelim(&eyelim, p->hd, gc); - gp[gc[1].w>>1] = gc[1].w&1 ? - hdlist[p->hd]->grid[gc[1].w>>1] : 0; - gp[hdwg0[gc[1].w]] = gc[1].i[0]; - gp[hdwg1[gc[1].w]] = gc[1].i[1]; - groweyelim(&eyelim, gp); - gp[hdwg0[gc[1].w]]++; - gp[hdwg1[gc[1].w]]++; - groweyelim(&eyelim, gp); - useyelim &= clipeyelim(rrng0, &eyelim); + initeyelim(&eyelim, hdlist[p->hd], gc); + groweyelim(&eyelim, gc+1, 0., 0., 0); + groweyelim(&eyelim, gc+1, 1., 1., 0); + useyelim = clipeyelim(rrng0, &eyelim); +#ifdef DEBUG + if (!useyelim) + error(WARNING, "no eye overlap in packrays"); +#endif } for (i = 0; i < p->nr; i++) { retry: if (useyelim) { + initeyelim(&eyelim, NULL, gc+1); p->ra[i].r[0][0] = (int)(frandom()*rrng0[0][1]) + rrng0[0][0]; p->ra[i].r[0][1] = (int)(frandom()*rrng0[1][1]) + rrng0[1][0]; - initeyelim(&eyelim, p->hd, gc+1); - gp[gc[0].w>>1] = gc[0].w&1 ? - hdlist[p->hd]->grid[gc[0].w>>1] : 0; - gp[hdwg0[gc[0].w]] = gc[0].i[0] + - (1./256.)*(p->ra[i].r[0][0]+.5); - gp[hdwg1[gc[0].w]] = gc[0].i[1] + - (1./256.)*(p->ra[i].r[0][1]+.5); - groweyelim(&eyelim, gp); + groweyelim(&eyelim, gc, + (1./256.)*(p->ra[i].r[0][0]+.5), + (1./256.)*(p->ra[i].r[0][1]+.5), 1); if (!clipeyelim(rrng1, &eyelim)) { - useyelim &= nretries-- > 0; + useyelim = nretries-- > 0; #ifdef DEBUG if (!useyelim) - error(WARNING, "exceeded retry limit in packrays"); + error(WARNING, + "exceeded retry limit in packrays"); #endif goto retry; } @@ -175,7 +230,7 @@ register PACKET *p; p->ra[i].r[1][1] = frandom() * 256.; } d = hdray(ro, rd, hdlist[p->hd], gc, p->ra[i].r); -#ifdef DEBUG +#if 0 VSUM(vt, ro, rd, d); dist2sum += dist2line(myeye.vpt, ro, vt); #endif @@ -190,10 +245,10 @@ register PACKET *p; VCOPY(rod, rd); rod += 3; } -#ifdef DEBUG - fprintf(stderr, "RMS distance = %f\n", sqrt(dist2sum/p->nr)); +#if 0 + fprintf(stderr, "%f RMS (%d retries)\t", sqrt(dist2sum/p->nr), + p->nr + 2 - nretries); #endif -#undef gp } @@ -222,7 +277,7 @@ done_rtrace() /* clean up and close rtrace calculati int status; /* already closed? */ if (!nprocs) - return; + return(0); /* flush beam queue */ done_packets(flush_queue()); /* sync holodeck */ @@ -293,7 +348,7 @@ getradfile() /* run rad and get needed variables */ pippt = NULL; } if (pippt != NULL) - strcpy(pippt, "> /dev/null"); /* nothing to match */ + strcpy(pippt, "> " NULL_DEVICE); /* nothing to match */ else sprintf(cp, ")[ \t]*=' > %s", tf2); #ifdef DEBUG