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.6 by gregl, Mon Nov 10 18:06:18 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  
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  
# Line 53 | Line 50 | int    nents;
50                  if (complist == NULL)
51                          goto memerr;
52                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
53 <                complen = nents;
57 <                listpos = 0;
58 <                lastin = -1;            /* flag for initial sort */
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 (i = 0; i < complen; i++)
61 <                        for (n = 0; n < nents; n++)
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 <                                        complist[i].nr += clist[n].nr;
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 <                                        lastin = -1;    /* flag full sort */
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 >
79 <                                bnrays(hdlist[clist[n].hd],clist[n].bi); n++)
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(
# Line 110 | Line 117 | int    nents;
117                                          lastin = -1;    /* flag full sort */
118                                          break;
119                                  }
113                if (lastin < 0)         /* sort updated list */
114                        sortcomplist();
120                  return;                 /* no display */
121          default:
122                  error(CONSISTENCY, "bundle_set called with unknown operation");
123          }
124 <        n = 0;                          /* allocate packet holder */
125 <        for (i = 0; i < nents; i++)
126 <                if (clist[i].nr > n)
122 <                        n = clist[i].nr;
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                          bcopy((char *)hdbray(b), (char *)packra(p),
140 <                                        (p->nr=b->nrm)*sizeof(RAYVAL));
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;
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 */
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 */
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;
183 <                        }
184 <                } else {
185 <                        if (ezy < 0) {
186 <                                z += sz;
190 <                                exz -= bx; ezy += by;
191 <                        } else {
192 <                                y += sy;
193 <                                exy -= bx; ezy -= bz;
194 <                        }
195 <                }
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 <        return(wres);
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;
204        int     i, j;
205        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);
# Line 217 | 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--) {
222                        hdlseg(lseg, hdlist[j], i);
220                          complist[k].hd = j;
221                          complist[k].bi = i;
222 <                        complist[k].nr = lineweight( hdlist[j],
226 <                                        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] );
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 <        }
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  
# Line 268 | Line 262 | sortcomplist()                 /* fix our list order */
262          PACKHEAD        *list2;
263          register int    i;
264  
271                                /* empty queue */
272        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 284 | Line 276 | sortcomplist()                 /* fix our list order */
276                                  complist+listpos, complen-listpos);
277                  free((char *)list2);
278          }
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        }
279                                          /* drop satisfied requests */
280 <        for (i = complen; i-- && complist[i].nr <=
295 <                        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 316 | 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.
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.
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   {
329        int     ncomp;
310          register int    i;
311  
332        if (complen <= 0)
333                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)
347 <                        > 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