--- ray/src/hd/rhdisp2.c 1997/11/20 14:46:07 3.2 +++ ray/src/hd/rhdisp2.c 1997/12/05 15:50:43 3.9 @@ -12,8 +12,14 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rhdisp.h" #include "rhdriver.h" -extern int *getviewcells(); +#ifndef MAXDIST +#define MAXDIST 13 /* 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 { @@ -155,33 +162,48 @@ int adopt; } -cbeamadd(bl, n, v, hr, vr) /* add beams to server list */ -register struct beamcomp *bl; -int n; +cbeamadj(v, hr, vr) /* adjust our beam list */ VIEW *v; int hr, vr; { 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 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); + int n; + /* first handle additions */ + pa = (PACKHEAD *)malloc(xcbeams*sizeof(PACKHEAD)); + if (xcbeams && pa == NULL) + goto memerr; + for (i = xcbeams; i--; ) { + pa[i].hd = cbeam[ncbeams+i].hd; + pa[i].bi = cbeam[ncbeams+i].bi; + pa[i].nr = npixels(v, hr, vr, hdlist[pa[i].hd], pa[i].bi); + } + n = xcbeams; /* now sort list for deletions */ + cbeamsort(0); + pa = (PACKHEAD *)realloc((char *)pa, (n+xcbeams)*sizeof(PACKHEAD)); + if (n+xcbeams && pa == NULL) + goto memerr; + for (i = xcbeams; i--; ) { + pa[n+i].hd = cbeam[ncbeams+i].hd; + pa[n+i].bi = cbeam[ncbeams+i].bi; + pa[n+i].nr = 0; + } + n += xcbeams; + xcbeams = 0; /* delete orphans */ + serv_request(DR_ADJSET, n*sizeof(PACKHEAD), (char *)pa); free((char *)pa); + return; +memerr: + error(SYSTEM, "out of memory in cbeamadj"); } -cbeamdel(bl, n) /* delete unwanted beam requests */ +cbeamop(op, bl, n, v, hr, vr) /* update beams on server list */ +int op; register struct beamcomp *bl; int n; +VIEW *v; +int hr, vr; { register PACKHEAD *pa; register int i; @@ -190,13 +212,14 @@ int n; return; pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD)); if (pa == NULL) - error(SYSTEM, "out of memory in cbeamdel"); + error(SYSTEM, "out of memory in cbeamadd"); 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); } - serv_request(DR_DELSET, n*sizeof(PACKHEAD), (char *)pa); + serv_request(op, n*sizeof(PACKHEAD), (char *)pa); free((char *)pa); } @@ -245,6 +268,7 @@ 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; @@ -275,7 +299,18 @@ FVECT vp; n++; } } - return(n); + /* check for really stupid move */ + if (bestd > MAXDIST) { + error(WARNING, "move to outer solar system"); + 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); } @@ -287,8 +322,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 */ @@ -348,17 +388,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"); } @@ -367,80 +405,93 @@ 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.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 (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; +beam_sync() /* synchronize beams on server */ { + cbeamop(DR_NEWSET, cbeam, ncbeams, &odev.v, odev.hres, odev.vres); +} + + +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 */ set_voxels(vlnew, 0); - cbeamdel(cbeam, ncbeams); + cbeamop(DR_DELSET, cbeam, ncbeams, NULL, 0, 0); ncbeams = 0; - return; + copystruct(&dvw, vn); + 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<