--- ray/src/hd/rhdisp2.c 1997/11/20 18:06:35 3.3 +++ ray/src/hd/rhdisp2.c 1998/01/01 13:00:16 3.17 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -12,8 +12,14 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rhdisp.h" #include "rhdriver.h" -extern int *getviewcells(); +#ifndef MAXDIST +#define MAXDIST 42 /* maximum distance outside section */ +#endif +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) */ @@ -39,6 +45,7 @@ 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 { @@ -87,6 +94,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 */ @@ -155,7 +164,8 @@ int adopt; } -cbeamadd(bl, n, v, hr, vr) /* add beams to server list */ +cbeamop(op, bl, n, v, hr, vr) /* update beams on server list */ +int op; register struct beamcomp *bl; int n; VIEW *v; @@ -169,34 +179,14 @@ int hr, vr; pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD)); if (pa == NULL) error(SYSTEM, "out of memory in cbeamadd"); - for (i = 0; i < n; i++) - pa[i].nr = npixels(v, hr, vr, - hdlist[pa[i].hd=bl[i].hd], - pa[i].bi=bl[i].bi) / 8; - - serv_request(DR_ADDSET, n*sizeof(PACKHEAD), (char *)pa); - free((char *)pa); -} - - -cbeamdel(bl, n) /* delete unwanted beam requests */ -register struct beamcomp *bl; -int n; -{ - register PACKHEAD *pa; - register int i; - - if (n <= 0) - return; - pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD)); - if (pa == NULL) - error(SYSTEM, "out of memory in cbeamdel"); for (i = 0; i < n; i++) { pa[i].hd = bl[i].hd; pa[i].bi = bl[i].bi; - pa[i].nr = 0; /* removes any request */ + pa[i].nr = v==NULL ? 0 : + npixels(v, hr, vr, hdlist[bl[i].hd], bl[i].bi); + pa[i].nc = 0; } - serv_request(DR_DELSET, n*sizeof(PACKHEAD), (char *)pa); + serv_request(op, n*sizeof(PACKHEAD), (char *)pa); free((char *)pa); } @@ -245,7 +235,6 @@ get_voxels(vl, vp) /* find voxels corresponding to vie VOXL vl[8]; FVECT vp; { - static int lastn = 0, lastd = -1; int n = 0; FVECT gp; double d; @@ -276,13 +265,12 @@ FVECT vp; 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); + } + return(n); } @@ -294,8 +282,13 @@ register struct beamact *bp; GCOORD gc[2]; int bi, i; /* compute beam index */ - copystruct(gc, gcp); - copystruct(gc+1, &bp->gc); + if (bp->ca.rev) { + copystruct(gc, &bp->gc); + copystruct(gc+1, gcp); + } else { + copystruct(gc, gcp); + copystruct(gc+1, &bp->gc); + } 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 */ @@ -319,34 +312,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[((gcp->w>>1)+1)%3] = gcp->i[0] + .5; + cgp[((gcp->w>>1)+2)%3] = 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->wn[axmax], vc) < 0.) + d = -d; /* reverse vertex ordering */ + 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)); } @@ -355,17 +373,15 @@ doview(cap, vp) /* visit cells for a given view */ struct cellact *cap; VIEW *vp; { + int orient; FVECT org, dir[4]; /* compute view pyramid */ - if (vp->type == VT_PAR) goto viewerr; - if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr; - if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr; - if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr; - if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr; + 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)); -viewerr: - error(INTERNAL, "unusable view in doview"); } @@ -374,80 +390,90 @@ int voxi; VIEW *vold, *vnew; { int netchange = 0; - int *ocl, *ncl; - struct cellact ca; + struct cellact oca, nca; int ocnt, ncnt; int c; + GCOORD *ogcl, *ngcl; register GCOORD *ogcp, *ngcp; /* get old and new cell lists */ - ocl = getviewcells(hdlist[voxel[voxi].hd], vold); - ncl = getviewcells(hdlist[voxel[voxi].hd], vnew); - if (ocl != NULL) { - ocnt = *ocl; ogcp = (GCOORD *)(ocl+1); - } else { - ocnt = 0; ogcp = NULL; - } - if (ncl != NULL) { - ncnt = *ncl; ngcp = (GCOORD *)(ncl+1); - } else { - ncnt = 0; ngcp = NULL; - } - ca.vi = voxi; /* add and delete cells */ - while (ocnt > 0 & ncnt > 0) - if ((c = cellcmp(ogcp, ngcp)) > 0) { - ca.add = 1; /* new cell */ - netchange += docell(ngcp++, &ca); - ncnt--; - } else if (c < 0) { - ca.add = 0; /* obsolete cell */ - netchange -= docell(ogcp++, &ca); - ocnt--; - } else { - ogcp++; ocnt--; /* unchanged cell */ - ngcp++; ncnt--; - } + 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 (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--; + } /* take care of list tails */ - for (ca.add = 1; ncnt > 0; ncnt--) - netchange += docell(ngcp++, &ca); - for (ca.add = 0; ocnt > 0; ocnt--) - netchange -= docell(ogcp++, &ca); + for ( ; ncnt > 0; ncnt--) + netchange += docell(ngcp++, &nca); + for ( ; ocnt > 0; ocnt--) + netchange -= docell(ogcp++, &oca); /* clean up */ - if (ocl != NULL) free((char *)ocl); - if (ncl != NULL) free((char *)ncl); + if (ogcl != NULL) free((char *)ogcl); + if (ngcl != NULL) free((char *)ngcl); return(netchange); } -beam_view(vo, vn) /* change beam view */ -VIEW *vo, *vn; +int +beam_sync() /* synchronize beams on server */ { + 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[8]; int n, comn; - if (!vn->type) { /* clear our beam list */ + if (vn == NULL || !vn->type) { /* clear our beam list */ set_voxels(vlnew, 0); - cbeamdel(cbeam, ncbeams); + 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<