ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo3.c
Revision: 3.10
Committed: Mon Nov 24 15:18:30 1997 UTC (26 years, 10 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.9: +2 -10 lines
Log Message:
fixed bug in next_packet() which caused core dumps with empty list

File Contents

# Content
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 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 return; /* no display */
114 default:
115 error(CONSISTENCY, "bundle_set called with unknown operation");
116 }
117 if (outdev == NULL)
118 return;
119 n = 8*RPACKSIZ; /* allocate packet holder */
120 p = (PACKHEAD *)malloc(packsiz(n));
121 if (p == NULL)
122 goto memerr;
123 /* display what we have */
124 for (i = 0; i < nents; i++)
125 if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
126 if (b->nrm > n) {
127 n = b->nrm;
128 p = (PACKHEAD *)realloc((char *)p, packsiz(n));
129 if (p == NULL)
130 goto memerr;
131 }
132 bcopy((char *)hdbray(b), (char *)packra(p),
133 (p->nr=b->nrm)*sizeof(RAYVAL));
134 p->hd = clist[i].hd;
135 p->bi = clist[i].bi;
136 disp_packet(p);
137 }
138 free((char *)p); /* clean up */
139 return;
140 memerr:
141 error(SYSTEM, "out of memory in bundle_set");
142 }
143
144
145 int
146 weightf(hp, x0, x1, x2) /* voxel weighting function */
147 register HOLO *hp;
148 register int x0, x1, x2;
149 {
150 switch (vlet(OCCUPANCY)) {
151 case 'U': /* uniform weighting */
152 return(1);
153 case 'C': /* center weighting (crude) */
154 x0 += x0 - hp->grid[0] + 1;
155 x0 = abs(x0)*hp->grid[1]*hp->grid[2];
156 x1 += x1 - hp->grid[1] + 1;
157 x1 = abs(x1)*hp->grid[0]*hp->grid[2];
158 x2 += x2 - hp->grid[2] + 1;
159 x2 = abs(x2)*hp->grid[0]*hp->grid[1];
160 return(hp->grid[0]*hp->grid[1]*hp->grid[2] -
161 (x0+x1+x2)/3);
162 default:
163 badvalue(OCCUPANCY);
164 }
165 }
166
167
168 /* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */
169 long
170 lineweight(hp, x, y, z, dx, dy, dz) /* compute weights along a line */
171 HOLO *hp;
172 int x, y, z, dx, dy, dz;
173 {
174 long wres = 0;
175 int n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
176
177 sx = sgn(dx); sy = sgn(dy); sz = sgn(dz);
178 ax = abs(dx); ay = abs(dy); az = abs(dz);
179 bx = 2*ax; by = 2*ay; bz = 2*az;
180 exy = ay-ax; exz = az-ax; ezy = ay-az;
181 n = ax+ay+az + 1; /* added increment to visit last */
182 while (n--) {
183 wres += weightf(hp, x, y, z);
184 if (exy < 0) {
185 if (exz < 0) {
186 x += sx;
187 exy += by; exz += bz;
188 } else {
189 z += sz;
190 exz -= bx; ezy += by;
191 }
192 } else {
193 if (ezy < 0) {
194 z += sz;
195 exz -= bx; ezy += by;
196 } else {
197 y += sy;
198 exy -= bx; ezy -= bz;
199 }
200 }
201 }
202 return(wres);
203 }
204
205
206 init_global() /* initialize global ray computation */
207 {
208 long wtotal = 0;
209 int i, j;
210 int lseg[2][3];
211 double frac;
212 register int k;
213 /* free old list */
214 if (complen > 0)
215 free((char *)complist);
216 /* allocate beam list */
217 complen = 0;
218 for (j = 0; hdlist[j] != NULL; j++)
219 complen += nbeams(hdlist[j]);
220 complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD));
221 if (complist == NULL)
222 error(SYSTEM, "out of memory in init_global");
223 /* compute beam weights */
224 k = 0;
225 for (j = 0; hdlist[j] != NULL; j++)
226 for (i = nbeams(hdlist[j]); i > 0; i--) {
227 hdlseg(lseg, hdlist[j], i);
228 complist[k].hd = j;
229 complist[k].bi = i;
230 complist[k].nr = lineweight( hdlist[j],
231 lseg[0][0], lseg[0][1], lseg[0][2],
232 lseg[1][0] - lseg[0][0],
233 lseg[1][1] - lseg[0][1],
234 lseg[1][2] - lseg[0][2] );
235 wtotal += complist[k++].nr;
236 }
237 /* adjust weights */
238 if (vdef(DISKSPACE)) {
239 frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
240 if (frac < 0.95 | frac > 1.05)
241 while (k--)
242 complist[k].nr = frac * complist[k].nr;
243 }
244 listpos = 0; lastin = -1; /* flag initial sort */
245 }
246
247
248 mergeclists(cdest, cl1, n1, cl2, n2) /* merge two sorted lists */
249 PACKHEAD *cdest;
250 PACKHEAD *cl1, *cl2;
251 int n1, n2;
252 {
253 int cmp;
254
255 while (n1 | n2) {
256 if (!n1) cmp = 1;
257 else if (!n2) cmp = -1;
258 else cmp = beamcmp(cl1, cl2);
259 if (cmp > 0) {
260 copystruct(cdest, cl2);
261 cl2++; n2--;
262 } else {
263 copystruct(cdest, cl1);
264 cl1++; n1--;
265 }
266 cdest++;
267 }
268 }
269
270
271 sortcomplist() /* fix our list order */
272 {
273 PACKHEAD *list2;
274 register int i;
275
276 /* empty queue */
277 done_packets(flush_queue());
278 if (complen <= 0) /* check to see if there is even a list */
279 return;
280 if (lastin < 0 || listpos*4 >= complen*3)
281 qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
282 else if (listpos) { /* else sort and merge sublist */
283 list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
284 if (list2 == NULL)
285 error(SYSTEM, "out of memory in sortcomplist");
286 bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD));
287 qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp);
288 mergeclists(complist, list2, listpos,
289 complist+listpos, complen-listpos);
290 free((char *)list2);
291 }
292 /* drop satisfied requests */
293 for (i = complen; i-- && complist[i].nr <=
294 bnrays(hdlist[complist[i].hd],complist[i].bi); )
295 ;
296 if (i < 0) {
297 free((char *)complist);
298 complist = NULL;
299 complen = 0;
300 } else if (i < complen-1) {
301 list2 = (PACKHEAD *)realloc((char *)complist,
302 (i+1)*sizeof(PACKHEAD));
303 if (list2 != NULL) {
304 complist = list2;
305 complen = i+1;
306 }
307 }
308 listpos = 0; lastin = i;
309 }
310
311
312 /*
313 * The following routine works on the assumption that the bundle weights are
314 * more or less evenly distributed, such that computing a packet causes
315 * a given bundle to move way down in the computation order. We keep
316 * track of where the computed bundle with the highest priority would end
317 * up, and if we get further in our compute list than this, we resort the
318 * list and start again from the beginning. We have to flush the queue
319 * each time we sort, to ensure that we are not disturbing the order.
320 * If our major assumption is violated, and we have a very steep
321 * descent in our weights, then we will end up resorting much more often
322 * than necessary, resulting in frequent flushing of the queue. Since
323 * a merge sort is used, the sorting costs will be minimal.
324 */
325 next_packet(p) /* prepare packet for computation */
326 register PACKET *p;
327 {
328 int ncomp;
329 register int i;
330
331 if (listpos > lastin) /* time to sort the list */
332 sortcomplist();
333 if (complen <= 0)
334 return(0);
335 p->hd = complist[listpos].hd;
336 p->bi = complist[listpos].bi;
337 ncomp = bnrays(hdlist[p->hd],p->bi);
338 p->nr = complist[listpos].nr - ncomp;
339 if (p->nr <= 0)
340 return(0);
341 if (p->nr > RPACKSIZ)
342 p->nr = RPACKSIZ;
343 ncomp += p->nr; /* find where this one would go */
344 while (lastin > listpos && complist[listpos].nr *
345 (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
346 > complist[lastin].nr * (ncomp+1))
347 lastin--;
348 listpos++;
349 return(1);
350 }