--- ray/src/hd/rhdisp2.c 1998/05/14 10:13:50 3.20 +++ ray/src/hd/rhdisp2.c 1998/11/24 17:05:36 3.26 @@ -11,48 +11,39 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rholo.h" #include "rhdisp.h" #include "rhdriver.h" +#include "random.h" #ifndef MAXDIST #define MAXDIST 42 /* maximum distance outside section */ #endif +#ifndef NVSAMPS +#define NVSAMPS 4096 /* number of ray samples per view */ +#endif +#ifndef MEYERNG +#define MEYERNG 0.2 /* target mean eye range (rel. to grid) */ +#endif +#ifndef MAXTODO +#define MAXTODO 3 /* maximum sections to look at */ +#endif +#ifndef MAXDRAT +#define MAXDRAT 3.0 /* maximum distance ratio btwn. cand. sect. */ +#endif -#define MAXVOXEL 16 /* maximum number of active voxels */ - -extern GCOORD *getviewcells(); - -static VIEW dvw; /* current view corresponding to beam list */ - -typedef struct { - int hd; /* holodeck section number (-1 if inactive) */ - int i[3]; /* voxel index (may be outside section) */ -} VOXL; /* a voxel */ - -static VOXL voxel[MAXVOXEL] = {{-1}}; /* current voxel list */ - #define CBEAMBLK 1024 /* cbeam allocation block size */ static struct beamcomp { - unsigned short wants; /* flags telling which voxels want us */ - unsigned short hd; /* holodeck section number */ + int hd; /* holodeck section number */ int bi; /* beam index */ + int4 nr; /* number of samples desired */ } *cbeam = NULL; /* current beam list */ +VIEWPOINT cureye; /* current eye position */ + static int ncbeams = 0; /* number of sorted beams in cbeam */ static int xcbeams = 0; /* extra (unregistered) beams past ncbeams */ static int maxcbeam = 0; /* size of cbeam array */ -struct cellact { - short vi; /* voxel index */ - short add; /* zero means delete */ - short rev; /* reverse ray direction? */ -}; /* action for cell */ -struct beamact { - struct cellact ca; /* cell action */ - GCOORD gc; /* grid coordinate */ -}; /* beam origin and action */ - - int newcbeam() /* allocate new entry at end of cbeam array */ { @@ -91,9 +82,9 @@ register struct beamcomp *cb1, *cb2; { register int c; - if (!cb1->wants) /* put orphans at the end, unsorted */ - return(cb2->wants); - if (!cb2->wants) + if (!cb1->nr) /* put orphans at the end, unsorted */ + return(cb2->nr); + if (!cb2->nr) return(-1); if ((c = cb1->bi - cb2->bi)) /* sort on beam index first */ return(c); @@ -110,7 +101,7 @@ int hd, bi; if (ncbeams <= 0) return(-1); - cb.wants = 0; cb.hd = hd; cb.bi = bi; + cb.hd = hd; cb.bi = bi; cb.nr = 0; p = (struct beamcomp *)bsearch((char *)&cb, (char *)cbeam, ncbeams, sizeof(struct beamcomp), cbeamcmp); if (p == NULL) @@ -138,7 +129,7 @@ int bi; if (bi < 1 | bi > nbeams(hdlist[hd])) error(INTERNAL, "illegal beam index in getcbeam"); n = newcbeam(); /* allocate and assign */ - cbeam[n].wants = 0; cbeam[n].hd = hd; cbeam[n].bi = bi; + cbeam[n].nr = 0; cbeam[n].hd = hd; cbeam[n].bi = bi; return(n); } @@ -156,19 +147,17 @@ int adopt; if (adopt) return; for (i = ncbeams; i--; ) /* identify orphans */ - if (cbeam[i].wants) + if (cbeam[i].nr) break; xcbeams = ncbeams - ++i; /* put orphans after ncbeams */ ncbeams = i; } -cbeamop(op, bl, n, v, hr, vr) /* update beams on server list */ +cbeamop(op, bl, n) /* update beams on server list */ int op; register struct beamcomp *bl; int n; -VIEW *v; -int hr, vr; { register PACKHEAD *pa; register int i; @@ -177,12 +166,11 @@ int hr, vr; return; pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD)); if (pa == NULL) - error(SYSTEM, "out of memory in cbeamadd"); + error(SYSTEM, "out of memory in cbeamop"); for (i = 0; i < n; i++) { pa[i].hd = bl[i].hd; pa[i].bi = bl[i].bi; - pa[i].nr = v==NULL ? 0 : - npixels(v, hr, vr, hdlist[bl[i].hd], bl[i].bi); + pa[i].nr = bl[i].nr; pa[i].nc = 0; } serv_request(op, n*sizeof(PACKHEAD), (char *)pa); @@ -191,322 +179,146 @@ int hr, vr; int -set_voxels(vl, n) /* set new voxel array */ -VOXL vl[MAXVOXEL]; -int n; +comptodo(tdl, vw) /* compute holodeck sections in view */ +int tdl[MAXTODO+1]; +VIEW *vw; { - short wmap[256]; - int vmap[MAXVOXEL]; - int comn = 0; - int no; + int n = 0; + FVECT gp, dv; + double dist2[MAXTODO+1], thisdist2; register int i, j; - /* find common voxels */ - for (j = 0; j < MAXVOXEL && voxel[j].hd >= 0; j++) { - vmap[j] = -1; - for (i = n; i--; ) - if (!bcmp((char *)(vl+i), (char *)(voxel+j), - sizeof(VOXL))) { - vmap[j] = i; - comn |= 1<= 0 && i & 1<= 0; first++) - ; - /* find closest voxels */ - for (n = first, i = 0; n < MAXVOXEL && hdlist[i]; i++) { - hdgrid(gp, hdlist[i], vp); - for (j = 0; n < MAXVOXEL && j < 8; j++) { - dist = 0; - for (k = 0; k < 3; k++) { - d = gp[k] - .5 + (j>>k & 1); - if (d < 0.) - dist += -(vox.i[k] = (int)d - 1); - else if ((vox.i[k] = d) >= hdlist[i]->grid[k]) - dist += vox.i[k] - - hdlist[i]->grid[k] + 1; - } - if (dist > bestd) /* others were closer */ - continue; - if (dist < bestd) { /* start closer list */ - n = first; - bestd = dist; - } - /* check if already in list */ - for (k = first; k--; ) - if (vl[k].hd == i && - vl[k].i[0] == vox.i[0] && - vl[k].i[1] == vox.i[1] && - vl[k].i[2] == vox.i[2]) - break; - if (k >= 0) - continue; - vox.hd = i; /* add this voxel */ - copystruct(&vl[n], &vox); - n++; + /* find closest MAXTODO sections */ + for (i = 0; hdlist[i]; i++) { + hdgrid(gp, hdlist[i], vw->vp); + for (j = 0; j < 3; j++) + if (gp[j] < 0.) + dv[j] = -gp[j]; + else if (gp[j] > hdlist[i]->grid[j]) + dv[j] = gp[j] - hdlist[i]->grid[j]; + else + dv[j] = 0.; + thisdist2 = DOT(dv,dv); + for (j = n; j > 0 && thisdist2 < dist2[j-1]; j--) { + tdl[j] = tdl[j-1]; + dist2[j] = dist2[j-1]; } + tdl[j] = i; + dist2[j] = thisdist2; + if (n < MAXTODO) + n++; } - /* check for really stupid move */ - if (bestd > MAXDIST) { + /* watch for bad move */ + if (n && dist2[0] > MAXDIST*MAXDIST) { error(COMMAND, "move past outer limits"); return(0); } - if (n < MAXVOXEL) - vl[n].hd = -1; + /* avoid big differences */ + for (j = 1; j < n; j++) + if (dist2[j] > MAXDRAT*MAXDRAT*dist2[j-1]) + n = j; + tdl[n] = -1; return(n); } -int -dobeam(gcp, bp) /* express interest or disintrest in a beam */ -GCOORD *gcp; -register struct beamact *bp; +addview(hd, vw, hres, vres) /* add view for section */ +int hd; +VIEW *vw; +int hres, vres; { + int sampquant; + int h, v, shr, svr; GCOORD gc[2]; - int bi, i; - /* compute beam index */ - if (bp->ca.rev) { - copystruct(gc, &bp->gc); - copystruct(gc+1, gcp); + FVECT rorg, rdir; + /* compute sampling grid */ + if (hres|vres && hres*vres <= NVSAMPS) { + shr = hres; svr = vres; + sampquant = 1; } else { - copystruct(gc, gcp); - copystruct(gc+1, &bp->gc); + shr = sqrt(NVSAMPS/viewaspect(vw)) + .5; + svr = (NVSAMPS + shr/2)/shr; + sampquant = (hres*vres + shr*svr/2)/(shr*svr); } - if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0) - error(CONSISTENCY, "bad grid coordinate in dobeam"); - if (bp->ca.add) { /* add it in */ - i = getcbeam(voxel[bp->ca.vi].hd, bi); - cbeam[i].wants |= 1<ca.vi; /* say we want it */ - return(i == ncbeams+xcbeams-1); /* return 1 if totally new */ - } - /* else delete it */ - i = findcbeam(voxel[bp->ca.vi].hd, bi); - if (i >= 0 && cbeam[i].wants & 1<ca.vi) { - cbeam[i].wants &= ~(1<ca.vi); /* we don't want it */ - return(1); /* indicate change */ - } - return(0); + /* intersect sample rays with section */ + for (v = svr; v--; ) + for (h = shr; h--; ) { + if (viewray(rorg, rdir, vw, (v+frandom())/svr, + (h+frandom())/shr) < -FTINY) + continue; + if (hdinter(gc, NULL, NULL, hdlist[hd], rorg, rdir) + >= FHUGE) + continue; + cbeam[getcbeam(hd,hdbindex(hdlist[hd],gc))].nr += + sampquant; + } } - -int -docell(gcp, cap) /* find beams corresponding to cell and voxel */ -GCOORD *gcp; -register struct cellact *cap; +beam_init(fresh) /* clear beam list for new view(s) */ +int fresh; { - register HOLO *hp = hdlist[voxel[cap->vi].hd]; - FVECT org, dir[4]; - FVECT vgp, cgp, vc; - FVECT v1, v2; - struct beamact bo; - int axmax, j; - double d, avmax; register int i; - /* compute cell center */ - cgp[gcp->w>>1] = gcp->w&1 ? hp->grid[gcp->w>>1] : 0 ; - cgp[hdwg0[gcp->w]] = gcp->i[0] + .5; - cgp[hdwg1[gcp->w]] = gcp->i[1] + .5; - hdworld(org, hp, cgp); - /* compute direction to voxel center */ - for (i = 3; i--; ) - vgp[i] = voxel[cap->vi].i[i] + .5; - hdworld(vc, hp, vgp); - for (i = 3; i--; ) - vc[i] -= org[i]; - /* compute maximum area axis */ - axmax = -1; avmax = 0; - for (i = 3; i--; ) { - d = vgp[i] - cgp[i]; - if (d < 0.) d = -d; - if (d > avmax) { - avmax = d; - axmax = i; - } - } -#ifdef DEBUG - if (axmax < 0.) - error(CONSISTENCY, "botched axis computation in docell"); -#endif - /* compute offset vectors */ - d = 0.5/hp->grid[j=(axmax+1)%3]; - for (i = 3; i--; ) - v1[i] = hp->xv[j][i] * d; - d = 0.5/hp->grid[j=(axmax+2)%3]; - if (DOT(hp->wg[axmax], vc) < 0.) - d = -d; /* reverse vertex order */ - for (i = 3; i--; ) - v2[i] = hp->xv[j][i] * d; - /* compute voxel pyramid */ - for (i = 3; i--; ) { - dir[0][i] = vc[i] - v1[i] - v2[i]; - dir[1][i] = vc[i] + v1[i] - v2[i]; - dir[2][i] = vc[i] + v1[i] + v2[i]; - dir[3][i] = vc[i] - v1[i] + v2[i]; - } - /* visit beams for opposite cells */ - copystruct(&bo.ca, cap); - copystruct(&bo.gc, gcp); - return(visit_cells(org, dir, hp, dobeam, &bo)); -} - -int -doview(cap, vp) /* visit cells for a given view */ -struct cellact *cap; -VIEW *vp; -{ - int orient; - FVECT org, dir[4]; - /* compute view pyramid */ - orient = viewpyramid(org, dir, hdlist[voxel[cap->vi].hd], vp); - if (!orient) - error(INTERNAL, "unusable view in doview"); - cap->rev = orient < 0; - /* visit cells within pyramid */ - return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], docell, cap)); + if (fresh) /* discard old beams? */ + ncbeams = xcbeams = 0; + else /* else clear sample requests */ + for (i = ncbeams+xcbeams; i--; ) + cbeam[i].nr = 0; + cureye.rng = 0.; } -mvview(voxi, vold, vnew) /* move view for a voxel */ -int voxi; -VIEW *vold, *vnew; +beam_view(vn, hr, vr) /* add beam view (if advisable) */ +VIEW *vn; +int hr, vr; { - int netchange = 0; - struct cellact oca, nca; - int ocnt, ncnt; - int nmatch = 0; - GCOORD *ogcl, *ngcl; - register int c; - register GCOORD *ogcp, *ngcp; - /* get old and new cell lists */ - ogcp = ogcl = getviewcells(&ocnt, hdlist[voxel[voxi].hd], vold); - ngcp = ngcl = getviewcells(&ncnt, hdlist[voxel[voxi].hd], vnew); - /* set up actions */ - oca.vi = nca.vi = voxi; - oca.add = 0; nca.add = 1; - if ((oca.rev = ocnt < 0)) - ocnt = -ocnt; - if ((nca.rev = ncnt < 0)) - ncnt = -ncnt; - if (oca.rev == nca.rev) { /* count matches */ - int oc = ocnt, nc = ncnt; - while (oc > 0 & nc > 0) { - c = cellcmp(ogcp, ngcp); - if (c >= 0) { ngcp++; nc--; } - if (c <= 0) { ogcp++; oc--; } - nmatch += c==0; - } - ogcp = ogcl; ngcp = ngcl; + int todo[MAXTODO+1], n; + double hdgsiz, d; + register HOLO *hp; + register int i; + /* sort our list */ + cbeamsort(1); + /* add view to nearby sections */ + if (!(n = comptodo(todo, vn))) + return(0); + for (i = 0; i < n; i++) + addview(todo[i], vn, hr, vr); + if (MEYERNG <= FTINY || vn->type == VT_PAR) + return(1); + hdgsiz = 0.; d = 1./3. / n; /* compute mean grid size */ + for (i = 0; i < n; i++) { + hp = hdlist[todo[i]]; + hdgsiz += d * ( VLEN(hp->xv[0])/hp->grid[0] + + VLEN(hp->xv[1])/hp->grid[1] + + VLEN(hp->xv[2])/hp->grid[2] ) ; } - if (nmatch < ocnt>>1) { /* faster to just delete old cells? */ - for (c = ncbeams; c--; ) - if (cbeam[c].wants & 1<vp); + cureye.rng = MEYERNG * hdgsiz; + } else if ((d = sqrt(dist2(vn->vp,cureye.vpt))) + MEYERNG*hdgsiz > + cureye.rng) { + for (i = 3; i--; ) + cureye.vpt[i] = 0.5*(cureye.vpt[i] + vn->vp[i]); + cureye.rng = 0.5*(cureye.rng + MEYERNG*hdgsiz + d); } - /* add and delete cells in order */ - while (ocnt > 0 & ncnt > 0) - if ((c = cellcmp(ogcp, ngcp)) > 0) { /* new cell */ - netchange += docell(ngcp++, &nca); - ncnt--; - } else if (c < 0) { /* old cell */ - netchange -= docell(ogcp++, &oca); - ocnt--; - } else { /* same cell */ - ogcp++; ocnt--; - ngcp++; ncnt--; - } - /* take care of list tails */ - for ( ; ncnt > 0; ncnt--) - netchange += docell(ngcp++, &nca); - for ( ; ocnt > 0; ocnt--) - netchange -= docell(ogcp++, &oca); - /* clean up */ - if (ogcl != NULL) free((char *)ogcl); - if (ngcl != NULL) free((char *)ngcl); - return(netchange); + return(1); } int -beam_sync() /* synchronize beams on server */ +beam_sync(all) /* update beam list on server */ +int all; { - cbeamop(DR_NEWSET, cbeam, ncbeams, &odev.v, odev.hres, odev.vres); - return(ncbeams); -} - - -beam_view(vn) /* change beam view (if advisable) */ -VIEW *vn; -{ - struct cellact ca; - VOXL vlnew[MAXVOXEL]; - int n, comn; - - if (vn == NULL || !vn->type) { /* clear our beam list */ - set_voxels(vlnew, 0); - cbeamop(DR_DELSET, cbeam, ncbeams, NULL, 0, 0); - ncbeams = 0; - dvw.type = 0; - return(1); - } - /* find our new voxels */ - n = get_voxels(vlnew, vn->vp); - if (dvw.type && !n) { - copystruct(vn, &dvw); /* cancel move */ - return(0); - } - /* set the new voxels */ - comn = set_voxels(vlnew, n); - if (!dvw.type) - comn = 0; - ca.add = 1; /* update our beam list */ - for (ca.vi = n; ca.vi--; ) - if (comn & 1<