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.4 by gregl, Fri Nov 21 16:10:17 1997 UTC vs.
Revision 3.26 by gwlarson, Tue Nov 24 17:05:36 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";
# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ SGI";
11   #include "rholo.h"
12   #include "rhdisp.h"
13   #include "rhdriver.h"
14 + #include "random.h"
15  
16 < extern int      *getviewcells();
16 > #ifndef MAXDIST
17 > #define MAXDIST         42      /* maximum distance outside section */
18 > #endif
19 > #ifndef NVSAMPS
20 > #define NVSAMPS         4096    /* number of ray samples per view */
21 > #endif
22 > #ifndef MEYERNG
23 > #define MEYERNG         0.2     /* target mean eye range (rel. to grid) */
24 > #endif
25 > #ifndef MAXTODO
26 > #define MAXTODO         3       /* maximum sections to look at */
27 > #endif
28 > #ifndef MAXDRAT
29 > #define MAXDRAT         3.0     /* maximum distance ratio btwn. cand. sect. */
30 > #endif
31  
17 typedef struct {
18        int     hd;             /* holodeck section number (-1 if inactive) */
19        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
32   #define CBEAMBLK        1024    /* cbeam allocation block size */
33  
34   static struct beamcomp {
35 <        short   wants;          /* flags telling which voxels want us */
31 <        short   hd;             /* holodeck section number */
35 >        int     hd;             /* holodeck section number */
36          int     bi;             /* beam index */
37 +        int4    nr;             /* number of samples desired */
38   } *cbeam = NULL;        /* current beam list */
39  
40 + VIEWPOINT       cureye;         /* current eye position */
41 +
42   static int      ncbeams = 0;    /* number of sorted beams in cbeam */
43   static int      xcbeams = 0;    /* extra (unregistered) beams past ncbeams */
44   static int      maxcbeam = 0;   /* size of cbeam array */
45  
39 struct cellact {
40        short   vi;             /* voxel index */
41        short   add;            /* zero means delete */
42 };              /* action for cell */
46  
44 struct beamact {
45        struct cellact  ca;     /* cell action */
46        GCOORD  gc;             /* grid coordinate */
47 };              /* beam origin and action */
48
49
47   int
48   newcbeam()              /* allocate new entry at end of cbeam array */
49   {
# Line 85 | Line 82 | register struct beamcomp       *cb1, *cb2;
82   {
83          register int    c;
84  
85 <        if (!cb1->wants)                /* put orphans at the end, unsorted */
86 <                return(cb2->wants);
85 >        if (!cb1->nr)                   /* put orphans at the end, unsorted */
86 >                return(cb2->nr);
87 >        if (!cb2->nr)
88 >                return(-1);
89          if ((c = cb1->bi - cb2->bi))    /* sort on beam index first */
90                  return(c);
91          return(cb1->hd - cb2->hd);      /* use hd to resolve matches */
# Line 102 | Line 101 | int    hd, bi;
101  
102          if (ncbeams <= 0)
103                  return(-1);
104 <        cb.wants = 0; cb.hd = hd; cb.bi = bi;
104 >        cb.hd = hd; cb.bi = bi; cb.nr = 0;
105          p = (struct beamcomp *)bsearch((char *)&cb, (char *)cbeam, ncbeams,
106                          sizeof(struct beamcomp), cbeamcmp);
107          if (p == NULL)
# Line 130 | Line 129 | int    bi;
129          if (bi < 1 | bi > nbeams(hdlist[hd]))
130                  error(INTERNAL, "illegal beam index in getcbeam");
131          n = newcbeam();         /* allocate and assign */
132 <        cbeam[n].wants = 0; cbeam[n].hd = hd; cbeam[n].bi = bi;
132 >        cbeam[n].nr = 0; cbeam[n].hd = hd; cbeam[n].bi = bi;
133          return(n);
134   }
135  
# Line 148 | Line 147 | int    adopt;
147          if (adopt)
148                  return;
149          for (i = ncbeams; i--; )        /* identify orphans */
150 <                if (cbeam[i].wants)
150 >                if (cbeam[i].nr)
151                          break;
152          xcbeams = ncbeams - ++i;        /* put orphans after ncbeams */
153          ncbeams = i;
154   }
155  
156  
157 < cbeamop(op, bl, n, v, hr, vr)   /* update beams on server list */
157 > cbeamop(op, bl, n)              /* update beams on server list */
158   int     op;
159   register struct beamcomp        *bl;
160   int     n;
162 VIEW    *v;
163 int     hr, vr;
161   {
162          register PACKHEAD       *pa;
163          register int    i;
# Line 169 | Line 166 | int    hr, vr;
166                  return;
167          pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD));
168          if (pa == NULL)
169 <                error(SYSTEM, "out of memory in cbeamadd");
169 >                error(SYSTEM, "out of memory in cbeamop");
170          for (i = 0; i < n; i++) {
171                  pa[i].hd = bl[i].hd;
172                  pa[i].bi = bl[i].bi;
173 <                pa[i].nr = v==NULL ? 0 :
174 <                                npixels(v, hr, vr, hdlist[bl[i].hd], bl[i].bi);
173 >                pa[i].nr = bl[i].nr;
174 >                pa[i].nc = 0;
175          }
176          serv_request(op, n*sizeof(PACKHEAD), (char *)pa);
177          free((char *)pa);
# Line 182 | Line 179 | int    hr, vr;
179  
180  
181   int
182 < set_voxels(vl, n)       /* set new voxel array */
183 < VOXL    vl[8];
184 < int     n;
182 > comptodo(tdl, vw)               /* compute holodeck sections in view */
183 > int     tdl[MAXTODO+1];
184 > VIEW    *vw;
185   {
186 <        short   wmap[256];
187 <        int     vmap[8];
188 <        int     comn = 0;
192 <        int     no;
186 >        int     n = 0;
187 >        FVECT   gp, dv;
188 >        double  dist2[MAXTODO+1], thisdist2;
189          register int    i, j;
190 <                                        /* find common voxels */
191 <        for (j = 0; j < 8 && voxel[j].hd >= 0; j++) {
192 <                vmap[j] = -1;
193 <                for (i = n; i--; )
194 <                        if (!bcmp((char *)(vl+i), (char *)(voxel+j),
195 <                                        sizeof(VOXL))) {
196 <                                vmap[j] = i;
197 <                                comn |= 1<<i;
198 <                                break;
199 <                        }
190 >                                        /* find closest MAXTODO sections */
191 >        for (i = 0; hdlist[i]; i++) {
192 >                hdgrid(gp, hdlist[i], vw->vp);
193 >                for (j = 0; j < 3; j++)
194 >                        if (gp[j] < 0.)
195 >                                dv[j] = -gp[j];
196 >                        else if (gp[j] > hdlist[i]->grid[j])
197 >                                dv[j] = gp[j] - hdlist[i]->grid[j];
198 >                        else
199 >                                dv[j] = 0.;
200 >                thisdist2 = DOT(dv,dv);
201 >                for (j = n; j > 0 && thisdist2 < dist2[j-1]; j--) {
202 >                        tdl[j] = tdl[j-1];
203 >                        dist2[j] = dist2[j-1];
204 >                }
205 >                tdl[j] = i;
206 >                dist2[j] = thisdist2;
207 >                if (n < MAXTODO)
208 >                        n++;
209          }
210 <        no = comn ? j : 0;              /* compute flag mapping */
211 <        for (i = 256; i--; ) {
212 <                wmap[i] = 0;
213 <                for (j = no; j--; )
209 <                        if (vmap[j] >= 0 && i & 1<<j)
210 <                                wmap[i] |= 1<<vmap[j];
210 >                                        /* watch for bad move */
211 >        if (n && dist2[0] > MAXDIST*MAXDIST) {
212 >                error(COMMAND, "move past outer limits");
213 >                return(0);
214          }
215 <                                        /* fix cbeam flags */
216 <        for (i = ncbeams; i--; )
217 <                cbeam[i].wants = wmap[cbeam[i].wants];
218 <                                        /* update our voxel list */
219 <        bcopy((char *)vl, (char *)voxel, n*sizeof(VOXL));
220 <        for (j = n; j < 8; j++)
218 <                voxel[j].hd = -1;
219 <        return(comn);                   /* return bit array of common voxels */
215 >                                        /* avoid big differences */
216 >        for (j = 1; j < n; j++)
217 >                if (dist2[j] > MAXDRAT*MAXDRAT*dist2[j-1])
218 >                        n = j;
219 >        tdl[n] = -1;
220 >        return(n);
221   }
222  
223  
224 < int
225 < get_voxels(vl, vp)      /* find voxels corresponding to view point */
226 < VOXL    vl[8];
227 < FVECT   vp;
224 > addview(hd, vw, hres, vres)     /* add view for section */
225 > int     hd;
226 > VIEW    *vw;
227 > int     hres, vres;
228   {
229 <        static int      lastn = 0, lastd = -1;
230 <        int     n = 0;
231 <        FVECT   gp;
232 <        double  d;
233 <        int     dist, bestd = 0x7fff;
234 <        VOXL    vox;
235 <        register int    i, j, k;
236 <                                        /* find closest voxels */
237 <        for (i = 0; n < 8 && hdlist[i]; i++) {
238 <                hdgrid(gp, hdlist[i], vp);
239 <                for (j = 0; n < 8 && j < 8; j++) {
240 <                        dist = 0;
241 <                        for (k = 0; k < 3; k++) {
242 <                                d = gp[k] - .5 + (j>>k & 1);
243 <                                if (d < 0.)
244 <                                        dist += -(vox.i[k] = (int)d - 1);
245 <                                else if ((vox.i[k] = d) >= hdlist[i]->grid[k])
246 <                                        dist += vox.i[k] -
246 <                                                        hdlist[i]->grid[k] + 1;
247 <                        }
248 <                        if (dist > bestd)       /* others were closer */
229 >        int     sampquant;
230 >        int     h, v, shr, svr;
231 >        GCOORD  gc[2];
232 >        FVECT   rorg, rdir;
233 >                                /* compute sampling grid */
234 >        if (hres|vres && hres*vres <= NVSAMPS) {
235 >                shr = hres; svr = vres;
236 >                sampquant = 1;
237 >        } else {
238 >                shr = sqrt(NVSAMPS/viewaspect(vw)) + .5;
239 >                svr = (NVSAMPS + shr/2)/shr;
240 >                sampquant = (hres*vres + shr*svr/2)/(shr*svr);
241 >        }
242 >                                /* intersect sample rays with section */
243 >        for (v = svr; v--; )
244 >                for (h = shr; h--; ) {
245 >                        if (viewray(rorg, rdir, vw, (v+frandom())/svr,
246 >                                                (h+frandom())/shr) < -FTINY)
247                                  continue;
248 <                        if (dist < bestd) {     /* start closer list */
249 <                                n = 0;
250 <                                bestd = dist;
251 <                        }
252 <                        vox.hd = i;             /* add this voxel */
255 <                        copystruct(&vl[n], &vox);
256 <                        n++;
248 >                        if (hdinter(gc, NULL, NULL, hdlist[hd], rorg, rdir)
249 >                                                >= FHUGE)
250 >                                continue;
251 >                        cbeam[getcbeam(hd,hdbindex(hdlist[hd],gc))].nr +=
252 >                                        sampquant;
253                  }
258        }
259                                        /* 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);
254   }
255  
256  
257 < int
258 < dobeam(gcp, bp)         /* express interest or disintrest in a beam */
271 < GCOORD  *gcp;
272 < register struct beamact *bp;
257 > beam_init(fresh)                /* clear beam list for new view(s) */
258 > int     fresh;
259   {
260 <        GCOORD  gc[2];
261 <        int     bi, i;
262 <                                        /* compute beam index */
263 <        copystruct(gc, gcp);
264 <        copystruct(gc+1, &bp->gc);
265 <        if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0)
266 <                error(CONSISTENCY, "bad grid coordinate in dobeam");
267 <        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);
260 >        register int    i;
261 >
262 >        if (fresh)                      /* discard old beams? */
263 >                ncbeams = xcbeams = 0;
264 >        else                            /* else clear sample requests */
265 >                for (i = ncbeams+xcbeams; i--; )
266 >                        cbeam[i].nr = 0;
267 >        cureye.rng = 0.;
268   }
269  
270  
271 <
272 < int
273 < docell(gcp, cap)        /* find beams corresponding to cell and voxel */
299 < GCOORD  *gcp;
300 < register struct cellact *cap;
271 > beam_view(vn, hr, vr)           /* add beam view (if advisable) */
272 > VIEW    *vn;
273 > int     hr, vr;
274   {
275 <        FVECT   org, dir[4];
276 <        FVECT   gp, cv[4], vc;
277 <        struct beamact  bo;
275 >        int     todo[MAXTODO+1], n;
276 >        double  hdgsiz, d;
277 >        register HOLO   *hp;
278          register int    i;
279 <                                /* compute cell vertices */
280 <        hdcell(cv, hdlist[voxel[cap->vi].hd], gcp);
281 <                                /* compute cell and voxel centers */
282 <        for (i = 0; i < 3; i++) {
283 <                org[i] = 0.5*(cv[0][i] + cv[2][i]);
284 <                gp[i] = voxel[cap->vi].i[i] + 0.5;
279 >                                        /* sort our list */
280 >        cbeamsort(1);
281 >                                        /* add view to nearby sections */
282 >        if (!(n = comptodo(todo, vn)))
283 >                return(0);
284 >        for (i = 0; i < n; i++)
285 >                addview(todo[i], vn, hr, vr);
286 >        if (MEYERNG <= FTINY || vn->type == VT_PAR)
287 >                return(1);
288 >        hdgsiz = 0.; d = 1./3. / n;     /* compute mean grid size */
289 >        for (i = 0; i < n; i++) {
290 >                hp = hdlist[todo[i]];
291 >                hdgsiz += d * ( VLEN(hp->xv[0])/hp->grid[0] +
292 >                                VLEN(hp->xv[1])/hp->grid[1] +
293 >                                VLEN(hp->xv[2])/hp->grid[2] ) ;
294          }
295 <        hdworld(vc, hdlist[voxel[cap->vi].hd], gp);
296 <                                /* compute voxel pyramid using vector trick */
297 <        for (i = 0; i < 3; i++) {
298 <                dir[0][i] = vc[i] - cv[0][i];           /* to 3 */
299 <                dir[2][i] = vc[i] - cv[3][i];           /* to 0 */
300 <                if (gcp->w & 1) {       /* watch vertex order! */
301 <                        dir[1][i] = vc[i] - cv[2][i];   /* to 1 */
302 <                        dir[3][i] = vc[i] - cv[1][i];   /* to 2 */
303 <                } else {
322 <                        dir[1][i] = vc[i] - cv[1][i];   /* to 2 */
323 <                        dir[3][i] = vc[i] - cv[2][i];   /* to 1 */
324 <                }
295 >                                        /* add to current eye position */
296 >        if (cureye.rng <= FTINY) {
297 >                VCOPY(cureye.vpt, vn->vp);
298 >                cureye.rng = MEYERNG * hdgsiz;
299 >        } else if ((d = sqrt(dist2(vn->vp,cureye.vpt))) + MEYERNG*hdgsiz >
300 >                        cureye.rng) {
301 >                for (i = 3; i--; )
302 >                        cureye.vpt[i] = 0.5*(cureye.vpt[i] + vn->vp[i]);
303 >                cureye.rng = 0.5*(cureye.rng + MEYERNG*hdgsiz + d);
304          }
305 <                                /* 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));
305 >        return(1);
306   }
307  
308  
309   int
310 < doview(cap, vp)                 /* visit cells for a given view */
311 < struct cellact  *cap;
336 < VIEW    *vp;
310 > beam_sync(all)                  /* update beam list on server */
311 > int     all;
312   {
313 <        FVECT   org, dir[4];
314 <                                        /* compute view pyramid */
315 <        if (vp->type == VT_PAR) goto viewerr;
316 <        if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr;
317 <        if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr;
318 <        if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr;
319 <        if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr;
320 <                                        /* visit cells within pyramid */
321 <        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 <                netchange += docell(ngcp++, &ca);
392 <        for (ca.add = 0; ocnt > 0; ocnt--)
393 <                netchange -= docell(ogcp++, &ca);
394 <                                /* clean up */
395 <        if (ocl != NULL) free((char *)ocl);
396 <        if (ncl != NULL) free((char *)ncl);
397 <        return(netchange);
398 < }
399 <
400 <
401 < beam_sync()             /* synchronize beams on server */
402 < {
403 <        cbeamop(DR_NEWSET, cbeam, ncbeams, &odev.v, odev.hres, odev.vres);
404 < }
405 <
406 <
407 < 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 <                cbeamop(DR_DELSET, cbeam, ncbeams, NULL, 0, 0);
417 <                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 <        cbeamop(DR_ADDSET, cbeam+ncbeams, xcbeams, vn, odev.hres, odev.vres);
434 <                                        /* sort list to put orphans at end */
435 <        cbeamsort(0);
436 <                                        /* tell server to delete orphans */
437 <        cbeamop(DR_DELSET, cbeam+ncbeams, xcbeams, NULL, 0, 0);
313 >                                        /* set new eye position */
314 >        serv_request(DR_VIEWPOINT, sizeof(VIEWPOINT), (char *)&cureye);
315 >                                        /* sort list (put orphans at end) */
316 >        cbeamsort(all < 0);
317 >                                        /* send beam request */
318 >        if (all)
319 >                cbeamop(DR_NEWSET, cbeam, ncbeams);
320 >        else
321 >                cbeamop(DR_ADJSET, cbeam, ncbeams+xcbeams);
322          xcbeams = 0;                    /* truncate our list */
323 +        return(ncbeams);
324   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines