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.21 by gwlarson, Thu May 14 13:23:00 1998 UTC vs.
Revision 3.27 by gwlarson, Thu Dec 3 15:18:08 1998 UTC

# 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   #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  
19 #define MAXVOXEL        16      /* maximum number of active voxels */
20
21 typedef struct {
22        int     hd;             /* holodeck section number (-1 if inactive) */
23        int     i[3];           /* voxel index (may be outside section) */
24 } VOXL;                 /* a voxel */
25
26 static VOXL voxel[MAXVOXEL] = {{-1}};   /* current voxel list */
27
32   #define CBEAMBLK        1024    /* cbeam allocation block size */
33  
34   static struct beamcomp {
35 <        unsigned int2   wants;  /* flags telling which voxels want us */
36 <        unsigned int2   hd;     /* holodeck section number */
33 <        int4    bi;             /* beam index */
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  
41 struct cellact {
42        short   vi;             /* voxel index */
43        short   add;            /* zero means delete */
44        short   rev;            /* reverse ray direction? */
45        VIEW    *vp;            /* view for image */
46        short   hr, vr;         /* image resolution */
47 };              /* action for cell */
46  
49 struct beamact {
50        struct cellact  ca;     /* cell action */
51        GCOORD  gc;             /* grid coordinate */
52 };              /* beam origin and action */
53
54
47   int
48   newcbeam()              /* allocate new entry at end of cbeam array */
49   {
# Line 90 | 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);
87 <        if (!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);
# Line 109 | Line 101 | int    hd, bi;
101  
102          if (ncbeams <= 0)
103                  return(-1);
104 <        cb.wants = 0; cb.hd = hd; cb.bi = bi; cb.nr = 0;
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 137 | 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 = cbeam[n].nr = 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 155 | 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;
# Line 186 | Line 178 | int    n;
178   }
179  
180  
181 < add_voxels(vp)          /* add voxels corresponding to view point */
182 < FVECT   vp;
181 > int
182 > comptodo(tdl, vw)               /* compute holodeck sections in view */
183 > int     tdl[MAXTODO+1];
184 > VIEW    *vw;
185   {
186 <        int     first, n, rfl = 0;
187 <        FVECT   gp;
188 <        double  d;
189 <        int     dist, bestd = 0x7fff;
190 <        VOXL    vox;
191 <        register int    i, j, k;
192 <                                        /* count voxels in list already */
193 <        for (first = 0; first < MAXVOXEL && voxel[first].hd >= 0; first++)
194 <                ;
195 <                                        /* find closest voxels */
196 <        for (n = first, i = 0; n < MAXVOXEL && hdlist[i]; i++) {
197 <                hdgrid(gp, hdlist[i], vp);
198 <                for (j = 0; n < MAXVOXEL && j < 8; j++) {
199 <                        dist = 0;
200 <                        for (k = 0; k < 3; k++) {
201 <                                d = gp[k] - .5 + (j>>k & 1);
202 <                                if (d < 0.)
203 <                                        dist += -(vox.i[k] = (int)d - 1);
210 <                                else if ((vox.i[k] = d) >= hdlist[i]->grid[k])
211 <                                        dist += vox.i[k] -
212 <                                                        hdlist[i]->grid[k] + 1;
213 <                        }
214 <                        if (dist > bestd)       /* others were closer */
215 <                                continue;
216 <                        if (dist < bestd) {     /* start closer list */
217 <                                n = first;
218 <                                bestd = dist;
219 <                                rfl = 0;
220 <                        }
221 <                                                /* check if already in list */
222 <                        for (k = first; k--; )
223 <                                if (voxel[k].hd == i &&
224 <                                                voxel[k].i[0] == vox.i[0] &&
225 <                                                voxel[k].i[1] == vox.i[1] &&
226 <                                                voxel[k].i[2] == vox.i[2])
227 <                                        break;
228 <                        if (k >= 0) {
229 <                                rfl |= 1<<k;
230 <                                continue;
231 <                        }
232 <                        vox.hd = i;             /* add this voxel */
233 <                        copystruct(&voxel[n], &vox);
234 <                        rfl |= 1<<n++;
186 >        int     n = 0;
187 >        FVECT   gp, dv;
188 >        double  dist2[MAXTODO+1], thisdist2;
189 >        register int    i, j;
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 <                                        /* check for really stupid move */
211 <        if (bestd > MAXDIST) {
210 >                                        /* watch for bad move */
211 >        if (n && dist2[0] > MAXDIST*MAXDIST) {
212                  error(COMMAND, "move past outer limits");
213                  return(0);
214          }
215 <        if (n < MAXVOXEL)
216 <                voxel[n].hd = -1;
217 <        return(rfl);
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 < dobeam(gcp, bp)         /* express interest or disintrest in a beam */
226 < GCOORD  *gcp;
227 < register struct beamact *bp;
224 > addview(hd, vw, hres, vres)     /* add view for section */
225 > int     hd;
226 > VIEW    *vw;
227 > int     hres, vres;
228   {
229 +        int     sampquant;
230 +        int     h, v, shr, svr;
231          GCOORD  gc[2];
232 <        int     bi, i, n;
233 <                                        /* compute beam index */
234 <        if (bp->ca.rev) {
235 <                copystruct(gc, &bp->gc);
236 <                copystruct(gc+1, gcp);
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 <                copystruct(gc, gcp);
239 <                copystruct(gc+1, &bp->gc);
238 >                shr = sqrt(NVSAMPS/viewaspect(vw)) + .5;
239 >                svr = (NVSAMPS + shr/2)/shr;
240 >                sampquant = (hres*vres + shr*svr/2)/(shr*svr);
241          }
242 <        if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0)
243 <                error(CONSISTENCY, "bad grid coordinate in dobeam");
244 <        if (bp->ca.add) {               /* add it in */
245 <                i = getcbeam(voxel[bp->ca.vi].hd, bi);
246 <                cbeam[i].wants |= 1<<bp->ca.vi; /* say we want it */
247 <                n = npixels(bp->ca.vp, bp->ca.hr, bp->ca.vr,
248 <                                hdlist[cbeam[i].hd], cbeam[i].bi);
249 <                if (n > cbeam[i].nr)
250 <                        cbeam[i].nr = n;
251 <                return(i == ncbeams+xcbeams-1); /* return 1 if totally new */
252 <        }
253 <                                        /* else delete it */
275 <        i = findcbeam(voxel[bp->ca.vi].hd, bi);
276 <        if (i >= 0 && cbeam[i].wants & 1<<bp->ca.vi) {
277 <                cbeam[i].wants &= ~(1<<bp->ca.vi);      /* we don't want it */
278 <                if (!cbeam[i].wants)
279 <                        cbeam[i].nr = 0;
280 <                return(1);                      /* indicate change */
281 <        }
282 <        return(0);
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 (hdinter(gc, NULL, NULL, hdlist[hd], rorg, rdir)
249 >                                                >= FHUGE)
250 >                                continue;
251 >                        cbeam[getcbeam(hd,hdbindex(hdlist[hd],gc))].nr +=
252 >                                        sampquant;
253 >                }
254   }
255  
256  
257 <
258 < int
288 < docell(gcp, cap)        /* find beams corresponding to cell and voxel */
289 < GCOORD  *gcp;
290 < register struct cellact *cap;
257 > beam_init(fresh)                /* clear beam list for new view(s) */
258 > int     fresh;
259   {
292        register HOLO   *hp = hdlist[voxel[cap->vi].hd];
293        FVECT   org, dir[4];
294        FVECT   vgp, cgp, vc;
295        FVECT   v1, v2;
296        struct beamact  bo;
297        int     axmax, j;
298        double  d, avmax;
260          register int    i;
300                                /* compute cell center */
301        cgp[gcp->w>>1] = gcp->w&1 ? hp->grid[gcp->w>>1] : 0 ;
302        cgp[hdwg0[gcp->w]] = gcp->i[0] + .5;
303        cgp[hdwg1[gcp->w]] = gcp->i[1] + .5;
304        hdworld(org, hp, cgp);
305                                /* compute direction to voxel center */
306        for (i = 3; i--; )
307                vgp[i] = voxel[cap->vi].i[i] + .5;
308        hdworld(vc, hp, vgp);
309        for (i = 3; i--; )
310                vc[i] -= org[i];
311                                /* compute maximum area axis */
312        axmax = -1; avmax = 0;
313        for (i = 3; i--; ) {
314                d = vgp[i] - cgp[i];
315                if (d < 0.) d = -d;
316                if (d > avmax) {
317                        avmax = d;
318                        axmax = i;
319                }
320        }
321 #ifdef DEBUG
322        if (axmax < 0.)
323                error(CONSISTENCY, "botched axis computation in docell");
324 #endif
325                                /* compute offset vectors */
326        d = 0.5/hp->grid[j=(axmax+1)%3];
327        for (i = 3; i--; )
328                v1[i] = hp->xv[j][i] * d;
329        d = 0.5/hp->grid[j=(axmax+2)%3];
330        if (DOT(hp->wg[axmax], vc) < 0.)
331                d = -d; /* reverse vertex order */
332        for (i = 3; i--; )
333                v2[i] = hp->xv[j][i] * d;
334                                /* compute voxel pyramid */
335        for (i = 3; i--; ) {
336                dir[0][i] = vc[i] - v1[i] - v2[i];
337                dir[1][i] = vc[i] + v1[i] - v2[i];
338                dir[2][i] = vc[i] + v1[i] + v2[i];
339                dir[3][i] = vc[i] - v1[i] + v2[i];
340        }
341                                /* visit beams for opposite cells */
342        copystruct(&bo.ca, cap);
343        copystruct(&bo.gc, gcp);
344        return(visit_cells(org, dir, hp, dobeam, &bo));
345 }
261  
262 <
263 < int
264 < doview(cap)                     /* visit cells for a given view */
265 < struct cellact  *cap;
266 < {
267 <        int     orient;
353 <        FVECT   org, dir[4];
354 <                                        /* compute view pyramid */
355 <        orient = viewpyramid(org, dir, hdlist[voxel[cap->vi].hd], cap->vp);
356 <        if (!orient)
357 <                error(INTERNAL, "unusable view in doview");
358 <        cap->rev = orient < 0;
359 <                                        /* visit cells within pyramid */
360 <        return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], docell, cap));
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  
364 beam_init()                     /* clear beam list for new view(s) */
365 {
366        register int    i;
367                                        /* clear desire flags */
368        for (i = ncbeams+xcbeams; i--; )
369                cbeam[i].wants = cbeam[i].nr = 0;
370        voxel[0].hd = -1;               /* clear voxel list */
371 }
372
373
271   beam_view(vn, hr, vr)           /* add beam view (if advisable) */
272   VIEW    *vn;
273   int     hr, vr;
274   {
275 <        struct cellact  ca;
276 <        int     vfl;
275 >        int     todo[MAXTODO+1], n;
276 >        double  hdgsiz, d;
277 >        register HOLO   *hp;
278 >        register int    i;
279                                          /* sort our list */
280          cbeamsort(1);
281 <                                        /* add new voxels */
282 <        vfl = add_voxels(vn->vp);
384 <        if (!vfl)
281 >                                        /* add view to nearby sections */
282 >        if (!(n = comptodo(todo, vn)))
283                  return(0);
284 <        ca.vp = vn; ca.hr = hr; ca.vr = vr;
285 <        ca.add = 1;                     /* update our beam list */
286 <        for (ca.vi = 0; vfl; vfl >>= 1, ca.vi++)
287 <                if (vfl & 1)
288 <                        doview(&ca);
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.;                    /* compute mean grid size */
289 >        for (i = 0; i < n; i++) {
290 >                hp = hdlist[todo[i]];
291 >                hdgsiz +=       1./3. / VLEN(hp->wg[0]) +
292 >                                1./3. / VLEN(hp->wg[1]) +
293 >                                1./3. / VLEN(hp->wg[2]) ;
294 >        }
295 >        hdgsiz /= (double)n;
296 >                                        /* add to current eye position */
297 >        if (cureye.rng <= FTINY) {
298 >                VCOPY(cureye.vpt, vn->vp);
299 >                cureye.rng = MEYERNG * hdgsiz;
300 >        } else if ((d = sqrt(dist2(vn->vp,cureye.vpt))) + MEYERNG*hdgsiz >
301 >                        cureye.rng) {
302 >                for (i = 3; i--; )
303 >                        cureye.vpt[i] = 0.5*(cureye.vpt[i] + vn->vp[i]);
304 >                cureye.rng = 0.5*(cureye.rng + MEYERNG*hdgsiz + d);
305 >        }
306          return(1);
307   }
308  
# Line 396 | Line 311 | int
311   beam_sync(all)                  /* update beam list on server */
312   int     all;
313   {
314 <                                        /* sort list to put orphans at end */
315 <        cbeamsort(0);
314 >                                        /* set new eye position */
315 >        serv_request(DR_VIEWPOINT, sizeof(VIEWPOINT), (char *)&cureye);
316 >                                        /* sort list (put orphans at end) */
317 >        cbeamsort(all < 0);
318 >                                        /* send beam request */
319          if (all)
320                  cbeamop(DR_NEWSET, cbeam, ncbeams);
321          else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines