ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo3.c
Revision: 3.2
Committed: Mon Nov 3 18:33:13 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.1: +140 -12 lines
Log Message:
added more routines for display support

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