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.8 by gregl, Thu Nov 20 11:39:57 1997 UTC vs.
Revision 3.20 by gregl, Thu Jan 22 11:25:51 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 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10  
11   #include "rholo.h"
12  
13
13   #define abs(x)          ((x) > 0 ? (x) : -(x))
14   #define sgn(x)          ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
15  
17
16   static PACKHEAD *complist=NULL; /* list of beams to compute */
17   static int      complen=0;      /* length of complist */
18   static int      listpos=0;      /* current list position for next_packet */
# Line 25 | Line 23 | int
23   beamcmp(b0, b1)                 /* comparison for descending compute order */
24   register PACKHEAD       *b0, *b1;
25   {
26 <        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
29 <                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
26 >        return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) );
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   {
64 <        BEAM    *b;
39 <        PACKHEAD        *p;
64 >        int     oldnr;
65          register int    i, n;
66 <
66 >                                /* look for common members */
67 >        for (n = 0; n < nents; n++) {
68 >                for (i = 0; i < complen; i++)
69 >                        if (clist[n].bi == complist[i].bi &&
70 >                                        clist[n].hd == complist[i].hd) {
71 >                                oldnr = complist[i].nr;
72 >                                clist[n].nc = complist[i].nc;
73 >                                switch (op) {
74 >                                case BS_ADD:            /* add to count */
75 >                                        complist[i].nr += clist[n].nr;
76 >                                        clist[n].nr = 0;
77 >                                        break;
78 >                                case BS_ADJ:            /* reset count */
79 >                                        complist[i].nr = clist[n].nr;
80 >                                        clist[n].nr = 0;
81 >                                        break;
82 >                                case BS_DEL:            /* delete count */
83 >                                        if (clist[n].nr == 0 ||
84 >                                                clist[n].nr >= complist[i].nr)
85 >                                                complist[i].nr = 0;
86 >                                        else
87 >                                                complist[i].nr -= clist[n].nr;
88 >                                        break;
89 >                                }
90 >                                if (complist[i].nr != oldnr)
91 >                                        lastin = -1;    /* flag sort */
92 >                                break;
93 >                        }
94 >                if (i >= complen)
95 >                        clist[n].nc = bnrays(hdlist[clist[n].hd], clist[n].bi);
96 >        }
97 >                                /* complete list operations */
98          switch (op) {
99          case BS_NEW:                    /* new computation set */
100 <                if (complen)
100 >                listpos = 0;
101 >                lastin = -1;
102 >                if (complen)            /* free old list */
103                          free((char *)complist);
104 <                if (nents <= 0) {
105 <                        complist = NULL;
48 <                        listpos = complen = 0;
49 <                        lastin = -1;
104 >                complist = NULL;
105 >                if (!(complen = nents))
106                          return;
51                }
107                  complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
108                  if (complist == NULL)
109                          goto memerr;
110                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
56                complen = nents;
57                listpos = 0;
58                lastin = -1;            /* flag for initial sort */
111                  break;
112          case BS_ADD:                    /* add to computation set */
113 +        case BS_ADJ:                    /* adjust set quantities */
114                  if (nents <= 0)
115                          return;
116 <                                        /* merge any common members */
64 <                for (i = 0; i < complen; i++)
65 <                        for (n = 0; n < nents; n++)
66 <                                if (clist[n].bi == complist[i].bi &&
67 <                                                clist[n].hd == complist[i].hd) {
68 <                                        complist[i].nr += clist[n].nr;
69 <                                        clist[n].nr = 0;
70 <                                        lastin = -1;    /* flag full sort */
71 <                                        break;
72 <                                }
73 <                                        /* sort updated list */
74 <                sortcomplist();
75 <                                        /* sort new entries */
116 >                sortcomplist();         /* sort updated list & new entries */
117                  qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
118                                          /* what can't we satisfy? */
119 <                for (n = 0; n < nents && clist[n].nr >
79 <                                bnrays(hdlist[clist[n].hd],clist[n].bi); n++)
119 >                for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
120                          ;
121 +                if (op == BS_ADJ)
122 +                        nents = n;
123                  if (n) {                /* allocate space for merged list */
124                          PACKHEAD        *newlist;
125                          newlist = (PACKHEAD *)malloc(
# Line 95 | Line 137 | int    nents;
137                  lastin = complen-1;     /* list is now sorted */
138                  break;
139          case BS_DEL:                    /* delete from computation set */
140 <                if (nents <= 0)
99 <                        return;
100 <                                        /* find each member */
101 <                for (i = 0; i < complen; i++)
102 <                        for (n = 0; n < nents; n++)
103 <                                if (clist[n].bi == complist[i].bi &&
104 <                                                clist[n].hd == complist[i].hd) {
105 <                                        if (clist[n].nr == 0 ||
106 <                                                clist[n].nr >= complist[i].nr)
107 <                                                complist[i].nr = 0;
108 <                                        else
109 <                                                complist[i].nr -= clist[n].nr;
110 <                                        lastin = -1;    /* flag full sort */
111 <                                        break;
112 <                                }
113 <                if (lastin < 0)         /* sort updated list */
114 <                        sortcomplist();
115 <                return;                 /* no display */
140 >                return;                 /* already done */
141          default:
142                  error(CONSISTENCY, "bundle_set called with unknown operation");
143          }
144 <        if (outdev == NULL)
145 <                return;
146 <        n = RPACKSIZ;                           /* allocate packet holder */
147 <        for (i = 0; i < nents; i++)
148 <                if (clist[i].nr > n)
149 <                        n = clist[i].nr;
150 <        p = (PACKHEAD *)malloc(packsiz(n));
126 <        if (p == NULL)
127 <                goto memerr;
128 <                                        /* display what we have */
129 <        for (i = 0; i < nents; i++)
130 <                if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
131 <                        if (b->nrm > n) {
132 <                                n = b->nrm;
133 <                                p = (PACKHEAD *)realloc((char *)p, packsiz(n));
134 <                                if (p == NULL)
135 <                                        goto memerr;
136 <                        }
137 <                        bcopy((char *)hdbray(b), (char *)packra(p),
138 <                                        (p->nr=b->nrm)*sizeof(RAYVAL));
139 <                        p->hd = clist[i].hd;
140 <                        p->bi = clist[i].bi;
141 <                        disp_packet(p);
144 >        if (outdev != NULL) {           /* load and display beams we have */
145 >                register HDBEAMI        *hb;
146 >
147 >                hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
148 >                for (i = 0; i < nents; i++) {
149 >                        hb[i].h = hdlist[clist[i].hd];
150 >                        hb[i].b = clist[i].bi;
151                  }
152 <        free((char *)p);                /* clean up */
152 >                hdloadbeams(hb, nents, dispbeam);
153 >                free((char *)hb);
154 >        }
155          return;
156   memerr:
157          error(SYSTEM, "out of memory in bundle_set");
158   }
159  
160  
161 < int
162 < weightf(hp, x0, x1, x2)         /* voxel weighting function */
152 < register HOLO   *hp;
153 < register int    x0, x1, x2;
154 < {
155 <        switch (vlet(OCCUPANCY)) {
156 <        case 'U':               /* uniform weighting */
157 <                return(1);
158 <        case 'C':               /* center weighting (crude) */
159 <                x0 += x0 - hp->grid[0] + 1;
160 <                x0 = abs(x0)*hp->grid[1]*hp->grid[2];
161 <                x1 += x1 - hp->grid[1] + 1;
162 <                x1 = abs(x1)*hp->grid[0]*hp->grid[2];
163 <                x2 += x2 - hp->grid[2] + 1;
164 <                x2 = abs(x2)*hp->grid[0]*hp->grid[1];
165 <                return(hp->grid[0]*hp->grid[1]*hp->grid[2] -
166 <                                (x0+x1+x2)/3);
167 <        default:
168 <                badvalue(OCCUPANCY);
169 <        }
170 < }
171 <
172 <
173 < /* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */
174 < long
175 < lineweight(hp, x, y, z, dx, dy, dz)     /* compute weights along a line */
161 > double
162 > beamvolume(hp, bi)      /* compute approximate volume of a beam */
163   HOLO    *hp;
164 < int     x, y, z, dx, dy, dz;
164 > int     bi;
165   {
166 <        long    wres = 0;
167 <        int     n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
168 <
169 <        sx = sgn(dx);   sy = sgn(dy);   sz = sgn(dz);
170 <        ax = abs(dx);   ay = abs(dy);   az = abs(dz);
171 <        bx = 2*ax;      by = 2*ay;      bz = 2*az;
172 <        exy = ay-ax;    exz = az-ax;    ezy = ay-az;
173 <        n = ax+ay+az + 1;               /* added increment to visit last */
174 <        while (n--) {
175 <                wres += weightf(hp, x, y, z);
176 <                if (exy < 0) {
177 <                        if (exz < 0) {
178 <                                x += sx;
179 <                                exy += by; exz += bz;
180 <                        } else {
181 <                                z += sz;
182 <                                exz -= bx; ezy += by;
196 <                        }
197 <                } else {
198 <                        if (ezy < 0) {
199 <                                z += sz;
200 <                                exz -= bx; ezy += by;
201 <                        } else {
202 <                                y += sy;
203 <                                exy -= bx; ezy -= bz;
204 <                        }
205 <                }
166 >        GCOORD  gc[2];
167 >        FVECT   cp[4], edgeA, edgeB, cent[2];
168 >        FVECT   v, crossp[2], diffv;
169 >        double  vol[2];
170 >        register int    i;
171 >                                        /* get grid coordinates */
172 >        if (!hdbcoord(gc, hp, bi))
173 >                error(CONSISTENCY, "bad beam index in beamvolume");
174 >        for (i = 0; i < 2; i++) {       /* compute cell area vectors */
175 >                hdcell(cp, hp, gc+i);
176 >                VSUM(edgeA, cp[1], cp[0], -1.0);
177 >                VSUM(edgeB, cp[3], cp[1], -1.0);
178 >                fcross(crossp[i], edgeA, edgeB);
179 >                                        /* compute center */
180 >                cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
181 >                cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
182 >                cent[i][2] = 0.5*(cp[0][2] + cp[2][2]);
183          }
184 <        return(wres);
184 >                                        /* compute difference vector */
185 >        VSUM(diffv, cent[1], cent[0], -1.0);
186 >        for (i = 0; i < 2; i++) {       /* compute volume contributions */
187 >                vol[i] = 0.5*DOT(crossp[i], diffv);
188 >                if (vol[i] < 0.) vol[i] = -vol[i];
189 >        }
190 >        return(vol[0] + vol[1]);        /* return total volume */
191   }
192  
193  
194   init_global()                   /* initialize global ray computation */
195   {
196          long    wtotal = 0;
214        int     i, j;
215        int     lseg[2][3];
197          double  frac;
198 <        register int    k;
199 <                                        /* free old list */
200 <        if (complen > 0)
198 >        int     i;
199 >        register int    j, k;
200 >                                        /* free old list and empty queue */
201 >        if (complen > 0) {
202                  free((char *)complist);
203 +                done_packets(flush_queue());
204 +        }
205                                          /* allocate beam list */
206          complen = 0;
207          for (j = 0; hdlist[j] != NULL; j++)
# Line 227 | Line 211 | init_global()                  /* initialize global ray computation *
211                  error(SYSTEM, "out of memory in init_global");
212                                          /* compute beam weights */
213          k = 0;
214 <        for (j = 0; hdlist[j] != NULL; j++)
214 >        for (j = 0; hdlist[j] != NULL; j++) {
215 >                frac = 512. * VLEN(hdlist[j]->wg[0]) *
216 >                                VLEN(hdlist[j]->wg[1]) *
217 >                                VLEN(hdlist[j]->wg[2]);
218                  for (i = nbeams(hdlist[j]); i > 0; i--) {
232                        hdlseg(lseg, hdlist[j], i);
219                          complist[k].hd = j;
220                          complist[k].bi = i;
221 <                        complist[k].nr = lineweight( hdlist[j],
222 <                                        lseg[0][0], lseg[0][1], lseg[0][2],
237 <                                        lseg[1][0] - lseg[0][0],
238 <                                        lseg[1][1] - lseg[0][1],
239 <                                        lseg[1][2] - lseg[0][2] );
221 >                        complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
222 >                        complist[k].nc = bnrays(hdlist[j], i);
223                          wtotal += complist[k++].nr;
224                  }
225 +        }
226                                          /* adjust weights */
227 <        if (vdef(DISKSPACE)) {
227 >        if (vdef(DISKSPACE))
228                  frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
229 <                if (frac < 0.95 | frac > 1.05)
230 <                        while (k--)
231 <                                complist[k].nr = frac * complist[k].nr;
232 <        }
229 >        else
230 >                frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
231 >        while (k--)
232 >                complist[k].nr = frac * complist[k].nr;
233          listpos = 0; lastin = -1;       /* flag initial sort */
234   }
235  
236  
237   mergeclists(cdest, cl1, n1, cl2, n2)    /* merge two sorted lists */
238 < PACKHEAD        *cdest;
239 < PACKHEAD        *cl1, *cl2;
238 > register PACKHEAD       *cdest;
239 > register PACKHEAD       *cl1, *cl2;
240   int     n1, n2;
241   {
242 <        int     cmp;
242 >        register int    cmp;
243  
244          while (n1 | n2) {
245                  if (!n1) cmp = 1;
# Line 278 | Line 262 | sortcomplist()                 /* fix our list order */
262          PACKHEAD        *list2;
263          register int    i;
264  
281                                /* empty queue */
282        done_packets(flush_queue());
265          if (complen <= 0)       /* check to see if there is even a list */
266                  return;
267          if (lastin < 0 || listpos*4 >= complen*3)
# Line 294 | Line 276 | sortcomplist()                 /* fix our list order */
276                                  complist+listpos, complen-listpos);
277                  free((char *)list2);
278          }
297                                        /* check for all finished */
298        if (complist[0].nr <= bnrays(hdlist[complist[0].hd],complist[0].bi)) {
299                free((char *)complist);
300                complist = NULL;
301                complen = 0;
302        }
279                                          /* drop satisfied requests */
280 <        for (i = complen; i-- && complist[i].nr <=
305 <                        bnrays(hdlist[complist[i].hd],complist[i].bi); )
280 >        for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
281                  ;
282          if (i < 0) {
283                  free((char *)complist);
# Line 326 | Line 301 | sortcomplist()                 /* fix our list order */
301   * a given bundle to move way down in the computation order.  We keep
302   * track of where the computed bundle with the highest priority would end
303   * up, and if we get further in our compute list than this, we resort the
304 < * list and start again from the beginning.  We have to flush the queue
305 < * each time we sort, to ensure that we are not disturbing the order.
331 < *      If our major assumption is violated, and we have a very steep
332 < * descent in our weights, then we will end up resorting much more often
333 < * than necessary, resulting in frequent flushing of the queue.  Since
334 < * a merge sort is used, the sorting costs will be minimal.
304 > * list and start again from the beginning.  Since
305 > * a merge sort is used, the sorting costs are minimal.
306   */
307 < next_packet(p)                  /* prepare packet for computation */
307 > next_packet(p, n)               /* prepare packet for computation */
308   register PACKET *p;
309 + int     n;
310   {
339        int     ncomp;
311          register int    i;
312  
342        if (complen <= 0)
343                return(0);
313          if (listpos > lastin)           /* time to sort the list */
314                  sortcomplist();
315 +        if (complen <= 0)
316 +                return(0);
317          p->hd = complist[listpos].hd;
318          p->bi = complist[listpos].bi;
319 <        ncomp = bnrays(hdlist[p->hd],p->bi);
320 <        p->nr = complist[listpos].nr - ncomp;
319 >        p->nc = complist[listpos].nc;
320 >        p->nr = complist[listpos].nr - p->nc;
321          if (p->nr <= 0)
322                  return(0);
323 <        if (p->nr > RPACKSIZ)
324 <                p->nr = RPACKSIZ;
325 <        ncomp += p->nr;                 /* find where this one would go */
326 <        while (lastin > listpos && complist[listpos].nr *
327 <                (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
328 <                        > complist[lastin].nr * (ncomp+1))
323 > #ifdef DEBUG
324 >        if (n < 1 | n > RPACKSIZ)
325 >                error(CONSISTENCY, "next_packet called with bad n value");
326 > #endif
327 >        if (p->nr > n)
328 >                p->nr = n;
329 >        complist[listpos].nc += p->nr;  /* find where this one would go */
330 >        while (lastin > listpos &&
331 >                        beamcmp(complist+lastin, complist+listpos) > 0)
332                  lastin--;
333          listpos++;
334          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines