1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
10 |
|
|
11 |
|
#include "rholo.h" |
12 |
|
|
13 |
– |
|
13 |
|
#define abs(x) ((x) > 0 ? (x) : -(x)) |
14 |
|
#define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0) |
15 |
|
|
17 |
– |
|
16 |
|
static PACKHEAD *complist=NULL; /* list of beams to compute */ |
17 |
|
static int complen=0; /* length of complist */ |
18 |
|
static int listpos=0; /* current list position for next_packet */ |
20 |
|
|
21 |
|
|
22 |
|
int |
23 |
< |
beamcmp(b0, b1) /* comparison for descending compute order */ |
23 |
> |
beamcmp(b0, b1) /* comparison for compute order */ |
24 |
|
register PACKHEAD *b0, *b1; |
25 |
|
{ |
26 |
< |
return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) - |
29 |
< |
b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) ); |
26 |
> |
return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) ); |
27 |
|
} |
28 |
|
|
29 |
|
|
30 |
+ |
int |
31 |
+ |
beamidcmp(b0, b1) /* comparison for beam searching */ |
32 |
+ |
register PACKHEAD *b0, *b1; |
33 |
+ |
{ |
34 |
+ |
register int c = b0->hd - b1->hd; |
35 |
+ |
|
36 |
+ |
if (c) return(c); |
37 |
+ |
return(b0->bi - b1->bi); |
38 |
+ |
} |
39 |
+ |
|
40 |
+ |
|
41 |
+ |
int |
42 |
+ |
dispbeam(b, hb) /* display a holodeck beam */ |
43 |
+ |
register BEAM *b; |
44 |
+ |
register HDBEAMI *hb; |
45 |
+ |
{ |
46 |
+ |
static int n = 0; |
47 |
+ |
static PACKHEAD *p = NULL; |
48 |
+ |
|
49 |
+ |
if (b == NULL) |
50 |
+ |
return; |
51 |
+ |
if (b->nrm > n) { /* (re)allocate packet holder */ |
52 |
+ |
n = b->nrm; |
53 |
+ |
if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n)); |
54 |
+ |
else p = (PACKHEAD *)realloc((char *)p, packsiz(n)); |
55 |
+ |
if (p == NULL) |
56 |
+ |
error(SYSTEM, "out of memory in dispbeam"); |
57 |
+ |
} |
58 |
+ |
/* assign packet fields */ |
59 |
+ |
bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL)); |
60 |
+ |
p->nr = p->nc = b->nrm; |
61 |
+ |
for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++) |
62 |
+ |
if (hdlist[p->hd] == NULL) |
63 |
+ |
error(CONSISTENCY, "unregistered holodeck in dispbeam"); |
64 |
+ |
p->bi = hb->b; |
65 |
+ |
disp_packet(p); /* display it */ |
66 |
+ |
} |
67 |
+ |
|
68 |
+ |
|
69 |
|
bundle_set(op, clist, nents) /* bundle set operation */ |
70 |
|
int op; |
71 |
|
PACKHEAD *clist; |
72 |
|
int nents; |
73 |
|
{ |
74 |
< |
BEAM *b; |
75 |
< |
PACKHEAD *p; |
76 |
< |
register int i, n; |
77 |
< |
|
74 |
> |
int oldnr, n; |
75 |
> |
HDBEAMI *hbarr; |
76 |
> |
register PACKHEAD *csm; |
77 |
> |
register int i; |
78 |
> |
/* search for common members */ |
79 |
> |
qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp); |
80 |
> |
for (csm = clist+nents; csm-- > clist; ) |
81 |
> |
csm->nc = -1; |
82 |
> |
for (i = 0; i < complen; i++) { |
83 |
> |
csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist, |
84 |
> |
nents, sizeof(PACKHEAD), beamidcmp); |
85 |
> |
if (csm == NULL) |
86 |
> |
continue; |
87 |
> |
oldnr = complist[i].nr; |
88 |
> |
csm->nc = complist[i].nc; |
89 |
> |
switch (op) { |
90 |
> |
case BS_ADD: /* add to count */ |
91 |
> |
complist[i].nr += csm->nr; |
92 |
> |
csm->nr = 0; |
93 |
> |
break; |
94 |
> |
case BS_ADJ: /* reset count */ |
95 |
> |
complist[i].nr = csm->nr; |
96 |
> |
csm->nr = 0; |
97 |
> |
break; |
98 |
> |
case BS_DEL: /* delete count */ |
99 |
> |
if (csm->nr == 0 || csm->nr >= complist[i].nr) |
100 |
> |
complist[i].nr = 0; |
101 |
> |
else |
102 |
> |
complist[i].nr -= csm->nr; |
103 |
> |
break; |
104 |
> |
} |
105 |
> |
if (complist[i].nr != oldnr) |
106 |
> |
lastin = -1; /* flag sort */ |
107 |
> |
} |
108 |
> |
/* computed rays for each uncommon beams */ |
109 |
> |
for (csm = clist+nents; csm-- > clist; ) |
110 |
> |
if (csm->nc < 0) |
111 |
> |
csm->nc = bnrays(hdlist[csm->hd], csm->bi); |
112 |
> |
/* complete list operations */ |
113 |
|
switch (op) { |
114 |
|
case BS_NEW: /* new computation set */ |
115 |
< |
if (complen) |
115 |
> |
listpos = 0; lastin = -1; |
116 |
> |
if (complen) /* free old list */ |
117 |
|
free((char *)complist); |
118 |
< |
if (nents <= 0) { |
119 |
< |
complist = NULL; |
48 |
< |
listpos = complen = 0; |
49 |
< |
lastin = -1; |
118 |
> |
complist = NULL; |
119 |
> |
if (!(complen = nents)) |
120 |
|
return; |
51 |
– |
} |
121 |
|
complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD)); |
122 |
|
if (complist == NULL) |
123 |
|
goto memerr; |
124 |
|
bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD)); |
56 |
– |
complen = nents; |
57 |
– |
listpos = 0; |
58 |
– |
lastin = -1; /* flag for initial sort */ |
125 |
|
break; |
126 |
|
case BS_ADD: /* add to computation set */ |
127 |
+ |
case BS_ADJ: /* adjust set quantities */ |
128 |
|
if (nents <= 0) |
129 |
|
return; |
130 |
< |
/* 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 */ |
130 |
> |
sortcomplist(); /* sort updated list & new entries */ |
131 |
|
qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp); |
132 |
|
/* what can't we satisfy? */ |
133 |
< |
for (n = 0; n < nents && clist[n].nr > |
79 |
< |
bnrays(hdlist[clist[n].hd],clist[n].bi); n++) |
133 |
> |
for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++) |
134 |
|
; |
135 |
+ |
n = csm - clist; |
136 |
+ |
if (op == BS_ADJ) { /* don't regenerate adjusted beams */ |
137 |
+ |
for (i = n; i < nents && clist[i].nr > 0; i++) |
138 |
+ |
; |
139 |
+ |
nents = i; |
140 |
+ |
} |
141 |
|
if (n) { /* allocate space for merged list */ |
142 |
|
PACKHEAD *newlist; |
143 |
|
newlist = (PACKHEAD *)malloc( |
155 |
|
lastin = complen-1; /* list is now sorted */ |
156 |
|
break; |
157 |
|
case BS_DEL: /* delete from computation set */ |
158 |
< |
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 */ |
158 |
> |
return; /* already done */ |
159 |
|
default: |
160 |
|
error(CONSISTENCY, "bundle_set called with unknown operation"); |
161 |
|
} |
162 |
< |
if (outdev == NULL) |
162 |
> |
if (outdev == NULL) /* nothing to display? */ |
163 |
|
return; |
164 |
< |
n = 8*RPACKSIZ; /* allocate packet holder */ |
165 |
< |
p = (PACKHEAD *)malloc(packsiz(n)); |
166 |
< |
if (p == NULL) |
167 |
< |
goto memerr; |
168 |
< |
/* display what we have */ |
169 |
< |
for (i = 0; i < nents; i++) |
170 |
< |
if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) { |
171 |
< |
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 */ |
164 |
> |
/* load and display beams we have */ |
165 |
> |
hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); |
166 |
> |
for (i = nents; i--; ) { |
167 |
> |
hbarr[i].h = hdlist[clist[i].hd]; |
168 |
> |
hbarr[i].b = clist[i].bi; |
169 |
> |
} |
170 |
> |
hdloadbeams(hbarr, nents, dispbeam); |
171 |
> |
free((char *)hbarr); |
172 |
|
return; |
173 |
|
memerr: |
174 |
|
error(SYSTEM, "out of memory in bundle_set"); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
< |
int |
179 |
< |
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 */ |
178 |
> |
double |
179 |
> |
beamvolume(hp, bi) /* compute approximate volume of a beam */ |
180 |
|
HOLO *hp; |
181 |
< |
int x, y, z, dx, dy, dz; |
181 |
> |
int bi; |
182 |
|
{ |
183 |
< |
long wres = 0; |
184 |
< |
int n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz; |
185 |
< |
|
186 |
< |
sx = sgn(dx); sy = sgn(dy); sz = sgn(dz); |
187 |
< |
ax = abs(dx); ay = abs(dy); az = abs(dz); |
188 |
< |
bx = 2*ax; by = 2*ay; bz = 2*az; |
189 |
< |
exy = ay-ax; exz = az-ax; ezy = ay-az; |
190 |
< |
n = ax+ay+az + 1; /* added increment to visit last */ |
191 |
< |
while (n--) { |
192 |
< |
wres += weightf(hp, x, y, z); |
193 |
< |
if (exy < 0) { |
194 |
< |
if (exz < 0) { |
195 |
< |
x += sx; |
196 |
< |
exy += by; exz += bz; |
197 |
< |
} else { |
198 |
< |
z += sz; |
199 |
< |
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 |
< |
} |
183 |
> |
GCOORD gc[2]; |
184 |
> |
FVECT cp[4], edgeA, edgeB, cent[2]; |
185 |
> |
FVECT v, crossp[2], diffv; |
186 |
> |
double vol[2]; |
187 |
> |
register int i; |
188 |
> |
/* get grid coordinates */ |
189 |
> |
if (!hdbcoord(gc, hp, bi)) |
190 |
> |
error(CONSISTENCY, "bad beam index in beamvolume"); |
191 |
> |
for (i = 0; i < 2; i++) { /* compute cell area vectors */ |
192 |
> |
hdcell(cp, hp, gc+i); |
193 |
> |
VSUM(edgeA, cp[1], cp[0], -1.0); |
194 |
> |
VSUM(edgeB, cp[3], cp[1], -1.0); |
195 |
> |
fcross(crossp[i], edgeA, edgeB); |
196 |
> |
/* compute center */ |
197 |
> |
cent[i][0] = 0.5*(cp[0][0] + cp[2][0]); |
198 |
> |
cent[i][1] = 0.5*(cp[0][1] + cp[2][1]); |
199 |
> |
cent[i][2] = 0.5*(cp[0][2] + cp[2][2]); |
200 |
|
} |
201 |
< |
return(wres); |
201 |
> |
/* compute difference vector */ |
202 |
> |
VSUM(diffv, cent[1], cent[0], -1.0); |
203 |
> |
for (i = 0; i < 2; i++) { /* compute volume contributions */ |
204 |
> |
vol[i] = 0.5*DOT(crossp[i], diffv); |
205 |
> |
if (vol[i] < 0.) vol[i] = -vol[i]; |
206 |
> |
} |
207 |
> |
return(vol[0] + vol[1]); /* return total volume */ |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
init_global() /* initialize global ray computation */ |
212 |
|
{ |
213 |
|
long wtotal = 0; |
209 |
– |
int i, j; |
210 |
– |
int lseg[2][3]; |
214 |
|
double frac; |
215 |
< |
register int k; |
216 |
< |
/* free old list */ |
217 |
< |
if (complen > 0) |
215 |
> |
int i; |
216 |
> |
register int j, k; |
217 |
> |
/* free old list and empty queue */ |
218 |
> |
if (complen > 0) { |
219 |
|
free((char *)complist); |
220 |
+ |
done_packets(flush_queue()); |
221 |
+ |
} |
222 |
|
/* allocate beam list */ |
223 |
|
complen = 0; |
224 |
|
for (j = 0; hdlist[j] != NULL; j++) |
228 |
|
error(SYSTEM, "out of memory in init_global"); |
229 |
|
/* compute beam weights */ |
230 |
|
k = 0; |
231 |
< |
for (j = 0; hdlist[j] != NULL; j++) |
231 |
> |
for (j = 0; hdlist[j] != NULL; j++) { |
232 |
> |
frac = 512. * VLEN(hdlist[j]->wg[0]) * |
233 |
> |
VLEN(hdlist[j]->wg[1]) * |
234 |
> |
VLEN(hdlist[j]->wg[2]); |
235 |
|
for (i = nbeams(hdlist[j]); i > 0; i--) { |
227 |
– |
hdlseg(lseg, hdlist[j], i); |
236 |
|
complist[k].hd = j; |
237 |
|
complist[k].bi = i; |
238 |
< |
complist[k].nr = lineweight( hdlist[j], |
239 |
< |
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] ); |
238 |
> |
complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5; |
239 |
> |
complist[k].nc = bnrays(hdlist[j], i); |
240 |
|
wtotal += complist[k++].nr; |
241 |
|
} |
242 |
+ |
} |
243 |
|
/* adjust weights */ |
244 |
< |
if (vdef(DISKSPACE)) { |
244 |
> |
if (vdef(DISKSPACE)) |
245 |
|
frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL)); |
246 |
< |
if (frac < 0.95 | frac > 1.05) |
247 |
< |
while (k--) |
248 |
< |
complist[k].nr = frac * complist[k].nr; |
249 |
< |
} |
250 |
< |
listpos = 0; lastin = -1; /* flag initial sort */ |
246 |
> |
else |
247 |
> |
frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL)); |
248 |
> |
while (k--) |
249 |
> |
complist[k].nr = frac * complist[k].nr; |
250 |
> |
listpos = 0; lastin = -1; /* perform initial sort */ |
251 |
> |
sortcomplist(); |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
mergeclists(cdest, cl1, n1, cl2, n2) /* merge two sorted lists */ |
256 |
< |
PACKHEAD *cdest; |
257 |
< |
PACKHEAD *cl1, *cl2; |
256 |
> |
register PACKHEAD *cdest; |
257 |
> |
register PACKHEAD *cl1, *cl2; |
258 |
|
int n1, n2; |
259 |
|
{ |
260 |
< |
int cmp; |
260 |
> |
register int cmp; |
261 |
|
|
262 |
|
while (n1 | n2) { |
263 |
|
if (!n1) cmp = 1; |
280 |
|
PACKHEAD *list2; |
281 |
|
register int i; |
282 |
|
|
276 |
– |
/* empty queue */ |
277 |
– |
done_packets(flush_queue()); |
283 |
|
if (complen <= 0) /* check to see if there is even a list */ |
284 |
|
return; |
285 |
|
if (lastin < 0 || listpos*4 >= complen*3) |
295 |
|
free((char *)list2); |
296 |
|
} |
297 |
|
/* drop satisfied requests */ |
298 |
< |
for (i = complen; i-- && complist[i].nr <= |
294 |
< |
bnrays(hdlist[complist[i].hd],complist[i].bi); ) |
298 |
> |
for (i = complen; i-- && complist[i].nr <= complist[i].nc; ) |
299 |
|
; |
300 |
|
if (i < 0) { |
301 |
|
free((char *)complist); |
319 |
|
* a given bundle to move way down in the computation order. We keep |
320 |
|
* track of where the computed bundle with the highest priority would end |
321 |
|
* up, and if we get further in our compute list than this, we resort the |
322 |
< |
* list and start again from the beginning. We have to flush the queue |
323 |
< |
* 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. |
322 |
> |
* list and start again from the beginning. Since |
323 |
> |
* a merge sort is used, the sorting costs are minimal. |
324 |
|
*/ |
325 |
< |
next_packet(p) /* prepare packet for computation */ |
325 |
> |
next_packet(p, n) /* prepare packet for computation */ |
326 |
|
register PACKET *p; |
327 |
+ |
int n; |
328 |
|
{ |
328 |
– |
int ncomp; |
329 |
|
register int i; |
330 |
|
|
331 |
|
if (listpos > lastin) /* time to sort the list */ |
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; |
337 |
> |
p->nc = complist[listpos].nc; |
338 |
> |
p->nr = complist[listpos].nr - p->nc; |
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)) |
341 |
> |
#ifdef DEBUG |
342 |
> |
if (n < 1 | n > RPACKSIZ) |
343 |
> |
error(CONSISTENCY, "next_packet called with bad n value"); |
344 |
> |
#endif |
345 |
> |
if (p->nr > n) |
346 |
> |
p->nr = n; |
347 |
> |
complist[listpos].nc += p->nr; /* find where this one would go */ |
348 |
> |
while (lastin > listpos && |
349 |
> |
beamcmp(complist+lastin, complist+listpos) > 0) |
350 |
|
lastin--; |
351 |
|
listpos++; |
352 |
|
return(1); |