ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo3.c
Revision: 3.9
Committed: Fri Nov 21 17:47:56 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.8: +1 -4 lines
Log Message:
fixed problem with allocating huge array for unreasonable request

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Routines for tracking beam compuatations
9     */
10    
11     #include "rholo.h"
12    
13    
14     #define abs(x) ((x) > 0 ? (x) : -(x))
15     #define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
16    
17    
18 gregl 3.4 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 gregl 3.1
23    
24     int
25 gregl 3.2 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 gregl 3.3 if (lastin < 0) /* sort updated list */
114     sortcomplist();
115 gregl 3.2 return; /* no display */
116     default:
117     error(CONSISTENCY, "bundle_set called with unknown operation");
118     }
119 gregl 3.8 if (outdev == NULL)
120     return;
121 gregl 3.9 n = 8*RPACKSIZ; /* allocate packet holder */
122 gregl 3.5 p = (PACKHEAD *)malloc(packsiz(n));
123 gregl 3.3 if (p == NULL)
124     goto memerr;
125     /* display what we have */
126 gregl 3.2 for (i = 0; i < nents; i++)
127 gregl 3.3 if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
128 gregl 3.7 if (b->nrm > n) {
129     n = b->nrm;
130     p = (PACKHEAD *)realloc((char *)p, packsiz(n));
131     if (p == NULL)
132     goto memerr;
133     }
134 gregl 3.6 bcopy((char *)hdbray(b), (char *)packra(p),
135 gregl 3.2 (p->nr=b->nrm)*sizeof(RAYVAL));
136 gregl 3.7 p->hd = clist[i].hd;
137     p->bi = clist[i].bi;
138 gregl 3.5 disp_packet(p);
139 gregl 3.2 }
140     free((char *)p); /* clean up */
141     return;
142     memerr:
143     error(SYSTEM, "out of memory in bundle_set");
144     }
145    
146    
147     int
148 gregl 3.1 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 */
173     HOLO *hp;
174     int x, y, z, dx, dy, dz;
175     {
176     long wres = 0;
177     int n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
178    
179     sx = sgn(dx); sy = sgn(dy); sz = sgn(dz);
180     ax = abs(dx); ay = abs(dy); az = abs(dz);
181     bx = 2*ax; by = 2*ay; bz = 2*az;
182     exy = ay-ax; exz = az-ax; ezy = ay-az;
183     n = ax+ay+az + 1; /* added increment to visit last */
184     while (n--) {
185     wres += weightf(hp, x, y, z);
186     if (exy < 0) {
187     if (exz < 0) {
188     x += sx;
189     exy += by; exz += bz;
190     } else {
191     z += sz;
192     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     }
203     }
204     return(wres);
205     }
206    
207    
208     init_global() /* initialize global ray computation */
209     {
210     long wtotal = 0;
211     int i, j;
212     int lseg[2][3];
213     double frac;
214     register int k;
215 gregl 3.3 /* free old list */
216     if (complen > 0)
217     free((char *)complist);
218 gregl 3.1 /* allocate beam list */
219     complen = 0;
220     for (j = 0; hdlist[j] != NULL; j++)
221     complen += nbeams(hdlist[j]);
222     complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD));
223     if (complist == NULL)
224     error(SYSTEM, "out of memory in init_global");
225     /* compute beam weights */
226     k = 0;
227     for (j = 0; hdlist[j] != NULL; j++)
228     for (i = nbeams(hdlist[j]); i > 0; i--) {
229     hdlseg(lseg, hdlist[j], i);
230     complist[k].hd = j;
231     complist[k].bi = i;
232     complist[k].nr = lineweight( hdlist[j],
233     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] );
237     wtotal += complist[k++].nr;
238     }
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;
245     }
246 gregl 3.4 listpos = 0; lastin = -1; /* flag initial sort */
247 gregl 3.1 }
248    
249    
250     mergeclists(cdest, cl1, n1, cl2, n2) /* merge two sorted lists */
251     PACKHEAD *cdest;
252     PACKHEAD *cl1, *cl2;
253     int n1, n2;
254     {
255     int cmp;
256    
257     while (n1 | n2) {
258     if (!n1) cmp = 1;
259     else if (!n2) cmp = -1;
260     else cmp = beamcmp(cl1, cl2);
261     if (cmp > 0) {
262     copystruct(cdest, cl2);
263     cl2++; n2--;
264     } else {
265     copystruct(cdest, cl1);
266     cl1++; n1--;
267     }
268     cdest++;
269     }
270     }
271    
272    
273     sortcomplist() /* fix our list order */
274     {
275     PACKHEAD *list2;
276 gregl 3.2 register int i;
277    
278 gregl 3.1 /* empty queue */
279     done_packets(flush_queue());
280 gregl 3.3 if (complen <= 0) /* check to see if there is even a list */
281 gregl 3.2 return;
282 gregl 3.6 if (lastin < 0 || listpos*4 >= complen*3)
283 gregl 3.1 qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
284     else if (listpos) { /* else sort and merge sublist */
285     list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
286     if (list2 == NULL)
287     error(SYSTEM, "out of memory in sortcomplist");
288     bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD));
289     qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp);
290     mergeclists(complist, list2, listpos,
291     complist+listpos, complen-listpos);
292     free((char *)list2);
293     }
294 gregl 3.2 /* 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     }
300     /* drop satisfied requests */
301     for (i = complen; i-- && complist[i].nr <=
302     bnrays(hdlist[complist[i].hd],complist[i].bi); )
303     ;
304 gregl 3.4 if (i < 0) {
305     free((char *)complist);
306     complist = NULL;
307     complen = 0;
308     } else if (i < complen-1) {
309 gregl 3.2 list2 = (PACKHEAD *)realloc((char *)complist,
310     (i+1)*sizeof(PACKHEAD));
311     if (list2 != NULL) {
312     complist = list2;
313     complen = i+1;
314     }
315     }
316     listpos = 0; lastin = i;
317 gregl 3.1 }
318    
319    
320     /*
321     * The following routine works on the assumption that the bundle weights are
322     * more or less evenly distributed, such that computing a packet causes
323     * a given bundle to move way down in the computation order. We keep
324     * track of where the computed bundle with the highest priority would end
325     * up, and if we get further in our compute list than this, we resort the
326     * list and start again from the beginning. We have to flush the queue
327     * 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.
332     */
333     next_packet(p) /* prepare packet for computation */
334     register PACKET *p;
335     {
336     int ncomp;
337     register int i;
338    
339     if (complen <= 0)
340     return(0);
341     if (listpos > lastin) /* time to sort the list */
342     sortcomplist();
343     p->hd = complist[listpos].hd;
344     p->bi = complist[listpos].bi;
345     ncomp = bnrays(hdlist[p->hd],p->bi);
346     p->nr = complist[listpos].nr - ncomp;
347     if (p->nr <= 0)
348     return(0);
349     if (p->nr > RPACKSIZ)
350     p->nr = RPACKSIZ;
351     ncomp += p->nr; /* find where this one would go */
352     while (lastin > listpos && complist[listpos].nr *
353     (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
354     > complist[lastin].nr * (ncomp+1))
355     lastin--;
356     listpos++;
357     return(1);
358     }