1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
19 |
|
|
20 |
|
|
21 |
|
int |
22 |
< |
npixels(vp, hr, vr, hp, bi) /* compute appropriate number to evaluate */ |
22 |
> |
npixels(vp, hr, vr, hp, bi) /* compute appropriate nrays to evaluate */ |
23 |
|
register VIEW *vp; |
24 |
|
int hr, vr; |
25 |
|
HOLO *hp; |
27 |
|
{ |
28 |
|
VIEW vrev; |
29 |
|
GCOORD gc[2]; |
30 |
< |
FVECT cp[4], ip[4]; |
31 |
< |
double af, ab; |
30 |
> |
FVECT cp[4], ip[4], pf, pb; |
31 |
> |
double af, ab, sf2, sb2, dfb2, df2, db2, penalty; |
32 |
|
register int i; |
33 |
+ |
/* special case */ |
34 |
+ |
if (hr <= 0 | vr <= 0) |
35 |
+ |
return(0); |
36 |
|
/* compute cell corners in image */ |
37 |
|
if (!hdbcoord(gc, hp, bi)) |
38 |
|
error(CONSISTENCY, "bad beam index in npixels"); |
39 |
|
hdcell(cp, hp, gc+1); /* find cell on front image */ |
40 |
< |
for (i = 0; i < 4; i++) { |
40 |
> |
for (i = 3; i--; ) /* compute front center */ |
41 |
> |
pf[i] = 0.5*(cp[0][i] + cp[2][i]); |
42 |
> |
sf2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */ |
43 |
> |
for (i = 0; i < 4; i++) { /* compute visible quad */ |
44 |
|
viewloc(ip[i], vp, cp[i]); |
45 |
|
if (ip[i][2] < 0.) { |
46 |
|
af = 0; |
54 |
|
(ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]); |
55 |
|
af += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
56 |
|
(ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]); |
57 |
< |
if (af >= 0) af *= 0.5; |
52 |
< |
else af *= -0.5; |
57 |
> |
af *= af >= 0 ? 0.5 : -0.5; |
58 |
|
getback: |
59 |
|
copystruct(&vrev, vp); /* compute reverse view */ |
60 |
|
for (i = 0; i < 3; i++) { |
64 |
|
vrev.vvec[i] = -vp->vvec[i]; |
65 |
|
} |
66 |
|
hdcell(cp, hp, gc); /* find cell on back image */ |
67 |
< |
for (i = 0; i < 4; i++) { |
67 |
> |
for (i = 3; i--; ) /* compute rear center */ |
68 |
> |
pb[i] = 0.5*(cp[0][i] + cp[2][i]); |
69 |
> |
sb2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */ |
70 |
> |
for (i = 0; i < 4; i++) { /* compute visible quad */ |
71 |
|
viewloc(ip[i], &vrev, cp[i]); |
72 |
< |
if (ip[i][2] < 0.) |
73 |
< |
return((int)(af + 0.5)); |
72 |
> |
if (ip[i][2] < 0.) { |
73 |
> |
ab = 0; |
74 |
> |
goto finish; |
75 |
> |
} |
76 |
|
ip[i][0] *= (double)hr; /* scale by resolution */ |
77 |
|
ip[i][1] *= (double)vr; |
78 |
|
} |
81 |
|
(ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]); |
82 |
|
ab += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
83 |
|
(ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]); |
84 |
< |
if (ab >= 0) ab *= 0.5; |
85 |
< |
else ab *= -0.5; |
86 |
< |
/* round off smaller area */ |
87 |
< |
if (af <= ab) |
88 |
< |
return((int)(af + 0.5)); |
89 |
< |
return((int)(ab + 0.5)); |
84 |
> |
ab *= ab >= 0 ? 0.5 : -0.5; |
85 |
> |
finish: /* compute penalty based on dist. sightline - viewpoint */ |
86 |
> |
df2 = dist2(vp->vp, pf); |
87 |
> |
db2 = dist2(vp->vp, pb); |
88 |
> |
dfb2 = dist2(pf, pb); |
89 |
> |
penalty = dfb2 + df2 - db2; |
90 |
> |
penalty = df2 - 0.25*penalty*penalty/dfb2; |
91 |
> |
if (df2 > db2) penalty /= df2 <= dfb2 ? sb2 : sb2*df2/dfb2; |
92 |
> |
else penalty /= db2 <= dfb2 ? sf2 : sf2*db2/dfb2; |
93 |
> |
if (penalty < 1.) penalty = 1.; |
94 |
> |
/* round off smaller non-zero area */ |
95 |
> |
if (ab <= FTINY || (af > FTINY && af <= ab)) |
96 |
> |
return((int)(af/penalty + 0.5)); |
97 |
> |
return((int)(ab/penalty + 0.5)); |
98 |
|
} |
99 |
|
|
100 |
|
|
102 |
|
* The ray directions that define the pyramid in visit_cells() needn't |
103 |
|
* be normalized, but they must be given in clockwise order as seen |
104 |
|
* from the pyramid's apex (origin). |
105 |
+ |
* If no cell centers fall within the domain, the closest cell is visited. |
106 |
|
*/ |
107 |
|
int |
108 |
|
visit_cells(orig, pyrd, hp, vf, dp) /* visit cells within a pyramid */ |
109 |
|
FVECT orig, pyrd[4]; /* pyramid ray directions in clockwise order */ |
110 |
< |
HOLO *hp; |
110 |
> |
register HOLO *hp; |
111 |
|
int (*vf)(); |
112 |
|
char *dp; |
113 |
|
{ |
114 |
< |
int n = 0; |
114 |
> |
int ncalls = 0, n = 0; |
115 |
|
int inflags = 0; |
116 |
|
FVECT gp, pn[4], lo, ld; |
117 |
|
double po[4], lbeg, lend, d, t; |
118 |
< |
GCOORD gc; |
118 |
> |
GCOORD gc, gc2[2]; |
119 |
|
register int i; |
120 |
|
/* figure out whose side we're on */ |
121 |
|
hdgrid(gp, hp, orig); |
133 |
|
if (!(inflags & 1<<gc.w)) /* origin on wrong side */ |
134 |
|
continue; |
135 |
|
/* scanline algorithm */ |
136 |
< |
for (gc.i[1] = hp->grid[((gc.w>>1)+2)%3]; gc.i[1]--; ) { |
136 |
> |
for (gc.i[1] = hp->grid[hdwg1[gc.w]]; gc.i[1]--; ) { |
137 |
|
/* compute scanline */ |
138 |
|
gp[gc.w>>1] = gc.w&1 ? hp->grid[gc.w>>1] : 0; |
139 |
< |
gp[((gc.w>>1)+1)%3] = 0; |
140 |
< |
gp[((gc.w>>1)+2)%3] = gc.i[1] + 0.5; |
139 |
> |
gp[hdwg0[gc.w]] = 0; |
140 |
> |
gp[hdwg1[gc.w]] = gc.i[1] + 0.5; |
141 |
|
hdworld(lo, hp, gp); |
142 |
< |
gp[((gc.w>>1)+1)%3] = 1; |
142 |
> |
gp[hdwg0[gc.w]] = 1; |
143 |
|
hdworld(ld, hp, gp); |
144 |
|
ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2]; |
145 |
|
/* find scanline limits */ |
146 |
< |
lbeg = 0; lend = hp->grid[((gc.w>>1)+1)%3]; |
146 |
> |
lbeg = 0; lend = hp->grid[hdwg0[gc.w]]; |
147 |
|
for (i = 0; i < 4; i++) { |
148 |
|
t = DOT(pn[i], lo) - po[i]; |
149 |
|
d = -DOT(pn[i], ld); |
161 |
|
if (lbeg >= lend) |
162 |
|
continue; |
163 |
|
i = lend + .5; /* visit cells on this scan */ |
164 |
< |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) |
164 |
> |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) { |
165 |
|
n += (*vf)(&gc, dp); |
166 |
+ |
ncalls++; |
167 |
+ |
} |
168 |
|
} |
169 |
|
} |
170 |
< |
return(n); |
170 |
> |
if (ncalls) /* got one at least */ |
171 |
> |
return(n); |
172 |
> |
/* else find closest cell */ |
173 |
> |
VSUM(ld, pyrd[0], pyrd[1], 1.); |
174 |
> |
VSUM(ld, ld, pyrd[2], 1.); |
175 |
> |
VSUM(ld, ld, pyrd[3], 1.); |
176 |
> |
#if 0 |
177 |
> |
if (normalize(ld) == 0.0) /* technically not necessary */ |
178 |
> |
return(0); |
179 |
> |
#endif |
180 |
> |
d = hdinter(gc2, NULL, &t, hp, orig, ld); |
181 |
> |
if (d >= FHUGE || t <= 0.) |
182 |
> |
return(0); |
183 |
> |
return((*vf)(gc2+1, dp)); /* visit it */ |
184 |
|
} |
185 |
|
|
186 |
|
|
284 |
|
if (cl.cl == NULL) |
285 |
|
goto memerr; |
286 |
|
cl.n = 0; /* add cells within pyramid */ |
287 |
< |
visit_cells(org, dir, hp, addcell, &cl); |
287 |
> |
visit_cells(org, dir, hp, addcell, (char *)&cl); |
288 |
|
if (!cl.n) { |
289 |
|
free((char *)cl.cl); |
290 |
|
return(NULL); |
304 |
|
return(cl.cl); |
305 |
|
memerr: |
306 |
|
error(SYSTEM, "out of memory in getviewcells"); |
307 |
+ |
} |
308 |
+ |
|
309 |
+ |
|
310 |
+ |
gridlines(f) /* run through holodeck section grid lines */ |
311 |
+ |
int (*f)(); |
312 |
+ |
{ |
313 |
+ |
register int hd, w, i; |
314 |
+ |
int g0, g1; |
315 |
+ |
FVECT wp[2], mov; |
316 |
+ |
double d; |
317 |
+ |
/* do each wall on each section */ |
318 |
+ |
for (hd = 0; hdlist[hd] != NULL; hd++) |
319 |
+ |
for (w = 0; w < 6; w++) { |
320 |
+ |
g0 = hdwg0[w]; |
321 |
+ |
g1 = hdwg1[w]; |
322 |
+ |
d = 1.0/hdlist[hd]->grid[g0]; |
323 |
+ |
mov[0] = d * hdlist[hd]->xv[g0][0]; |
324 |
+ |
mov[1] = d * hdlist[hd]->xv[g0][1]; |
325 |
+ |
mov[2] = d * hdlist[hd]->xv[g0][2]; |
326 |
+ |
if (w & 1) { |
327 |
+ |
VSUM(wp[0], hdlist[hd]->orig, |
328 |
+ |
hdlist[hd]->xv[w>>1], 1.); |
329 |
+ |
VSUM(wp[0], wp[0], mov, 1.); |
330 |
+ |
} else |
331 |
+ |
VCOPY(wp[0], hdlist[hd]->orig); |
332 |
+ |
VSUM(wp[1], wp[0], hdlist[hd]->xv[g1], 1.); |
333 |
+ |
for (i = hdlist[hd]->grid[g0]; ; ) { /* g0 lines */ |
334 |
+ |
(*f)(wp); |
335 |
+ |
if (!--i) break; |
336 |
+ |
wp[0][0] += mov[0]; wp[0][1] += mov[1]; |
337 |
+ |
wp[0][2] += mov[2]; wp[1][0] += mov[0]; |
338 |
+ |
wp[1][1] += mov[1]; wp[1][2] += mov[2]; |
339 |
+ |
} |
340 |
+ |
d = 1.0/hdlist[hd]->grid[g1]; |
341 |
+ |
mov[0] = d * hdlist[hd]->xv[g1][0]; |
342 |
+ |
mov[1] = d * hdlist[hd]->xv[g1][1]; |
343 |
+ |
mov[2] = d * hdlist[hd]->xv[g1][2]; |
344 |
+ |
if (w & 1) |
345 |
+ |
VSUM(wp[0], hdlist[hd]->orig, |
346 |
+ |
hdlist[hd]->xv[w>>1], 1.); |
347 |
+ |
else |
348 |
+ |
VSUM(wp[0], hdlist[hd]->orig, mov, 1.); |
349 |
+ |
VSUM(wp[1], wp[0], hdlist[hd]->xv[g0], 1.); |
350 |
+ |
for (i = hdlist[hd]->grid[g1]; ; ) { /* g1 lines */ |
351 |
+ |
(*f)(wp); |
352 |
+ |
if (!--i) break; |
353 |
+ |
wp[0][0] += mov[0]; wp[0][1] += mov[1]; |
354 |
+ |
wp[0][2] += mov[2]; wp[1][0] += mov[0]; |
355 |
+ |
wp[1][1] += mov[1]; wp[1][2] += mov[2]; |
356 |
+ |
} |
357 |
+ |
} |
358 |
|
} |