--- ray/src/hd/rhdisp2.c 1997/11/21 18:17:37 3.5 +++ 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"; @@ -12,23 +12,28 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rhdisp.h" #include "rhdriver.h" +#ifndef MAXDIST +#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 */ + 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[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 */ @@ -88,6 +93,8 @@ register struct beamcomp *cb1, *cb2; if (!cb1->wants) /* put orphans at the end, unsorted */ return(cb2->wants); + if (!cb2->wants) + return(-1); if ((c = cb1->bi - cb2->bi)) /* sort on beam index first */ return(c); return(cb1->hd - cb2->hd); /* use hd to resolve matches */ @@ -176,6 +183,7 @@ int hr, vr; 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].nc = 0; } serv_request(op, n*sizeof(PACKHEAD), (char *)pa); free((char *)pa); @@ -184,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), @@ -215,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); @@ -249,21 +259,31 @@ 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++; } } - /* 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); + /* check for really stupid move */ + if (bestd > MAXDIST) { + error(COMMAND, "move past outer limits"); + return(0); + } + if (n < MAXVOXEL) + vl[n].hd = -1; + return(n); } @@ -305,34 +325,59 @@ docell(gcp, cap) /* find beams corresponding to cell a GCOORD *gcp; register struct cellact *cap; { + register HOLO *hp = hdlist[voxel[cap->vi].hd]; FVECT org, dir[4]; - FVECT gp, cv[4], vc; + FVECT vgp, cgp, vc; + FVECT v1, v2; struct beamact bo; + int axmax, j; + double d, avmax; register int i; - /* compute cell vertices */ - hdcell(cv, hdlist[voxel[cap->vi].hd], gcp); - /* compute cell and voxel centers */ - for (i = 0; i < 3; i++) { - org[i] = 0.5*(cv[0][i] + cv[2][i]); - gp[i] = voxel[cap->vi].i[i] + 0.5; - } - hdworld(vc, hdlist[voxel[cap->vi].hd], gp); - /* compute voxel pyramid using vector trick */ - for (i = 0; i < 3; i++) { - dir[0][i] = vc[i] - cv[0][i]; /* to 3 */ - dir[2][i] = vc[i] - cv[3][i]; /* to 0 */ - if (gcp->w & 1) { /* watch vertex order! */ - dir[1][i] = vc[i] - cv[2][i]; /* to 1 */ - dir[3][i] = vc[i] - cv[1][i]; /* to 2 */ - } else { - dir[1][i] = vc[i] - cv[1][i]; /* to 2 */ - dir[3][i] = vc[i] - cv[2][i]; /* to 1 */ + /* 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, hdlist[voxel[cap->vi].hd], dobeam, &bo)); + return(visit_cells(org, dir, hp, dobeam, &bo)); } @@ -360,31 +405,48 @@ 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 = getviewcells(&ocnt, hdlist[voxel[voxi].hd], vold); - ngcp = getviewcells(&ncnt, hdlist[voxel[voxi].hd], vnew); + 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.rev = nca.rev = 0; 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; + } + 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) { /* obsolete cell */ + } else if (c < 0) { /* old cell */ netchange -= docell(ogcp++, &oca); ocnt--; - } else { /* same cell? */ - if (oca.rev != nca.rev) /* not */ - netchange += docell(ngcp, &nca) - - docell(ogcp, &oca); + } else { /* same cell */ ogcp++; ocnt--; ngcp++; ncnt--; } @@ -394,41 +456,48 @@ VIEW *vold, *vnew; for ( ; ocnt > 0; ocnt--) netchange -= docell(ogcp++, &oca); /* clean up */ - if (ogcp != NULL) free((char *)ogcp); - if (ngcp != NULL) free((char *)ngcp); + if (ogcl != NULL) free((char *)ogcl); + if (ngcl != NULL) free((char *)ngcl); return(netchange); } +int beam_sync() /* synchronize beams on server */ { cbeamop(DR_NEWSET, cbeam, ncbeams, &odev.v, odev.hres, odev.vres); + return(ncbeams); } -beam_view(vo, vn) /* change beam view */ -VIEW *vo, *vn; +beam_view(vn) /* change beam view (if advisable) */ +VIEW *vn; { struct cellact ca; - VOXL vlnew[8]; + VOXL vlnew[MAXVOXEL]; int n, comn; - if (!vn->type) { /* clear our beam list */ + if (vn == NULL || !vn->type) { /* clear our beam list */ set_voxels(vlnew, 0); cbeamop(DR_DELSET, cbeam, ncbeams, NULL, 0, 0); ncbeams = 0; - return; + 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 (!vo->type) + if (!dvw.type) comn = 0; ca.add = 1; /* update our beam list */ for (ca.vi = n; ca.vi--; ) if (comn & 1<