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.1 by gregl, Fri Oct 31 10:23:29 1997 UTC vs.
Revision 3.14 by gregl, Thu Dec 18 14:46:00 1997 UTC

# 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  
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 */
19 + static int      lastin= -1;     /* last ordered position in list */
20  
18 static PACKHEAD *complist;      /* list of beams to compute */
19 static int      complen;        /* length of complist */
20 static int      listpos;        /* current list position for next_pack */
21 static int      lastin;         /* last ordered position in list */
21  
22 + int
23 + beamcmp(b0, b1)                 /* comparison for descending compute order */
24 + register PACKHEAD       *b0, *b1;
25 + {
26 +        return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) );
27 + }
28  
29 +
30   int
31 < weightf(hp, x0, x1, x2)         /* voxel weighting function */
32 < register HOLO   *hp;
33 < register int    x0, x1, x2;
31 > dispbeam(b, hp, bi)                     /* display a holodeck beam */
32 > register BEAM   *b;
33 > HOLO    *hp;
34 > int     bi;
35   {
36 <        switch (vlet(OCCUPANCY)) {
37 <        case 'U':               /* uniform weighting */
38 <                return(1);
39 <        case 'C':               /* center weighting (crude) */
40 <                x0 += x0 - hp->grid[0] + 1;
41 <                x0 = abs(x0)*hp->grid[1]*hp->grid[2];
42 <                x1 += x1 - hp->grid[1] + 1;
43 <                x1 = abs(x1)*hp->grid[0]*hp->grid[2];
44 <                x2 += x2 - hp->grid[2] + 1;
45 <                x2 = abs(x2)*hp->grid[0]*hp->grid[1];
46 <                return(hp->grid[0]*hp->grid[1]*hp->grid[2] -
40 <                                (x0+x1+x2)/3);
41 <        default:
42 <                badvalue(OCCUPANCY);
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 < /* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */
60 < long
61 < lineweight(hp, x, y, z, dx, dy, dz)     /* compute weights along a line */
62 < HOLO    *hp;
51 < int     x, y, z, dx, dy, dz;
59 > bundle_set(op, clist, nents)    /* bundle set operation */
60 > int     op;
61 > register PACKHEAD       *clist;
62 > int     nents;
63   {
64 <        long    wres = 0;
54 <        int     n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
64 >        register int    i, n;
65  
66 <        sx = sgn(dx);   sy = sgn(dy);   sz = sgn(dz);
67 <        ax = abs(dx);   ay = abs(dy);   az = abs(dz);
68 <        bx = 2*ax;      by = 2*ay;      bz = 2*az;
69 <        exy = ay-ax;    exz = az-ax;    ezy = ay-az;
70 <        n = ax+ay+az + 1;               /* added increment to visit last */
71 <        while (n--) {
72 <                wres += weightf(hp, x, y, z);
73 <                if (exy < 0) {
74 <                        if (exz < 0) {
65 <                                x += sx;
66 <                                exy += by; exz += bz;
67 <                        } else {
68 <                                z += sz;
69 <                                exz -= bx; ezy += by;
70 <                        }
71 <                } else {
72 <                        if (ezy < 0) {
73 <                                z += sz;
74 <                                exz -= bx; ezy += by;
75 <                        } else {
76 <                                y += sy;
77 <                                exy -= bx; ezy -= bz;
78 <                        }
66 >        switch (op) {
67 >        case BS_NEW:                    /* new computation set */
68 >                if (complen)
69 >                        free((char *)complist);
70 >                if (nents <= 0) {
71 >                        complist = NULL;
72 >                        listpos = complen = 0;
73 >                        lastin = -1;
74 >                        return;
75                  }
76 +                complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
77 +                if (complist == NULL)
78 +                        goto memerr;
79 +                bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
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 (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 = complist[i].nc;
98 +                                        if (complist[i].nr != oldnr)
99 +                                                lastin = -1;    /* flag sort */
100 +                                        break;
101 +                                }
102 +                        if (i >= complen)
103 +                                clist[n].nc = bnrays(hdlist[clist[n].hd],
104 +                                                clist[n].bi);
105 +                }
106 +                                        /* sort updated list */
107 +                sortcomplist();
108 +                                        /* sort new entries */
109 +                qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
110 +                                        /* what can't we satisfy? */
111 +                for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
112 +                        ;
113 +                if (op == BS_ADJ)
114 +                        nents = n;
115 +                if (n) {                /* allocate space for merged list */
116 +                        PACKHEAD        *newlist;
117 +                        newlist = (PACKHEAD *)malloc(
118 +                                        (complen+n)*sizeof(PACKHEAD) );
119 +                        if (newlist == NULL)
120 +                                goto memerr;
121 +                                                /* merge lists */
122 +                        mergeclists(newlist, clist, n, complist, complen);
123 +                        if (complen)
124 +                                free((char *)complist);
125 +                        complist = newlist;
126 +                        complen += n;
127 +                }
128 +                listpos = 0;
129 +                lastin = complen-1;     /* list is now sorted */
130 +                break;
131 +        case BS_DEL:                    /* delete from computation set */
132 +                if (nents <= 0)
133 +                        return;
134 +                                        /* find each member */
135 +                for (i = 0; i < complen; i++)
136 +                        for (n = 0; n < nents; n++)
137 +                                if (clist[n].bi == complist[i].bi &&
138 +                                                clist[n].hd == complist[i].hd) {
139 +                                        if (clist[n].nr == 0 ||
140 +                                                clist[n].nr >= complist[i].nr)
141 +                                                complist[i].nr = 0;
142 +                                        else
143 +                                                complist[i].nr -= clist[n].nr;
144 +                                        lastin = -1;    /* flag full sort */
145 +                                        break;
146 +                                }
147 +                return;                 /* no display */
148 +        default:
149 +                error(CONSISTENCY, "bundle_set called with unknown operation");
150          }
151 <        return(wres);
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 >                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:
172 >        error(SYSTEM, "out of memory in bundle_set");
173   }
174  
175  
176 < int
177 < beamcmp(b0, b1)                 /* comparison for descending compute order */
178 < register PACKHEAD       *b0, *b1;
176 > double
177 > beamvolume(hp, bi)      /* compute approximate volume of a beam */
178 > HOLO    *hp;
179 > int     bi;
180   {
181 <        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
182 <                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
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 >                VSUM(edgeA, cp[2], cp[3], -1.0);
195 >                VSUM(edgeB, cp[0], cp[2], -1.0);
196 >                fcross(v, edgeA, edgeB);
197 >                VSUM(crossp[i], crossp[i], v, 1.0);
198 >                                        /* compute center */
199 >                cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
200 >                cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
201 >                cent[i][2] = 0.5*(cp[0][2] + cp[2][2]);
202 >        }
203 >                                        /* compute difference vector */
204 >        VSUM(diffv, cent[1], cent[0], -1.0);
205 >        for (i = 0; i < 2; i++) {       /* compute volume contributions */
206 >                vol[i] = 0.25*DOT(crossp[i], diffv);
207 >                if (vol[i] < 0.) vol[i] = -vol[i];
208 >        }
209 >        return(vol[0] + vol[1]);        /* return total volume */
210   }
211  
212  
213   init_global()                   /* initialize global ray computation */
214   {
215          long    wtotal = 0;
97        int     i, j;
98        int     lseg[2][3];
216          double  frac;
217 <        register int    k;
217 >        int     i;
218 >        register int    j, k;
219 >                                        /* free old list */
220 >        if (complen > 0)
221 >                free((char *)complist);
222                                          /* allocate beam list */
223          complen = 0;
224          for (j = 0; hdlist[j] != NULL; j++)
# Line 107 | Line 228 | init_global()                  /* initialize global ray computation *
228                  error(SYSTEM, "out of memory in init_global");
229                                          /* compute beam weights */
230          k = 0;
231 <        for (j = 0; hdlist[j] != NULL; j++)
231 >        for (j = 0; hdlist[j] != NULL; j++) {
232 >                frac = 512. * hdlist[j]->wg[0] *
233 >                                hdlist[j]->wg[1] * hdlist[j]->wg[2];
234                  for (i = nbeams(hdlist[j]); i > 0; i--) {
112                        hdlseg(lseg, hdlist[j], i);
235                          complist[k].hd = j;
236                          complist[k].bi = i;
237 <                        complist[k].nr = lineweight( hdlist[j],
116 <                                        lseg[0][0], lseg[0][1], lseg[0][2],
117 <                                        lseg[1][0] - lseg[0][0],
118 <                                        lseg[1][1] - lseg[0][1],
119 <                                        lseg[1][2] - lseg[0][2] );
237 >                        complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
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 <        }
248 <        listpos = 0; lastin = -1;
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  
# Line 156 | Line 275 | int    n1, n2;
275   sortcomplist()                  /* fix our list order */
276   {
277          PACKHEAD        *list2;
278 <                                /* empty queue */
279 <        done_packets(flush_queue());
280 <        if (lastin < 0)         /* flag to sort entire list */
278 >        register int    i;
279 >
280 >        if (complen <= 0)       /* check to see if there is even a list */
281 >                return;
282 >        if (lastin < 0 || listpos*4 >= complen*3)
283                  qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
284          else if (listpos) {     /* else sort and merge sublist */
285                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
# Line 170 | Line 291 | sortcomplist()                 /* fix our list order */
291                                  complist+listpos, complen-listpos);
292                  free((char *)list2);
293          }
294 <        listpos = 0; lastin = complen-1;
294 >                                        /* drop satisfied requests */
295 >        for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
296 >                ;
297 >        if (i < 0) {
298 >                free((char *)complist);
299 >                complist = NULL;
300 >                complen = 0;
301 >        } else if (i < complen-1) {
302 >                list2 = (PACKHEAD *)realloc((char *)complist,
303 >                                (i+1)*sizeof(PACKHEAD));
304 >                if (list2 != NULL) {
305 >                        complist = list2;
306 >                        complen = i+1;
307 >                }
308 >        }
309 >        listpos = 0; lastin = i;
310   }
311  
312  
# Line 180 | Line 316 | sortcomplist()                 /* fix our list order */
316   * a given bundle to move way down in the computation order.  We keep
317   * track of where the computed bundle with the highest priority would end
318   * up, and if we get further in our compute list than this, we resort the
319 < * list and start again from the beginning.  We have to flush the queue
320 < * each time we sort, to ensure that we are not disturbing the order.
185 < *      If our major assumption is violated, and we have a very steep
186 < * descent in our weights, then we will end up resorting much more often
187 < * than necessary, resulting in frequent flushing of the queue.  Since
188 < * a merge sort is used, the sorting costs will be minimal.
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 */
323   register PACKET *p;
324   {
193        int     ncomp;
325          register int    i;
326  
196        if (complen <= 0)
197                return(0);
327          if (listpos > lastin)           /* time to sort the list */
328                  sortcomplist();
329 +        if (complen <= 0)
330 +                return(0);
331          p->hd = complist[listpos].hd;
332          p->bi = complist[listpos].bi;
333 <        ncomp = bnrays(hdlist[p->hd],p->bi);
334 <        p->nr = complist[listpos].nr - ncomp;
333 >        p->nc = complist[listpos].nc;
334 >        p->nr = complist[listpos].nr - p->nc;
335          if (p->nr <= 0)
336                  return(0);
337          if (p->nr > RPACKSIZ)
338                  p->nr = RPACKSIZ;
339 <        ncomp += p->nr;                 /* find where this one would go */
340 <        while (lastin > listpos && complist[listpos].nr *
341 <                (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
211 <                        > complist[lastin].nr * (ncomp+1))
339 >        complist[listpos].nc += p->nr;  /* find where this one would go */
340 >        while (lastin > listpos &&
341 >                        beamcmp(complist+lastin, complist+listpos) > 0)
342                  lastin--;
343          listpos++;
344          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines