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 |
5 |
> |
* Holodeck beam support for display process |
6 |
|
*/ |
7 |
|
|
8 |
|
#include "rholo.h" |
9 |
|
#include "rhdisp.h" |
13 |
– |
#include "view.h" |
10 |
|
|
11 |
+ |
struct cellist { |
12 |
+ |
GCOORD *cl; |
13 |
+ |
int n; |
14 |
+ |
}; |
15 |
|
|
16 |
+ |
|
17 |
|
int |
18 |
< |
npixels(vp, hr, vr, hp, bi) /* compute appropriate number to evaluate */ |
18 |
> |
npixels(vp, hr, vr, hp, bi) /* compute appropriate nrays to evaluate */ |
19 |
|
VIEW *vp; |
20 |
|
int hr, vr; |
21 |
|
HOLO *hp; |
22 |
|
int bi; |
23 |
|
{ |
24 |
+ |
VIEW vrev; |
25 |
|
GCOORD gc[2]; |
26 |
< |
FVECT cp[4]; |
27 |
< |
FVECT ip[4]; |
28 |
< |
double d; |
29 |
< |
register int i; |
26 |
> |
FVECT cp[4], ip[4], pf, pb; |
27 |
> |
double af, ab, sf2, sb2, dfb2, df2, db2, penalty; |
28 |
> |
int i; |
29 |
> |
/* special case */ |
30 |
> |
if (hr <= 0 | vr <= 0) |
31 |
> |
return(0); |
32 |
|
/* compute cell corners in image */ |
33 |
|
if (!hdbcoord(gc, hp, bi)) |
34 |
|
error(CONSISTENCY, "bad beam index in npixels"); |
35 |
< |
hdcell(cp, hp, gc+1); |
36 |
< |
for (i = 0; i < 4; i++) { |
37 |
< |
viewloc(ip[i], vp, cp[i]); |
38 |
< |
if (ip[i][2] < 0.) |
39 |
< |
return(0); |
35 |
> |
hdcell(cp, hp, gc+1); /* find cell on front image */ |
36 |
> |
for (i = 3; i--; ) /* compute front center */ |
37 |
> |
pf[i] = 0.5*(cp[0][i] + cp[2][i]); |
38 |
> |
sf2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */ |
39 |
> |
for (i = 0; i < 4; i++) { /* compute visible quad */ |
40 |
> |
if (viewloc(ip[i], vp, cp[i]) <= 0) { |
41 |
> |
af = 0; |
42 |
> |
goto getback; |
43 |
> |
} |
44 |
|
ip[i][0] *= (double)hr; /* scale by resolution */ |
45 |
|
ip[i][1] *= (double)vr; |
46 |
|
} |
47 |
< |
/* compute quad area */ |
48 |
< |
d = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) - |
47 |
> |
/* compute front area */ |
48 |
> |
af = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) - |
49 |
|
(ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]); |
50 |
< |
d += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
50 |
> |
af += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
51 |
|
(ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]); |
52 |
< |
if (d < 0) |
53 |
< |
d = -d; |
54 |
< |
/* round off result */ |
55 |
< |
return((int)(.5*d+.5)); |
52 |
> |
af *= af >= 0 ? 0.5 : -0.5; |
53 |
> |
getback: |
54 |
> |
vrev = *vp; /* compute reverse view */ |
55 |
> |
for (i = 0; i < 3; i++) { |
56 |
> |
vrev.vdir[i] = -vp->vdir[i]; |
57 |
> |
vrev.vup[i] = -vp->vup[i]; |
58 |
> |
vrev.hvec[i] = -vp->hvec[i]; |
59 |
> |
vrev.vvec[i] = -vp->vvec[i]; |
60 |
> |
} |
61 |
> |
hdcell(cp, hp, gc); /* find cell on back image */ |
62 |
> |
for (i = 3; i--; ) /* compute rear center */ |
63 |
> |
pb[i] = 0.5*(cp[0][i] + cp[2][i]); |
64 |
> |
sb2 = 0.25*dist2(cp[0], cp[2]); /* compute half diagonal length */ |
65 |
> |
for (i = 0; i < 4; i++) { /* compute visible quad */ |
66 |
> |
if (viewloc(ip[i], &vrev, cp[i]) <= 0) { |
67 |
> |
ab = 0; |
68 |
> |
goto finish; |
69 |
> |
} |
70 |
> |
ip[i][0] *= (double)hr; /* scale by resolution */ |
71 |
> |
ip[i][1] *= (double)vr; |
72 |
> |
} |
73 |
> |
/* compute back area */ |
74 |
> |
ab = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) - |
75 |
> |
(ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]); |
76 |
> |
ab += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
77 |
> |
(ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]); |
78 |
> |
ab *= ab >= 0 ? 0.5 : -0.5; |
79 |
> |
finish: /* compute penalty based on dist. sightline - viewpoint */ |
80 |
> |
df2 = dist2(vp->vp, pf); |
81 |
> |
db2 = dist2(vp->vp, pb); |
82 |
> |
dfb2 = dist2(pf, pb); |
83 |
> |
penalty = dfb2 + df2 - db2; |
84 |
> |
penalty = df2 - 0.25*penalty*penalty/dfb2; |
85 |
> |
if (df2 > db2) penalty /= df2 <= dfb2 ? sb2 : sb2*df2/dfb2; |
86 |
> |
else penalty /= db2 <= dfb2 ? sf2 : sf2*db2/dfb2; |
87 |
> |
if (penalty < 1.) penalty = 1.; |
88 |
> |
/* round off smaller non-zero area */ |
89 |
> |
if (ab <= FTINY || (af > FTINY && af <= ab)) |
90 |
> |
return((int)(af/penalty + 0.5)); |
91 |
> |
return((int)(ab/penalty + 0.5)); |
92 |
|
} |
93 |
|
|
94 |
|
|
96 |
|
* The ray directions that define the pyramid in visit_cells() needn't |
97 |
|
* be normalized, but they must be given in clockwise order as seen |
98 |
|
* from the pyramid's apex (origin). |
99 |
+ |
* If no cell centers fall within the domain, the closest cell is visited. |
100 |
|
*/ |
101 |
|
int |
102 |
|
visit_cells(orig, pyrd, hp, vf, dp) /* visit cells within a pyramid */ |
105 |
|
int (*vf)(); |
106 |
|
char *dp; |
107 |
|
{ |
108 |
< |
int n = 0; |
108 |
> |
int ncalls = 0, n = 0; |
109 |
|
int inflags = 0; |
110 |
|
FVECT gp, pn[4], lo, ld; |
111 |
|
double po[4], lbeg, lend, d, t; |
112 |
< |
GCOORD gc; |
113 |
< |
register int i; |
112 |
> |
GCOORD gc, gc2[2]; |
113 |
> |
int i; |
114 |
|
/* figure out whose side we're on */ |
115 |
|
hdgrid(gp, hp, orig); |
116 |
|
for (i = 0; i < 3; i++) { |
127 |
|
if (!(inflags & 1<<gc.w)) /* origin on wrong side */ |
128 |
|
continue; |
129 |
|
/* scanline algorithm */ |
130 |
< |
for (gc.i[1] = hp->grid[((gc.w>>1)+2)%3]; gc.i[1]--; ) { |
130 |
> |
for (gc.i[1] = hp->grid[hdwg1[gc.w]]; gc.i[1]--; ) { |
131 |
|
/* compute scanline */ |
132 |
|
gp[gc.w>>1] = gc.w&1 ? hp->grid[gc.w>>1] : 0; |
133 |
< |
gp[((gc.w>>1)+1)%3] = 0; |
134 |
< |
gp[((gc.w>>1)+2)%3] = gc.i[1] + 0.5; |
133 |
> |
gp[hdwg0[gc.w]] = 0; |
134 |
> |
gp[hdwg1[gc.w]] = gc.i[1] + 0.5; |
135 |
|
hdworld(lo, hp, gp); |
136 |
< |
gp[((gc.w>>1)+1)%3] = 1; |
136 |
> |
gp[hdwg0[gc.w]] = 1; |
137 |
|
hdworld(ld, hp, gp); |
138 |
< |
ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[1]; |
138 |
> |
ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2]; |
139 |
|
/* find scanline limits */ |
140 |
< |
lbeg = 0; lend = hp->grid[((gc.w>>1)+1)%3]; |
140 |
> |
lbeg = 0; lend = hp->grid[hdwg0[gc.w]]; |
141 |
|
for (i = 0; i < 4; i++) { |
142 |
|
t = DOT(pn[i], lo) - po[i]; |
143 |
|
d = -DOT(pn[i], ld); |
144 |
< |
if (d <= FTINY && d >= -FTINY) { |
100 |
< |
if (t < 0) |
101 |
< |
goto nextscan; |
102 |
< |
continue; |
103 |
< |
} |
104 |
< |
if (t > 0) { |
144 |
> |
if (d > FTINY) { /* <- plane */ |
145 |
|
if ((t /= d) < lend) |
146 |
|
lend = t; |
147 |
< |
} else { |
147 |
> |
} else if (d < -FTINY) { /* plane -> */ |
148 |
|
if ((t /= d) > lbeg) |
149 |
|
lbeg = t; |
150 |
+ |
} else if (t < 0) { /* outside */ |
151 |
+ |
lend = -1; |
152 |
+ |
break; |
153 |
|
} |
154 |
|
} |
155 |
+ |
if (lbeg >= lend) |
156 |
+ |
continue; |
157 |
|
i = lend + .5; /* visit cells on this scan */ |
158 |
< |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) |
158 |
> |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) { |
159 |
|
n += (*vf)(&gc, dp); |
160 |
< |
nextscan:; |
160 |
> |
ncalls++; |
161 |
> |
} |
162 |
|
} |
163 |
|
} |
164 |
< |
return(n); |
164 |
> |
if (ncalls) /* got one at least */ |
165 |
> |
return(n); |
166 |
> |
/* else find closest cell */ |
167 |
> |
VSUM(ld, pyrd[0], pyrd[1], 1.); |
168 |
> |
VSUM(ld, ld, pyrd[2], 1.); |
169 |
> |
VSUM(ld, ld, pyrd[3], 1.); |
170 |
> |
#if 0 |
171 |
> |
if (normalize(ld) == 0.0) /* technically not necessary */ |
172 |
> |
return(0); |
173 |
> |
#endif |
174 |
> |
d = hdinter(gc2, NULL, &t, hp, orig, ld); |
175 |
> |
if (d >= FHUGE || t <= 0.) |
176 |
> |
return(0); |
177 |
> |
return((*vf)(gc2+1, dp)); /* visit it */ |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
+ |
sect_behind(hp, vp) /* check if section is "behind" viewpoint */ |
182 |
+ |
HOLO *hp; |
183 |
+ |
VIEW *vp; |
184 |
+ |
{ |
185 |
+ |
FVECT hcent; |
186 |
+ |
/* compute holodeck section center */ |
187 |
+ |
VSUM(hcent, hp->orig, hp->xv[0], 0.5); |
188 |
+ |
VSUM(hcent, hcent, hp->xv[1], 0.5); |
189 |
+ |
VSUM(hcent, hcent, hp->xv[2], 0.5); |
190 |
+ |
/* behind if center is behind */ |
191 |
+ |
return(DOT(vp->vdir,hcent) < DOT(vp->vdir,vp->vp)); |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
|
195 |
+ |
viewpyramid(org, dir, hp, vp) /* compute view pyramid */ |
196 |
+ |
FVECT org, dir[4]; |
197 |
+ |
HOLO *hp; |
198 |
+ |
VIEW *vp; |
199 |
+ |
{ |
200 |
+ |
int i; |
201 |
+ |
/* check view type */ |
202 |
+ |
if (vp->type == VT_PAR) |
203 |
+ |
return(0); |
204 |
+ |
/* in front or behind? */ |
205 |
+ |
if (!sect_behind(hp, vp)) { |
206 |
+ |
if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) |
207 |
+ |
return(0); |
208 |
+ |
if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) |
209 |
+ |
return(0); |
210 |
+ |
if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) |
211 |
+ |
return(0); |
212 |
+ |
if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) |
213 |
+ |
return(0); |
214 |
+ |
return(1); |
215 |
+ |
} /* reverse pyramid */ |
216 |
+ |
if (viewray(org, dir[3], vp, 0., 0.) < -FTINY) |
217 |
+ |
return(0); |
218 |
+ |
if (viewray(org, dir[2], vp, 0., 1.) < -FTINY) |
219 |
+ |
return(0); |
220 |
+ |
if (viewray(org, dir[1], vp, 1., 1.) < -FTINY) |
221 |
+ |
return(0); |
222 |
+ |
if (viewray(org, dir[0], vp, 1., 0.) < -FTINY) |
223 |
+ |
return(0); |
224 |
+ |
for (i = 0; i < 3; i++) { |
225 |
+ |
dir[0][i] = -dir[0][i]; |
226 |
+ |
dir[1][i] = -dir[1][i]; |
227 |
+ |
dir[2][i] = -dir[2][i]; |
228 |
+ |
dir[3][i] = -dir[3][i]; |
229 |
+ |
} |
230 |
+ |
return(-1); |
231 |
+ |
} |
232 |
+ |
|
233 |
+ |
|
234 |
|
int |
235 |
|
addcell(gcp, cl) /* add a cell to a list */ |
236 |
|
GCOORD *gcp; |
237 |
< |
register int *cl; |
237 |
> |
struct cellist *cl; |
238 |
|
{ |
239 |
< |
copystruct((GCOORD *)(cl+1) + *cl, gcp); |
240 |
< |
(*cl)++; |
239 |
> |
*(cl->cl+cl->n) = *gcp; |
240 |
> |
cl->n++; |
241 |
|
return(1); |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
int |
246 |
|
cellcmp(gcp1, gcp2) /* visit_cells() cell ordering */ |
247 |
< |
register GCOORD *gcp1, *gcp2; |
247 |
> |
GCOORD *gcp1, *gcp2; |
248 |
|
{ |
249 |
< |
register int c; |
249 |
> |
int c; |
250 |
|
|
251 |
|
if ((c = gcp1->w - gcp2->w)) |
252 |
|
return(c); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
< |
int * |
260 |
< |
getviewcells(hp, vp) /* get ordered cell list for section view */ |
261 |
< |
register HOLO *hp; |
259 |
> |
GCOORD * |
260 |
> |
getviewcells(np, hp, vp) /* get ordered cell list for section view */ |
261 |
> |
int *np; /* returned number of cells (negative if reversed) */ |
262 |
> |
HOLO *hp; |
263 |
|
VIEW *vp; |
264 |
|
{ |
265 |
|
FVECT org, dir[4]; |
266 |
< |
int n; |
267 |
< |
register int *cl; |
266 |
> |
int orient; |
267 |
> |
struct cellist cl; |
268 |
|
/* compute view pyramid */ |
269 |
< |
if (vp->type == VT_PAR) goto viewerr; |
270 |
< |
if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr; |
271 |
< |
if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr; |
272 |
< |
if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr; |
160 |
< |
if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr; |
269 |
> |
*np = 0; |
270 |
> |
orient = viewpyramid(org, dir, hp, vp); |
271 |
> |
if (!orient) |
272 |
> |
return(NULL); |
273 |
|
/* allocate enough list space */ |
274 |
< |
n = 2*( hp->grid[0]*hp->grid[1] + |
275 |
< |
hp->grid[0]*hp->grid[2] + |
276 |
< |
hp->grid[1]*hp->grid[2] ); |
277 |
< |
cl = (int *)malloc(sizeof(int) + n*sizeof(GCOORD)); |
278 |
< |
if (cl == NULL) |
274 |
> |
cl.n = 2*( hp->grid[0]*hp->grid[1] + |
275 |
> |
hp->grid[0]*hp->grid[2] + |
276 |
> |
hp->grid[1]*hp->grid[2] ); |
277 |
> |
cl.cl = (GCOORD *)malloc(cl.n*sizeof(GCOORD)); |
278 |
> |
if (cl.cl == NULL) |
279 |
|
goto memerr; |
280 |
< |
*cl = 0; |
281 |
< |
/* add cells within pyramid */ |
282 |
< |
visit_cells(org, dir, hp, addcell, cl); |
283 |
< |
if (!*cl) { |
172 |
< |
free((char *)cl); |
280 |
> |
cl.n = 0; /* add cells within pyramid */ |
281 |
> |
visit_cells(org, dir, hp, addcell, (char *)&cl); |
282 |
> |
if (!cl.n) { |
283 |
> |
free((void *)cl.cl); |
284 |
|
return(NULL); |
285 |
|
} |
286 |
+ |
*np = cl.n * orient; |
287 |
|
#if 0 |
288 |
< |
/* We're just going to free this memory in a moment, and list is */ |
289 |
< |
/* sorted automatically by visit_cells(), so we don't need this. */ |
290 |
< |
if (*cl < n) { /* optimize memory use */ |
291 |
< |
cl = (int *)realloc((char *)cl, |
292 |
< |
sizeof(int) + *cl*sizeof(GCOORD)); |
293 |
< |
if (cl == NULL) |
294 |
< |
goto memerr; |
183 |
< |
} |
288 |
> |
/* We're just going to free this memory in a moment, and list is |
289 |
> |
* sorted automatically by visit_cells(), so we don't need this. |
290 |
> |
*/ |
291 |
> |
/* optimize memory use */ |
292 |
> |
cl.cl = (GCOORD *)realloc((void *)cl.cl, cl.n*sizeof(GCOORD)); |
293 |
> |
if (cl.cl == NULL) |
294 |
> |
goto memerr; |
295 |
|
/* sort the list */ |
296 |
< |
qsort((char *)(cl+1), *cl, sizeof(GCOORD), cellcmp); |
296 |
> |
qsort((char *)cl.cl, cl.n, sizeof(GCOORD), cellcmp); |
297 |
|
#endif |
298 |
< |
return(cl); |
188 |
< |
viewerr: |
189 |
< |
error(INTERNAL, "unusable view in getviewcells"); |
298 |
> |
return(cl.cl); |
299 |
|
memerr: |
300 |
|
error(SYSTEM, "out of memory in getviewcells"); |
301 |
+ |
} |
302 |
+ |
|
303 |
+ |
|
304 |
+ |
void |
305 |
+ |
gridlines( /* run through holodeck section grid lines */ |
306 |
+ |
void (*f)(FVECT wp[2]) |
307 |
+ |
) |
308 |
+ |
{ |
309 |
+ |
int hd, w, i; |
310 |
+ |
int g0, g1; |
311 |
+ |
FVECT wp[2], mov; |
312 |
+ |
double d; |
313 |
+ |
/* do each wall on each section */ |
314 |
+ |
for (hd = 0; hdlist[hd] != NULL; hd++) |
315 |
+ |
for (w = 0; w < 6; w++) { |
316 |
+ |
g0 = hdwg0[w]; |
317 |
+ |
g1 = hdwg1[w]; |
318 |
+ |
d = 1.0/hdlist[hd]->grid[g0]; |
319 |
+ |
mov[0] = d * hdlist[hd]->xv[g0][0]; |
320 |
+ |
mov[1] = d * hdlist[hd]->xv[g0][1]; |
321 |
+ |
mov[2] = d * hdlist[hd]->xv[g0][2]; |
322 |
+ |
if (w & 1) { |
323 |
+ |
VSUM(wp[0], hdlist[hd]->orig, |
324 |
+ |
hdlist[hd]->xv[w>>1], 1.); |
325 |
+ |
VSUM(wp[0], wp[0], mov, 1.); |
326 |
+ |
} else |
327 |
+ |
VCOPY(wp[0], hdlist[hd]->orig); |
328 |
+ |
VSUM(wp[1], wp[0], hdlist[hd]->xv[g1], 1.); |
329 |
+ |
for (i = hdlist[hd]->grid[g0]; ; ) { /* g0 lines */ |
330 |
+ |
(*f)(wp); |
331 |
+ |
if (!--i) break; |
332 |
+ |
wp[0][0] += mov[0]; wp[0][1] += mov[1]; |
333 |
+ |
wp[0][2] += mov[2]; wp[1][0] += mov[0]; |
334 |
+ |
wp[1][1] += mov[1]; wp[1][2] += mov[2]; |
335 |
+ |
} |
336 |
+ |
d = 1.0/hdlist[hd]->grid[g1]; |
337 |
+ |
mov[0] = d * hdlist[hd]->xv[g1][0]; |
338 |
+ |
mov[1] = d * hdlist[hd]->xv[g1][1]; |
339 |
+ |
mov[2] = d * hdlist[hd]->xv[g1][2]; |
340 |
+ |
if (w & 1) |
341 |
+ |
VSUM(wp[0], hdlist[hd]->orig, |
342 |
+ |
hdlist[hd]->xv[w>>1], 1.); |
343 |
+ |
else |
344 |
+ |
VSUM(wp[0], hdlist[hd]->orig, mov, 1.); |
345 |
+ |
VSUM(wp[1], wp[0], hdlist[hd]->xv[g0], 1.); |
346 |
+ |
for (i = hdlist[hd]->grid[g1]; ; ) { /* g1 lines */ |
347 |
+ |
(*f)(wp); |
348 |
+ |
if (!--i) break; |
349 |
+ |
wp[0][0] += mov[0]; wp[0][1] += mov[1]; |
350 |
+ |
wp[0][2] += mov[2]; wp[1][0] += mov[0]; |
351 |
+ |
wp[1][1] += mov[1]; wp[1][2] += mov[2]; |
352 |
+ |
} |
353 |
+ |
} |
354 |
|
} |