ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp2.c
(Generate patch)

Comparing ray/src/hd/rhdisp2.c (file contents):
Revision 3.1 by gregl, Wed Nov 19 18:01:03 1997 UTC vs.
Revision 3.20 by gwlarson, Thu May 14 10:13:50 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
8 < * Holodeck beam tracking
8 > * Holodeck beam tracking for display process
9   */
10  
11   #include "rholo.h"
12   #include "rhdisp.h"
13   #include "rhdriver.h"
14  
15 < extern int      *getviewcells();
15 > #ifndef MAXDIST
16 > #define MAXDIST         42      /* maximum distance outside section */
17 > #endif
18  
19 + #define MAXVOXEL        16      /* maximum number of active voxels */
20 +
21 + extern GCOORD   *getviewcells();
22 +
23 + static VIEW     dvw;            /* current view corresponding to beam list */
24 +
25   typedef struct {
26 <        int     hd;             /* holodeck section number */
26 >        int     hd;             /* holodeck section number (-1 if inactive) */
27          int     i[3];           /* voxel index (may be outside section) */
28   } VOXL;                 /* a voxel */
29  
30 < static VOXL voxel[8] = {        /* current voxel list */
23 <        {-1}, {-1}, {-1}, {-1},
24 <        {-1}, {-1}, {-1}, {-1}
25 < };
30 > static VOXL voxel[MAXVOXEL] = {{-1}};   /* current voxel list */
31  
32   #define CBEAMBLK        1024    /* cbeam allocation block size */
33  
34   static struct beamcomp {
35 <        short   wants;          /* flags telling which voxels want us */
36 <        short   hd;             /* holodeck section number */
35 >        unsigned short  wants;  /* flags telling which voxels want us */
36 >        unsigned short  hd;     /* holodeck section number */
37          int     bi;             /* beam index */
38   } *cbeam = NULL;        /* current beam list */
39  
# Line 36 | Line 41 | static int     ncbeams = 0;    /* number of sorted beams in c
41   static int      xcbeams = 0;    /* extra (unregistered) beams past ncbeams */
42   static int      maxcbeam = 0;   /* size of cbeam array */
43  
39
44   struct cellact {
45          short   vi;             /* voxel index */
46          short   add;            /* zero means delete */
47 +        short   rev;            /* reverse ray direction? */
48   };              /* action for cell */
49  
45
50   struct beamact {
51          struct cellact  ca;     /* cell action */
52          GCOORD  gc;             /* grid coordinate */
# Line 89 | Line 93 | register struct beamcomp       *cb1, *cb2;
93  
94          if (!cb1->wants)                /* put orphans at the end, unsorted */
95                  return(cb2->wants);
96 +        if (!cb2->wants)
97 +                return(-1);
98          if ((c = cb1->bi - cb2->bi))    /* sort on beam index first */
99                  return(c);
100          return(cb1->hd - cb2->hd);      /* use hd to resolve matches */
# Line 157 | Line 163 | int    adopt;
163   }
164  
165  
166 < cbeamadd(bl, n, v, hr, vr)      /* add beams to server list */
166 > cbeamop(op, bl, n, v, hr, vr)   /* update beams on server list */
167 > int     op;
168   register struct beamcomp        *bl;
169   int     n;
170   VIEW    *v;
# Line 171 | Line 178 | int    hr, vr;
178          pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD));
179          if (pa == NULL)
180                  error(SYSTEM, "out of memory in cbeamadd");
174        for (i = 0; i < n; i++)
175                pa[i].nr = npixels(v, hr, vr,
176                                hdlist[pa[i].hd=bl[i].hd],
177                                pa[i].bi=bl[i].bi) / 8;
178
179        serv_request(DR_ADDSET, n*sizeof(PACKHEAD), (char *)pa);
180        free((char *)pa);
181 }
182
183
184 cbeamdel(bl, n)         /* delete unwanted beam requests */
185 register struct beamcomp        *bl;
186 int     n;
187 {
188        register PACKHEAD       *pa;
189        register int    i;
190
191        if (n <= 0)
192                return;
193        pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD));
194        if (pa == NULL)
195                error(SYSTEM, "out of memory in cbeamdel");
181          for (i = 0; i < n; i++) {
182                  pa[i].hd = bl[i].hd;
183                  pa[i].bi = bl[i].bi;
184 <                pa[i].nr = 0;           /* removes any request */
184 >                pa[i].nr = v==NULL ? 0 :
185 >                                npixels(v, hr, vr, hdlist[bl[i].hd], bl[i].bi);
186 >                pa[i].nc = 0;
187          }
188 <        serv_request(DR_DELSET, n*sizeof(PACKHEAD), (char *)pa);
188 >        serv_request(op, n*sizeof(PACKHEAD), (char *)pa);
189          free((char *)pa);
190   }
191  
192  
193   int
194   set_voxels(vl, n)       /* set new voxel array */
195 < VOXL    vl[8];
195 > VOXL    vl[MAXVOXEL];
196   int     n;
197   {
198          short   wmap[256];
199 <        int     vmap[8];
199 >        int     vmap[MAXVOXEL];
200          int     comn = 0;
201          int     no;
202          register int    i, j;
203                                          /* find common voxels */
204 <        for (j = 0; j < 8 && voxel[j].hd >= 0; j++) {
204 >        for (j = 0; j < MAXVOXEL && voxel[j].hd >= 0; j++) {
205                  vmap[j] = -1;
206                  for (i = n; i--; )
207                          if (!bcmp((char *)(vl+i), (char *)(voxel+j),
# Line 236 | Line 223 | int    n;
223                  cbeam[i].wants = wmap[cbeam[i].wants];
224                                          /* update our voxel list */
225          bcopy((char *)vl, (char *)voxel, n*sizeof(VOXL));
226 <        for (j = n; j < 8; j++)
227 <                voxel[j].hd = -1;
226 >        if (n < MAXVOXEL)
227 >                voxel[n].hd = -1;
228          return(comn);                   /* return bit array of common voxels */
229   }
230  
231  
232   int
233   get_voxels(vl, vp)      /* find voxels corresponding to view point */
234 < VOXL    vl[8];
234 > VOXL    vl[MAXVOXEL];
235   FVECT   vp;
236   {
237 <        int     n = 0;
237 >        int     first, n;
238          FVECT   gp;
239          double  d;
240          int     dist, bestd = 0x7fff;
241          VOXL    vox;
242          register int    i, j, k;
243 +                                        /* count voxels in list already */
244 +        for (first = 0; first < MAXVOXEL && vl[first].hd >= 0; first++)
245 +                ;
246                                          /* find closest voxels */
247 <        for (i = 0; n < 8 && hdlist[i]; i++) {
247 >        for (n = first, i = 0; n < MAXVOXEL && hdlist[i]; i++) {
248                  hdgrid(gp, hdlist[i], vp);
249 <                for (j = 0; n < 8 && j < 8; j++) {
249 >                for (j = 0; n < MAXVOXEL && j < 8; j++) {
250                          dist = 0;
251                          for (k = 0; k < 3; k++) {
252                                  d = gp[k] - .5 + (j>>k & 1);
# Line 269 | Line 259 | FVECT  vp;
259                          if (dist > bestd)       /* others were closer */
260                                  continue;
261                          if (dist < bestd) {     /* start closer list */
262 <                                n = 0;
262 >                                n = first;
263                                  bestd = dist;
264                          }
265 +                                                /* check if already in list */
266 +                        for (k = first; k--; )
267 +                                if (vl[k].hd == i &&
268 +                                                vl[k].i[0] == vox.i[0] &&
269 +                                                vl[k].i[1] == vox.i[1] &&
270 +                                                vl[k].i[2] == vox.i[2])
271 +                                        break;
272 +                        if (k >= 0)
273 +                                continue;
274                          vox.hd = i;             /* add this voxel */
275                          copystruct(&vl[n], &vox);
276                          n++;
277                  }
278          }
279 +                                        /* check for really stupid move */
280 +        if (bestd > MAXDIST) {
281 +                error(COMMAND, "move past outer limits");
282 +                return(0);
283 +        }
284 +        if (n < MAXVOXEL)
285 +                vl[n].hd = -1;
286          return(n);
287   }
288  
# Line 289 | Line 295 | register struct beamact        *bp;
295          GCOORD  gc[2];
296          int     bi, i;
297                                          /* compute beam index */
298 <        copystruct(gc, gcp);
299 <        copystruct(gc+1, &bp->gc);
298 >        if (bp->ca.rev) {
299 >                copystruct(gc, &bp->gc);
300 >                copystruct(gc+1, gcp);
301 >        } else {
302 >                copystruct(gc, gcp);
303 >                copystruct(gc+1, &bp->gc);
304 >        }
305          if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0)
306 <                return(0);              /* should report an error? */
306 >                error(CONSISTENCY, "bad grid coordinate in dobeam");
307          if (bp->ca.add) {               /* add it in */
308                  i = getcbeam(voxel[bp->ca.vi].hd, bi);
309                  cbeam[i].wants |= 1<<bp->ca.vi; /* say we want it */
# Line 314 | Line 325 | docell(gcp, cap)       /* find beams corresponding to cell a
325   GCOORD  *gcp;
326   register struct cellact *cap;
327   {
328 +        register HOLO   *hp = hdlist[voxel[cap->vi].hd];
329          FVECT   org, dir[4];
330 <        FVECT   gp, cv[4], vc;
330 >        FVECT   vgp, cgp, vc;
331 >        FVECT   v1, v2;
332          struct beamact  bo;
333 +        int     axmax, j;
334 +        double  d, avmax;
335          register int    i;
336 <                                /* compute cell vertices */
337 <        hdcell(cv, hdlist[voxel[cap->vi].hd], gcp);
338 <                                /* compute cell and voxel centers */
339 <        for (i = 0; i < 3; i++) {
340 <                org[i] = 0.5*(cv[0][i] + cv[2][i]);
341 <                gp[i] = voxel[cap->vi].i[i] + 0.5;
342 <        }
343 <        hdworld(vc, hdlist[voxel[cap->vi].hd], gp);
344 <                                /* compute voxel pyramid using vector trick */
345 <        for (i = 0; i < 3; i++) {
346 <                dir[0][i] = vc[i] - cv[0][i];           /* to 3 */
347 <                dir[2][i] = vc[i] - cv[3][i];           /* to 0 */
348 <                if (gcp->w & 1) {       /* watch vertex order! */
349 <                        dir[1][i] = vc[i] - cv[2][i];   /* to 1 */
350 <                        dir[3][i] = vc[i] - cv[1][i];   /* to 2 */
351 <                } else {
352 <                        dir[1][i] = vc[i] - cv[1][i];   /* to 2 */
353 <                        dir[3][i] = vc[i] - cv[2][i];   /* to 1 */
336 >                                /* compute cell center */
337 >        cgp[gcp->w>>1] = gcp->w&1 ? hp->grid[gcp->w>>1] : 0 ;
338 >        cgp[hdwg0[gcp->w]] = gcp->i[0] + .5;
339 >        cgp[hdwg1[gcp->w]] = gcp->i[1] + .5;
340 >        hdworld(org, hp, cgp);
341 >                                /* compute direction to voxel center */
342 >        for (i = 3; i--; )
343 >                vgp[i] = voxel[cap->vi].i[i] + .5;
344 >        hdworld(vc, hp, vgp);
345 >        for (i = 3; i--; )
346 >                vc[i] -= org[i];
347 >                                /* compute maximum area axis */
348 >        axmax = -1; avmax = 0;
349 >        for (i = 3; i--; ) {
350 >                d = vgp[i] - cgp[i];
351 >                if (d < 0.) d = -d;
352 >                if (d > avmax) {
353 >                        avmax = d;
354 >                        axmax = i;
355                  }
356          }
357 + #ifdef DEBUG
358 +        if (axmax < 0.)
359 +                error(CONSISTENCY, "botched axis computation in docell");
360 + #endif
361 +                                /* compute offset vectors */
362 +        d = 0.5/hp->grid[j=(axmax+1)%3];
363 +        for (i = 3; i--; )
364 +                v1[i] = hp->xv[j][i] * d;
365 +        d = 0.5/hp->grid[j=(axmax+2)%3];
366 +        if (DOT(hp->wg[axmax], vc) < 0.)
367 +                d = -d; /* reverse vertex order */
368 +        for (i = 3; i--; )
369 +                v2[i] = hp->xv[j][i] * d;
370 +                                /* compute voxel pyramid */
371 +        for (i = 3; i--; ) {
372 +                dir[0][i] = vc[i] - v1[i] - v2[i];
373 +                dir[1][i] = vc[i] + v1[i] - v2[i];
374 +                dir[2][i] = vc[i] + v1[i] + v2[i];
375 +                dir[3][i] = vc[i] - v1[i] + v2[i];
376 +        }
377                                  /* visit beams for opposite cells */
378          copystruct(&bo.ca, cap);
379          copystruct(&bo.gc, gcp);
380 <        return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], dobeam, &bo));
380 >        return(visit_cells(org, dir, hp, dobeam, &bo));
381   }
382  
383  
# Line 350 | Line 386 | doview(cap, vp)                        /* visit cells for a given view */
386   struct cellact  *cap;
387   VIEW    *vp;
388   {
389 +        int     orient;
390          FVECT   org, dir[4];
391                                          /* compute view pyramid */
392 <        if (vp->type == VT_PAR) goto viewerr;
393 <        if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr;
394 <        if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr;
395 <        if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr;
359 <        if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr;
392 >        orient = viewpyramid(org, dir, hdlist[voxel[cap->vi].hd], vp);
393 >        if (!orient)
394 >                error(INTERNAL, "unusable view in doview");
395 >        cap->rev = orient < 0;
396                                          /* visit cells within pyramid */
397          return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], docell, cap));
362 viewerr:
363        error(INTERNAL, "unusable view in doview");
398   }
399  
400  
# Line 369 | Line 403 | int    voxi;
403   VIEW    *vold, *vnew;
404   {
405          int     netchange = 0;
406 <        int     *ocl, *ncl;
373 <        struct cellact  ca;
406 >        struct cellact  oca, nca;
407          int     ocnt, ncnt;
408 <        int     c;
408 >        int     nmatch = 0;
409 >        GCOORD  *ogcl, *ngcl;
410 >        register int    c;
411          register GCOORD *ogcp, *ngcp;
412                                  /* get old and new cell lists */
413 <        ocl = getviewcells(hdlist[voxel[voxi].hd], vold);
414 <        ncl = getviewcells(hdlist[voxel[voxi].hd], vnew);
415 <        if (ocl != NULL) {
416 <                ocnt = *ocl; ogcp = (GCOORD *)(ocl+1);
417 <        } else {
418 <                ocnt = 0; ogcp = NULL;
413 >        ogcp = ogcl = getviewcells(&ocnt, hdlist[voxel[voxi].hd], vold);
414 >        ngcp = ngcl = getviewcells(&ncnt, hdlist[voxel[voxi].hd], vnew);
415 >                                /* set up actions */
416 >        oca.vi = nca.vi = voxi;
417 >        oca.add = 0; nca.add = 1;
418 >        if ((oca.rev = ocnt < 0))
419 >                ocnt = -ocnt;
420 >        if ((nca.rev = ncnt < 0))
421 >                ncnt = -ncnt;
422 >        if (oca.rev == nca.rev) {       /* count matches */
423 >                int     oc = ocnt, nc = ncnt;
424 >                while (oc > 0 & nc > 0) {
425 >                        c = cellcmp(ogcp, ngcp);
426 >                        if (c >= 0) { ngcp++; nc--; }
427 >                        if (c <= 0) { ogcp++; oc--; }
428 >                        nmatch += c==0;
429 >                }
430 >                ogcp = ogcl; ngcp = ngcl;
431          }
432 <        if (ncl != NULL) {
433 <                ncnt = *ncl; ngcp = (GCOORD *)(ncl+1);
434 <        } else {
435 <                ncnt = 0; ngcp = NULL;
432 >        if (nmatch < ocnt>>1) {         /* faster to just delete old cells? */
433 >                for (c = ncbeams; c--; )
434 >                        if (cbeam[c].wants & 1<<voxi) {
435 >                                cbeam[c].wants &= ~(1<<voxi);
436 >                                netchange--;
437 >                        }
438 >                free((char *)ogcl);
439 >                ogcl = NULL; ocnt = 0;
440          }
441 <        ca.vi = voxi;           /* add and delete cells */
441 >                                /* add and delete cells in order */
442          while (ocnt > 0 & ncnt > 0)
443 <                if ((c = cellcmp(ogcp, ngcp)) > 0) {
444 <                        ca.add = 1;             /* new cell */
394 <                        netchange += docell(ngcp++, &ca);
443 >                if ((c = cellcmp(ogcp, ngcp)) > 0) {    /* new cell */
444 >                        netchange += docell(ngcp++, &nca);
445                          ncnt--;
446 <                } else if (c < 0) {
447 <                        ca.add = 0;             /* obsolete cell */
398 <                        netchange -= docell(ogcp++, &ca);
446 >                } else if (c < 0) {                     /* old cell */
447 >                        netchange -= docell(ogcp++, &oca);
448                          ocnt--;
449 <                } else {
450 <                        ogcp++; ocnt--;         /* unchanged cell */
449 >                } else {                                /* same cell */
450 >                        ogcp++; ocnt--;
451                          ngcp++; ncnt--;
452                  }
453                                  /* take care of list tails */
454 <        for (ca.add = 1; ncnt > 0; ncnt--)
455 <                docell(ngcp++, &ca);
456 <        for (ca.add = 0; ocnt > 0; ocnt--)
457 <                docell(ogcp++, &ca);
454 >        for ( ; ncnt > 0; ncnt--)
455 >                netchange += docell(ngcp++, &nca);
456 >        for ( ; ocnt > 0; ocnt--)
457 >                netchange -= docell(ogcp++, &oca);
458                                  /* clean up */
459 <        if (ocl != NULL) free((char *)ocl);
460 <        if (ncl != NULL) free((char *)ncl);
459 >        if (ogcl != NULL) free((char *)ogcl);
460 >        if (ngcl != NULL) free((char *)ngcl);
461          return(netchange);
462   }
463  
464  
465 < beam_view(vo, vn)       /* change beam view */
466 < VIEW    *vo, *vn;
465 > int
466 > beam_sync()             /* synchronize beams on server */
467   {
468 +        cbeamop(DR_NEWSET, cbeam, ncbeams, &odev.v, odev.hres, odev.vres);
469 +        return(ncbeams);
470 + }
471 +
472 +
473 + beam_view(vn)                   /* change beam view (if advisable) */
474 + VIEW    *vn;
475 + {
476          struct cellact  ca;
477 <        VOXL    vlnew[8];
477 >        VOXL    vlnew[MAXVOXEL];
478          int     n, comn;
479  
480 <        if (!vn->type) {                /* clear our beam list */
480 >        if (vn == NULL || !vn->type) {  /* clear our beam list */
481                  set_voxels(vlnew, 0);
482 <                cbeamdel(cbeam, ncbeams);
482 >                cbeamop(DR_DELSET, cbeam, ncbeams, NULL, 0, 0);
483                  ncbeams = 0;
484 <                return;
484 >                dvw.type = 0;
485 >                return(1);
486          }
487                                          /* find our new voxels */
488          n = get_voxels(vlnew, vn->vp);
489 +        if (dvw.type && !n) {
490 +                copystruct(vn, &dvw);           /* cancel move */
491 +                return(0);
492 +        }
493                                          /* set the new voxels */
494          comn = set_voxels(vlnew, n);
495 <        if (!vo->type)
495 >        if (!dvw.type)
496                  comn = 0;
497          ca.add = 1;                     /* update our beam list */
498          for (ca.vi = n; ca.vi--; )
499                  if (comn & 1<<ca.vi)    /* change which cells we see */
500 <                        mvview(ca.vi, vo, vn);
500 >                        mvview(ca.vi, &dvw, vn);
501                  else                    /* else add all new cells */
502                          doview(&ca, vn);
503                                          /* inform server of new beams */
504 <        cbeamadd(cbeam+ncbeams, xcbeams, vn, odev.hres, odev.vres);
504 >        cbeamop(DR_ADDSET, cbeam+ncbeams, xcbeams, vn, odev.hres, odev.vres);
505                                          /* sort list to put orphans at end */
506          cbeamsort(0);
507                                          /* tell server to delete orphans */
508 <        cbeamdel(cbeam+ncbeams, xcbeams);
508 >        cbeamop(DR_DELSET, cbeam+ncbeams, xcbeams, NULL, 0, 0);
509          xcbeams = 0;                    /* truncate our list */
510 +        copystruct(&dvw, vn);           /* record new view */
511 +        return(1);
512   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines