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.8 by gregl, Thu Nov 20 11:39:57 1997 UTC

# Line 15 | Line 15 | static char SCCSid[] = "$SunId$ SGI";
15   #define sgn(x)          ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
16  
17  
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 */
18 > static PACKHEAD *complist=NULL; /* list of beams to compute */
19 > static int      complen=0;      /* length of complist */
20 > static int      listpos=0;      /* current list position for next_packet */
21 > static int      lastin= -1;     /* last ordered position in list */
22  
23  
24   int
25 + beamcmp(b0, b1)                 /* comparison for descending compute order */
26 + register PACKHEAD       *b0, *b1;
27 + {
28 +        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
29 +                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
30 + }
31 +
32 +
33 + bundle_set(op, clist, nents)    /* bundle set operation */
34 + int     op;
35 + PACKHEAD        *clist;
36 + int     nents;
37 + {
38 +        BEAM    *b;
39 +        PACKHEAD        *p;
40 +        register int    i, n;
41 +
42 +        switch (op) {
43 +        case BS_NEW:                    /* new computation set */
44 +                if (complen)
45 +                        free((char *)complist);
46 +                if (nents <= 0) {
47 +                        complist = NULL;
48 +                        listpos = complen = 0;
49 +                        lastin = -1;
50 +                        return;
51 +                }
52 +                complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
53 +                if (complist == NULL)
54 +                        goto memerr;
55 +                bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
56 +                complen = nents;
57 +                listpos = 0;
58 +                lastin = -1;            /* flag for initial sort */
59 +                break;
60 +        case BS_ADD:                    /* add to computation set */
61 +                if (nents <= 0)
62 +                        return;
63 +                                        /* 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 */
76 +                qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
77 +                                        /* what can't we satisfy? */
78 +                for (n = 0; n < nents && clist[n].nr >
79 +                                bnrays(hdlist[clist[n].hd],clist[n].bi); n++)
80 +                        ;
81 +                if (n) {                /* allocate space for merged list */
82 +                        PACKHEAD        *newlist;
83 +                        newlist = (PACKHEAD *)malloc(
84 +                                        (complen+n)*sizeof(PACKHEAD) );
85 +                        if (newlist == NULL)
86 +                                goto memerr;
87 +                                                /* merge lists */
88 +                        mergeclists(newlist, clist, n, complist, complen);
89 +                        if (complen)
90 +                                free((char *)complist);
91 +                        complist = newlist;
92 +                        complen += n;
93 +                }
94 +                listpos = 0;
95 +                lastin = complen-1;     /* list is now sorted */
96 +                break;
97 +        case BS_DEL:                    /* delete from computation set */
98 +                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 */
116 +        default:
117 +                error(CONSISTENCY, "bundle_set called with unknown operation");
118 +        }
119 +        if (outdev == NULL)
120 +                return;
121 +        n = RPACKSIZ;                           /* allocate packet holder */
122 +        for (i = 0; i < nents; i++)
123 +                if (clist[i].nr > n)
124 +                        n = clist[i].nr;
125 +        p = (PACKHEAD *)malloc(packsiz(n));
126 +        if (p == NULL)
127 +                goto memerr;
128 +                                        /* display what we have */
129 +        for (i = 0; i < nents; i++)
130 +                if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
131 +                        if (b->nrm > n) {
132 +                                n = b->nrm;
133 +                                p = (PACKHEAD *)realloc((char *)p, packsiz(n));
134 +                                if (p == NULL)
135 +                                        goto memerr;
136 +                        }
137 +                        bcopy((char *)hdbray(b), (char *)packra(p),
138 +                                        (p->nr=b->nrm)*sizeof(RAYVAL));
139 +                        p->hd = clist[i].hd;
140 +                        p->bi = clist[i].bi;
141 +                        disp_packet(p);
142 +                }
143 +        free((char *)p);                /* clean up */
144 +        return;
145 + memerr:
146 +        error(SYSTEM, "out of memory in bundle_set");
147 + }
148 +
149 +
150 + int
151   weightf(hp, x0, x1, x2)         /* voxel weighting function */
152   register HOLO   *hp;
153   register int    x0, x1, x2;
# Line 82 | Line 208 | int    x, y, z, dx, dy, dz;
208   }
209  
210  
85 int
86 beamcmp(b0, b1)                 /* comparison for descending compute order */
87 register PACKHEAD       *b0, *b1;
88 {
89        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
90                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
91 }
92
93
211   init_global()                   /* initialize global ray computation */
212   {
213          long    wtotal = 0;
# Line 98 | Line 215 | init_global()                  /* initialize global ray computation *
215          int     lseg[2][3];
216          double  frac;
217          register int    k;
218 +                                        /* free old list */
219 +        if (complen > 0)
220 +                free((char *)complist);
221                                          /* allocate beam list */
222          complen = 0;
223          for (j = 0; hdlist[j] != NULL; j++)
# Line 126 | Line 246 | init_global()                  /* initialize global ray computation *
246                          while (k--)
247                                  complist[k].nr = frac * complist[k].nr;
248          }
249 <        listpos = 0; lastin = -1;
249 >        listpos = 0; lastin = -1;       /* flag initial sort */
250   }
251  
252  
# Line 156 | Line 276 | int    n1, n2;
276   sortcomplist()                  /* fix our list order */
277   {
278          PACKHEAD        *list2;
279 +        register int    i;
280 +
281                                  /* empty queue */
282          done_packets(flush_queue());
283 <        if (lastin < 0)         /* flag to sort entire list */
283 >        if (complen <= 0)       /* check to see if there is even a list */
284 >                return;
285 >        if (lastin < 0 || listpos*4 >= complen*3)
286                  qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
287          else if (listpos) {     /* else sort and merge sublist */
288                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
# Line 170 | Line 294 | sortcomplist()                 /* fix our list order */
294                                  complist+listpos, complen-listpos);
295                  free((char *)list2);
296          }
297 <        listpos = 0; lastin = complen-1;
297 >                                        /* check for all finished */
298 >        if (complist[0].nr <= bnrays(hdlist[complist[0].hd],complist[0].bi)) {
299 >                free((char *)complist);
300 >                complist = NULL;
301 >                complen = 0;
302 >        }
303 >                                        /* drop satisfied requests */
304 >        for (i = complen; i-- && complist[i].nr <=
305 >                        bnrays(hdlist[complist[i].hd],complist[i].bi); )
306 >                ;
307 >        if (i < 0) {
308 >                free((char *)complist);
309 >                complist = NULL;
310 >                complen = 0;
311 >        } else if (i < complen-1) {
312 >                list2 = (PACKHEAD *)realloc((char *)complist,
313 >                                (i+1)*sizeof(PACKHEAD));
314 >                if (list2 != NULL) {
315 >                        complist = list2;
316 >                        complen = i+1;
317 >                }
318 >        }
319 >        listpos = 0; lastin = i;
320   }
321  
322  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines