| 1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* Routines for tracking beam compuatations |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
|
#include "rholo.h" |
| 9 |
+ |
#include "view.h" |
| 10 |
|
|
| 11 |
+ |
#ifndef NFRAG2CHUNK |
| 12 |
+ |
#define NFRAG2CHUNK 4096 /* number of fragments to start chunking */ |
| 13 |
+ |
#endif |
| 14 |
|
|
| 15 |
+ |
#ifndef abs |
| 16 |
|
#define abs(x) ((x) > 0 ? (x) : -(x)) |
| 17 |
+ |
#endif |
| 18 |
+ |
#ifndef sgn |
| 19 |
|
#define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0) |
| 20 |
+ |
#endif |
| 21 |
|
|
| 22 |
+ |
#define rchunk(n) (((n)+(RPACKSIZ/2))/RPACKSIZ) |
| 23 |
|
|
| 24 |
< |
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 */ |
| 24 |
> |
extern time_t time(); |
| 25 |
|
|
| 26 |
+ |
int chunkycmp = 0; /* clump beams together on disk */ |
| 27 |
|
|
| 28 |
+ |
static PACKHEAD *complist=NULL; /* list of beams to compute */ |
| 29 |
+ |
static int complen=0; /* length of complist */ |
| 30 |
+ |
static int listpos=0; /* current list position for next_packet */ |
| 31 |
+ |
static int lastin= -1; /* last ordered position in list */ |
| 32 |
+ |
|
| 33 |
+ |
|
| 34 |
|
int |
| 35 |
< |
beamcmp(b0, b1) /* comparison for descending compute order */ |
| 35 |
> |
beamcmp(b0, b1) /* comparison for compute order */ |
| 36 |
|
register PACKHEAD *b0, *b1; |
| 37 |
|
{ |
| 38 |
< |
return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) - |
| 39 |
< |
b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) ); |
| 38 |
> |
BEAMI *bip0, *bip1; |
| 39 |
> |
register long c; |
| 40 |
> |
/* first check desired quantities */ |
| 41 |
> |
if (chunkycmp) |
| 42 |
> |
c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) - |
| 43 |
> |
rchunk(b0->nr)*(rchunk(b1->nc)+1L); |
| 44 |
> |
else |
| 45 |
> |
c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L); |
| 46 |
> |
if (c > 0) return(1); |
| 47 |
> |
if (c < 0) return(-1); |
| 48 |
> |
/* only one file, so skip the following: */ |
| 49 |
> |
#if 0 |
| 50 |
> |
/* next, check file descriptors */ |
| 51 |
> |
c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd; |
| 52 |
> |
if (c) return(c); |
| 53 |
> |
#endif |
| 54 |
> |
/* finally, check file positions */ |
| 55 |
> |
bip0 = &hdlist[b0->hd]->bi[b0->bi]; |
| 56 |
> |
bip1 = &hdlist[b1->hd]->bi[b1->bi]; |
| 57 |
> |
/* put diskless beams last */ |
| 58 |
> |
if (!bip0->nrd) |
| 59 |
> |
return(bip1->nrd > 0); |
| 60 |
> |
if (!bip1->nrd) |
| 61 |
> |
return(-1); |
| 62 |
> |
c = bip0->fo - bip1->fo; |
| 63 |
> |
return(c < 0 ? -1 : c > 0); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
+ |
int |
| 68 |
+ |
beamidcmp(b0, b1) /* comparison for beam searching */ |
| 69 |
+ |
register PACKHEAD *b0, *b1; |
| 70 |
+ |
{ |
| 71 |
+ |
register int c = b0->hd - b1->hd; |
| 72 |
+ |
|
| 73 |
+ |
if (c) return(c); |
| 74 |
+ |
return(b0->bi - b1->bi); |
| 75 |
+ |
} |
| 76 |
+ |
|
| 77 |
+ |
|
| 78 |
+ |
int |
| 79 |
+ |
dispbeam(b, hb) /* display a holodeck beam */ |
| 80 |
+ |
register BEAM *b; |
| 81 |
+ |
register HDBEAMI *hb; |
| 82 |
+ |
{ |
| 83 |
+ |
static int n = 0; |
| 84 |
+ |
static PACKHEAD *p = NULL; |
| 85 |
+ |
|
| 86 |
+ |
if (b == NULL) |
| 87 |
+ |
return; |
| 88 |
+ |
if (b->nrm > n) { /* (re)allocate packet holder */ |
| 89 |
+ |
n = b->nrm; |
| 90 |
+ |
if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n)); |
| 91 |
+ |
else p = (PACKHEAD *)realloc((void *)p, packsiz(n)); |
| 92 |
+ |
CHECK(p==NULL, SYSTEM, "out of memory in dispbeam"); |
| 93 |
+ |
} |
| 94 |
+ |
/* assign packet fields */ |
| 95 |
+ |
bcopy((void *)hdbray(b), (void *)packra(p), b->nrm*sizeof(RAYVAL)); |
| 96 |
+ |
p->nr = p->nc = b->nrm; |
| 97 |
+ |
for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++) |
| 98 |
+ |
if (hdlist[p->hd] == NULL) |
| 99 |
+ |
error(CONSISTENCY, "unregistered holodeck in dispbeam"); |
| 100 |
+ |
p->bi = hb->b; |
| 101 |
+ |
disp_packet(p); /* display it */ |
| 102 |
+ |
if (n >= 1024) { /* free ridiculous packets */ |
| 103 |
+ |
free((void *)p); |
| 104 |
+ |
p = NULL; n = 0; |
| 105 |
+ |
} |
| 106 |
+ |
} |
| 107 |
+ |
|
| 108 |
+ |
|
| 109 |
|
bundle_set(op, clist, nents) /* bundle set operation */ |
| 110 |
|
int op; |
| 111 |
|
PACKHEAD *clist; |
| 112 |
|
int nents; |
| 113 |
|
{ |
| 114 |
< |
BEAM *b; |
| 115 |
< |
PACKHEAD *p; |
| 116 |
< |
register int i, n; |
| 117 |
< |
|
| 114 |
> |
int oldnr, n; |
| 115 |
> |
HDBEAMI *hbarr; |
| 116 |
> |
register PACKHEAD *csm; |
| 117 |
> |
register int i; |
| 118 |
> |
/* search for common members */ |
| 119 |
> |
for (csm = clist+nents; csm-- > clist; ) |
| 120 |
> |
csm->nc = -1; |
| 121 |
> |
qsort((void *)clist, nents, sizeof(PACKHEAD), beamidcmp); |
| 122 |
> |
for (i = 0; i < complen; i++) { |
| 123 |
> |
csm = (PACKHEAD *)bsearch((void *)(complist+i), (void *)clist, |
| 124 |
> |
nents, sizeof(PACKHEAD), beamidcmp); |
| 125 |
> |
if (csm == NULL) |
| 126 |
> |
continue; |
| 127 |
> |
oldnr = complist[i].nr; |
| 128 |
> |
csm->nc = complist[i].nc; |
| 129 |
> |
switch (op) { |
| 130 |
> |
case BS_ADD: /* add to count */ |
| 131 |
> |
complist[i].nr += csm->nr; |
| 132 |
> |
csm->nr = 0; |
| 133 |
> |
break; |
| 134 |
> |
case BS_MAX: /* maximum of counts */ |
| 135 |
> |
if (csm->nr > complist[i].nr) |
| 136 |
> |
complist[i].nr = csm->nr; |
| 137 |
> |
csm->nr = 0; |
| 138 |
> |
break; |
| 139 |
> |
case BS_ADJ: /* reset count */ |
| 140 |
> |
complist[i].nr = csm->nr; |
| 141 |
> |
csm->nr = 0; |
| 142 |
> |
break; |
| 143 |
> |
case BS_DEL: /* delete count */ |
| 144 |
> |
if (csm->nr == 0 || csm->nr >= complist[i].nr) |
| 145 |
> |
complist[i].nr = 0; |
| 146 |
> |
else |
| 147 |
> |
complist[i].nr -= csm->nr; |
| 148 |
> |
break; |
| 149 |
> |
} |
| 150 |
> |
if (complist[i].nr != oldnr) |
| 151 |
> |
lastin = -1; /* flag sort */ |
| 152 |
> |
} |
| 153 |
> |
/* record computed rays for uncommon beams */ |
| 154 |
> |
for (csm = clist+nents; csm-- > clist; ) |
| 155 |
> |
if (csm->nc < 0) |
| 156 |
> |
csm->nc = bnrays(hdlist[csm->hd], csm->bi); |
| 157 |
> |
/* complete list operations */ |
| 158 |
|
switch (op) { |
| 159 |
|
case BS_NEW: /* new computation set */ |
| 160 |
< |
if (complen) |
| 161 |
< |
free((char *)complist); |
| 162 |
< |
if (nents <= 0) { |
| 163 |
< |
complist = NULL; |
| 164 |
< |
listpos = complen = 0; |
| 49 |
< |
lastin = -1; |
| 160 |
> |
listpos = 0; lastin = -1; |
| 161 |
> |
if (complen) /* free old list */ |
| 162 |
> |
free((void *)complist); |
| 163 |
> |
complist = NULL; |
| 164 |
> |
if (!(complen = nents)) |
| 165 |
|
return; |
| 51 |
– |
} |
| 166 |
|
complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD)); |
| 167 |
|
if (complist == NULL) |
| 168 |
|
goto memerr; |
| 169 |
< |
bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD)); |
| 56 |
< |
complen = nents; |
| 57 |
< |
listpos = 0; |
| 58 |
< |
lastin = -1; /* flag for initial sort */ |
| 169 |
> |
bcopy((void *)clist, (void *)complist, nents*sizeof(PACKHEAD)); |
| 170 |
|
break; |
| 171 |
|
case BS_ADD: /* add to computation set */ |
| 172 |
+ |
case BS_MAX: /* maximum of quantities */ |
| 173 |
+ |
case BS_ADJ: /* adjust set quantities */ |
| 174 |
|
if (nents <= 0) |
| 175 |
|
return; |
| 176 |
< |
/* merge any common members */ |
| 177 |
< |
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); |
| 176 |
> |
sortcomplist(); /* sort updated list & new entries */ |
| 177 |
> |
qsort((void *)clist, nents, sizeof(PACKHEAD), beamcmp); |
| 178 |
|
/* what can't we satisfy? */ |
| 179 |
< |
for (n = 0; n < nents && clist[n].nr > |
| 79 |
< |
bnrays(hdlist[clist[n].hd],clist[n].bi); n++) |
| 179 |
> |
for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++) |
| 180 |
|
; |
| 181 |
+ |
n = csm - clist; |
| 182 |
+ |
if (op != BS_ADD) { /* don't regenerate adjusted beams */ |
| 183 |
+ |
for (++i; i-- && csm->nr > 0; csm++) |
| 184 |
+ |
; |
| 185 |
+ |
nents = csm - clist; |
| 186 |
+ |
} |
| 187 |
|
if (n) { /* allocate space for merged list */ |
| 188 |
|
PACKHEAD *newlist; |
| 189 |
|
newlist = (PACKHEAD *)malloc( |
| 193 |
|
/* merge lists */ |
| 194 |
|
mergeclists(newlist, clist, n, complist, complen); |
| 195 |
|
if (complen) |
| 196 |
< |
free((char *)complist); |
| 196 |
> |
free((void *)complist); |
| 197 |
|
complist = newlist; |
| 198 |
|
complen += n; |
| 199 |
|
} |
| 201 |
|
lastin = complen-1; /* list is now sorted */ |
| 202 |
|
break; |
| 203 |
|
case BS_DEL: /* delete from computation set */ |
| 204 |
< |
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 */ |
| 204 |
> |
return; /* already done */ |
| 205 |
|
default: |
| 206 |
|
error(CONSISTENCY, "bundle_set called with unknown operation"); |
| 207 |
|
} |
| 208 |
< |
n = 0; /* allocate packet holder */ |
| 209 |
< |
for (i = 0; i < nents; i++) |
| 210 |
< |
if (clist[i].nr > n) |
| 211 |
< |
n = clist[i].nr; |
| 212 |
< |
p = (PACKHEAD *)malloc(sizeof(PACKHEAD) + n*sizeof(RAYVAL)); |
| 213 |
< |
if (p == NULL) |
| 214 |
< |
goto memerr; |
| 215 |
< |
/* display what we have */ |
| 216 |
< |
for (i = 0; i < nents; i++) |
| 217 |
< |
if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) { |
| 218 |
< |
bcopy((char *)hdbray(b), (char *)(p+1), |
| 219 |
< |
(p->nr=b->nrm)*sizeof(RAYVAL)); |
| 220 |
< |
disp_packet((PACKET *)p); |
| 221 |
< |
} |
| 133 |
< |
free((char *)p); /* clean up */ |
| 208 |
> |
if (outdev == NULL || !nents) /* nothing to display? */ |
| 209 |
> |
return; |
| 210 |
> |
/* load and display beams we have */ |
| 211 |
> |
hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); |
| 212 |
> |
for (i = nents; i--; ) { |
| 213 |
> |
hbarr[i].h = hdlist[clist[i].hd]; |
| 214 |
> |
hbarr[i].b = clist[i].bi; |
| 215 |
> |
} |
| 216 |
> |
hdloadbeams(hbarr, nents, dispbeam); |
| 217 |
> |
free((void *)hbarr); |
| 218 |
> |
if (hdfragflags&FF_READ) { |
| 219 |
> |
listpos = 0; |
| 220 |
> |
lastin = -1; /* need to re-sort list */ |
| 221 |
> |
} |
| 222 |
|
return; |
| 223 |
|
memerr: |
| 224 |
|
error(SYSTEM, "out of memory in bundle_set"); |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
< |
int |
| 229 |
< |
weightf(hp, x0, x1, x2) /* voxel weighting function */ |
| 142 |
< |
register HOLO *hp; |
| 143 |
< |
register int x0, x1, x2; |
| 144 |
< |
{ |
| 145 |
< |
switch (vlet(OCCUPANCY)) { |
| 146 |
< |
case 'U': /* uniform weighting */ |
| 147 |
< |
return(1); |
| 148 |
< |
case 'C': /* center weighting (crude) */ |
| 149 |
< |
x0 += x0 - hp->grid[0] + 1; |
| 150 |
< |
x0 = abs(x0)*hp->grid[1]*hp->grid[2]; |
| 151 |
< |
x1 += x1 - hp->grid[1] + 1; |
| 152 |
< |
x1 = abs(x1)*hp->grid[0]*hp->grid[2]; |
| 153 |
< |
x2 += x2 - hp->grid[2] + 1; |
| 154 |
< |
x2 = abs(x2)*hp->grid[0]*hp->grid[1]; |
| 155 |
< |
return(hp->grid[0]*hp->grid[1]*hp->grid[2] - |
| 156 |
< |
(x0+x1+x2)/3); |
| 157 |
< |
default: |
| 158 |
< |
badvalue(OCCUPANCY); |
| 159 |
< |
} |
| 160 |
< |
} |
| 161 |
< |
|
| 162 |
< |
|
| 163 |
< |
/* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */ |
| 164 |
< |
long |
| 165 |
< |
lineweight(hp, x, y, z, dx, dy, dz) /* compute weights along a line */ |
| 228 |
> |
double |
| 229 |
> |
beamvolume(hp, bi) /* compute approximate volume of a beam */ |
| 230 |
|
HOLO *hp; |
| 231 |
< |
int x, y, z, dx, dy, dz; |
| 231 |
> |
int bi; |
| 232 |
|
{ |
| 233 |
< |
long wres = 0; |
| 234 |
< |
int n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz; |
| 235 |
< |
|
| 236 |
< |
sx = sgn(dx); sy = sgn(dy); sz = sgn(dz); |
| 237 |
< |
ax = abs(dx); ay = abs(dy); az = abs(dz); |
| 238 |
< |
bx = 2*ax; by = 2*ay; bz = 2*az; |
| 239 |
< |
exy = ay-ax; exz = az-ax; ezy = ay-az; |
| 240 |
< |
n = ax+ay+az + 1; /* added increment to visit last */ |
| 241 |
< |
while (n--) { |
| 242 |
< |
wres += weightf(hp, x, y, z); |
| 243 |
< |
if (exy < 0) { |
| 244 |
< |
if (exz < 0) { |
| 245 |
< |
x += sx; |
| 246 |
< |
exy += by; exz += bz; |
| 247 |
< |
} else { |
| 248 |
< |
z += sz; |
| 249 |
< |
exz -= bx; ezy += by; |
| 186 |
< |
} |
| 187 |
< |
} else { |
| 188 |
< |
if (ezy < 0) { |
| 189 |
< |
z += sz; |
| 190 |
< |
exz -= bx; ezy += by; |
| 191 |
< |
} else { |
| 192 |
< |
y += sy; |
| 193 |
< |
exy -= bx; ezy -= bz; |
| 194 |
< |
} |
| 195 |
< |
} |
| 233 |
> |
GCOORD gc[2]; |
| 234 |
> |
FVECT cp[4], edgeA, edgeB, cent[2]; |
| 235 |
> |
FVECT v, crossp[2], diffv; |
| 236 |
> |
double vol[2]; |
| 237 |
> |
register int i; |
| 238 |
> |
/* get grid coordinates */ |
| 239 |
> |
if (!hdbcoord(gc, hp, bi)) |
| 240 |
> |
error(CONSISTENCY, "bad beam index in beamvolume"); |
| 241 |
> |
for (i = 0; i < 2; i++) { /* compute cell area vectors */ |
| 242 |
> |
hdcell(cp, hp, gc+i); |
| 243 |
> |
VSUM(edgeA, cp[1], cp[0], -1.0); |
| 244 |
> |
VSUM(edgeB, cp[3], cp[1], -1.0); |
| 245 |
> |
fcross(crossp[i], edgeA, edgeB); |
| 246 |
> |
/* compute center */ |
| 247 |
> |
cent[i][0] = 0.5*(cp[0][0] + cp[2][0]); |
| 248 |
> |
cent[i][1] = 0.5*(cp[0][1] + cp[2][1]); |
| 249 |
> |
cent[i][2] = 0.5*(cp[0][2] + cp[2][2]); |
| 250 |
|
} |
| 251 |
< |
return(wres); |
| 251 |
> |
/* compute difference vector */ |
| 252 |
> |
VSUM(diffv, cent[1], cent[0], -1.0); |
| 253 |
> |
for (i = 0; i < 2; i++) { /* compute volume contributions */ |
| 254 |
> |
vol[i] = 0.5*DOT(crossp[i], diffv); |
| 255 |
> |
if (vol[i] < 0.) vol[i] = -vol[i]; |
| 256 |
> |
} |
| 257 |
> |
return(vol[0] + vol[1]); /* return total volume */ |
| 258 |
|
} |
| 259 |
|
|
| 260 |
|
|
| 261 |
< |
init_global() /* initialize global ray computation */ |
| 261 |
> |
ambient_list() /* compute ambient beam list */ |
| 262 |
|
{ |
| 263 |
< |
long wtotal = 0; |
| 204 |
< |
int i, j; |
| 205 |
< |
int lseg[2][3]; |
| 263 |
> |
int32 wtotal, minrt; |
| 264 |
|
double frac; |
| 265 |
< |
register int k; |
| 266 |
< |
/* free old list */ |
| 267 |
< |
if (complen > 0) |
| 210 |
< |
free((char *)complist); |
| 211 |
< |
/* allocate beam list */ |
| 265 |
> |
int i; |
| 266 |
> |
register int j, k; |
| 267 |
> |
|
| 268 |
|
complen = 0; |
| 269 |
|
for (j = 0; hdlist[j] != NULL; j++) |
| 270 |
|
complen += nbeams(hdlist[j]); |
| 271 |
|
complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD)); |
| 272 |
< |
if (complist == NULL) |
| 217 |
< |
error(SYSTEM, "out of memory in init_global"); |
| 272 |
> |
CHECK(complist==NULL, SYSTEM, "out of memory in ambient_list"); |
| 273 |
|
/* compute beam weights */ |
| 274 |
< |
k = 0; |
| 275 |
< |
for (j = 0; hdlist[j] != NULL; j++) |
| 274 |
> |
k = 0; wtotal = 0; |
| 275 |
> |
for (j = 0; hdlist[j] != NULL; j++) { |
| 276 |
> |
frac = 512. * VLEN(hdlist[j]->wg[0]) * |
| 277 |
> |
VLEN(hdlist[j]->wg[1]) * |
| 278 |
> |
VLEN(hdlist[j]->wg[2]); |
| 279 |
|
for (i = nbeams(hdlist[j]); i > 0; i--) { |
| 222 |
– |
hdlseg(lseg, hdlist[j], i); |
| 280 |
|
complist[k].hd = j; |
| 281 |
|
complist[k].bi = i; |
| 282 |
< |
complist[k].nr = lineweight( hdlist[j], |
| 283 |
< |
lseg[0][0], lseg[0][1], lseg[0][2], |
| 227 |
< |
lseg[1][0] - lseg[0][0], |
| 228 |
< |
lseg[1][1] - lseg[0][1], |
| 229 |
< |
lseg[1][2] - lseg[0][2] ); |
| 282 |
> |
complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5; |
| 283 |
> |
complist[k].nc = bnrays(hdlist[j], i); |
| 284 |
|
wtotal += complist[k++].nr; |
| 285 |
|
} |
| 286 |
< |
/* adjust weights */ |
| 287 |
< |
if (vdef(DISKSPACE)) { |
| 286 |
> |
} |
| 287 |
> |
/* adjust sample weights */ |
| 288 |
> |
if (vdef(DISKSPACE)) |
| 289 |
|
frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL)); |
| 290 |
< |
if (frac < 0.95 | frac > 1.05) |
| 291 |
< |
while (k--) |
| 292 |
< |
complist[k].nr = frac * complist[k].nr; |
| 290 |
> |
else |
| 291 |
> |
frac = 1024.*1024.*2048. / (wtotal*sizeof(RAYVAL)); |
| 292 |
> |
minrt = .02*frac*wtotal/complen + .5; /* heuristic mimimum */ |
| 293 |
> |
if (minrt > RPACKSIZ) |
| 294 |
> |
minrt = RPACKSIZ; |
| 295 |
> |
for (k = complen; k--; ) |
| 296 |
> |
if ((complist[k].nr = frac*complist[k].nr + 0.5) < minrt) |
| 297 |
> |
complist[k].nr = minrt; |
| 298 |
> |
listpos = 0; lastin = -1; /* flag initial sort */ |
| 299 |
> |
} |
| 300 |
> |
|
| 301 |
> |
|
| 302 |
> |
view_list(fp) /* assign beam priority from view list */ |
| 303 |
> |
FILE *fp; |
| 304 |
> |
{ |
| 305 |
> |
double pa = 1.; |
| 306 |
> |
VIEW curview; |
| 307 |
> |
int xr, yr; |
| 308 |
> |
char *err; |
| 309 |
> |
BEAMLIST blist; |
| 310 |
> |
|
| 311 |
> |
copystruct(&curview, &stdview); |
| 312 |
> |
while (nextview(&curview, fp) != EOF) { |
| 313 |
> |
if ((err = setview(&curview)) != NULL) { |
| 314 |
> |
error(WARNING, err); |
| 315 |
> |
continue; |
| 316 |
> |
} |
| 317 |
> |
xr = yr = 1024; |
| 318 |
> |
normaspect(viewaspect(&curview), &pa, &xr, &yr); |
| 319 |
> |
viewbeams(&curview, xr, yr, &blist); |
| 320 |
> |
bundle_set(BS_MAX, blist.bl, blist.nb); |
| 321 |
> |
free((void *)blist.bl); |
| 322 |
|
} |
| 239 |
– |
listpos = 0; lastin = -1; |
| 323 |
|
} |
| 324 |
|
|
| 325 |
|
|
| 326 |
+ |
init_global() /* initialize global ray computation */ |
| 327 |
+ |
{ |
| 328 |
+ |
register int k; |
| 329 |
+ |
/* free old list and empty queue */ |
| 330 |
+ |
if (complen > 0) { |
| 331 |
+ |
free((void *)complist); |
| 332 |
+ |
done_packets(flush_queue()); |
| 333 |
+ |
} |
| 334 |
+ |
/* reseed random number generator */ |
| 335 |
+ |
srandom(time(NULL)); |
| 336 |
+ |
/* allocate beam list */ |
| 337 |
+ |
if (readinp) |
| 338 |
+ |
view_list(stdin); |
| 339 |
+ |
else |
| 340 |
+ |
ambient_list(); |
| 341 |
+ |
/* no view vicinity */ |
| 342 |
+ |
myeye.rng = 0; |
| 343 |
+ |
} |
| 344 |
+ |
|
| 345 |
+ |
|
| 346 |
|
mergeclists(cdest, cl1, n1, cl2, n2) /* merge two sorted lists */ |
| 347 |
< |
PACKHEAD *cdest; |
| 348 |
< |
PACKHEAD *cl1, *cl2; |
| 347 |
> |
register PACKHEAD *cdest; |
| 348 |
> |
register PACKHEAD *cl1, *cl2; |
| 349 |
|
int n1, n2; |
| 350 |
|
{ |
| 351 |
< |
int cmp; |
| 351 |
> |
register int cmp; |
| 352 |
|
|
| 353 |
|
while (n1 | n2) { |
| 354 |
|
if (!n1) cmp = 1; |
| 369 |
|
sortcomplist() /* fix our list order */ |
| 370 |
|
{ |
| 371 |
|
PACKHEAD *list2; |
| 372 |
+ |
int listlen; |
| 373 |
|
register int i; |
| 374 |
|
|
| 271 |
– |
/* empty queue */ |
| 272 |
– |
done_packets(flush_queue()); |
| 375 |
|
if (complen <= 0) /* check to see if there is even a list */ |
| 376 |
|
return; |
| 377 |
< |
if (lastin < 0) /* flag to sort entire list */ |
| 378 |
< |
qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp); |
| 377 |
> |
if (!chunkycmp) /* check to see if fragment list is full */ |
| 378 |
> |
if (!hdfragOK(hdlist[0]->fd, &listlen, NULL) |
| 379 |
> |
#if NFRAG2CHUNK |
| 380 |
> |
|| listlen >= NFRAG2CHUNK |
| 381 |
> |
#endif |
| 382 |
> |
) { |
| 383 |
> |
chunkycmp++; /* use "chunky" comparison */ |
| 384 |
> |
lastin = -1; /* need to re-sort list */ |
| 385 |
> |
#ifdef DEBUG |
| 386 |
> |
error(WARNING, "using chunky comparison mode"); |
| 387 |
> |
#endif |
| 388 |
> |
} |
| 389 |
> |
if (lastin < 0 || listpos*4 >= complen*3) |
| 390 |
> |
qsort((void *)complist, complen, sizeof(PACKHEAD), beamcmp); |
| 391 |
|
else if (listpos) { /* else sort and merge sublist */ |
| 392 |
|
list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD)); |
| 393 |
< |
if (list2 == NULL) |
| 394 |
< |
error(SYSTEM, "out of memory in sortcomplist"); |
| 395 |
< |
bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD)); |
| 282 |
< |
qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp); |
| 393 |
> |
CHECK(list2==NULL, SYSTEM, "out of memory in sortcomplist"); |
| 394 |
> |
bcopy((void *)complist,(void *)list2,listpos*sizeof(PACKHEAD)); |
| 395 |
> |
qsort((void *)list2, listpos, sizeof(PACKHEAD), beamcmp); |
| 396 |
|
mergeclists(complist, list2, listpos, |
| 397 |
|
complist+listpos, complen-listpos); |
| 398 |
< |
free((char *)list2); |
| 398 |
> |
free((void *)list2); |
| 399 |
|
} |
| 287 |
– |
/* check for all finished */ |
| 288 |
– |
if (complist[0].nr <= bnrays(hdlist[complist[0].hd],complist[0].bi)) { |
| 289 |
– |
free((char *)complist); |
| 290 |
– |
complist = NULL; |
| 291 |
– |
complen = 0; |
| 292 |
– |
} |
| 400 |
|
/* drop satisfied requests */ |
| 401 |
< |
for (i = complen; i-- && complist[i].nr <= |
| 295 |
< |
bnrays(hdlist[complist[i].hd],complist[i].bi); ) |
| 401 |
> |
for (i = complen; i-- && complist[i].nr <= complist[i].nc; ) |
| 402 |
|
; |
| 403 |
< |
if (i < complen-1) { |
| 404 |
< |
list2 = (PACKHEAD *)realloc((char *)complist, |
| 403 |
> |
if (i < 0) { |
| 404 |
> |
free((void *)complist); |
| 405 |
> |
complist = NULL; |
| 406 |
> |
complen = 0; |
| 407 |
> |
} else if (i < complen-1) { |
| 408 |
> |
list2 = (PACKHEAD *)realloc((void *)complist, |
| 409 |
|
(i+1)*sizeof(PACKHEAD)); |
| 410 |
< |
if (list2 != NULL) { |
| 410 |
> |
if (list2 != NULL) |
| 411 |
|
complist = list2; |
| 412 |
< |
complen = i+1; |
| 303 |
< |
} |
| 412 |
> |
complen = i+1; |
| 413 |
|
} |
| 414 |
|
listpos = 0; lastin = i; |
| 415 |
|
} |
| 420 |
|
* more or less evenly distributed, such that computing a packet causes |
| 421 |
|
* a given bundle to move way down in the computation order. We keep |
| 422 |
|
* track of where the computed bundle with the highest priority would end |
| 423 |
< |
* up, and if we get further in our compute list than this, we resort the |
| 424 |
< |
* list and start again from the beginning. We have to flush the queue |
| 425 |
< |
* each time we sort, to ensure that we are not disturbing the order. |
| 317 |
< |
* If our major assumption is violated, and we have a very steep |
| 318 |
< |
* descent in our weights, then we will end up resorting much more often |
| 319 |
< |
* than necessary, resulting in frequent flushing of the queue. Since |
| 320 |
< |
* a merge sort is used, the sorting costs will be minimal. |
| 423 |
> |
* up, and if we get further in our compute list than this, we re-sort the |
| 424 |
> |
* list and start again from the beginning. Since |
| 425 |
> |
* a merge sort is used, the sorting costs are minimal. |
| 426 |
|
*/ |
| 427 |
< |
next_packet(p) /* prepare packet for computation */ |
| 427 |
> |
next_packet(p, n) /* prepare packet for computation */ |
| 428 |
|
register PACKET *p; |
| 429 |
+ |
int n; |
| 430 |
|
{ |
| 325 |
– |
int ncomp; |
| 431 |
|
register int i; |
| 432 |
|
|
| 328 |
– |
if (complen <= 0) |
| 329 |
– |
return(0); |
| 433 |
|
if (listpos > lastin) /* time to sort the list */ |
| 434 |
|
sortcomplist(); |
| 435 |
+ |
if (complen <= 0) |
| 436 |
+ |
return(0); |
| 437 |
|
p->hd = complist[listpos].hd; |
| 438 |
|
p->bi = complist[listpos].bi; |
| 439 |
< |
ncomp = bnrays(hdlist[p->hd],p->bi); |
| 440 |
< |
p->nr = complist[listpos].nr - ncomp; |
| 439 |
> |
p->nc = complist[listpos].nc; |
| 440 |
> |
p->nr = complist[listpos].nr - p->nc; |
| 441 |
|
if (p->nr <= 0) |
| 442 |
|
return(0); |
| 443 |
< |
if (p->nr > RPACKSIZ) |
| 444 |
< |
p->nr = RPACKSIZ; |
| 445 |
< |
ncomp += p->nr; /* find where this one would go */ |
| 446 |
< |
while (lastin > listpos && complist[listpos].nr * |
| 447 |
< |
(bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1) |
| 448 |
< |
> complist[lastin].nr * (ncomp+1)) |
| 443 |
> |
DCHECK(n < 1 | n > RPACKSIZ, |
| 444 |
> |
CONSISTENCY, "next_packet called with bad n value"); |
| 445 |
> |
if (p->nr > n) |
| 446 |
> |
p->nr = n; |
| 447 |
> |
complist[listpos].nc += p->nr; /* find where this one would go */ |
| 448 |
> |
if (hdgetbeam(hdlist[p->hd], p->bi) != NULL) |
| 449 |
> |
hdfreefrag(hdlist[p->hd], p->bi); |
| 450 |
> |
while (lastin > listpos && |
| 451 |
> |
beamcmp(complist+lastin, complist+listpos) > 0) |
| 452 |
|
lastin--; |
| 453 |
|
listpos++; |
| 454 |
|
return(1); |