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.9 by gregl, Fri Nov 21 17:47:56 1997 UTC vs.
Revision 3.31 by gwlarson, Thu Mar 4 10:29:23 1999 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 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 + #include "view.h"
13 + #include <sys/types.h>
14  
15 + #ifndef NFRAG2CHUNK
16 + #define NFRAG2CHUNK     4096    /* number of fragments to start chunking */
17 + #endif
18  
19 + #ifndef abs
20   #define abs(x)          ((x) > 0 ? (x) : -(x))
21 + #endif
22 + #ifndef sgn
23   #define sgn(x)          ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
24 + #endif
25  
26 + #define rchunk(n)       (((n)+(RPACKSIZ/2))/RPACKSIZ)
27  
28 + extern time_t   time();
29 +
30 + int     chunkycmp = 0;          /* clump beams together on disk */
31 +
32   static PACKHEAD *complist=NULL; /* list of beams to compute */
33   static int      complen=0;      /* length of complist */
34   static int      listpos=0;      /* current list position for next_packet */
# Line 22 | Line 36 | static int     lastin= -1;     /* last ordered position in lis
36  
37  
38   int
39 < beamcmp(b0, b1)                 /* comparison for descending compute order */
39 > beamcmp(b0, b1)                         /* comparison for compute order */
40   register PACKHEAD       *b0, *b1;
41   {
42 <        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
43 <                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
42 >        BEAMI   *bip0, *bip1;
43 >        register long   c;
44 >                                        /* first check desired quantities */
45 >        if (chunkycmp)
46 >                c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) -
47 >                                rchunk(b0->nr)*(rchunk(b1->nc)+1L);
48 >        else
49 >                c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L);
50 >        if (c > 0) return(1);
51 >        if (c < 0) return(-1);
52 >                                /* only one file, so skip the following: */
53 > #if 0
54 >                                        /* next, check file descriptors */
55 >        c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd;
56 >        if (c) return(c);
57 > #endif
58 >                                        /* finally, check file positions */
59 >        bip0 = &hdlist[b0->hd]->bi[b0->bi];
60 >        bip1 = &hdlist[b1->hd]->bi[b1->bi];
61 >                                        /* put diskless beams last */
62 >        if (!bip0->nrd)
63 >                return(bip1->nrd > 0);
64 >        if (!bip1->nrd)
65 >                return(-1);
66 >        c = bip0->fo - bip1->fo;
67 >        return(c < 0 ? -1 : c > 0);
68   }
69  
70  
71 + int
72 + beamidcmp(b0, b1)                       /* comparison for beam searching */
73 + register PACKHEAD       *b0, *b1;
74 + {
75 +        register int    c = b0->hd - b1->hd;
76 +
77 +        if (c) return(c);
78 +        return(b0->bi - b1->bi);
79 + }
80 +
81 +
82 + int
83 + dispbeam(b, hb)                         /* display a holodeck beam */
84 + register BEAM   *b;
85 + register HDBEAMI        *hb;
86 + {
87 +        static int      n = 0;
88 +        static PACKHEAD *p = NULL;
89 +
90 +        if (b == NULL)
91 +                return;
92 +        if (b->nrm > n) {               /* (re)allocate packet holder */
93 +                n = b->nrm;
94 +                if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n));
95 +                else p = (PACKHEAD *)realloc((char *)p, packsiz(n));
96 +                CHECK(p==NULL, SYSTEM, "out of memory in dispbeam");
97 +        }
98 +                                        /* assign packet fields */
99 +        bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL));
100 +        p->nr = p->nc = b->nrm;
101 +        for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++)
102 +                if (hdlist[p->hd] == NULL)
103 +                        error(CONSISTENCY, "unregistered holodeck in dispbeam");
104 +        p->bi = hb->b;
105 +        disp_packet(p);                 /* display it */
106 +        if (n >= 1024) {                /* free ridiculous packets */
107 +                free((char *)p);
108 +                p = NULL; n = 0;
109 +        }
110 + }
111 +
112 +
113   bundle_set(op, clist, nents)    /* bundle set operation */
114   int     op;
115   PACKHEAD        *clist;
116   int     nents;
117   {
118 <        BEAM    *b;
119 <        PACKHEAD        *p;
120 <        register int    i, n;
121 <
118 >        int     oldnr, n;
119 >        HDBEAMI *hbarr;
120 >        register PACKHEAD       *csm;
121 >        register int    i;
122 >                                        /* search for common members */
123 >        for (csm = clist+nents; csm-- > clist; )
124 >                csm->nc = -1;
125 >        qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp);
126 >        for (i = 0; i < complen; i++) {
127 >                csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist,
128 >                                nents, sizeof(PACKHEAD), beamidcmp);
129 >                if (csm == NULL)
130 >                        continue;
131 >                oldnr = complist[i].nr;
132 >                csm->nc = complist[i].nc;
133 >                switch (op) {
134 >                case BS_ADD:            /* add to count */
135 >                        complist[i].nr += csm->nr;
136 >                        csm->nr = 0;
137 >                        break;
138 >                case BS_MAX:            /* maximum of counts */
139 >                        if (csm->nr > complist[i].nr)
140 >                                complist[i].nr = csm->nr;
141 >                        csm->nr = 0;
142 >                        break;
143 >                case BS_ADJ:            /* reset count */
144 >                        complist[i].nr = csm->nr;
145 >                        csm->nr = 0;
146 >                        break;
147 >                case BS_DEL:            /* delete count */
148 >                        if (csm->nr == 0 || csm->nr >= complist[i].nr)
149 >                                complist[i].nr = 0;
150 >                        else
151 >                                complist[i].nr -= csm->nr;
152 >                        break;
153 >                }
154 >                if (complist[i].nr != oldnr)
155 >                        lastin = -1;    /* flag sort */
156 >        }
157 >                                /* record computed rays for uncommon beams */
158 >        for (csm = clist+nents; csm-- > clist; )
159 >                if (csm->nc < 0)
160 >                        csm->nc = bnrays(hdlist[csm->hd], csm->bi);
161 >                                /* complete list operations */
162          switch (op) {
163          case BS_NEW:                    /* new computation set */
164 <                if (complen)
164 >                listpos = 0; lastin = -1;
165 >                if (complen)            /* free old list */
166                          free((char *)complist);
167 <                if (nents <= 0) {
168 <                        complist = NULL;
48 <                        listpos = complen = 0;
49 <                        lastin = -1;
167 >                complist = NULL;
168 >                if (!(complen = nents))
169                          return;
51                }
170                  complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
171                  if (complist == NULL)
172                          goto memerr;
173                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
56                complen = nents;
57                listpos = 0;
58                lastin = -1;            /* flag for initial sort */
174                  break;
175          case BS_ADD:                    /* add to computation set */
176 +        case BS_MAX:                    /* maximum of quantities */
177 +        case BS_ADJ:                    /* adjust set quantities */
178                  if (nents <= 0)
179                          return;
180 <                                        /* 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 */
180 >                sortcomplist();         /* sort updated list & new entries */
181                  qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
182                                          /* what can't we satisfy? */
183 <                for (n = 0; n < nents && clist[n].nr >
79 <                                bnrays(hdlist[clist[n].hd],clist[n].bi); n++)
183 >                for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++)
184                          ;
185 +                n = csm - clist;
186 +                if (op != BS_ADD) {     /* don't regenerate adjusted beams */
187 +                        for (++i; i-- && csm->nr > 0; csm++)
188 +                                ;
189 +                        nents = csm - clist;
190 +                }
191                  if (n) {                /* allocate space for merged list */
192                          PACKHEAD        *newlist;
193                          newlist = (PACKHEAD *)malloc(
# Line 95 | Line 205 | int    nents;
205                  lastin = complen-1;     /* list is now sorted */
206                  break;
207          case BS_DEL:                    /* delete from computation set */
208 <                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 */
208 >                return;                 /* already done */
209          default:
210                  error(CONSISTENCY, "bundle_set called with unknown operation");
211          }
212 <        if (outdev == NULL)
212 >        if (outdev == NULL || !nents)   /* nothing to display? */
213                  return;
214 <        n = 8*RPACKSIZ;                         /* allocate packet holder */
215 <        p = (PACKHEAD *)malloc(packsiz(n));
216 <        if (p == NULL)
217 <                goto memerr;
218 <                                        /* display what we have */
219 <        for (i = 0; i < nents; i++)
220 <                if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
221 <                        if (b->nrm > n) {
222 <                                n = b->nrm;
223 <                                p = (PACKHEAD *)realloc((char *)p, packsiz(n));
224 <                                if (p == NULL)
225 <                                        goto memerr;
133 <                        }
134 <                        bcopy((char *)hdbray(b), (char *)packra(p),
135 <                                        (p->nr=b->nrm)*sizeof(RAYVAL));
136 <                        p->hd = clist[i].hd;
137 <                        p->bi = clist[i].bi;
138 <                        disp_packet(p);
139 <                }
140 <        free((char *)p);                /* clean up */
214 >                                        /* load and display beams we have */
215 >        hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
216 >        for (i = nents; i--; ) {
217 >                hbarr[i].h = hdlist[clist[i].hd];
218 >                hbarr[i].b = clist[i].bi;
219 >        }
220 >        hdloadbeams(hbarr, nents, dispbeam);
221 >        free((char *)hbarr);
222 >        if (hdfragflags&FF_READ) {
223 >                listpos = 0;
224 >                lastin = -1;            /* need to re-sort list */
225 >        }
226          return;
227   memerr:
228          error(SYSTEM, "out of memory in bundle_set");
229   }
230  
231  
232 < int
233 < weightf(hp, x0, x1, x2)         /* voxel weighting function */
149 < register HOLO   *hp;
150 < register int    x0, x1, x2;
151 < {
152 <        switch (vlet(OCCUPANCY)) {
153 <        case 'U':               /* uniform weighting */
154 <                return(1);
155 <        case 'C':               /* center weighting (crude) */
156 <                x0 += x0 - hp->grid[0] + 1;
157 <                x0 = abs(x0)*hp->grid[1]*hp->grid[2];
158 <                x1 += x1 - hp->grid[1] + 1;
159 <                x1 = abs(x1)*hp->grid[0]*hp->grid[2];
160 <                x2 += x2 - hp->grid[2] + 1;
161 <                x2 = abs(x2)*hp->grid[0]*hp->grid[1];
162 <                return(hp->grid[0]*hp->grid[1]*hp->grid[2] -
163 <                                (x0+x1+x2)/3);
164 <        default:
165 <                badvalue(OCCUPANCY);
166 <        }
167 < }
168 <
169 <
170 < /* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */
171 < long
172 < lineweight(hp, x, y, z, dx, dy, dz)     /* compute weights along a line */
232 > double
233 > beamvolume(hp, bi)      /* compute approximate volume of a beam */
234   HOLO    *hp;
235 < int     x, y, z, dx, dy, dz;
235 > int     bi;
236   {
237 <        long    wres = 0;
238 <        int     n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
239 <
240 <        sx = sgn(dx);   sy = sgn(dy);   sz = sgn(dz);
241 <        ax = abs(dx);   ay = abs(dy);   az = abs(dz);
242 <        bx = 2*ax;      by = 2*ay;      bz = 2*az;
243 <        exy = ay-ax;    exz = az-ax;    ezy = ay-az;
244 <        n = ax+ay+az + 1;               /* added increment to visit last */
245 <        while (n--) {
246 <                wres += weightf(hp, x, y, z);
247 <                if (exy < 0) {
248 <                        if (exz < 0) {
249 <                                x += sx;
250 <                                exy += by; exz += bz;
251 <                        } else {
252 <                                z += sz;
253 <                                exz -= bx; ezy += by;
193 <                        }
194 <                } else {
195 <                        if (ezy < 0) {
196 <                                z += sz;
197 <                                exz -= bx; ezy += by;
198 <                        } else {
199 <                                y += sy;
200 <                                exy -= bx; ezy -= bz;
201 <                        }
202 <                }
237 >        GCOORD  gc[2];
238 >        FVECT   cp[4], edgeA, edgeB, cent[2];
239 >        FVECT   v, crossp[2], diffv;
240 >        double  vol[2];
241 >        register int    i;
242 >                                        /* get grid coordinates */
243 >        if (!hdbcoord(gc, hp, bi))
244 >                error(CONSISTENCY, "bad beam index in beamvolume");
245 >        for (i = 0; i < 2; i++) {       /* compute cell area vectors */
246 >                hdcell(cp, hp, gc+i);
247 >                VSUM(edgeA, cp[1], cp[0], -1.0);
248 >                VSUM(edgeB, cp[3], cp[1], -1.0);
249 >                fcross(crossp[i], edgeA, edgeB);
250 >                                        /* compute center */
251 >                cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
252 >                cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
253 >                cent[i][2] = 0.5*(cp[0][2] + cp[2][2]);
254          }
255 <        return(wres);
255 >                                        /* compute difference vector */
256 >        VSUM(diffv, cent[1], cent[0], -1.0);
257 >        for (i = 0; i < 2; i++) {       /* compute volume contributions */
258 >                vol[i] = 0.5*DOT(crossp[i], diffv);
259 >                if (vol[i] < 0.) vol[i] = -vol[i];
260 >        }
261 >        return(vol[0] + vol[1]);        /* return total volume */
262   }
263  
264  
265 < init_global()                   /* initialize global ray computation */
265 > ambient_list()                  /* compute ambient beam list */
266   {
267          long    wtotal = 0;
211        int     i, j;
212        int     lseg[2][3];
268          double  frac;
269 <        register int    k;
270 <                                        /* free old list */
271 <        if (complen > 0)
217 <                free((char *)complist);
218 <                                        /* allocate beam list */
269 >        int     i;
270 >        register int    j, k;
271 >
272          complen = 0;
273          for (j = 0; hdlist[j] != NULL; j++)
274                  complen += nbeams(hdlist[j]);
275          complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD));
276 <        if (complist == NULL)
224 <                error(SYSTEM, "out of memory in init_global");
276 >        CHECK(complist==NULL, SYSTEM, "out of memory in ambient_list");
277                                          /* compute beam weights */
278          k = 0;
279 <        for (j = 0; hdlist[j] != NULL; j++)
279 >        for (j = 0; hdlist[j] != NULL; j++) {
280 >                frac = 512. * VLEN(hdlist[j]->wg[0]) *
281 >                                VLEN(hdlist[j]->wg[1]) *
282 >                                VLEN(hdlist[j]->wg[2]);
283                  for (i = nbeams(hdlist[j]); i > 0; i--) {
229                        hdlseg(lseg, hdlist[j], i);
284                          complist[k].hd = j;
285                          complist[k].bi = i;
286 <                        complist[k].nr = lineweight( hdlist[j],
287 <                                        lseg[0][0], lseg[0][1], lseg[0][2],
234 <                                        lseg[1][0] - lseg[0][0],
235 <                                        lseg[1][1] - lseg[0][1],
236 <                                        lseg[1][2] - lseg[0][2] );
286 >                        complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
287 >                        complist[k].nc = bnrays(hdlist[j], i);
288                          wtotal += complist[k++].nr;
289                  }
239                                        /* adjust weights */
240        if (vdef(DISKSPACE)) {
241                frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
242                if (frac < 0.95 | frac > 1.05)
243                        while (k--)
244                                complist[k].nr = frac * complist[k].nr;
290          }
291 +                                        /* adjust sample weights */
292 +        if (vdef(DISKSPACE))
293 +                frac = 1024.*1024.*vflt(DISKSPACE) /
294 +                                (wtotal*sizeof(RAYVAL));
295 +        else
296 +                frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
297 +        if (frac > 1.11 || frac < 0.9)
298 +                for (k = complen; k--; )
299 +                        complist[k].nr = frac*complist[k].nr + 0.5;
300          listpos = 0; lastin = -1;       /* flag initial sort */
301   }
302  
303  
304 + view_list(fp)                   /* assign beam priority from view list */
305 + FILE    *fp;
306 + {
307 +        double  pa = 1.;
308 +        VIEW    curview;
309 +        int     xr, yr;
310 +        char    *err;
311 +        BEAMLIST        blist;
312 +
313 +        copystruct(&curview, &stdview);
314 +        while (nextview(&curview, fp) != EOF) {
315 +                if ((err = setview(&curview)) != NULL) {
316 +                        error(WARNING, err);
317 +                        continue;
318 +                }
319 +                xr = yr = 1024;
320 +                normaspect(viewaspect(&curview), &pa, &xr, &yr);
321 +                viewbeams(&curview, xr, yr, &blist);
322 +                bundle_set(BS_MAX, blist.bl, blist.nb);
323 +                free((char *)blist.bl);
324 +        }
325 + }
326 +
327 +
328 + init_global()                   /* initialize global ray computation */
329 + {
330 +        register int    k;
331 +                                        /* free old list and empty queue */
332 +        if (complen > 0) {
333 +                free((char *)complist);
334 +                done_packets(flush_queue());
335 +        }
336 +                                        /* reseed random number generator */
337 +        srandom(time(NULL));
338 +                                        /* allocate beam list */
339 +        if (readinp)
340 +                view_list(stdin);
341 +        else
342 +                ambient_list();
343 +                                        /* no view vicinity */
344 +        myeye.rng = 0;
345 + }
346 +
347 +
348   mergeclists(cdest, cl1, n1, cl2, n2)    /* merge two sorted lists */
349 < PACKHEAD        *cdest;
350 < PACKHEAD        *cl1, *cl2;
349 > register PACKHEAD       *cdest;
350 > register PACKHEAD       *cl1, *cl2;
351   int     n1, n2;
352   {
353 <        int     cmp;
353 >        register int    cmp;
354  
355          while (n1 | n2) {
356                  if (!n1) cmp = 1;
# Line 273 | Line 371 | int    n1, n2;
371   sortcomplist()                  /* fix our list order */
372   {
373          PACKHEAD        *list2;
374 +        int     listlen;
375          register int    i;
376  
278                                /* empty queue */
279        done_packets(flush_queue());
377          if (complen <= 0)       /* check to see if there is even a list */
378                  return;
379 +        if (!chunkycmp)         /* check to see if fragment list is full */
380 +                if (!hdfragOK(hdlist[0]->fd, &listlen, NULL)
381 + #if NFRAG2CHUNK
382 +                                || listlen >= NFRAG2CHUNK
383 + #endif
384 +                                ) {
385 +                        chunkycmp++;    /* use "chunky" comparison */
386 +                        lastin = -1;    /* need to re-sort list */
387 + #ifdef DEBUG
388 +                        error(WARNING, "using chunky comparison mode");
389 + #endif
390 +                }
391          if (lastin < 0 || listpos*4 >= complen*3)
392                  qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
393          else if (listpos) {     /* else sort and merge sublist */
394                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
395 <                if (list2 == NULL)
287 <                        error(SYSTEM, "out of memory in sortcomplist");
395 >                CHECK(list2==NULL, SYSTEM, "out of memory in sortcomplist");
396                  bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD));
397                  qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp);
398                  mergeclists(complist, list2, listpos,
399                                  complist+listpos, complen-listpos);
400                  free((char *)list2);
401          }
294                                        /* check for all finished */
295        if (complist[0].nr <= bnrays(hdlist[complist[0].hd],complist[0].bi)) {
296                free((char *)complist);
297                complist = NULL;
298                complen = 0;
299        }
402                                          /* drop satisfied requests */
403 <        for (i = complen; i-- && complist[i].nr <=
302 <                        bnrays(hdlist[complist[i].hd],complist[i].bi); )
403 >        for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
404                  ;
405          if (i < 0) {
406                  free((char *)complist);
# Line 308 | Line 409 | sortcomplist()                 /* fix our list order */
409          } else if (i < complen-1) {
410                  list2 = (PACKHEAD *)realloc((char *)complist,
411                                  (i+1)*sizeof(PACKHEAD));
412 <                if (list2 != NULL) {
412 >                if (list2 != NULL)
413                          complist = list2;
414 <                        complen = i+1;
314 <                }
414 >                complen = i+1;
415          }
416          listpos = 0; lastin = i;
417   }
# Line 322 | Line 422 | sortcomplist()                 /* fix our list order */
422   * more or less evenly distributed, such that computing a packet causes
423   * a given bundle to move way down in the computation order.  We keep
424   * track of where the computed bundle with the highest priority would end
425 < * up, and if we get further in our compute list than this, we resort the
426 < * list and start again from the beginning.  We have to flush the queue
427 < * each time we sort, to ensure that we are not disturbing the order.
328 < *      If our major assumption is violated, and we have a very steep
329 < * descent in our weights, then we will end up resorting much more often
330 < * than necessary, resulting in frequent flushing of the queue.  Since
331 < * a merge sort is used, the sorting costs will be minimal.
425 > * up, and if we get further in our compute list than this, we re-sort the
426 > * list and start again from the beginning.  Since
427 > * a merge sort is used, the sorting costs are minimal.
428   */
429 < next_packet(p)                  /* prepare packet for computation */
429 > next_packet(p, n)               /* prepare packet for computation */
430   register PACKET *p;
431 + int     n;
432   {
336        int     ncomp;
433          register int    i;
434  
339        if (complen <= 0)
340                return(0);
435          if (listpos > lastin)           /* time to sort the list */
436                  sortcomplist();
437 +        if (complen <= 0)
438 +                return(0);
439          p->hd = complist[listpos].hd;
440          p->bi = complist[listpos].bi;
441 <        ncomp = bnrays(hdlist[p->hd],p->bi);
442 <        p->nr = complist[listpos].nr - ncomp;
441 >        p->nc = complist[listpos].nc;
442 >        p->nr = complist[listpos].nr - p->nc;
443          if (p->nr <= 0)
444                  return(0);
445 <        if (p->nr > RPACKSIZ)
446 <                p->nr = RPACKSIZ;
447 <        ncomp += p->nr;                 /* find where this one would go */
448 <        while (lastin > listpos && complist[listpos].nr *
449 <                (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
450 <                        > complist[lastin].nr * (ncomp+1))
445 >        DCHECK(n < 1 | n > RPACKSIZ,
446 >                        CONSISTENCY, "next_packet called with bad n value");
447 >        if (p->nr > n)
448 >                p->nr = n;
449 >        complist[listpos].nc += p->nr;  /* find where this one would go */
450 >        if (hdgetbeam(hdlist[p->hd], p->bi) != NULL)
451 >                hdfreefrag(hdlist[p->hd], p->bi);
452 >        while (lastin > listpos &&
453 >                        beamcmp(complist+lastin, complist+listpos) > 0)
454                  lastin--;
455          listpos++;
456          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines