--- ray/src/hd/rhdisp3.c 1997/11/20 16:43:42 3.3 +++ ray/src/hd/rhdisp3.c 1997/11/21 18:17:37 3.4 @@ -12,7 +12,12 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rhdisp.h" #include "view.h" +struct cellist { + GCOORD *cl; + int n; +}; + int npixels(vp, hr, vr, hp, bi) /* compute appropriate number to evaluate */ VIEW *vp; @@ -20,6 +25,8 @@ int hr, vr; HOLO *hp; int bi; { + static VIEW vdo, vlast; + static HOLO *hplast; GCOORD gc[2]; FVECT cp[4]; FVECT ip[4]; @@ -28,9 +35,21 @@ int bi; /* compute cell corners in image */ if (!hdbcoord(gc, hp, bi)) error(CONSISTENCY, "bad beam index in npixels"); - hdcell(cp, hp, gc+1); + /* has holodeck or view changed? */ + if (hp != hplast || bcmp((char *)vp, (char *)&vlast, sizeof(VIEW))) { + copystruct(&vdo, vp); + if (sect_behind(hp, &vdo)) { /* reverse view sense */ + vdo.vdir[0] = -vdo.vdir[0]; + vdo.vdir[1] = -vdo.vdir[1]; + vdo.vdir[2] = -vdo.vdir[2]; + setview(&vdo); + } + hplast = hp; + copystruct(&vlast, vp); + } + hdcell(cp, hp, gc+1); /* find cell on image */ for (i = 0; i < 4; i++) { - viewloc(ip[i], vp, cp[i]); + viewloc(ip[i], &vdo, cp[i]); if (ip[i][2] < 0.) return(0); ip[i][0] *= (double)hr; /* scale by resolution */ @@ -118,13 +137,66 @@ char *dp; } +sect_behind(hp, vp) /* check if section is "behind" viewpoint */ +register HOLO *hp; +register VIEW *vp; +{ + FVECT hcent; + /* compute holodeck section center */ + VSUM(hcent, hp->orig, hp->xv[0], 0.5); + VSUM(hcent, hcent, hp->xv[1], 0.5); + VSUM(hcent, hcent, hp->xv[2], 0.5); + /* behind if center is behind */ + return(DOT(vp->vdir,hcent) < DOT(vp->vdir,vp->vp)); +} + + +viewpyramid(org, dir, hp, vp) /* compute view pyramid */ +FVECT org, dir[4]; +HOLO *hp; +VIEW *vp; +{ + register int i; + /* check view type */ + if (vp->type == VT_PAR) + return(0); + /* in front or behind? */ + if (!sect_behind(hp, vp)) { + if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) + return(0); + if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) + return(0); + if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) + return(0); + if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) + return(0); + return(1); + } /* reverse pyramid */ + if (viewray(org, dir[3], vp, 0., 0.) < -FTINY) + return(0); + if (viewray(org, dir[2], vp, 0., 1.) < -FTINY) + return(0); + if (viewray(org, dir[1], vp, 1., 1.) < -FTINY) + return(0); + if (viewray(org, dir[0], vp, 1., 0.) < -FTINY) + return(0); + for (i = 0; i < 3; i++) { + dir[0][i] = -dir[0][i]; + dir[1][i] = -dir[1][i]; + dir[2][i] = -dir[2][i]; + dir[3][i] = -dir[3][i]; + } + return(-1); +} + + int addcell(gcp, cl) /* add a cell to a list */ GCOORD *gcp; -register int *cl; +register struct cellist *cl; { - copystruct((GCOORD *)(cl+1) + *cl, gcp); - (*cl)++; + copystruct(cl->cl+cl->n, gcp); + cl->n++; return(1); } @@ -143,50 +215,46 @@ register GCOORD *gcp1, *gcp2; } -int * -getviewcells(hp, vp) /* get ordered cell list for section view */ +GCOORD * +getviewcells(np, hp, vp) /* get ordered cell list for section view */ +int *np; /* returned number of cells (negative if reversed) */ register HOLO *hp; VIEW *vp; { FVECT org, dir[4]; - int n; - register int *cl; + int orient; + struct cellist cl; /* 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; + *np = 0; + orient = viewpyramid(org, dir, hp, vp); + if (!orient) + return(NULL); /* allocate enough list space */ - n = 2*( hp->grid[0]*hp->grid[1] + - hp->grid[0]*hp->grid[2] + - hp->grid[1]*hp->grid[2] ); - cl = (int *)malloc(sizeof(int) + n*sizeof(GCOORD)); - if (cl == NULL) + cl.n = 2*( hp->grid[0]*hp->grid[1] + + hp->grid[0]*hp->grid[2] + + hp->grid[1]*hp->grid[2] ); + cl.cl = (GCOORD *)malloc(cl.n*sizeof(GCOORD)); + if (cl.cl == NULL) goto memerr; - *cl = 0; - /* add cells within pyramid */ - visit_cells(org, dir, hp, addcell, cl); - if (!*cl) { - free((char *)cl); + cl.n = 0; /* add cells within pyramid */ + visit_cells(org, dir, hp, addcell, &cl); + if (!cl.n) { + free((char *)cl.cl); return(NULL); } + *np = cl.n * orient; #if 0 /* We're just going to free this memory in a moment, and list is * sorted automatically by visit_cells(), so we don't need this. */ - if (*cl < n) { /* optimize memory use */ - cl = (int *)realloc((char *)cl, - sizeof(int) + *cl*sizeof(GCOORD)); - if (cl == NULL) - goto memerr; - } + /* optimize memory use */ + cl.cl = (GCOORD *)realloc((char *)cl.cl, cl.n*sizeof(GCOORD)); + if (cl.cl == NULL) + goto memerr; /* sort the list */ - qsort((char *)(cl+1), *cl, sizeof(GCOORD), cellcmp); + qsort((char *)cl.cl, cl.n, sizeof(GCOORD), cellcmp); #endif - return(cl); -viewerr: - error(INTERNAL, "unusable view in getviewcells"); + return(cl.cl); memerr: error(SYSTEM, "out of memory in getviewcells"); }