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.13 by gregl, Mon Dec 15 20:44:28 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  
23
22   int
23 < weightf(hp, x0, x1, x2)         /* voxel weighting function */
24 < register HOLO   *hp;
27 < register int    x0, x1, x2;
23 > beamcmp(b0, b1)                 /* comparison for descending compute order */
24 > register PACKHEAD       *b0, *b1;
25   {
26 <        switch (vlet(OCCUPANCY)) {
30 <        case 'U':               /* uniform weighting */
31 <                return(1);
32 <        case 'C':               /* center weighting (crude) */
33 <                x0 += x0 - hp->grid[0] + 1;
34 <                x0 = abs(x0)*hp->grid[1]*hp->grid[2];
35 <                x1 += x1 - hp->grid[1] + 1;
36 <                x1 = abs(x1)*hp->grid[0]*hp->grid[2];
37 <                x2 += x2 - hp->grid[2] + 1;
38 <                x2 = abs(x2)*hp->grid[0]*hp->grid[1];
39 <                return(hp->grid[0]*hp->grid[1]*hp->grid[2] -
40 <                                (x0+x1+x2)/3);
41 <        default:
42 <                badvalue(OCCUPANCY);
43 <        }
26 >        return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) );
27   }
28  
29  
30 < /* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */
31 < long
32 < lineweight(hp, x, y, z, dx, dy, dz)     /* compute weights along a line */
33 < HOLO    *hp;
51 < int     x, y, z, dx, dy, dz;
30 > bundle_set(op, clist, nents)    /* bundle set operation */
31 > int     op;
32 > PACKHEAD        *clist;
33 > int     nents;
34   {
35 <        long    wres = 0;
36 <        int     n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
35 >        BEAM    *b;
36 >        PACKHEAD        *p;
37 >        register int    i, n;
38  
39 <        sx = sgn(dx);   sy = sgn(dy);   sz = sgn(dz);
40 <        ax = abs(dx);   ay = abs(dy);   az = abs(dz);
41 <        bx = 2*ax;      by = 2*ay;      bz = 2*az;
42 <        exy = ay-ax;    exz = az-ax;    ezy = ay-az;
43 <        n = ax+ay+az + 1;               /* added increment to visit last */
44 <        while (n--) {
45 <                wres += weightf(hp, x, y, z);
46 <                if (exy < 0) {
47 <                        if (exz < 0) {
48 <                                x += sx;
49 <                                exy += by; exz += bz;
50 <                        } else {
51 <                                z += sz;
52 <                                exz -= bx; ezy += by;
39 >        switch (op) {
40 >        case BS_NEW:                    /* new computation set */
41 >                if (complen)
42 >                        free((char *)complist);
43 >                if (nents <= 0) {
44 >                        complist = NULL;
45 >                        listpos = complen = 0;
46 >                        lastin = -1;
47 >                        return;
48 >                }
49 >                complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
50 >                if (complist == NULL)
51 >                        goto memerr;
52 >                bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
53 >                complen = nents;        /* finish initialization below */
54 >                break;
55 >        case BS_ADD:                    /* add to computation set */
56 >        case BS_ADJ:                    /* adjust set quantities */
57 >                if (nents <= 0)
58 >                        return;
59 >                                        /* merge any common members */
60 >                for (n = 0; n < nents; n++) {
61 >                        for (i = 0; i < complen; i++)
62 >                                if (clist[n].bi == complist[i].bi &&
63 >                                                clist[n].hd == complist[i].hd) {
64 >                                        int     oldnr = complist[i].nr;
65 >                                        if (op == BS_ADD)
66 >                                                complist[i].nr += clist[n].nr;
67 >                                        else /* op == BS_ADJ */
68 >                                                complist[i].nr = clist[n].nr;
69 >                                        clist[n].nr = 0;
70 >                                        clist[n].nc = complist[i].nc;
71 >                                        if (complist[i].nr != oldnr)
72 >                                                lastin = -1;    /* flag sort */
73 >                                        break;
74 >                                }
75 >                        if (i >= complen)
76 >                                clist[n].nc = bnrays(hdlist[clist[n].hd],
77 >                                                clist[n].bi);
78 >                }
79 >                                        /* sort updated list */
80 >                sortcomplist();
81 >                                        /* sort new entries */
82 >                qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
83 >                                        /* what can't we satisfy? */
84 >                for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
85 >                        ;
86 >                if (op == BS_ADJ)
87 >                        nents = n;
88 >                if (n) {                /* allocate space for merged list */
89 >                        PACKHEAD        *newlist;
90 >                        newlist = (PACKHEAD *)malloc(
91 >                                        (complen+n)*sizeof(PACKHEAD) );
92 >                        if (newlist == NULL)
93 >                                goto memerr;
94 >                                                /* merge lists */
95 >                        mergeclists(newlist, clist, n, complist, complen);
96 >                        if (complen)
97 >                                free((char *)complist);
98 >                        complist = newlist;
99 >                        complen += n;
100 >                }
101 >                listpos = 0;
102 >                lastin = complen-1;     /* list is now sorted */
103 >                break;
104 >        case BS_DEL:                    /* delete from computation set */
105 >                if (nents <= 0)
106 >                        return;
107 >                                        /* find each member */
108 >                for (i = 0; i < complen; i++)
109 >                        for (n = 0; n < nents; n++)
110 >                                if (clist[n].bi == complist[i].bi &&
111 >                                                clist[n].hd == complist[i].hd) {
112 >                                        if (clist[n].nr == 0 ||
113 >                                                clist[n].nr >= complist[i].nr)
114 >                                                complist[i].nr = 0;
115 >                                        else
116 >                                                complist[i].nr -= clist[n].nr;
117 >                                        lastin = -1;    /* flag full sort */
118 >                                        break;
119 >                                }
120 >                return;                 /* no display */
121 >        default:
122 >                error(CONSISTENCY, "bundle_set called with unknown operation");
123 >        }
124 >        if (outdev == NULL)
125 >                return;
126 >        n = 32*RPACKSIZ;                /* allocate packet holder */
127 >        p = (PACKHEAD *)malloc(packsiz(n));
128 >        if (p == NULL)
129 >                goto memerr;
130 >                                        /* 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 <                } else {
140 <                        if (ezy < 0) {
141 <                                z += sz;
142 <                                exz -= bx; ezy += by;
143 <                        } else {
144 <                                y += sy;
77 <                                exy -= bx; ezy -= bz;
78 <                        }
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);
145                  }
146 +        free((char *)p);                /* clean up */
147 +        if (op == BS_NEW) {
148 +                done_packets(flush_queue());    /* empty queue, so we can... */
149 +                for (i = 0; i < complen; i++)   /* ...get number computed */
150 +                        complist[i].nc = bnrays(hdlist[complist[i].hd],
151 +                                                complist[i].bi);
152 +                listpos = 0;
153 +                lastin = -1;            /* flag for initial sort */
154          }
155 <        return(wres);
155 >        return;
156 > memerr:
157 >        error(SYSTEM, "out of memory in bundle_set");
158   }
159  
160  
161 < int
162 < beamcmp(b0, b1)                 /* comparison for descending compute order */
163 < register PACKHEAD       *b0, *b1;
161 > double
162 > beamvolume(hp, bi)      /* compute approximate volume of a beam */
163 > HOLO    *hp;
164 > int     bi;
165   {
166 <        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
167 <                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
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 >                VSUM(edgeA, cp[2], cp[3], -1.0);
180 >                VSUM(edgeB, cp[0], cp[2], -1.0);
181 >                fcross(v, edgeA, edgeB);
182 >                VSUM(crossp[i], crossp[i], v, 1.0);
183 >                                        /* compute center */
184 >                cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
185 >                cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
186 >                cent[i][2] = 0.5*(cp[0][2] + cp[2][2]);
187 >        }
188 >                                        /* compute difference vector */
189 >        VSUM(diffv, cent[1], cent[0], -1.0);
190 >        for (i = 0; i < 2; i++) {       /* compute volume contributions */
191 >                vol[i] = 0.25*DOT(crossp[i], diffv);
192 >                if (vol[i] < 0.) vol[i] = -vol[i];
193 >        }
194 >        return(vol[0] + vol[1]);        /* return total volume */
195   }
196  
197  
198   init_global()                   /* initialize global ray computation */
199   {
200          long    wtotal = 0;
97        int     i, j;
98        int     lseg[2][3];
201          double  frac;
202 <        register int    k;
202 >        int     i;
203 >        register int    j, k;
204 >                                        /* free old list */
205 >        if (complen > 0)
206 >                free((char *)complist);
207                                          /* allocate beam list */
208          complen = 0;
209          for (j = 0; hdlist[j] != NULL; j++)
# Line 107 | Line 213 | init_global()                  /* initialize global ray computation *
213                  error(SYSTEM, "out of memory in init_global");
214                                          /* compute beam weights */
215          k = 0;
216 <        for (j = 0; hdlist[j] != NULL; j++)
216 >        for (j = 0; hdlist[j] != NULL; j++) {
217 >                frac = 512. * hdlist[j]->wg[0] *
218 >                                hdlist[j]->wg[1] * hdlist[j]->wg[2];
219                  for (i = nbeams(hdlist[j]); i > 0; i--) {
112                        hdlseg(lseg, hdlist[j], i);
220                          complist[k].hd = j;
221                          complist[k].bi = i;
222 <                        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] );
222 >                        complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
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 <        }
233 <        listpos = 0; lastin = -1;
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  
# Line 156 | Line 260 | int    n1, n2;
260   sortcomplist()                  /* fix our list order */
261   {
262          PACKHEAD        *list2;
263 <                                /* empty queue */
264 <        done_packets(flush_queue());
265 <        if (lastin < 0)         /* flag to sort entire list */
263 >        register int    i;
264 >
265 >        if (complen <= 0)       /* check to see if there is even a list */
266 >                return;
267 >        if (lastin < 0 || listpos*4 >= complen*3)
268                  qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
269          else if (listpos) {     /* else sort and merge sublist */
270                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
# Line 170 | Line 276 | sortcomplist()                 /* fix our list order */
276                                  complist+listpos, complen-listpos);
277                  free((char *)list2);
278          }
279 <        listpos = 0; lastin = complen-1;
279 >                                        /* drop satisfied requests */
280 >        for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
281 >                ;
282 >        if (i < 0) {
283 >                free((char *)complist);
284 >                complist = NULL;
285 >                complen = 0;
286 >        } else if (i < complen-1) {
287 >                list2 = (PACKHEAD *)realloc((char *)complist,
288 >                                (i+1)*sizeof(PACKHEAD));
289 >                if (list2 != NULL) {
290 >                        complist = list2;
291 >                        complen = i+1;
292 >                }
293 >        }
294 >        listpos = 0; lastin = i;
295   }
296  
297  
# Line 180 | 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.
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.
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 */
308   register PACKET *p;
309   {
193        int     ncomp;
310          register int    i;
311  
196        if (complen <= 0)
197                return(0);
312          if (listpos > lastin)           /* time to sort the list */
313                  sortcomplist();
314 +        if (complen <= 0)
315 +                return(0);
316          p->hd = complist[listpos].hd;
317          p->bi = complist[listpos].bi;
318 <        ncomp = bnrays(hdlist[p->hd],p->bi);
319 <        p->nr = complist[listpos].nr - ncomp;
318 >        p->nc = complist[listpos].nc;
319 >        p->nr = complist[listpos].nr - p->nc;
320          if (p->nr <= 0)
321                  return(0);
322          if (p->nr > RPACKSIZ)
323                  p->nr = RPACKSIZ;
324 <        ncomp += p->nr;                 /* find where this one would go */
325 <        while (lastin > listpos && complist[listpos].nr *
326 <                (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
211 <                        > complist[lastin].nr * (ncomp+1))
324 >        complist[listpos].nc += p->nr;  /* find where this one would go */
325 >        while (lastin > listpos &&
326 >                        beamcmp(complist+lastin, complist+listpos) > 0)
327                  lastin--;
328          listpos++;
329          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines