ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp2.c
Revision: 3.4
Committed: Fri Nov 21 16:10:17 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.3: +14 -28 lines
Log Message:
added server-driven redraw function to replace display quadtree

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8 gregl 3.2 * Holodeck beam tracking for display process
9 gregl 3.1 */
10    
11     #include "rholo.h"
12     #include "rhdisp.h"
13     #include "rhdriver.h"
14    
15     extern int *getviewcells();
16    
17     typedef struct {
18 gregl 3.2 int hd; /* holodeck section number (-1 if inactive) */
19 gregl 3.1 int i[3]; /* voxel index (may be outside section) */
20     } VOXL; /* a voxel */
21    
22     static VOXL voxel[8] = { /* current voxel list */
23     {-1}, {-1}, {-1}, {-1},
24     {-1}, {-1}, {-1}, {-1}
25     };
26    
27     #define CBEAMBLK 1024 /* cbeam allocation block size */
28    
29     static struct beamcomp {
30     short wants; /* flags telling which voxels want us */
31     short hd; /* holodeck section number */
32     int bi; /* beam index */
33     } *cbeam = NULL; /* current beam list */
34    
35     static int ncbeams = 0; /* number of sorted beams in cbeam */
36     static int xcbeams = 0; /* extra (unregistered) beams past ncbeams */
37     static int maxcbeam = 0; /* size of cbeam array */
38    
39     struct cellact {
40     short vi; /* voxel index */
41     short add; /* zero means delete */
42     }; /* action for cell */
43    
44     struct beamact {
45     struct cellact ca; /* cell action */
46     GCOORD gc; /* grid coordinate */
47     }; /* beam origin and action */
48    
49    
50     int
51     newcbeam() /* allocate new entry at end of cbeam array */
52     {
53     int i;
54    
55     if ((i = ncbeams + xcbeams++) >= maxcbeam) { /* grow array */
56     maxcbeam += CBEAMBLK;
57     if (cbeam == NULL)
58     cbeam = (struct beamcomp *)malloc(
59     maxcbeam*sizeof(struct beamcomp) );
60     else
61     cbeam = (struct beamcomp *)realloc( (char *)cbeam,
62     maxcbeam*sizeof(struct beamcomp) );
63     if (cbeam == NULL)
64     error(SYSTEM, "out of memory in newcbeam");
65     }
66     return(i);
67     }
68    
69    
70     int
71     cbeamcmp(cb1, cb2) /* compare two cbeam entries for sort: keep orphans */
72     register struct beamcomp *cb1, *cb2;
73     {
74     register int c;
75    
76     if ((c = cb1->bi - cb2->bi)) /* sort on beam index first */
77     return(c);
78     return(cb1->hd - cb2->hd); /* use hd to resolve matches */
79     }
80    
81    
82     int
83     cbeamcmp2(cb1, cb2) /* compare two cbeam entries for sort: no orphans */
84     register struct beamcomp *cb1, *cb2;
85     {
86     register int c;
87    
88     if (!cb1->wants) /* put orphans at the end, unsorted */
89     return(cb2->wants);
90     if ((c = cb1->bi - cb2->bi)) /* sort on beam index first */
91     return(c);
92     return(cb1->hd - cb2->hd); /* use hd to resolve matches */
93     }
94    
95    
96     int
97     findcbeam(hd, bi) /* find the specified beam in our sorted list */
98     int hd, bi;
99     {
100     struct beamcomp cb;
101     register struct beamcomp *p;
102    
103     if (ncbeams <= 0)
104     return(-1);
105     cb.wants = 0; cb.hd = hd; cb.bi = bi;
106     p = (struct beamcomp *)bsearch((char *)&cb, (char *)cbeam, ncbeams,
107     sizeof(struct beamcomp), cbeamcmp);
108     if (p == NULL)
109     return(-1);
110     return(p - cbeam);
111     }
112    
113    
114     int
115     getcbeam(hd, bi) /* get the specified beam, allocating as necessary */
116     register int hd;
117     int bi;
118     {
119     register int n;
120     /* first, look in sorted list */
121     if ((n = findcbeam(hd, bi)) >= 0)
122     return(n);
123     /* linear search through xcbeams to be sure */
124     for (n = ncbeams+xcbeams; n-- > ncbeams; )
125     if (cbeam[n].bi == bi && cbeam[n].hd == hd)
126     return(n);
127     /* check legality */
128     if (hd < 0 | hd >= HDMAX || hdlist[hd] == NULL)
129     error(INTERNAL, "illegal holodeck number in getcbeam");
130     if (bi < 1 | bi > nbeams(hdlist[hd]))
131     error(INTERNAL, "illegal beam index in getcbeam");
132     n = newcbeam(); /* allocate and assign */
133     cbeam[n].wants = 0; cbeam[n].hd = hd; cbeam[n].bi = bi;
134     return(n);
135     }
136    
137    
138     cbeamsort(adopt) /* sort our beam list, possibly turning out orphans */
139     int adopt;
140     {
141     register int i;
142    
143     if (!(ncbeams += xcbeams))
144     return;
145     xcbeams = 0;
146     qsort((char *)cbeam, ncbeams, sizeof(struct beamcomp),
147     adopt ? cbeamcmp : cbeamcmp2);
148     if (adopt)
149     return;
150     for (i = ncbeams; i--; ) /* identify orphans */
151     if (cbeam[i].wants)
152     break;
153     xcbeams = ncbeams - ++i; /* put orphans after ncbeams */
154     ncbeams = i;
155     }
156    
157    
158 gregl 3.4 cbeamop(op, bl, n, v, hr, vr) /* update beams on server list */
159     int op;
160 gregl 3.1 register struct beamcomp *bl;
161     int n;
162     VIEW *v;
163     int hr, vr;
164     {
165     register PACKHEAD *pa;
166     register int i;
167    
168     if (n <= 0)
169     return;
170     pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD));
171     if (pa == NULL)
172     error(SYSTEM, "out of memory in cbeamadd");
173     for (i = 0; i < n; i++) {
174     pa[i].hd = bl[i].hd;
175     pa[i].bi = bl[i].bi;
176 gregl 3.4 pa[i].nr = v==NULL ? 0 :
177     npixels(v, hr, vr, hdlist[bl[i].hd], bl[i].bi);
178 gregl 3.1 }
179 gregl 3.4 serv_request(op, n*sizeof(PACKHEAD), (char *)pa);
180 gregl 3.1 free((char *)pa);
181     }
182    
183    
184     int
185     set_voxels(vl, n) /* set new voxel array */
186     VOXL vl[8];
187     int n;
188     {
189     short wmap[256];
190     int vmap[8];
191     int comn = 0;
192     int no;
193     register int i, j;
194     /* find common voxels */
195     for (j = 0; j < 8 && voxel[j].hd >= 0; j++) {
196     vmap[j] = -1;
197     for (i = n; i--; )
198     if (!bcmp((char *)(vl+i), (char *)(voxel+j),
199     sizeof(VOXL))) {
200     vmap[j] = i;
201     comn |= 1<<i;
202     break;
203     }
204     }
205     no = comn ? j : 0; /* compute flag mapping */
206     for (i = 256; i--; ) {
207     wmap[i] = 0;
208     for (j = no; j--; )
209     if (vmap[j] >= 0 && i & 1<<j)
210     wmap[i] |= 1<<vmap[j];
211     }
212     /* fix cbeam flags */
213     for (i = ncbeams; i--; )
214     cbeam[i].wants = wmap[cbeam[i].wants];
215     /* update our voxel list */
216     bcopy((char *)vl, (char *)voxel, n*sizeof(VOXL));
217     for (j = n; j < 8; j++)
218     voxel[j].hd = -1;
219     return(comn); /* return bit array of common voxels */
220     }
221    
222    
223     int
224     get_voxels(vl, vp) /* find voxels corresponding to view point */
225     VOXL vl[8];
226     FVECT vp;
227     {
228 gregl 3.3 static int lastn = 0, lastd = -1;
229 gregl 3.1 int n = 0;
230     FVECT gp;
231     double d;
232     int dist, bestd = 0x7fff;
233     VOXL vox;
234     register int i, j, k;
235     /* find closest voxels */
236     for (i = 0; n < 8 && hdlist[i]; i++) {
237     hdgrid(gp, hdlist[i], vp);
238     for (j = 0; n < 8 && j < 8; j++) {
239     dist = 0;
240     for (k = 0; k < 3; k++) {
241     d = gp[k] - .5 + (j>>k & 1);
242     if (d < 0.)
243     dist += -(vox.i[k] = (int)d - 1);
244     else if ((vox.i[k] = d) >= hdlist[i]->grid[k])
245     dist += vox.i[k] -
246     hdlist[i]->grid[k] + 1;
247     }
248     if (dist > bestd) /* others were closer */
249     continue;
250     if (dist < bestd) { /* start closer list */
251     n = 0;
252     bestd = dist;
253     }
254     vox.hd = i; /* add this voxel */
255     copystruct(&vl[n], &vox);
256     n++;
257     }
258     }
259 gregl 3.3 /* warn of dangerous moves */
260     if (n < lastn && bestd >= lastd)
261     error(WARNING, "moving outside holodeck section");
262     else if (n > lastn && bestd <= lastd)
263     error(WARNING, "moving inside holodeck section");
264     lastd = bestd;
265     return(lastn = n);
266 gregl 3.1 }
267    
268    
269     int
270     dobeam(gcp, bp) /* express interest or disintrest in a beam */
271     GCOORD *gcp;
272     register struct beamact *bp;
273     {
274     GCOORD gc[2];
275     int bi, i;
276     /* compute beam index */
277     copystruct(gc, gcp);
278     copystruct(gc+1, &bp->gc);
279     if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0)
280 gregl 3.2 error(CONSISTENCY, "bad grid coordinate in dobeam");
281 gregl 3.1 if (bp->ca.add) { /* add it in */
282     i = getcbeam(voxel[bp->ca.vi].hd, bi);
283     cbeam[i].wants |= 1<<bp->ca.vi; /* say we want it */
284     return(i == ncbeams+xcbeams-1); /* return 1 if totally new */
285     }
286     /* else delete it */
287     i = findcbeam(voxel[bp->ca.vi].hd, bi);
288     if (i >= 0 && cbeam[i].wants & 1<<bp->ca.vi) {
289     cbeam[i].wants &= ~(1<<bp->ca.vi); /* we don't want it */
290     return(1); /* indicate change */
291     }
292     return(0);
293     }
294    
295    
296    
297     int
298     docell(gcp, cap) /* find beams corresponding to cell and voxel */
299     GCOORD *gcp;
300     register struct cellact *cap;
301     {
302     FVECT org, dir[4];
303     FVECT gp, cv[4], vc;
304     struct beamact bo;
305     register int i;
306     /* compute cell vertices */
307     hdcell(cv, hdlist[voxel[cap->vi].hd], gcp);
308     /* compute cell and voxel centers */
309     for (i = 0; i < 3; i++) {
310     org[i] = 0.5*(cv[0][i] + cv[2][i]);
311     gp[i] = voxel[cap->vi].i[i] + 0.5;
312     }
313     hdworld(vc, hdlist[voxel[cap->vi].hd], gp);
314     /* compute voxel pyramid using vector trick */
315     for (i = 0; i < 3; i++) {
316     dir[0][i] = vc[i] - cv[0][i]; /* to 3 */
317     dir[2][i] = vc[i] - cv[3][i]; /* to 0 */
318     if (gcp->w & 1) { /* watch vertex order! */
319     dir[1][i] = vc[i] - cv[2][i]; /* to 1 */
320     dir[3][i] = vc[i] - cv[1][i]; /* to 2 */
321     } else {
322     dir[1][i] = vc[i] - cv[1][i]; /* to 2 */
323     dir[3][i] = vc[i] - cv[2][i]; /* to 1 */
324     }
325     }
326     /* visit beams for opposite cells */
327     copystruct(&bo.ca, cap);
328     copystruct(&bo.gc, gcp);
329     return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], dobeam, &bo));
330     }
331    
332    
333     int
334     doview(cap, vp) /* visit cells for a given view */
335     struct cellact *cap;
336     VIEW *vp;
337     {
338     FVECT org, dir[4];
339     /* compute view pyramid */
340     if (vp->type == VT_PAR) goto viewerr;
341     if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr;
342     if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr;
343     if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr;
344     if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr;
345     /* visit cells within pyramid */
346     return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], docell, cap));
347     viewerr:
348     error(INTERNAL, "unusable view in doview");
349     }
350    
351    
352     mvview(voxi, vold, vnew) /* move view for a voxel */
353     int voxi;
354     VIEW *vold, *vnew;
355     {
356     int netchange = 0;
357     int *ocl, *ncl;
358     struct cellact ca;
359     int ocnt, ncnt;
360     int c;
361     register GCOORD *ogcp, *ngcp;
362     /* get old and new cell lists */
363     ocl = getviewcells(hdlist[voxel[voxi].hd], vold);
364     ncl = getviewcells(hdlist[voxel[voxi].hd], vnew);
365     if (ocl != NULL) {
366     ocnt = *ocl; ogcp = (GCOORD *)(ocl+1);
367     } else {
368     ocnt = 0; ogcp = NULL;
369     }
370     if (ncl != NULL) {
371     ncnt = *ncl; ngcp = (GCOORD *)(ncl+1);
372     } else {
373     ncnt = 0; ngcp = NULL;
374     }
375     ca.vi = voxi; /* add and delete cells */
376     while (ocnt > 0 & ncnt > 0)
377     if ((c = cellcmp(ogcp, ngcp)) > 0) {
378     ca.add = 1; /* new cell */
379     netchange += docell(ngcp++, &ca);
380     ncnt--;
381     } else if (c < 0) {
382     ca.add = 0; /* obsolete cell */
383     netchange -= docell(ogcp++, &ca);
384     ocnt--;
385     } else {
386     ogcp++; ocnt--; /* unchanged cell */
387     ngcp++; ncnt--;
388     }
389     /* take care of list tails */
390     for (ca.add = 1; ncnt > 0; ncnt--)
391 gregl 3.2 netchange += docell(ngcp++, &ca);
392 gregl 3.1 for (ca.add = 0; ocnt > 0; ocnt--)
393 gregl 3.2 netchange -= docell(ogcp++, &ca);
394 gregl 3.1 /* clean up */
395     if (ocl != NULL) free((char *)ocl);
396     if (ncl != NULL) free((char *)ncl);
397     return(netchange);
398     }
399    
400    
401 gregl 3.4 beam_sync() /* synchronize beams on server */
402     {
403     cbeamop(DR_NEWSET, cbeam, ncbeams, &odev.v, odev.hres, odev.vres);
404     }
405    
406    
407 gregl 3.1 beam_view(vo, vn) /* change beam view */
408     VIEW *vo, *vn;
409     {
410     struct cellact ca;
411     VOXL vlnew[8];
412     int n, comn;
413    
414     if (!vn->type) { /* clear our beam list */
415     set_voxels(vlnew, 0);
416 gregl 3.4 cbeamop(DR_DELSET, cbeam, ncbeams, NULL, 0, 0);
417 gregl 3.1 ncbeams = 0;
418     return;
419     }
420     /* find our new voxels */
421     n = get_voxels(vlnew, vn->vp);
422     /* set the new voxels */
423     comn = set_voxels(vlnew, n);
424     if (!vo->type)
425     comn = 0;
426     ca.add = 1; /* update our beam list */
427     for (ca.vi = n; ca.vi--; )
428     if (comn & 1<<ca.vi) /* change which cells we see */
429     mvview(ca.vi, vo, vn);
430     else /* else add all new cells */
431     doview(&ca, vn);
432     /* inform server of new beams */
433 gregl 3.4 cbeamop(DR_ADDSET, cbeam+ncbeams, xcbeams, vn, odev.hres, odev.vres);
434 gregl 3.1 /* sort list to put orphans at end */
435     cbeamsort(0);
436     /* tell server to delete orphans */
437 gregl 3.4 cbeamop(DR_DELSET, cbeam+ncbeams, xcbeams, NULL, 0, 0);
438 gregl 3.1 xcbeams = 0; /* truncate our list */
439     }