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

Comparing ray/src/hd/rholo3.c (file contents):
Revision 3.11 by gregl, Wed Nov 26 21:34:28 1997 UTC vs.
Revision 3.18 by gregl, Mon Jan 5 16:46:34 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 27 | Line 27 | register PACKHEAD      *b0, *b1;
27   }
28  
29  
30 + int
31 + dispbeam(b, hp, bi)                     /* display a holodeck beam */
32 + register BEAM   *b;
33 + HOLO    *hp;
34 + int     bi;
35 + {
36 +        static int      n = 0;
37 +        static PACKHEAD *p = NULL;
38 +
39 +        if (b == NULL)
40 +                return;
41 +        if (b->nrm > n) {               /* (re)allocate packet holder */
42 +                n = b->nrm;
43 +                if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n));
44 +                else p = (PACKHEAD *)realloc((char *)p, packsiz(n));
45 +                if (p == NULL)
46 +                        error(SYSTEM, "out of memory in dispbeam");
47 +        }
48 +                                        /* assign packet fields */
49 +        bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL));
50 +        p->nr = p->nc = b->nrm;
51 +        for (p->hd = 0; hdlist[p->hd] != hp; p->hd++)
52 +                if (hdlist[p->hd] == NULL)
53 +                        error(CONSISTENCY, "unregistered holodeck in dispbeam");
54 +        p->bi = bi;
55 +        disp_packet(p);                 /* display it */
56 + }
57 +
58 +
59   bundle_set(op, clist, nents)    /* bundle set operation */
60   int     op;
61 < PACKHEAD        *clist;
61 > register PACKHEAD       *clist;
62   int     nents;
63   {
35        BEAM    *b;
36        PACKHEAD        *p;
64          register int    i, n;
65  
66          switch (op) {
# Line 50 | Line 77 | int    nents;
77                  if (complist == NULL)
78                          goto memerr;
79                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
80 <                complen = nents;
54 <                listpos = 0;
55 <                lastin = -1;            /* flag for initial sort */
80 >                complen = nents;        /* finish initialization below */
81                  break;
82          case BS_ADD:                    /* add to computation set */
83          case BS_ADJ:                    /* adjust set quantities */
84                  if (nents <= 0)
85                          return;
86                                          /* merge any common members */
87 <                for (i = 0; i < complen; i++) {
88 <                        for (n = 0; n < nents; n++)
87 >                for (n = 0; n < nents; n++) {
88 >                        for (i = 0; i < complen; i++)
89                                  if (clist[n].bi == complist[i].bi &&
90                                                  clist[n].hd == complist[i].hd) {
91 +                                        int     oldnr = complist[i].nr;
92                                          if (op == BS_ADD)
93                                                  complist[i].nr += clist[n].nr;
94                                          else /* op == BS_ADJ */
95                                                  complist[i].nr = clist[n].nr;
96                                          clist[n].nr = 0;
97 <                                        clist[n].nc = 1;
98 <                                        lastin = -1;    /* flag full sort */
97 >                                        clist[n].nc = complist[i].nc;
98 >                                        if (complist[i].nr != oldnr)
99 >                                                lastin = -1;    /* flag sort */
100                                          break;
101                                  }
102 <                        if (n >= nents)
103 <                                clist[i].nc = bnrays(hdlist[clist[i].hd],
104 <                                                clist[i].bi);
102 >                        if (i >= complen)
103 >                                clist[n].nc = bnrays(hdlist[clist[n].hd],
104 >                                                clist[n].bi);
105                  }
106                                          /* sort updated list */
107                  sortcomplist();
# Line 121 | Line 148 | int    nents;
148          default:
149                  error(CONSISTENCY, "bundle_set called with unknown operation");
150          }
151 <        if (outdev == NULL)
152 <                return;
153 <        n = 32*RPACKSIZ;                /* allocate packet holder */
154 <        p = (PACKHEAD *)malloc(packsiz(n));
155 <        if (p == NULL)
156 <                goto memerr;
157 <                                        /* display what we have */
131 <        for (i = 0; i < nents; i++)
132 <                if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
133 <                        if (b->nrm > n) {
134 <                                n = b->nrm;
135 <                                p = (PACKHEAD *)realloc((char *)p, packsiz(n));
136 <                                if (p == NULL)
137 <                                        goto memerr;
138 <                        }
139 <                        bcopy((char *)hdbray(b), (char *)packra(p),
140 <                                        b->nrm*sizeof(RAYVAL));
141 <                        p->hd = clist[i].hd;
142 <                        p->bi = clist[i].bi;
143 <                        p->nr = p->nc = b->nrm;
144 <                        disp_packet(p);
151 >        if (outdev != NULL) {           /* load and display beams we have */
152 >                register HDBEAMI        *hb;
153 >
154 >                hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
155 >                for (i = 0; i < nents; i++) {
156 >                        hb[i].h = hdlist[clist[i].hd];
157 >                        hb[i].b = clist[i].bi;
158                  }
159 <        free((char *)p);                /* clean up */
159 >                hdloadbeams(hb, nents, dispbeam);
160 >                free((char *)hb);
161 >        }
162          if (op == BS_NEW) {
163                  done_packets(flush_queue());    /* empty queue, so we can... */
164                  for (i = 0; i < complen; i++)   /* ...get number computed */
165                          complist[i].nc = bnrays(hdlist[complist[i].hd],
166                                                  complist[i].bi);
167 +                listpos = 0;
168 +                lastin = -1;            /* flag for initial sort */
169          }
170          return;
171   memerr:
# Line 156 | Line 173 | memerr:
173   }
174  
175  
176 < int
177 < weightf(hp, x0, x1, x2)         /* voxel weighting function */
161 < register HOLO   *hp;
162 < register int    x0, x1, x2;
163 < {
164 <        switch (vlet(OCCUPANCY)) {
165 <        case 'U':               /* uniform weighting */
166 <                return(1);
167 <        case 'C':               /* center weighting (crude) */
168 <                x0 += x0 - hp->grid[0] + 1;
169 <                x0 = abs(x0)*hp->grid[1]*hp->grid[2];
170 <                x1 += x1 - hp->grid[1] + 1;
171 <                x1 = abs(x1)*hp->grid[0]*hp->grid[2];
172 <                x2 += x2 - hp->grid[2] + 1;
173 <                x2 = abs(x2)*hp->grid[0]*hp->grid[1];
174 <                return(hp->grid[0]*hp->grid[1]*hp->grid[2] -
175 <                                (x0+x1+x2)/3);
176 <        default:
177 <                badvalue(OCCUPANCY);
178 <        }
179 < }
180 <
181 <
182 < /* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */
183 < long
184 < lineweight(hp, x, y, z, dx, dy, dz)     /* compute weights along a line */
176 > double
177 > beamvolume(hp, bi)      /* compute approximate volume of a beam */
178   HOLO    *hp;
179 < int     x, y, z, dx, dy, dz;
179 > int     bi;
180   {
181 <        long    wres = 0;
182 <        int     n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
183 <
184 <        sx = sgn(dx);   sy = sgn(dy);   sz = sgn(dz);
185 <        ax = abs(dx);   ay = abs(dy);   az = abs(dz);
186 <        bx = 2*ax;      by = 2*ay;      bz = 2*az;
187 <        exy = ay-ax;    exz = az-ax;    ezy = ay-az;
188 <        n = ax+ay+az + 1;               /* added increment to visit last */
189 <        while (n--) {
190 <                wres += weightf(hp, x, y, z);
191 <                if (exy < 0) {
192 <                        if (exz < 0) {
193 <                                x += sx;
194 <                                exy += by; exz += bz;
195 <                        } else {
196 <                                z += sz;
197 <                                exz -= bx; ezy += by;
205 <                        }
206 <                } else {
207 <                        if (ezy < 0) {
208 <                                z += sz;
209 <                                exz -= bx; ezy += by;
210 <                        } else {
211 <                                y += sy;
212 <                                exy -= bx; ezy -= bz;
213 <                        }
214 <                }
181 >        GCOORD  gc[2];
182 >        FVECT   cp[4], edgeA, edgeB, cent[2];
183 >        FVECT   v, crossp[2], diffv;
184 >        double  vol[2];
185 >        register int    i;
186 >                                        /* get grid coordinates */
187 >        if (!hdbcoord(gc, hp, bi))
188 >                error(CONSISTENCY, "bad beam index in beamvolume");
189 >        for (i = 0; i < 2; i++) {       /* compute cell area vectors */
190 >                hdcell(cp, hp, gc+i);
191 >                VSUM(edgeA, cp[1], cp[0], -1.0);
192 >                VSUM(edgeB, cp[3], cp[1], -1.0);
193 >                fcross(crossp[i], edgeA, edgeB);
194 >                                        /* compute center */
195 >                cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
196 >                cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
197 >                cent[i][2] = 0.5*(cp[0][2] + cp[2][2]);
198          }
199 <        return(wres);
199 >                                        /* compute difference vector */
200 >        VSUM(diffv, cent[1], cent[0], -1.0);
201 >        for (i = 0; i < 2; i++) {       /* compute volume contributions */
202 >                vol[i] = 0.5*DOT(crossp[i], diffv);
203 >                if (vol[i] < 0.) vol[i] = -vol[i];
204 >        }
205 >        return(vol[0] + vol[1]);        /* return total volume */
206   }
207  
208  
209   init_global()                   /* initialize global ray computation */
210   {
211          long    wtotal = 0;
223        int     i, j;
224        int     lseg[2][3];
212          double  frac;
213 <        register int    k;
214 <                                        /* free old list */
215 <        if (complen > 0)
213 >        int     i;
214 >        register int    j, k;
215 >                                        /* free old list and empty queue */
216 >        if (complen > 0) {
217                  free((char *)complist);
218 +                done_packets(flush_queue());
219 +        }
220                                          /* allocate beam list */
221          complen = 0;
222          for (j = 0; hdlist[j] != NULL; j++)
# Line 236 | Line 226 | init_global()                  /* initialize global ray computation *
226                  error(SYSTEM, "out of memory in init_global");
227                                          /* compute beam weights */
228          k = 0;
229 <        for (j = 0; hdlist[j] != NULL; j++)
229 >        for (j = 0; hdlist[j] != NULL; j++) {
230 >                frac = 512. * hdlist[j]->wg[0] *
231 >                                hdlist[j]->wg[1] * hdlist[j]->wg[2];
232 >                if (frac < 0.) frac = -frac;
233                  for (i = nbeams(hdlist[j]); i > 0; i--) {
241                        hdlseg(lseg, hdlist[j], i);
234                          complist[k].hd = j;
235                          complist[k].bi = i;
236 <                        complist[k].nr = lineweight( hdlist[j],
237 <                                        lseg[0][0], lseg[0][1], lseg[0][2],
246 <                                        lseg[1][0] - lseg[0][0],
247 <                                        lseg[1][1] - lseg[0][1],
248 <                                        lseg[1][2] - lseg[0][2] );
236 >                        complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
237 >                        complist[k].nc = bnrays(hdlist[j], i);
238                          wtotal += complist[k++].nr;
239                  }
240 +        }
241                                          /* adjust weights */
242 <        if (vdef(DISKSPACE)) {
242 >        if (vdef(DISKSPACE))
243                  frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
244 <                if (frac < 0.95 | frac > 1.05)
245 <                        while (k--)
246 <                                complist[k].nr = frac * complist[k].nr;
247 <        }
244 >        else
245 >                frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
246 >        while (k--)
247 >                complist[k].nr = frac * complist[k].nr;
248          listpos = 0; lastin = -1;       /* flag initial sort */
249   }
250  
251  
252   mergeclists(cdest, cl1, n1, cl2, n2)    /* merge two sorted lists */
253 < PACKHEAD        *cdest;
254 < PACKHEAD        *cl1, *cl2;
253 > register PACKHEAD       *cdest;
254 > register PACKHEAD       *cl1, *cl2;
255   int     n1, n2;
256   {
257 <        int     cmp;
257 >        register int    cmp;
258  
259          while (n1 | n2) {
260                  if (!n1) cmp = 1;
# Line 329 | Line 319 | sortcomplist()                 /* fix our list order */
319   * list and start again from the beginning.  Since
320   * a merge sort is used, the sorting costs are minimal.
321   */
322 < next_packet(p)                  /* prepare packet for computation */
322 > next_packet(p, n)               /* prepare packet for computation */
323   register PACKET *p;
324 + int     n;
325   {
326          register int    i;
327  
# Line 344 | Line 335 | register PACKET        *p;
335          p->nr = complist[listpos].nr - p->nc;
336          if (p->nr <= 0)
337                  return(0);
338 <        if (p->nr > RPACKSIZ)
339 <                p->nr = RPACKSIZ;
338 > #ifdef DEBUG
339 >        if (n < 1 | n > RPACKSIZ)
340 >                error(CONSISTENCY, "next_packet called with bad n value");
341 > #endif
342 >        if (p->nr > n)
343 >                p->nr = n;
344          complist[listpos].nc += p->nr;  /* find where this one would go */
345          while (lastin > listpos &&
346                          beamcmp(complist+lastin, complist+listpos) > 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines