--- ray/src/hd/rhdisp2.c 1997/12/16 11:57:32 3.16 +++ ray/src/hd/rhdisp2.c 1998/05/14 10:13:50 3.20 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -16,6 +16,8 @@ static char SCCSid[] = "$SunId$ SGI"; #define MAXDIST 42 /* maximum distance outside section */ #endif +#define MAXVOXEL 16 /* maximum number of active voxels */ + extern GCOORD *getviewcells(); static VIEW dvw; /* current view corresponding to beam list */ @@ -25,16 +27,13 @@ typedef struct { int i[3]; /* voxel index (may be outside section) */ } VOXL; /* a voxel */ -static VOXL voxel[8] = { /* current voxel list */ - {-1}, {-1}, {-1}, {-1}, - {-1}, {-1}, {-1}, {-1} -}; +static VOXL voxel[MAXVOXEL] = {{-1}}; /* current voxel list */ #define CBEAMBLK 1024 /* cbeam allocation block size */ static struct beamcomp { - short wants; /* flags telling which voxels want us */ - short hd; /* holodeck section number */ + unsigned short wants; /* flags telling which voxels want us */ + unsigned short hd; /* holodeck section number */ int bi; /* beam index */ } *cbeam = NULL; /* current beam list */ @@ -164,68 +163,6 @@ int adopt; } -cbeamadj(v, hr, vr) /* adjust our beam list */ -VIEW *v; -int hr, vr; -{ - PACKHEAD *pa, *pa2; - register PACKHEAD *pp; - int n, n2; - FVECT gp; - int igp[3], vcflgs; - register int i; - /* figure out center voxel(s) */ - n = -1; vcflgs = 0; - for (i = 0; i < 8 && voxel[i].hd >= 0; i++) { - if (voxel[i].hd != n) { - hdgrid(gp, hdlist[n=voxel[i].hd], v->vp); - igp[0] = gp[0]; igp[1] = gp[1]; igp[2] = gp[2]; - } - if (voxel[i].i[0] == igp[0] && voxel[i].i[1] == igp[1] && - voxel[i].i[2] == igp[2]) - vcflgs |= 1<hd = cbeam[ncbeams+i].hd; - pp->bi = cbeam[ncbeams+i].bi; - pp->nr = npixels(v, hr, vr, hdlist[pp->hd], pp->bi) + 1; - pp->nc = 0; - } - n = xcbeams - n2; - /* now sort list for deletions */ - cbeamsort(0); - pa2 = (PACKHEAD *)realloc((char *)pa2, (n+n2+xcbeams)*sizeof(PACKHEAD)); - if (n+n2+xcbeams && pa2 == NULL) - goto memerr; - pa = pa2 + n2; - for (i = xcbeams; i--; ) { - pp = pa + n++; - pp->hd = cbeam[ncbeams+i].hd; - pp->bi = cbeam[ncbeams+i].bi; - pp->nr = 0; - pp->nc = 0; - } - if (n) /* adjust the set */ - serv_request(DR_ADJSET, n*sizeof(PACKHEAD), (char *)pa); - if (n2) /* make secondary additions */ - serv_request(DR_ADDSET, n2*sizeof(PACKHEAD), (char *)pa2); - xcbeams = 0; /* clean up */ - free((char *)pa2); - return; -memerr: - error(SYSTEM, "out of memory in cbeamadj"); -} - - cbeamop(op, bl, n, v, hr, vr) /* update beams on server list */ int op; register struct beamcomp *bl; @@ -255,16 +192,16 @@ int hr, vr; int set_voxels(vl, n) /* set new voxel array */ -VOXL vl[8]; +VOXL vl[MAXVOXEL]; int n; { short wmap[256]; - int vmap[8]; + int vmap[MAXVOXEL]; int comn = 0; int no; register int i, j; /* find common voxels */ - for (j = 0; j < 8 && voxel[j].hd >= 0; j++) { + 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), @@ -286,28 +223,30 @@ int n; cbeam[i].wants = wmap[cbeam[i].wants]; /* update our voxel list */ bcopy((char *)vl, (char *)voxel, n*sizeof(VOXL)); - for (j = n; j < 8; j++) - voxel[j].hd = -1; + if (n < MAXVOXEL) + voxel[n].hd = -1; return(comn); /* return bit array of common voxels */ } int get_voxels(vl, vp) /* find voxels corresponding to view point */ -VOXL vl[8]; +VOXL vl[MAXVOXEL]; FVECT vp; { - static int lastn = 0, lastd = -1; - int n = 0; + int first, n; FVECT gp; double d; int dist, bestd = 0x7fff; VOXL vox; register int i, j, k; + /* count voxels in list already */ + for (first = 0; first < MAXVOXEL && vl[first].hd >= 0; first++) + ; /* find closest voxels */ - for (i = 0; n < 8 && hdlist[i]; i++) { + for (n = first, i = 0; n < MAXVOXEL && hdlist[i]; i++) { hdgrid(gp, hdlist[i], vp); - for (j = 0; n < 8 && j < 8; j++) { + for (j = 0; n < MAXVOXEL && j < 8; j++) { dist = 0; for (k = 0; k < 3; k++) { d = gp[k] - .5 + (j>>k & 1); @@ -320,9 +259,18 @@ FVECT vp; if (dist > bestd) /* others were closer */ continue; if (dist < bestd) { /* start closer list */ - n = 0; + 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++; @@ -333,13 +281,9 @@ FVECT vp; error(COMMAND, "move past outer limits"); return(0); } - /* warn of dangerous moves */ - if (n < lastn && bestd >= lastd) - error(WARNING, "moving outside holodeck section"); - else if (n > lastn && bestd <= lastd) - error(WARNING, "moving inside holodeck section"); - lastd = bestd; - return(lastn = n); + if (n < MAXVOXEL) + vl[n].hd = -1; + return(n); } @@ -391,8 +335,8 @@ register struct cellact *cap; register int i; /* compute cell center */ cgp[gcp->w>>1] = gcp->w&1 ? hp->grid[gcp->w>>1] : 0 ; - cgp[((gcp->w>>1)+1)%3] = gcp->i[0] + .5; - cgp[((gcp->w>>1)+2)%3] = gcp->i[1] + .5; + 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--; ) @@ -419,8 +363,8 @@ register struct cellact *cap; for (i = 3; i--; ) v1[i] = hp->xv[j][i] * d; d = 0.5/hp->grid[j=(axmax+2)%3]; - if (DOT(hp->wn[axmax], vc) < 0.) - d = -d; /* reverse vertex ordering */ + 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 */ @@ -461,8 +405,9 @@ VIEW *vold, *vnew; int netchange = 0; struct cellact oca, nca; int ocnt, ncnt; - int c; + 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); @@ -474,18 +419,37 @@ VIEW *vold, *vnew; ocnt = -ocnt; if ((nca.rev = ncnt < 0)) ncnt = -ncnt; - if (nca.rev == oca.rev) /* 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--; + 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; + } + if (nmatch < ocnt>>1) { /* faster to just delete old cells? */ + for (c = ncbeams; c--; ) + if (cbeam[c].wants & 1< 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); @@ -510,7 +474,7 @@ beam_view(vn) /* change beam view (if advisable) */ VIEW *vn; { struct cellact ca; - VOXL vlnew[8]; + VOXL vlnew[MAXVOXEL]; int n, comn; if (vn == NULL || !vn->type) { /* clear our beam list */ @@ -536,9 +500,6 @@ VIEW *vn; mvview(ca.vi, &dvw, vn); else /* else add all new cells */ doview(&ca, vn); -#if 1 - cbeamadj(vn, odev.hres, odev.vres); -#else /* inform server of new beams */ cbeamop(DR_ADDSET, cbeam+ncbeams, xcbeams, vn, odev.hres, odev.vres); /* sort list to put orphans at end */ @@ -546,7 +507,6 @@ VIEW *vn; /* tell server to delete orphans */ cbeamop(DR_DELSET, cbeam+ncbeams, xcbeams, NULL, 0, 0); xcbeams = 0; /* truncate our list */ -#endif copystruct(&dvw, vn); /* record new view */ return(1); }