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

Comparing ray/src/hd/rhdisp3.c (file contents):
Revision 3.1 by gregl, Wed Nov 19 18:01:03 1997 UTC vs.
Revision 3.16 by greg, Tue Jun 8 19:48:30 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5 < * Holodeck beam support
5 > * Holodeck beam support for display process
6   */
7  
8   #include "rholo.h"
9   #include "rhdisp.h"
13 #include "view.h"
10  
11 + struct cellist {
12 +        GCOORD  *cl;
13 +        int     n;
14 + };
15  
16 +
17   int
18 < npixels(vp, hr, vr, hp, bi)     /* compute appropriate number to evaluate */
19 < VIEW    *vp;
18 > npixels(vp, hr, vr, hp, bi)     /* compute appropriate nrays to evaluate */
19 > register VIEW   *vp;
20   int     hr, vr;
21   HOLO    *hp;
22   int     bi;
23   {
24 +        VIEW    vrev;
25          GCOORD  gc[2];
26 <        FVECT   cp[4];
27 <        FVECT   ip[4];
26 <        double  d;
26 >        FVECT   cp[4], ip[4], pf, pb;
27 >        double  af, ab, sf2, sb2, dfb2, df2, db2, penalty;
28          register int    i;
29 +                                        /* special case */
30 +        if (hr <= 0 | vr <= 0)
31 +                return(0);
32                                          /* compute cell corners in image */
33          if (!hdbcoord(gc, hp, bi))
34                  error(CONSISTENCY, "bad beam index in npixels");
35 <        hdcell(cp, hp, gc+1);
36 <        for (i = 0; i < 4; i++) {
35 >        hdcell(cp, hp, gc+1);           /* find cell on front image */
36 >        for (i = 3; i--; )              /* compute front center */
37 >                pf[i] = 0.5*(cp[0][i] + cp[2][i]);
38 >        sf2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */
39 >        for (i = 0; i < 4; i++) {       /* compute visible quad */
40                  viewloc(ip[i], vp, cp[i]);
41 <                if (ip[i][2] < 0.)
42 <                        return(0);
41 >                if (ip[i][2] < 0.) {
42 >                        af = 0;
43 >                        goto getback;
44 >                }
45                  ip[i][0] *= (double)hr; /* scale by resolution */
46                  ip[i][1] *= (double)vr;
47          }
48 <                                        /* compute quad area */
49 <        d = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) -
48 >                                        /* compute front area */
49 >        af = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) -
50                  (ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]);
51 <        d += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) -
51 >        af += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) -
52                  (ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]);
53 <        if (d < 0)
54 <                d = -d;
55 <                                        /* round off result */
56 <        return((int)(.5*d+.5));
53 >        af *= af >= 0 ? 0.5 : -0.5;
54 > getback:
55 >        vrev = *vp;             /* compute reverse view */
56 >        for (i = 0; i < 3; i++) {
57 >                vrev.vdir[i] = -vp->vdir[i];
58 >                vrev.vup[i] = -vp->vup[i];
59 >                vrev.hvec[i] = -vp->hvec[i];
60 >                vrev.vvec[i] = -vp->vvec[i];
61 >        }
62 >        hdcell(cp, hp, gc);             /* find cell on back image */
63 >        for (i = 3; i--; )              /* compute rear center */
64 >                pb[i] = 0.5*(cp[0][i] + cp[2][i]);
65 >        sb2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */
66 >        for (i = 0; i < 4; i++) {       /* compute visible quad */
67 >                viewloc(ip[i], &vrev, cp[i]);
68 >                if (ip[i][2] < 0.) {
69 >                        ab = 0;
70 >                        goto finish;
71 >                }
72 >                ip[i][0] *= (double)hr; /* scale by resolution */
73 >                ip[i][1] *= (double)vr;
74 >        }
75 >                                        /* compute back area */
76 >        ab = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) -
77 >                (ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]);
78 >        ab += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) -
79 >                (ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]);
80 >        ab *= ab >= 0 ? 0.5 : -0.5;
81 > finish:         /* compute penalty based on dist. sightline - viewpoint */
82 >        df2 = dist2(vp->vp, pf);
83 >        db2 = dist2(vp->vp, pb);
84 >        dfb2 = dist2(pf, pb);
85 >        penalty = dfb2 + df2 - db2;
86 >        penalty = df2 - 0.25*penalty*penalty/dfb2;
87 >        if (df2 > db2)  penalty /= df2 <= dfb2 ? sb2 : sb2*df2/dfb2;
88 >        else            penalty /= db2 <= dfb2 ? sf2 : sf2*db2/dfb2;
89 >        if (penalty < 1.) penalty = 1.;
90 >                                        /* round off smaller non-zero area */
91 >        if (ab <= FTINY || (af > FTINY && af <= ab))
92 >                return((int)(af/penalty + 0.5));
93 >        return((int)(ab/penalty + 0.5));
94   }
95  
96  
# Line 52 | Line 98 | int    bi;
98   * The ray directions that define the pyramid in visit_cells() needn't
99   * be normalized, but they must be given in clockwise order as seen
100   * from the pyramid's apex (origin).
101 + * If no cell centers fall within the domain, the closest cell is visited.
102   */
103   int
104   visit_cells(orig, pyrd, hp, vf, dp)     /* visit cells within a pyramid */
105   FVECT   orig, pyrd[4];          /* pyramid ray directions in clockwise order */
106 < HOLO    *hp;
106 > register HOLO   *hp;
107   int     (*vf)();
108   char    *dp;
109   {
110 <        int     n = 0;
110 >        int     ncalls = 0, n = 0;
111          int     inflags = 0;
112          FVECT   gp, pn[4], lo, ld;
113          double  po[4], lbeg, lend, d, t;
114 <        GCOORD  gc;
114 >        GCOORD  gc, gc2[2];
115          register int    i;
116                                          /* figure out whose side we're on */
117          hdgrid(gp, hp, orig);
# Line 82 | Line 129 | char   *dp;
129                  if (!(inflags & 1<<gc.w))       /* origin on wrong side */
130                          continue;
131                                          /* scanline algorithm */
132 <                for (gc.i[1] = hp->grid[((gc.w>>1)+2)%3]; gc.i[1]--; ) {
132 >                for (gc.i[1] = hp->grid[hdwg1[gc.w]]; gc.i[1]--; ) {
133                                                  /* compute scanline */
134                          gp[gc.w>>1] = gc.w&1 ? hp->grid[gc.w>>1] : 0;
135 <                        gp[((gc.w>>1)+1)%3] = 0;
136 <                        gp[((gc.w>>1)+2)%3] = gc.i[1] + 0.5;
135 >                        gp[hdwg0[gc.w]] = 0;
136 >                        gp[hdwg1[gc.w]] = gc.i[1] + 0.5;
137                          hdworld(lo, hp, gp);
138 <                        gp[((gc.w>>1)+1)%3] = 1;
138 >                        gp[hdwg0[gc.w]] = 1;
139                          hdworld(ld, hp, gp);
140 <                        ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[1];
140 >                        ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2];
141                                                  /* find scanline limits */
142 <                        lbeg = 0; lend = hp->grid[((gc.w>>1)+1)%3];
142 >                        lbeg = 0; lend = hp->grid[hdwg0[gc.w]];
143                          for (i = 0; i < 4; i++) {
144                                  t = DOT(pn[i], lo) - po[i];
145                                  d = -DOT(pn[i], ld);
146 <                                if (d <= FTINY && d >= -FTINY) {
100 <                                        if (t < 0)
101 <                                                goto nextscan;
102 <                                        continue;
103 <                                }
104 <                                if (t > 0) {
146 >                                if (d > FTINY) {                /* <- plane */
147                                          if ((t /= d) < lend)
148                                                  lend = t;
149 <                                } else {
149 >                                } else if (d < -FTINY) {        /* plane -> */
150                                          if ((t /= d) > lbeg)
151                                                  lbeg = t;
152 +                                } else if (t < 0) {             /* outside */
153 +                                        lend = -1;
154 +                                        break;
155                                  }
156                          }
157 +                        if (lbeg >= lend)
158 +                                continue;
159                          i = lend + .5;          /* visit cells on this scan */
160 <                        for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++)
160 >                        for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) {
161                                  n += (*vf)(&gc, dp);
162 <                nextscan:;
162 >                                ncalls++;
163 >                        }
164                  }
165          }
166 <        return(n);
166 >        if (ncalls)                     /* got one at least */
167 >                return(n);
168 >                                        /* else find closest cell */
169 >        VSUM(ld, pyrd[0], pyrd[1], 1.);
170 >        VSUM(ld, ld, pyrd[2], 1.);
171 >        VSUM(ld, ld, pyrd[3], 1.);
172 > #if 0
173 >        if (normalize(ld) == 0.0)       /* technically not necessary */
174 >                return(0);
175 > #endif
176 >        d = hdinter(gc2, NULL, &t, hp, orig, ld);
177 >        if (d >= FHUGE || t <= 0.)
178 >                return(0);
179 >        return((*vf)(gc2+1, dp));       /* visit it */
180   }
181  
182  
183 + sect_behind(hp, vp)             /* check if section is "behind" viewpoint */
184 + register HOLO   *hp;
185 + register VIEW   *vp;
186 + {
187 +        FVECT   hcent;
188 +                                        /* compute holodeck section center */
189 +        VSUM(hcent, hp->orig, hp->xv[0], 0.5);
190 +        VSUM(hcent, hcent, hp->xv[1], 0.5);
191 +        VSUM(hcent, hcent, hp->xv[2], 0.5);
192 +                                        /* behind if center is behind */
193 +        return(DOT(vp->vdir,hcent) < DOT(vp->vdir,vp->vp));
194 + }
195 +
196 +
197 + viewpyramid(org, dir, hp, vp)   /* compute view pyramid */
198 + FVECT   org, dir[4];
199 + HOLO    *hp;
200 + VIEW    *vp;
201 + {
202 +        register int    i;
203 +                                        /* check view type */
204 +        if (vp->type == VT_PAR)
205 +                return(0);
206 +                                        /* in front or behind? */
207 +        if (!sect_behind(hp, vp)) {
208 +                if (viewray(org, dir[0], vp, 0., 0.) < -FTINY)
209 +                        return(0);
210 +                if (viewray(org, dir[1], vp, 0., 1.) < -FTINY)
211 +                        return(0);
212 +                if (viewray(org, dir[2], vp, 1., 1.) < -FTINY)
213 +                        return(0);
214 +                if (viewray(org, dir[3], vp, 1., 0.) < -FTINY)
215 +                        return(0);
216 +                return(1);
217 +        }                               /* reverse pyramid */
218 +        if (viewray(org, dir[3], vp, 0., 0.) < -FTINY)
219 +                return(0);
220 +        if (viewray(org, dir[2], vp, 0., 1.) < -FTINY)
221 +                return(0);
222 +        if (viewray(org, dir[1], vp, 1., 1.) < -FTINY)
223 +                return(0);
224 +        if (viewray(org, dir[0], vp, 1., 0.) < -FTINY)
225 +                return(0);
226 +        for (i = 0; i < 3; i++) {
227 +                dir[0][i] = -dir[0][i];
228 +                dir[1][i] = -dir[1][i];
229 +                dir[2][i] = -dir[2][i];
230 +                dir[3][i] = -dir[3][i];
231 +        }
232 +        return(-1);
233 + }
234 +
235 +
236   int
237   addcell(gcp, cl)                /* add a cell to a list */
238   GCOORD  *gcp;
239 < register int    *cl;
239 > register struct cellist *cl;
240   {
241 <        copystruct((GCOORD *)(cl+1) + *cl, gcp);
242 <        (*cl)++;
241 >        *(cl->cl+cl->n) = *gcp;
242 >        cl->n++;
243          return(1);
244   }
245  
# Line 144 | Line 258 | register GCOORD        *gcp1, *gcp2;
258   }
259  
260  
261 < int *
262 < getviewcells(hp, vp)            /* get ordered cell list for section view */
261 > GCOORD *
262 > getviewcells(np, hp, vp)        /* get ordered cell list for section view */
263 > int     *np;            /* returned number of cells (negative if reversed) */
264   register HOLO   *hp;
265   VIEW    *vp;
266   {
267          FVECT   org, dir[4];
268 <        int     n;
269 <        register int    *cl;
268 >        int     orient;
269 >        struct cellist  cl;
270                                          /* compute view pyramid */
271 <        if (vp->type == VT_PAR) goto viewerr;
272 <        if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr;
273 <        if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr;
274 <        if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr;
160 <        if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr;
271 >        *np = 0;
272 >        orient = viewpyramid(org, dir, hp, vp);
273 >        if (!orient)
274 >                return(NULL);
275                                          /* allocate enough list space */
276 <        n = 2*( hp->grid[0]*hp->grid[1] +
277 <                hp->grid[0]*hp->grid[2] +
278 <                hp->grid[1]*hp->grid[2] );
279 <        cl = (int *)malloc(sizeof(int) + n*sizeof(GCOORD));
280 <        if (cl == NULL)
276 >        cl.n = 2*(      hp->grid[0]*hp->grid[1] +
277 >                        hp->grid[0]*hp->grid[2] +
278 >                        hp->grid[1]*hp->grid[2] );
279 >        cl.cl = (GCOORD *)malloc(cl.n*sizeof(GCOORD));
280 >        if (cl.cl == NULL)
281                  goto memerr;
282 <        *cl = 0;
283 <                                        /* add cells within pyramid */
284 <        visit_cells(org, dir, hp, addcell, cl);
285 <        if (!*cl) {
172 <                free((char *)cl);
282 >        cl.n = 0;                       /* add cells within pyramid */
283 >        visit_cells(org, dir, hp, addcell, (char *)&cl);
284 >        if (!cl.n) {
285 >                free((void *)cl.cl);
286                  return(NULL);
287          }
288 +        *np = cl.n * orient;
289   #if 0
290 <        /* We're just going to free this memory in a moment, and list is */
291 <        /* sorted automatically by visit_cells(), so we don't need this. */
292 <        if (*cl < n) {                  /* optimize memory use */
293 <                cl = (int *)realloc((char *)cl,
294 <                                sizeof(int) + *cl*sizeof(GCOORD));
295 <                if (cl == NULL)
296 <                        goto memerr;
183 <        }
290 >        /* We're just going to free this memory in a moment, and list is
291 >         * sorted automatically by visit_cells(), so we don't need this.
292 >         */
293 >                                        /* optimize memory use */
294 >        cl.cl = (GCOORD *)realloc((void *)cl.cl, cl.n*sizeof(GCOORD));
295 >        if (cl.cl == NULL)
296 >                goto memerr;
297                                          /* sort the list */
298 <        qsort((char *)(cl+1), *cl, sizeof(GCOORD), cellcmp);
298 >        qsort((char *)cl.cl, cl.n, sizeof(GCOORD), cellcmp);
299   #endif
300 <        return(cl);
188 < viewerr:
189 <        error(INTERNAL, "unusable view in getviewcells");
300 >        return(cl.cl);
301   memerr:
302          error(SYSTEM, "out of memory in getviewcells");
303 + }
304 +
305 +
306 + extern void
307 + gridlines(                      /* run through holodeck section grid lines */
308 +        void    (*f)(FVECT wp[2])
309 + )
310 + {
311 +        register int    hd, w, i;
312 +        int     g0, g1;
313 +        FVECT   wp[2], mov;
314 +        double  d;
315 +                                        /* do each wall on each section */
316 +        for (hd = 0; hdlist[hd] != NULL; hd++)
317 +                for (w = 0; w < 6; w++) {
318 +                        g0 = hdwg0[w];
319 +                        g1 = hdwg1[w];
320 +                        d = 1.0/hdlist[hd]->grid[g0];
321 +                        mov[0] = d * hdlist[hd]->xv[g0][0];
322 +                        mov[1] = d * hdlist[hd]->xv[g0][1];
323 +                        mov[2] = d * hdlist[hd]->xv[g0][2];
324 +                        if (w & 1) {
325 +                                VSUM(wp[0], hdlist[hd]->orig,
326 +                                                hdlist[hd]->xv[w>>1], 1.);
327 +                                VSUM(wp[0], wp[0], mov, 1.);
328 +                        } else
329 +                                VCOPY(wp[0], hdlist[hd]->orig);
330 +                        VSUM(wp[1], wp[0], hdlist[hd]->xv[g1], 1.);
331 +                        for (i = hdlist[hd]->grid[g0]; ; ) {    /* g0 lines */
332 +                                (*f)(wp);
333 +                                if (!--i) break;
334 +                                wp[0][0] += mov[0]; wp[0][1] += mov[1];
335 +                                wp[0][2] += mov[2]; wp[1][0] += mov[0];
336 +                                wp[1][1] += mov[1]; wp[1][2] += mov[2];
337 +                        }
338 +                        d = 1.0/hdlist[hd]->grid[g1];
339 +                        mov[0] = d * hdlist[hd]->xv[g1][0];
340 +                        mov[1] = d * hdlist[hd]->xv[g1][1];
341 +                        mov[2] = d * hdlist[hd]->xv[g1][2];
342 +                        if (w & 1)
343 +                                VSUM(wp[0], hdlist[hd]->orig,
344 +                                                hdlist[hd]->xv[w>>1], 1.);
345 +                        else
346 +                                VSUM(wp[0], hdlist[hd]->orig, mov, 1.);
347 +                        VSUM(wp[1], wp[0], hdlist[hd]->xv[g0], 1.);
348 +                        for (i = hdlist[hd]->grid[g1]; ; ) {    /* g1 lines */
349 +                                (*f)(wp);
350 +                                if (!--i) break;
351 +                                wp[0][0] += mov[0]; wp[0][1] += mov[1];
352 +                                wp[0][2] += mov[2]; wp[1][0] += mov[0];
353 +                                wp[1][1] += mov[1]; wp[1][2] += mov[2];
354 +                        }
355 +                }
356   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines