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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines