| 13 |
|
#include "random.h" |
| 14 |
|
|
| 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 erg[2]; /* eye range in wall grid coords */ |
| 23 |
+ |
double gmin[2], gmax[2]; /* grid coordinate limits */ |
| 24 |
+ |
}; /* a grid coordinate range */ |
| 25 |
+ |
|
| 26 |
+ |
|
| 27 |
+ |
static |
| 28 |
+ |
initeyelim(gcl, hd, gc) /* initialize grid coordinate limits */ |
| 29 |
+ |
register struct gclim *gcl; |
| 30 |
+ |
int hd; |
| 31 |
+ |
GCOORD *gc; |
| 32 |
+ |
{ |
| 33 |
+ |
register FLOAT *v; |
| 34 |
+ |
register int i; |
| 35 |
+ |
|
| 36 |
+ |
gcl->hp = hdlist[hd]; |
| 37 |
+ |
copystruct(&gcl->gc, gc); |
| 38 |
+ |
hdgrid(gcl->egp, gcl->hp, myeye.vpt); |
| 39 |
+ |
for (i = 0; i < 2; i++) { |
| 40 |
+ |
v = gcl->hp->wg[((gcl->gc.w>>1)+i+1)%3]; |
| 41 |
+ |
gcl->erg[i] = myeye.rng * VLEN(v); |
| 42 |
+ |
gcl->gmin[i] = FHUGE; gcl->gmax[i] = -FHUGE; |
| 43 |
+ |
} |
| 44 |
+ |
} |
| 45 |
+ |
|
| 46 |
+ |
|
| 47 |
+ |
static |
| 48 |
+ |
groweyelim(gcl, gp) /* grow grid limits about eye point */ |
| 49 |
+ |
register struct gclim *gcl; |
| 50 |
+ |
FVECT gp; |
| 51 |
+ |
{ |
| 52 |
+ |
FVECT ab; |
| 53 |
+ |
double l2, d, mult, wg; |
| 54 |
+ |
register int i, g; |
| 55 |
+ |
|
| 56 |
+ |
VSUB(ab, gcl->egp, gp); |
| 57 |
+ |
l2 = DOT(ab,ab); |
| 58 |
+ |
if (l2 <= gcl->erg[0]*gcl->erg[1]) { |
| 59 |
+ |
gcl->gmin[0] = gcl->gmin[1] = -FHUGE; |
| 60 |
+ |
gcl->gmax[0] = gcl->gmax[1] = FHUGE; |
| 61 |
+ |
return; |
| 62 |
+ |
} |
| 63 |
+ |
mult = gp[g = gcl->gc.w>>1]; |
| 64 |
+ |
if (gcl->gc.w&1) |
| 65 |
+ |
mult -= gcl->hp->grid[g]; |
| 66 |
+ |
if (ab[g]*ab[g] > gcl->erg[0]*gcl->erg[1]) |
| 67 |
+ |
mult /= -ab[g]; |
| 68 |
+ |
else if (fabs(ab[hdwg0[gcl->gc.w]]) > fabs(ab[hdwg1[gcl->gc.w]])) |
| 69 |
+ |
mult = (gcl->gc.i[0] + .5 - gp[hdwg0[gcl->gc.w]]) / |
| 70 |
+ |
ab[hdwg0[gcl->gc.w]]; |
| 71 |
+ |
else |
| 72 |
+ |
mult = (gcl->gc.i[1] + .5 - gp[hdwg1[gcl->gc.w]]) / |
| 73 |
+ |
ab[hdwg1[gcl->gc.w]]; |
| 74 |
+ |
for (i = 0; i < 2; i++) { |
| 75 |
+ |
g = ((gcl->gc.w>>1)+i+1)%3; |
| 76 |
+ |
wg = gp[g] + mult*ab[g]; |
| 77 |
+ |
d = mult*gcl->erg[i]; |
| 78 |
+ |
if (d < 0.) d = -d; |
| 79 |
+ |
if (wg - d < gcl->gmin[i]) |
| 80 |
+ |
gcl->gmin[i] = wg - d; |
| 81 |
+ |
if (wg + d > gcl->gmax[i]) |
| 82 |
+ |
gcl->gmax[i] = wg + d; |
| 83 |
+ |
} |
| 84 |
+ |
} |
| 85 |
+ |
|
| 86 |
+ |
|
| 87 |
+ |
static int |
| 88 |
+ |
clipeyelim(rrng, gcl) /* clip eye limits to grid cell */ |
| 89 |
+ |
register short rrng[2][2]; |
| 90 |
+ |
register struct gclim *gcl; |
| 91 |
+ |
{ |
| 92 |
+ |
int incell = 1; |
| 93 |
+ |
register int i; |
| 94 |
+ |
|
| 95 |
+ |
for (i = 0; i < 2; i++) { |
| 96 |
+ |
if (gcl->gmin[i] < gcl->gc.i[i]) |
| 97 |
+ |
gcl->gmin[i] = gcl->gc.i[i]; |
| 98 |
+ |
if (gcl->gmax[i] > gcl->gc.i[i]+1) |
| 99 |
+ |
gcl->gmax[i] = gcl->gc.i[i]+1; |
| 100 |
+ |
if ((incell &= gcl->gmax[i] > gcl->gmin[i])) { |
| 101 |
+ |
rrng[i][0] = 256.*(gcl->gmin[i] - gcl->gc.i[i]) + |
| 102 |
+ |
(1.-FTINY); |
| 103 |
+ |
rrng[i][1] = 256.*(gcl->gmax[i] - gcl->gc.i[i]) + |
| 104 |
+ |
(1.-FTINY) - rrng[i][0]; |
| 105 |
+ |
incell &= rrng[i][1] > 0; |
| 106 |
+ |
} |
| 107 |
+ |
} |
| 108 |
+ |
return(incell); |
| 109 |
+ |
} |
| 110 |
+ |
|
| 111 |
+ |
|
| 112 |
|
packrays(rod, p) /* pack ray origins and directions */ |
| 113 |
|
register float *rod; |
| 114 |
|
register PACKET *p; |
| 115 |
|
{ |
| 116 |
< |
static FVECT ro, rd; |
| 116 |
> |
#define gp ro |
| 117 |
> |
#ifdef DEBUG |
| 118 |
> |
double dist2sum = 0.; |
| 119 |
> |
FVECT vt; |
| 120 |
> |
#endif |
| 121 |
> |
int nretries = p->nr + 2; |
| 122 |
> |
struct gclim eyelim; |
| 123 |
> |
short rrng0[2][2], rrng1[2][2]; |
| 124 |
> |
int useyelim; |
| 125 |
|
GCOORD gc[2]; |
| 126 |
< |
int ila[2], hsh; |
| 127 |
< |
double d, sl[4]; |
| 126 |
> |
FVECT ro, rd; |
| 127 |
> |
double d; |
| 128 |
|
register int i; |
| 129 |
|
|
| 130 |
|
if (!hdbcoord(gc, hdlist[p->hd], p->bi)) |
| 131 |
|
error(CONSISTENCY, "bad beam index in packrays"); |
| 132 |
< |
ila[0] = p->hd; ila[1] = p->bi; |
| 133 |
< |
hsh = ilhash(ila,2) + p->nc; |
| 132 |
> |
if ((useyelim = myeye.rng > FTINY)) { |
| 133 |
> |
initeyelim(&eyelim, p->hd, gc); |
| 134 |
> |
gp[gc[1].w>>1] = gc[1].w&1 ? |
| 135 |
> |
hdlist[p->hd]->grid[gc[1].w>>1] : 0; |
| 136 |
> |
gp[hdwg0[gc[1].w]] = gc[1].i[0]; |
| 137 |
> |
gp[hdwg1[gc[1].w]] = gc[1].i[1]; |
| 138 |
> |
groweyelim(&eyelim, gp); |
| 139 |
> |
gp[hdwg0[gc[1].w]]++; |
| 140 |
> |
gp[hdwg1[gc[1].w]]++; |
| 141 |
> |
groweyelim(&eyelim, gp); |
| 142 |
> |
useyelim &= clipeyelim(rrng0, &eyelim); |
| 143 |
> |
} |
| 144 |
|
for (i = 0; i < p->nr; i++) { |
| 145 |
< |
multisamp(sl, 4, urand(hsh+i)); |
| 146 |
< |
p->ra[i].r[0][0] = sl[0] * 256.; |
| 147 |
< |
p->ra[i].r[0][1] = sl[1] * 256.; |
| 148 |
< |
p->ra[i].r[1][0] = sl[2] * 256.; |
| 149 |
< |
p->ra[i].r[1][1] = sl[3] * 256.; |
| 145 |
> |
retry: |
| 146 |
> |
if (useyelim) { |
| 147 |
> |
p->ra[i].r[0][0] = (int)(frandom()*rrng0[0][1]) |
| 148 |
> |
+ rrng0[0][0]; |
| 149 |
> |
p->ra[i].r[0][1] = (int)(frandom()*rrng0[1][1]) |
| 150 |
> |
+ rrng0[1][0]; |
| 151 |
> |
initeyelim(&eyelim, p->hd, gc+1); |
| 152 |
> |
gp[gc[0].w>>1] = gc[0].w&1 ? |
| 153 |
> |
hdlist[p->hd]->grid[gc[0].w>>1] : 0; |
| 154 |
> |
gp[hdwg0[gc[0].w]] = gc[0].i[0] + |
| 155 |
> |
(1./256.)*(p->ra[i].r[0][0]+.5); |
| 156 |
> |
gp[hdwg1[gc[0].w]] = gc[0].i[1] + |
| 157 |
> |
(1./256.)*(p->ra[i].r[0][1]+.5); |
| 158 |
> |
groweyelim(&eyelim, gp); |
| 159 |
> |
if (!clipeyelim(rrng1, &eyelim)) { |
| 160 |
> |
useyelim &= nretries-- > 0; |
| 161 |
> |
#ifdef DEBUG |
| 162 |
> |
if (!useyelim) |
| 163 |
> |
error(WARNING, "exceeded retry limit in packrays"); |
| 164 |
> |
#endif |
| 165 |
> |
goto retry; |
| 166 |
> |
} |
| 167 |
> |
p->ra[i].r[1][0] = (int)(frandom()*rrng1[0][1]) |
| 168 |
> |
+ rrng1[0][0]; |
| 169 |
> |
p->ra[i].r[1][1] = (int)(frandom()*rrng1[1][1]) |
| 170 |
> |
+ rrng1[1][0]; |
| 171 |
> |
} else { |
| 172 |
> |
p->ra[i].r[0][0] = frandom() * 256.; |
| 173 |
> |
p->ra[i].r[0][1] = frandom() * 256.; |
| 174 |
> |
p->ra[i].r[1][0] = frandom() * 256.; |
| 175 |
> |
p->ra[i].r[1][1] = frandom() * 256.; |
| 176 |
> |
} |
| 177 |
|
d = hdray(ro, rd, hdlist[p->hd], gc, p->ra[i].r); |
| 178 |
< |
if (!vdef(OBSTRUCTIONS)) |
| 179 |
< |
d *= frandom(); /* random offset */ |
| 178 |
> |
#ifdef DEBUG |
| 179 |
> |
VSUM(vt, ro, rd, d); |
| 180 |
> |
dist2sum += dist2line(myeye.vpt, ro, vt); |
| 181 |
> |
#endif |
| 182 |
|
if (p->offset != NULL) { |
| 183 |
+ |
if (!vdef(OBSTRUCTIONS)) |
| 184 |
+ |
d *= frandom(); /* random offset */ |
| 185 |
|
VSUM(ro, ro, rd, d); /* advance ray */ |
| 186 |
|
p->offset[i] = d; |
| 187 |
|
} |
| 190 |
|
VCOPY(rod, rd); |
| 191 |
|
rod += 3; |
| 192 |
|
} |
| 193 |
+ |
#ifdef DEBUG |
| 194 |
+ |
fprintf(stderr, "RMS distance = %f\n", sqrt(dist2sum/p->nr)); |
| 195 |
+ |
#endif |
| 196 |
+ |
#undef gp |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
|
| 296 |
|
strcpy(pippt, "> /dev/null"); /* nothing to match */ |
| 297 |
|
else |
| 298 |
|
sprintf(cp, ")[ \t]*=' > %s", tf2); |
| 299 |
< |
if (system(combuf)) { |
| 300 |
< |
unlink(tf2); /* clean up */ |
| 301 |
< |
unlink(tf1); |
| 302 |
< |
error(WARNING, "error executing rad command"); |
| 154 |
< |
return(-1); |
| 155 |
< |
} |
| 299 |
> |
#ifdef DEBUG |
| 300 |
> |
wputs(combuf); wputs("\n"); |
| 301 |
> |
#endif |
| 302 |
> |
system(combuf); /* ignore exit code */ |
| 303 |
|
if (pippt == NULL) { |
| 304 |
|
loadvars(tf2); /* load variables */ |
| 305 |
|
unlink(tf2); |