--- ray/src/hd/rhdisp2.c 1997/11/20 14:46:07 3.2 +++ ray/src/hd/rhdisp2.c 1998/05/22 15:12:36 3.22 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -12,24 +12,25 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rhdisp.h" #include "rhdriver.h" -extern int *getviewcells(); +#ifndef MAXDIST +#define MAXDIST 42 /* maximum distance outside section */ +#endif +#define MAXVOXEL 32 /* maximum number of active voxels */ + 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 */ - int bi; /* beam index */ + int hd; /* holodeck section number */ + int4 bi; /* beam index */ + int4 nr; /* number of samples desired */ } *cbeam = NULL; /* current beam list */ static int ncbeams = 0; /* number of sorted beams in cbeam */ @@ -38,7 +39,9 @@ static int maxcbeam = 0; /* size of cbeam array */ struct cellact { short vi; /* voxel index */ - short add; /* zero means delete */ + short rev; /* reverse ray direction? */ + VIEW *vp; /* view for image */ + short hr, vr; /* image resolution */ }; /* action for cell */ struct beamact { @@ -85,8 +88,10 @@ register struct beamcomp *cb1, *cb2; { register int c; - if (!cb1->wants) /* put orphans at the end, unsorted */ - return(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); return(cb1->hd - cb2->hd); /* use hd to resolve matches */ @@ -102,7 +107,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) @@ -130,7 +135,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); } @@ -148,18 +153,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; } -cbeamadd(bl, n, v, hr, vr) /* add beams to 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; @@ -168,93 +172,35 @@ int hr, vr; 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); - 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"); + 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 = 0; /* removes any request */ + pa[i].nr = bl[i].nr; + pa[i].nc = 0; } - serv_request(DR_DELSET, n*sizeof(PACKHEAD), (char *)pa); + serv_request(op, n*sizeof(PACKHEAD), (char *)pa); free((char *)pa); } -int -set_voxels(vl, n) /* set new voxel array */ -VOXL vl[8]; -int n; -{ - short wmap[256]; - int vmap[8]; - int comn = 0; - int no; - register int i, j; - /* find common voxels */ - for (j = 0; j < 8 && 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 (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); @@ -267,15 +213,34 @@ FVECT vp; if (dist > bestd) /* others were closer */ continue; if (dist < bestd) { /* start closer list */ - n = 0; + n = first; bestd = dist; + rfl = 0; } + /* check if already in list */ + for (k = first; k--; ) + if (voxel[k].hd == i && + voxel[k].i[0] == vox.i[0] && + voxel[k].i[1] == vox.i[1] && + voxel[k].i[2] == vox.i[2]) + break; + if (k >= 0) { + rfl |= 1< MAXDIST) { + error(COMMAND, "move past outer limits"); + return(0); + } + if (n < MAXVOXEL) + voxel[n].hd = -1; + return(rfl); } @@ -285,24 +250,24 @@ GCOORD *gcp; register struct beamact *bp; { GCOORD gc[2]; - int bi, i; + int bi, i, n; /* 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 */ - 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); + /* add it in */ + i = getcbeam(voxel[bp->ca.vi].hd, bi); + n = npixels(bp->ca.vp, bp->ca.hr, bp->ca.vr, + hdlist[cbeam[i].hd], cbeam[i].bi); + if (n > cbeam[i].nr) + cbeam[i].nr = n; + return(i == ncbeams+xcbeams-1); /* return 1 if totally new */ } @@ -312,135 +277,119 @@ 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)); } int -doview(cap, vp) /* visit cells for a given view */ +doview(cap) /* 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], cap->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"); } -mvview(voxi, vold, vnew) /* move view for a voxel */ -int voxi; -VIEW *vold, *vnew; +beam_init() /* clear beam list for new view(s) */ { - int netchange = 0; - int *ocl, *ncl; - struct cellact ca; - int ocnt, ncnt; - int c; - 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--; - } - /* 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); - /* clean up */ - if (ocl != NULL) free((char *)ocl); - if (ncl != NULL) free((char *)ncl); - return(netchange); + register int i; + /* clear desire flags */ + for (i = ncbeams+xcbeams; i--; ) + cbeam[i].nr = 0; + voxel[0].hd = -1; /* clear voxel list */ } -beam_view(vo, vn) /* change beam view */ -VIEW *vo, *vn; +beam_view(vn, hr, vr) /* add beam view (if advisable) */ +VIEW *vn; +int hr, vr; { struct cellact ca; - VOXL vlnew[8]; - int n, comn; + unsigned int4 vfl; + /* sort our list */ + cbeamsort(1); + /* add new voxels */ + vfl = add_voxels(vn->vp); + if (!vfl) + return(0); + ca.vp = vn; ca.hr = hr; ca.vr = vr; + /* update our beam list */ + for (ca.vi = 0; vfl; vfl >>= 1, ca.vi++) + if (vfl & 1) + doview(&ca); + return(1); +} - if (!vn->type) { /* clear our beam list */ - set_voxels(vlnew, 0); - cbeamdel(cbeam, ncbeams); - ncbeams = 0; - return; - } - /* find our new voxels */ - n = get_voxels(vlnew, vn->vp); - /* set the new voxels */ - comn = set_voxels(vlnew, n); - if (!vo->type) - comn = 0; - ca.add = 1; /* update our beam list */ - for (ca.vi = n; ca.vi--; ) - if (comn & 1<