| 1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
| 1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
| 9 |
|
*/ |
| 10 |
|
|
| 11 |
|
#include "rholo.h" |
| 12 |
+ |
#include "paths.h" |
| 13 |
|
#include "random.h" |
| 14 |
|
|
| 15 |
|
|
| 16 |
+ |
VIEWPOINT myeye; /* target view position */ |
| 17 |
+ |
|
| 18 |
+ |
|
| 19 |
|
packrays(rod, p) /* pack ray origins and directions */ |
| 20 |
|
register float *rod; |
| 21 |
|
register PACKET *p; |
| 22 |
|
{ |
| 23 |
< |
static FVECT ro, rd; |
| 24 |
< |
BCOORD gc; |
| 25 |
< |
double d; |
| 26 |
< |
register int i; |
| 23 |
> |
short packord[RPACKSIZ]; |
| 24 |
> |
float packdc2[RPACKSIZ]; |
| 25 |
> |
int iterleft = 3*p->nr; |
| 26 |
> |
BYTE rpos[2][2]; |
| 27 |
> |
FVECT ro, rd, rp1; |
| 28 |
> |
GCOORD gc[2]; |
| 29 |
> |
double d, dc2, md2, td2; |
| 30 |
> |
int i; |
| 31 |
> |
register int ii; |
| 32 |
|
|
| 33 |
|
if (!hdbcoord(gc, hdlist[p->hd], p->bi)) |
| 34 |
|
error(CONSISTENCY, "bad beam index in packrays"); |
| 35 |
< |
for (i = 0; i < p->nr; i++) { |
| 36 |
< |
*(long *)p->ra[i].r = random()<<16 ^ random(); |
| 37 |
< |
d = hdray(ro, rd, hdlist[p->hd], gc, p->ra[i].r); |
| 35 |
> |
td2 = (myeye.rng+FTINY)*(myeye.rng+FTINY); |
| 36 |
> |
for (i = 0, md2 = 0.; i < p->nr || md2 > td2; ) { |
| 37 |
> |
rpos[0][0] = frandom() * 256.; |
| 38 |
> |
rpos[0][1] = frandom() * 256.; |
| 39 |
> |
rpos[1][0] = frandom() * 256.; |
| 40 |
> |
rpos[1][1] = frandom() * 256.; |
| 41 |
> |
d = hdray(ro, rd, hdlist[p->hd], gc, rpos); |
| 42 |
> |
if (myeye.rng > FTINY) { /* check eyepoint */ |
| 43 |
> |
register int nexti; |
| 44 |
> |
|
| 45 |
> |
VSUM(rp1, ro, rd, d); |
| 46 |
> |
dc2 = dist2line(myeye.vpt, ro, rp1); |
| 47 |
> |
dc2 /= (double)(p->nr*p->nr); |
| 48 |
> |
if (i == p->nr) { /* packet full */ |
| 49 |
> |
nexti = packord[i-1]; |
| 50 |
> |
if (!iterleft--) |
| 51 |
> |
break; /* tried enough! */ |
| 52 |
> |
if (dc2 >= packdc2[nexti]) |
| 53 |
> |
continue; /* worse than worst */ |
| 54 |
> |
md2 -= packdc2[nexti]; |
| 55 |
> |
} else |
| 56 |
> |
nexti = i++; |
| 57 |
> |
md2 += packdc2[nexti] = dc2; /* new distance */ |
| 58 |
> |
for (ii = i; --ii; ) { /* insertion sort */ |
| 59 |
> |
if (dc2 > packdc2[packord[ii-1]]) |
| 60 |
> |
break; |
| 61 |
> |
packord[ii] = packord[ii-1]; |
| 62 |
> |
} |
| 63 |
> |
packord[ii] = nexti; |
| 64 |
> |
ii = nexti; /* put it here */ |
| 65 |
> |
} else |
| 66 |
> |
ii = i++; |
| 67 |
|
if (p->offset != NULL) { |
| 68 |
< |
VSUM(ro, ro, rd, d); /* exterior only */ |
| 69 |
< |
p->offset[i] = d; |
| 68 |
> |
if (!vdef(OBSTRUCTIONS)) |
| 69 |
> |
d *= frandom(); /* random offset */ |
| 70 |
> |
VSUM(ro, ro, rd, d); /* advance ray */ |
| 71 |
> |
p->offset[ii] = d; |
| 72 |
|
} |
| 73 |
< |
VCOPY(rod, ro); |
| 74 |
< |
rod += 3; |
| 75 |
< |
VCOPY(rod, rd); |
| 76 |
< |
rod += 3; |
| 73 |
> |
p->ra[ii].r[0][0] = rpos[0][0]; |
| 74 |
> |
p->ra[ii].r[0][1] = rpos[0][1]; |
| 75 |
> |
p->ra[ii].r[1][0] = rpos[1][0]; |
| 76 |
> |
p->ra[ii].r[1][1] = rpos[1][1]; |
| 77 |
> |
VCOPY(rod+6*ii, ro); |
| 78 |
> |
VCOPY(rod+6*ii+3, rd); |
| 79 |
|
} |
| 80 |
+ |
#ifdef DEBUG |
| 81 |
+ |
fprintf(stderr, "%f mean distance for target %f (%d iterations left)\n", |
| 82 |
+ |
sqrt(md2), myeye.rng, iterleft); |
| 83 |
+ |
#endif |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
|
| 99 |
|
p->ra[i].d = hdcode(hdlist[p->hd], d); |
| 100 |
|
rvl += 4; |
| 101 |
|
} |
| 102 |
+ |
p->nc += p->nr; |
| 103 |
+ |
} |
| 104 |
+ |
|
| 105 |
+ |
|
| 106 |
+ |
int |
| 107 |
+ |
done_rtrace() /* clean up and close rtrace calculation */ |
| 108 |
+ |
{ |
| 109 |
+ |
int status; |
| 110 |
+ |
/* already closed? */ |
| 111 |
+ |
if (!nprocs) |
| 112 |
+ |
return; |
| 113 |
+ |
/* flush beam queue */ |
| 114 |
+ |
done_packets(flush_queue()); |
| 115 |
+ |
/* sync holodeck */ |
| 116 |
+ |
hdsync(NULL, 1); |
| 117 |
+ |
/* close rtrace */ |
| 118 |
+ |
if ((status = end_rtrace())) |
| 119 |
+ |
error(WARNING, "bad exit status from rtrace"); |
| 120 |
+ |
if (vdef(REPORT)) { /* report time */ |
| 121 |
+ |
eputs("rtrace process closed\n"); |
| 122 |
+ |
report(0); |
| 123 |
+ |
} |
| 124 |
+ |
return(status); /* return status */ |
| 125 |
+ |
} |
| 126 |
+ |
|
| 127 |
+ |
|
| 128 |
+ |
new_rtrace() /* restart rtrace calculation */ |
| 129 |
+ |
{ |
| 130 |
+ |
char combuf[128]; |
| 131 |
+ |
|
| 132 |
+ |
if (nprocs > 0) /* already running? */ |
| 133 |
+ |
return; |
| 134 |
+ |
starttime = time(NULL); /* reset start time and counts */ |
| 135 |
+ |
npacksdone = nraysdone = 0L; |
| 136 |
+ |
if (vdef(TIME)) /* reset end time */ |
| 137 |
+ |
endtime = starttime + vflt(TIME)*3600. + .5; |
| 138 |
+ |
if (vdef(RIF)) { /* rerun rad to update octree */ |
| 139 |
+ |
sprintf(combuf, "rad -v 0 -s -w %s", vval(RIF)); |
| 140 |
+ |
if (system(combuf)) |
| 141 |
+ |
error(WARNING, "error running rad"); |
| 142 |
+ |
} |
| 143 |
+ |
if (start_rtrace() < 1) /* start rtrace */ |
| 144 |
+ |
error(WARNING, "cannot restart rtrace"); |
| 145 |
+ |
else if (vdef(REPORT)) { |
| 146 |
+ |
eputs("rtrace process restarted\n"); |
| 147 |
+ |
report(0); |
| 148 |
+ |
} |
| 149 |
+ |
} |
| 150 |
+ |
|
| 151 |
+ |
|
| 152 |
+ |
getradfile() /* run rad and get needed variables */ |
| 153 |
+ |
{ |
| 154 |
+ |
static short mvar[] = {OCTREE,EYESEP,-1}; |
| 155 |
+ |
static char tf1[] = TEMPLATE; |
| 156 |
+ |
char tf2[64]; |
| 157 |
+ |
char combuf[256]; |
| 158 |
+ |
char *pippt; |
| 159 |
+ |
register int i; |
| 160 |
+ |
register char *cp; |
| 161 |
+ |
/* check if rad file specified */ |
| 162 |
+ |
if (!vdef(RIF)) |
| 163 |
+ |
return(0); |
| 164 |
+ |
/* create rad command */ |
| 165 |
+ |
mktemp(tf1); |
| 166 |
+ |
sprintf(tf2, "%s.rif", tf1); |
| 167 |
+ |
sprintf(combuf, |
| 168 |
+ |
"rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", |
| 169 |
+ |
vval(RIF), tf1); |
| 170 |
+ |
cp = combuf; |
| 171 |
+ |
while (*cp){ |
| 172 |
+ |
if (*cp == '|') pippt = cp; |
| 173 |
+ |
cp++; |
| 174 |
+ |
} /* match unset variables */ |
| 175 |
+ |
for (i = 0; mvar[i] >= 0; i++) |
| 176 |
+ |
if (!vdef(mvar[i])) { |
| 177 |
+ |
*cp++ = '|'; |
| 178 |
+ |
strcpy(cp, vnam(mvar[i])); |
| 179 |
+ |
while (*cp) cp++; |
| 180 |
+ |
pippt = NULL; |
| 181 |
+ |
} |
| 182 |
+ |
if (pippt != NULL) |
| 183 |
+ |
strcpy(pippt, "> /dev/null"); /* nothing to match */ |
| 184 |
+ |
else |
| 185 |
+ |
sprintf(cp, ")[ \t]*=' > %s", tf2); |
| 186 |
+ |
#ifdef DEBUG |
| 187 |
+ |
wputs(combuf); wputs("\n"); |
| 188 |
+ |
#endif |
| 189 |
+ |
system(combuf); /* ignore exit code */ |
| 190 |
+ |
if (pippt == NULL) { |
| 191 |
+ |
loadvars(tf2); /* load variables */ |
| 192 |
+ |
unlink(tf2); |
| 193 |
+ |
} |
| 194 |
+ |
rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ |
| 195 |
+ |
unlink(tf1); /* clean up */ |
| 196 |
+ |
return(1); |
| 197 |
+ |
} |
| 198 |
+ |
|
| 199 |
+ |
|
| 200 |
+ |
report(t) /* report progress so far */ |
| 201 |
+ |
time_t t; |
| 202 |
+ |
{ |
| 203 |
+ |
static time_t seconds2go = 1000000; |
| 204 |
+ |
|
| 205 |
+ |
if (t == 0L) |
| 206 |
+ |
t = time(NULL); |
| 207 |
+ |
sprintf(errmsg, "%ld packets (%ld rays) done after %.2f hours\n", |
| 208 |
+ |
npacksdone, nraysdone, (t-starttime)/3600.); |
| 209 |
+ |
eputs(errmsg); |
| 210 |
+ |
if (seconds2go == 1000000) |
| 211 |
+ |
seconds2go = vdef(REPORT) ? (long)(vflt(REPORT)*60. + .5) : 0L; |
| 212 |
+ |
if (seconds2go) |
| 213 |
+ |
reporttime = t + seconds2go; |
| 214 |
|
} |