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