1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rhdisp3.c,v 3.19 2020/03/12 17:19:18 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Holodeck beam support for display process |
6 |
*/ |
7 |
|
8 |
#include "rholo.h" |
9 |
#include "rhdisp.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 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], 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); /* 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]) != VL_GOOD) { |
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 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 |
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 |
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]) != VL_GOOD) { |
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 |
|
95 |
/* |
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 */ |
103 |
FVECT orig, pyrd[4]; /* pyramid ray directions in clockwise order */ |
104 |
HOLO *hp; |
105 |
int (*vf)(); |
106 |
char *dp; |
107 |
{ |
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, 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++) { |
117 |
inflags |= (gp[i] > FTINY) << (i<<1); |
118 |
inflags |= (gp[i] < hp->grid[i]-FTINY) << (i<<1 | 1); |
119 |
} |
120 |
/* compute pyramid planes */ |
121 |
for (i = 0; i < 4; i++) { |
122 |
fcross(pn[i], pyrd[i], pyrd[(i+1)&03]); |
123 |
po[i] = DOT(pn[i], orig); |
124 |
} |
125 |
/* traverse each wall */ |
126 |
for (gc.w = 0; gc.w < 6; gc.w++) { |
127 |
if (!(inflags & 1<<gc.w)) /* origin on wrong side */ |
128 |
continue; |
129 |
/* scanline algorithm */ |
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[hdwg0[gc.w]] = 0; |
134 |
gp[hdwg1[gc.w]] = gc.i[1] + 0.5; |
135 |
hdworld(lo, hp, gp); |
136 |
gp[hdwg0[gc.w]] = 1; |
137 |
hdworld(ld, hp, gp); |
138 |
ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2]; |
139 |
/* find scanline limits */ |
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) { /* <- plane */ |
145 |
if ((t /= d) < lend) |
146 |
lend = t; |
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]++) { |
159 |
n += (*vf)(&gc, dp); |
160 |
ncalls++; |
161 |
} |
162 |
} |
163 |
} |
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*.99 || 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 |
struct cellist *cl; |
238 |
{ |
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 |
GCOORD *gcp1, *gcp2; |
248 |
{ |
249 |
int c; |
250 |
|
251 |
if ((c = gcp1->w - gcp2->w)) |
252 |
return(c); |
253 |
if ((c = gcp2->i[1] - gcp1->i[1])) /* wg1 is reverse-ordered */ |
254 |
return(c); |
255 |
return(gcp1->i[0] - gcp2->i[0]); |
256 |
} |
257 |
|
258 |
|
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 orient; |
267 |
struct cellist cl; |
268 |
/* compute view pyramid */ |
269 |
*np = 0; |
270 |
orient = viewpyramid(org, dir, hp, vp); |
271 |
if (!orient) |
272 |
return(NULL); |
273 |
/* allocate enough list space */ |
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.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 |
*/ |
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.cl, cl.n, sizeof(GCOORD), cellcmp); |
297 |
#endif |
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 |
} |