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.4 by gregl, Tue Nov 4 11:52:21 1997 UTC vs.
Revision 3.22 by gwlarson, Wed Jul 8 17:59:58 1998 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines