| 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 |
|
* Holodeck beam support for display process |
| 6 |
|
*/ |
| 16 |
|
|
| 17 |
|
|
| 18 |
|
int |
| 19 |
< |
npixels(vp, hr, vr, hp, bi) /* compute appropriate number to evaluate */ |
| 20 |
< |
VIEW *vp; |
| 19 |
> |
npixels(vp, hr, vr, hp, bi) /* compute appropriate nrays to evaluate */ |
| 20 |
> |
register VIEW *vp; |
| 21 |
|
int hr, vr; |
| 22 |
|
HOLO *hp; |
| 23 |
|
int bi; |
| 24 |
|
{ |
| 25 |
< |
static VIEW vdo, vlast; |
| 29 |
< |
static HOLO *hplast; |
| 25 |
> |
VIEW vrev; |
| 26 |
|
GCOORD gc[2]; |
| 27 |
< |
FVECT cp[4]; |
| 28 |
< |
FVECT ip[4]; |
| 33 |
< |
double d; |
| 27 |
> |
FVECT cp[4], ip[4], pf, pb; |
| 28 |
> |
double af, ab, sf2, sb2, dfb2, df2, db2, penalty; |
| 29 |
|
register int i; |
| 30 |
+ |
/* special case */ |
| 31 |
+ |
if (hr <= 0 | vr <= 0) |
| 32 |
+ |
return(0); |
| 33 |
|
/* compute cell corners in image */ |
| 34 |
|
if (!hdbcoord(gc, hp, bi)) |
| 35 |
|
error(CONSISTENCY, "bad beam index in npixels"); |
| 36 |
< |
/* has holodeck or view changed? */ |
| 37 |
< |
if (hp != hplast || bcmp((char *)vp, (char *)&vlast, sizeof(VIEW))) { |
| 38 |
< |
copystruct(&vdo, vp); |
| 39 |
< |
if (sect_behind(hp, &vdo)) { /* reverse view sense */ |
| 40 |
< |
vdo.vdir[0] = -vdo.vdir[0]; |
| 41 |
< |
vdo.vdir[1] = -vdo.vdir[1]; |
| 42 |
< |
vdo.vdir[2] = -vdo.vdir[2]; |
| 43 |
< |
setview(&vdo); |
| 36 |
> |
hdcell(cp, hp, gc+1); /* find cell on front image */ |
| 37 |
> |
for (i = 3; i--; ) /* compute front center */ |
| 38 |
> |
pf[i] = 0.5*(cp[0][i] + cp[2][i]); |
| 39 |
> |
sf2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */ |
| 40 |
> |
for (i = 0; i < 4; i++) { /* compute visible quad */ |
| 41 |
> |
viewloc(ip[i], vp, cp[i]); |
| 42 |
> |
if (ip[i][2] < 0.) { |
| 43 |
> |
af = 0; |
| 44 |
> |
goto getback; |
| 45 |
|
} |
| 46 |
< |
hplast = hp; |
| 47 |
< |
copystruct(&vlast, vp); |
| 46 |
> |
ip[i][0] *= (double)hr; /* scale by resolution */ |
| 47 |
> |
ip[i][1] *= (double)vr; |
| 48 |
|
} |
| 49 |
< |
hdcell(cp, hp, gc+1); /* find cell on image */ |
| 50 |
< |
for (i = 0; i < 4; i++) { |
| 51 |
< |
viewloc(ip[i], &vdo, cp[i]); |
| 52 |
< |
if (ip[i][2] < 0.) |
| 53 |
< |
return(0); |
| 49 |
> |
/* compute front area */ |
| 50 |
> |
af = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) - |
| 51 |
> |
(ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]); |
| 52 |
> |
af += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
| 53 |
> |
(ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]); |
| 54 |
> |
af *= af >= 0 ? 0.5 : -0.5; |
| 55 |
> |
getback: |
| 56 |
> |
vrev = *vp; /* compute reverse view */ |
| 57 |
> |
for (i = 0; i < 3; i++) { |
| 58 |
> |
vrev.vdir[i] = -vp->vdir[i]; |
| 59 |
> |
vrev.vup[i] = -vp->vup[i]; |
| 60 |
> |
vrev.hvec[i] = -vp->hvec[i]; |
| 61 |
> |
vrev.vvec[i] = -vp->vvec[i]; |
| 62 |
> |
} |
| 63 |
> |
hdcell(cp, hp, gc); /* find cell on back image */ |
| 64 |
> |
for (i = 3; i--; ) /* compute rear center */ |
| 65 |
> |
pb[i] = 0.5*(cp[0][i] + cp[2][i]); |
| 66 |
> |
sb2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */ |
| 67 |
> |
for (i = 0; i < 4; i++) { /* compute visible quad */ |
| 68 |
> |
viewloc(ip[i], &vrev, cp[i]); |
| 69 |
> |
if (ip[i][2] < 0.) { |
| 70 |
> |
ab = 0; |
| 71 |
> |
goto finish; |
| 72 |
> |
} |
| 73 |
|
ip[i][0] *= (double)hr; /* scale by resolution */ |
| 74 |
|
ip[i][1] *= (double)vr; |
| 75 |
|
} |
| 76 |
< |
/* compute quad area */ |
| 77 |
< |
d = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) - |
| 76 |
> |
/* compute back area */ |
| 77 |
> |
ab = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) - |
| 78 |
|
(ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]); |
| 79 |
< |
d += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
| 79 |
> |
ab += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
| 80 |
|
(ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]); |
| 81 |
< |
if (d < 0) |
| 82 |
< |
d = -d; |
| 83 |
< |
/* round off result */ |
| 84 |
< |
return((int)(.5*d+.5)); |
| 81 |
> |
ab *= ab >= 0 ? 0.5 : -0.5; |
| 82 |
> |
finish: /* compute penalty based on dist. sightline - viewpoint */ |
| 83 |
> |
df2 = dist2(vp->vp, pf); |
| 84 |
> |
db2 = dist2(vp->vp, pb); |
| 85 |
> |
dfb2 = dist2(pf, pb); |
| 86 |
> |
penalty = dfb2 + df2 - db2; |
| 87 |
> |
penalty = df2 - 0.25*penalty*penalty/dfb2; |
| 88 |
> |
if (df2 > db2) penalty /= df2 <= dfb2 ? sb2 : sb2*df2/dfb2; |
| 89 |
> |
else penalty /= db2 <= dfb2 ? sf2 : sf2*db2/dfb2; |
| 90 |
> |
if (penalty < 1.) penalty = 1.; |
| 91 |
> |
/* round off smaller non-zero area */ |
| 92 |
> |
if (ab <= FTINY || (af > FTINY && af <= ab)) |
| 93 |
> |
return((int)(af/penalty + 0.5)); |
| 94 |
> |
return((int)(ab/penalty + 0.5)); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
|
| 99 |
|
* The ray directions that define the pyramid in visit_cells() needn't |
| 100 |
|
* be normalized, but they must be given in clockwise order as seen |
| 101 |
|
* from the pyramid's apex (origin). |
| 102 |
+ |
* If no cell centers fall within the domain, the closest cell is visited. |
| 103 |
|
*/ |
| 104 |
|
int |
| 105 |
|
visit_cells(orig, pyrd, hp, vf, dp) /* visit cells within a pyramid */ |
| 106 |
|
FVECT orig, pyrd[4]; /* pyramid ray directions in clockwise order */ |
| 107 |
< |
HOLO *hp; |
| 107 |
> |
register HOLO *hp; |
| 108 |
|
int (*vf)(); |
| 109 |
|
char *dp; |
| 110 |
|
{ |
| 111 |
< |
int n = 0; |
| 111 |
> |
int ncalls = 0, n = 0; |
| 112 |
|
int inflags = 0; |
| 113 |
|
FVECT gp, pn[4], lo, ld; |
| 114 |
|
double po[4], lbeg, lend, d, t; |
| 115 |
< |
GCOORD gc; |
| 115 |
> |
GCOORD gc, gc2[2]; |
| 116 |
|
register int i; |
| 117 |
|
/* figure out whose side we're on */ |
| 118 |
|
hdgrid(gp, hp, orig); |
| 130 |
|
if (!(inflags & 1<<gc.w)) /* origin on wrong side */ |
| 131 |
|
continue; |
| 132 |
|
/* scanline algorithm */ |
| 133 |
< |
for (gc.i[1] = hp->grid[((gc.w>>1)+2)%3]; gc.i[1]--; ) { |
| 133 |
> |
for (gc.i[1] = hp->grid[hdwg1[gc.w]]; gc.i[1]--; ) { |
| 134 |
|
/* compute scanline */ |
| 135 |
|
gp[gc.w>>1] = gc.w&1 ? hp->grid[gc.w>>1] : 0; |
| 136 |
< |
gp[((gc.w>>1)+1)%3] = 0; |
| 137 |
< |
gp[((gc.w>>1)+2)%3] = gc.i[1] + 0.5; |
| 136 |
> |
gp[hdwg0[gc.w]] = 0; |
| 137 |
> |
gp[hdwg1[gc.w]] = gc.i[1] + 0.5; |
| 138 |
|
hdworld(lo, hp, gp); |
| 139 |
< |
gp[((gc.w>>1)+1)%3] = 1; |
| 139 |
> |
gp[hdwg0[gc.w]] = 1; |
| 140 |
|
hdworld(ld, hp, gp); |
| 141 |
|
ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2]; |
| 142 |
|
/* find scanline limits */ |
| 143 |
< |
lbeg = 0; lend = hp->grid[((gc.w>>1)+1)%3]; |
| 143 |
> |
lbeg = 0; lend = hp->grid[hdwg0[gc.w]]; |
| 144 |
|
for (i = 0; i < 4; i++) { |
| 145 |
|
t = DOT(pn[i], lo) - po[i]; |
| 146 |
|
d = -DOT(pn[i], ld); |
| 158 |
|
if (lbeg >= lend) |
| 159 |
|
continue; |
| 160 |
|
i = lend + .5; /* visit cells on this scan */ |
| 161 |
< |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) |
| 161 |
> |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) { |
| 162 |
|
n += (*vf)(&gc, dp); |
| 163 |
+ |
ncalls++; |
| 164 |
+ |
} |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
< |
return(n); |
| 167 |
> |
if (ncalls) /* got one at least */ |
| 168 |
> |
return(n); |
| 169 |
> |
/* else find closest cell */ |
| 170 |
> |
VSUM(ld, pyrd[0], pyrd[1], 1.); |
| 171 |
> |
VSUM(ld, ld, pyrd[2], 1.); |
| 172 |
> |
VSUM(ld, ld, pyrd[3], 1.); |
| 173 |
> |
#if 0 |
| 174 |
> |
if (normalize(ld) == 0.0) /* technically not necessary */ |
| 175 |
> |
return(0); |
| 176 |
> |
#endif |
| 177 |
> |
d = hdinter(gc2, NULL, &t, hp, orig, ld); |
| 178 |
> |
if (d >= FHUGE || t <= 0.) |
| 179 |
> |
return(0); |
| 180 |
> |
return((*vf)(gc2+1, dp)); /* visit it */ |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 239 |
|
GCOORD *gcp; |
| 240 |
|
register struct cellist *cl; |
| 241 |
|
{ |
| 242 |
< |
copystruct(cl->cl+cl->n, gcp); |
| 242 |
> |
*(cl->cl+cl->n) = *gcp; |
| 243 |
|
cl->n++; |
| 244 |
|
return(1); |
| 245 |
|
} |
| 281 |
|
if (cl.cl == NULL) |
| 282 |
|
goto memerr; |
| 283 |
|
cl.n = 0; /* add cells within pyramid */ |
| 284 |
< |
visit_cells(org, dir, hp, addcell, &cl); |
| 284 |
> |
visit_cells(org, dir, hp, addcell, (char *)&cl); |
| 285 |
|
if (!cl.n) { |
| 286 |
< |
free((char *)cl.cl); |
| 286 |
> |
free((void *)cl.cl); |
| 287 |
|
return(NULL); |
| 288 |
|
} |
| 289 |
|
*np = cl.n * orient; |
| 292 |
|
* sorted automatically by visit_cells(), so we don't need this. |
| 293 |
|
*/ |
| 294 |
|
/* optimize memory use */ |
| 295 |
< |
cl.cl = (GCOORD *)realloc((char *)cl.cl, cl.n*sizeof(GCOORD)); |
| 295 |
> |
cl.cl = (GCOORD *)realloc((void *)cl.cl, cl.n*sizeof(GCOORD)); |
| 296 |
|
if (cl.cl == NULL) |
| 297 |
|
goto memerr; |
| 298 |
|
/* sort the list */ |
| 301 |
|
return(cl.cl); |
| 302 |
|
memerr: |
| 303 |
|
error(SYSTEM, "out of memory in getviewcells"); |
| 304 |
+ |
} |
| 305 |
+ |
|
| 306 |
+ |
|
| 307 |
+ |
extern void |
| 308 |
+ |
gridlines( /* run through holodeck section grid lines */ |
| 309 |
+ |
void (*f)(FVECT wp[2]) |
| 310 |
+ |
) |
| 311 |
+ |
{ |
| 312 |
+ |
register int hd, w, i; |
| 313 |
+ |
int g0, g1; |
| 314 |
+ |
FVECT wp[2], mov; |
| 315 |
+ |
double d; |
| 316 |
+ |
/* do each wall on each section */ |
| 317 |
+ |
for (hd = 0; hdlist[hd] != NULL; hd++) |
| 318 |
+ |
for (w = 0; w < 6; w++) { |
| 319 |
+ |
g0 = hdwg0[w]; |
| 320 |
+ |
g1 = hdwg1[w]; |
| 321 |
+ |
d = 1.0/hdlist[hd]->grid[g0]; |
| 322 |
+ |
mov[0] = d * hdlist[hd]->xv[g0][0]; |
| 323 |
+ |
mov[1] = d * hdlist[hd]->xv[g0][1]; |
| 324 |
+ |
mov[2] = d * hdlist[hd]->xv[g0][2]; |
| 325 |
+ |
if (w & 1) { |
| 326 |
+ |
VSUM(wp[0], hdlist[hd]->orig, |
| 327 |
+ |
hdlist[hd]->xv[w>>1], 1.); |
| 328 |
+ |
VSUM(wp[0], wp[0], mov, 1.); |
| 329 |
+ |
} else |
| 330 |
+ |
VCOPY(wp[0], hdlist[hd]->orig); |
| 331 |
+ |
VSUM(wp[1], wp[0], hdlist[hd]->xv[g1], 1.); |
| 332 |
+ |
for (i = hdlist[hd]->grid[g0]; ; ) { /* g0 lines */ |
| 333 |
+ |
(*f)(wp); |
| 334 |
+ |
if (!--i) break; |
| 335 |
+ |
wp[0][0] += mov[0]; wp[0][1] += mov[1]; |
| 336 |
+ |
wp[0][2] += mov[2]; wp[1][0] += mov[0]; |
| 337 |
+ |
wp[1][1] += mov[1]; wp[1][2] += mov[2]; |
| 338 |
+ |
} |
| 339 |
+ |
d = 1.0/hdlist[hd]->grid[g1]; |
| 340 |
+ |
mov[0] = d * hdlist[hd]->xv[g1][0]; |
| 341 |
+ |
mov[1] = d * hdlist[hd]->xv[g1][1]; |
| 342 |
+ |
mov[2] = d * hdlist[hd]->xv[g1][2]; |
| 343 |
+ |
if (w & 1) |
| 344 |
+ |
VSUM(wp[0], hdlist[hd]->orig, |
| 345 |
+ |
hdlist[hd]->xv[w>>1], 1.); |
| 346 |
+ |
else |
| 347 |
+ |
VSUM(wp[0], hdlist[hd]->orig, mov, 1.); |
| 348 |
+ |
VSUM(wp[1], wp[0], hdlist[hd]->xv[g0], 1.); |
| 349 |
+ |
for (i = hdlist[hd]->grid[g1]; ; ) { /* g1 lines */ |
| 350 |
+ |
(*f)(wp); |
| 351 |
+ |
if (!--i) break; |
| 352 |
+ |
wp[0][0] += mov[0]; wp[0][1] += mov[1]; |
| 353 |
+ |
wp[0][2] += mov[2]; wp[1][0] += mov[0]; |
| 354 |
+ |
wp[1][1] += mov[1]; wp[1][2] += mov[2]; |
| 355 |
+ |
} |
| 356 |
+ |
} |
| 357 |
|
} |