15 |
|
|
16 |
|
VIEWPOINT myeye; /* target view position */ |
17 |
|
|
18 |
+ |
struct gclim { |
19 |
+ |
HOLO *hp; /* holodeck pointer */ |
20 |
+ |
GCOORD gc; /* grid cell */ |
21 |
+ |
FVECT egp; /* eye grid point */ |
22 |
+ |
double erg2; /* mean square eye grid range */ |
23 |
+ |
double gmin[2], gmax[2]; /* grid coordinate limits */ |
24 |
+ |
}; /* a grid coordinate range */ |
25 |
|
|
26 |
+ |
|
27 |
+ |
static |
28 |
+ |
initeyelim(gcl, hp, gc) /* initialize grid coordinate limits */ |
29 |
+ |
register struct gclim *gcl; |
30 |
+ |
register HOLO *hp; |
31 |
+ |
GCOORD *gc; |
32 |
+ |
{ |
33 |
+ |
register FLOAT *v; |
34 |
+ |
register int i; |
35 |
+ |
|
36 |
+ |
if (hp != NULL) { |
37 |
+ |
hdgrid(gcl->egp, gcl->hp = hp, myeye.vpt); |
38 |
+ |
gcl->erg2 = 0; |
39 |
+ |
for (i = 0, v = hp->wg[0]; i < 3; i++, v += 3) |
40 |
+ |
gcl->erg2 += DOT(v,v); |
41 |
+ |
gcl->erg2 *= (1./3.) * myeye.rng*myeye.rng; |
42 |
+ |
} |
43 |
+ |
if (gc != NULL) |
44 |
+ |
copystruct(&gcl->gc, gc); |
45 |
+ |
gcl->gmin[0] = gcl->gmin[1] = FHUGE; |
46 |
+ |
gcl->gmax[0] = gcl->gmax[1] = -FHUGE; |
47 |
+ |
} |
48 |
+ |
|
49 |
+ |
|
50 |
+ |
static |
51 |
+ |
groweyelim(gcl, gc, r0, r1) /* grow grid limits about eye point */ |
52 |
+ |
register struct gclim *gcl; |
53 |
+ |
GCOORD *gc; |
54 |
+ |
double r0, r1; |
55 |
+ |
{ |
56 |
+ |
FVECT gp, ab; |
57 |
+ |
double vlen, plen, dv0, dv1; |
58 |
+ |
double rd2, dwall, gpos; |
59 |
+ |
int eyeout; |
60 |
+ |
register int i, g0, g1; |
61 |
+ |
|
62 |
+ |
i = gc->w>>1; |
63 |
+ |
if (gc->w&1) |
64 |
+ |
eyeout = (gp[i] = gcl->hp->grid[i]) < gcl->egp[i]; |
65 |
+ |
else |
66 |
+ |
eyeout = (gp[i] = 0) > gcl->egp[i]; |
67 |
+ |
gp[hdwg0[gc->w]] = gc->i[0] + r0; |
68 |
+ |
gp[hdwg1[gc->w]] = gc->i[1] + r1; |
69 |
+ |
VSUB(ab, gcl->egp, gp); |
70 |
+ |
rd2 = DOT(ab,ab); |
71 |
+ |
if (rd2 <= gcl->erg2) { |
72 |
+ |
gcl->gmin[0] = gcl->gmin[1] = -FHUGE; |
73 |
+ |
gcl->gmax[0] = gcl->gmax[1] = FHUGE; |
74 |
+ |
return; |
75 |
+ |
} |
76 |
+ |
rd2 = gcl->erg2 / rd2; |
77 |
+ |
vlen = 1. - rd2; |
78 |
+ |
plen = sqrt(rd2 * vlen); |
79 |
+ |
g0 = gcl->gc.w>>1; |
80 |
+ |
dwall = (gcl->gc.w&1 ? gcl->hp->grid[g0] : 0) - gp[g0]; |
81 |
+ |
for (i = 0; i < 4; i++) { |
82 |
+ |
if (i == 2) |
83 |
+ |
plen = -plen; |
84 |
+ |
g1 = (g0+(i&1)+1)%3; |
85 |
+ |
dv0 = vlen*ab[g0] + plen*ab[g1]; |
86 |
+ |
dv1 = vlen*ab[g1] - plen*ab[g0]; |
87 |
+ |
if ((dv0 < 0 ^ dwall < 0 ^ eyeout) || |
88 |
+ |
(dv0 <= FTINY && dv0 >= -FTINY)) { |
89 |
+ |
if (eyeout) |
90 |
+ |
dv1 = -dv1; |
91 |
+ |
if (dv1 > FTINY) |
92 |
+ |
gcl->gmax[i&1] = FHUGE; |
93 |
+ |
else if (dv1 < -FTINY) |
94 |
+ |
gcl->gmin[i&1] = -FHUGE; |
95 |
+ |
} else { |
96 |
+ |
gpos = gp[g1] + dv1*dwall/dv0; |
97 |
+ |
if (gpos < gcl->gmin[i&1]) |
98 |
+ |
gcl->gmin[i&1] = gpos; |
99 |
+ |
if (gpos > gcl->gmax[i&1]) |
100 |
+ |
gcl->gmax[i&1] = gpos; |
101 |
+ |
} |
102 |
+ |
} |
103 |
+ |
} |
104 |
+ |
|
105 |
+ |
|
106 |
+ |
static int |
107 |
+ |
clipeyelim(rrng, gcl) /* clip eye limits to grid cell */ |
108 |
+ |
register short rrng[2][2]; |
109 |
+ |
register struct gclim *gcl; |
110 |
+ |
{ |
111 |
+ |
int incell = 1; |
112 |
+ |
register int i; |
113 |
+ |
|
114 |
+ |
for (i = 0; i < 2; i++) { |
115 |
+ |
if (gcl->gmin[i] < gcl->gc.i[i]) |
116 |
+ |
gcl->gmin[i] = gcl->gc.i[i]; |
117 |
+ |
if (gcl->gmax[i] > gcl->gc.i[i]+1) |
118 |
+ |
gcl->gmax[i] = gcl->gc.i[i]+1; |
119 |
+ |
if (gcl->gmax[i] > gcl->gmin[i]) { |
120 |
+ |
rrng[i][0] = 256.*(gcl->gmin[i] - gcl->gc.i[i]) + |
121 |
+ |
(1.-FTINY); |
122 |
+ |
rrng[i][1] = 256.*(gcl->gmax[i] - gcl->gc.i[i]) + |
123 |
+ |
(1.-FTINY) - rrng[i][0]; |
124 |
+ |
} else |
125 |
+ |
rrng[i][0] = rrng[i][1] = 0; |
126 |
+ |
incell &= rrng[i][1] > 0; |
127 |
+ |
} |
128 |
+ |
return(incell); |
129 |
+ |
} |
130 |
+ |
|
131 |
+ |
|
132 |
|
packrays(rod, p) /* pack ray origins and directions */ |
133 |
|
register float *rod; |
134 |
|
register PACKET *p; |
135 |
|
{ |
136 |
< |
float packdc2[RPACKSIZ]; |
137 |
< |
int iterleft = 3*p->nr + 9; |
138 |
< |
BYTE rpos[2][2]; |
139 |
< |
FVECT ro, rd, rp1; |
136 |
> |
int nretries = p->nr + 2; |
137 |
> |
struct gclim eyelim; |
138 |
> |
short rrng0[2][2], rrng1[2][2]; |
139 |
> |
int useyelim; |
140 |
|
GCOORD gc[2]; |
141 |
< |
double d, dc2, md2, td2, dc2worst = FHUGE; |
142 |
< |
int i; |
143 |
< |
register int ii; |
141 |
> |
FVECT ro, rd; |
142 |
> |
double d; |
143 |
> |
register int i; |
144 |
|
|
145 |
|
if (!hdbcoord(gc, hdlist[p->hd], p->bi)) |
146 |
|
error(CONSISTENCY, "bad beam index in packrays"); |
147 |
< |
td2 = myeye.rng + FTINY; td2 *= td2; |
148 |
< |
for (i = 0, md2 = 0.; i < p->nr || (md2 > td2 && iterleft--); ) { |
149 |
< |
rpos[0][0] = frandom() * 256.; |
150 |
< |
rpos[0][1] = frandom() * 256.; |
151 |
< |
rpos[1][0] = frandom() * 256.; |
152 |
< |
rpos[1][1] = frandom() * 256.; |
153 |
< |
d = hdray(ro, rd, hdlist[p->hd], gc, rpos); |
154 |
< |
if (myeye.rng > FTINY) { /* check eyepoint */ |
155 |
< |
register int nexti; |
156 |
< |
|
157 |
< |
VSUM(rp1, ro, rd, d); |
158 |
< |
dc2 = dist2line(myeye.vpt, ro, rp1) / p->nr; |
159 |
< |
if (i == p->nr) { /* packet full */ |
160 |
< |
if (dc2 >= dc2worst) /* quick check */ |
161 |
< |
continue; |
162 |
< |
nexti = 0; /* find worst */ |
163 |
< |
for (ii = i; --ii; ) |
164 |
< |
if (packdc2[ii] > packdc2[nexti]) |
165 |
< |
nexti = ii; |
166 |
< |
if (dc2 >= (dc2worst = packdc2[nexti])) |
167 |
< |
continue; /* worse than worst */ |
168 |
< |
md2 -= dc2worst; |
169 |
< |
} else |
170 |
< |
nexti = i++; |
171 |
< |
md2 += packdc2[nexti] = dc2; /* new distance */ |
172 |
< |
ii = nexti; /* put it here */ |
173 |
< |
} else |
174 |
< |
ii = i++; |
147 |
> |
if ((useyelim = myeye.rng > FTINY)) { |
148 |
> |
initeyelim(&eyelim, hdlist[p->hd], gc); |
149 |
> |
groweyelim(&eyelim, gc+1, 0., 0.); |
150 |
> |
groweyelim(&eyelim, gc+1, 1., 1.); |
151 |
> |
useyelim &= clipeyelim(rrng0, &eyelim); |
152 |
> |
} |
153 |
> |
for (i = 0; i < p->nr; i++) { |
154 |
> |
retry: |
155 |
> |
if (useyelim) { |
156 |
> |
initeyelim(&eyelim, NULL, gc+1); |
157 |
> |
p->ra[i].r[0][0] = (int)(frandom()*rrng0[0][1]) |
158 |
> |
+ rrng0[0][0]; |
159 |
> |
p->ra[i].r[0][1] = (int)(frandom()*rrng0[1][1]) |
160 |
> |
+ rrng0[1][0]; |
161 |
> |
groweyelim(&eyelim, gc, |
162 |
> |
(1./256.)*(p->ra[i].r[0][0]+.5), |
163 |
> |
(1./256.)*(p->ra[i].r[0][1]+.5)); |
164 |
> |
if (!clipeyelim(rrng1, &eyelim)) { |
165 |
> |
useyelim &= nretries-- > 0; |
166 |
> |
goto retry; |
167 |
> |
} |
168 |
> |
p->ra[i].r[1][0] = (int)(frandom()*rrng1[0][1]) |
169 |
> |
+ rrng1[0][0]; |
170 |
> |
p->ra[i].r[1][1] = (int)(frandom()*rrng1[1][1]) |
171 |
> |
+ rrng1[1][0]; |
172 |
> |
} else { |
173 |
> |
p->ra[i].r[0][0] = frandom() * 256.; |
174 |
> |
p->ra[i].r[0][1] = frandom() * 256.; |
175 |
> |
p->ra[i].r[1][0] = frandom() * 256.; |
176 |
> |
p->ra[i].r[1][1] = frandom() * 256.; |
177 |
> |
} |
178 |
> |
d = hdray(ro, rd, hdlist[p->hd], gc, p->ra[i].r); |
179 |
|
if (p->offset != NULL) { |
180 |
|
if (!vdef(OBSTRUCTIONS)) |
181 |
|
d *= frandom(); /* random offset */ |
182 |
|
VSUM(ro, ro, rd, d); /* advance ray */ |
183 |
< |
p->offset[ii] = d; |
183 |
> |
p->offset[i] = d; |
184 |
|
} |
185 |
< |
p->ra[ii].r[0][0] = rpos[0][0]; |
186 |
< |
p->ra[ii].r[0][1] = rpos[0][1]; |
187 |
< |
p->ra[ii].r[1][0] = rpos[1][0]; |
188 |
< |
p->ra[ii].r[1][1] = rpos[1][1]; |
72 |
< |
VCOPY(rod+6*ii, ro); |
73 |
< |
VCOPY(rod+6*ii+3, rd); |
185 |
> |
VCOPY(rod, ro); |
186 |
> |
rod += 3; |
187 |
> |
VCOPY(rod, rd); |
188 |
> |
rod += 3; |
189 |
|
} |
75 |
– |
#ifdef DEBUG |
76 |
– |
fprintf(stderr, "%f mean distance for target %f (%d iterations left)\n", |
77 |
– |
sqrt(md2), myeye.rng, iterleft); |
78 |
– |
#endif |
190 |
|
} |
191 |
|
|
192 |
|
|