1 |
greg |
1.1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Source sampling routines |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include "standard.h" |
12 |
|
|
|
13 |
|
|
#include "object.h" |
14 |
|
|
|
15 |
|
|
#include "source.h" |
16 |
|
|
|
17 |
|
|
#include "random.h" |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
extern int dimlist[]; /* dimension list for distribution */ |
21 |
|
|
extern int ndims; /* number of dimensions so far */ |
22 |
|
|
extern int samplendx; /* index for this sample */ |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
double |
26 |
|
|
nextssamp(org, dir, si) /* compute sample for source, rtn. distance */ |
27 |
|
|
FVECT org, dir; /* origin is read only, direction is set */ |
28 |
|
|
register SRCINDEX *si; /* source index (modified to current) */ |
29 |
|
|
{ |
30 |
|
|
int cent[3], size[3], parr[2]; |
31 |
|
|
FVECT vpos; |
32 |
|
|
double d; |
33 |
|
|
register int i; |
34 |
|
|
tryagain: |
35 |
|
|
while (++si->sp >= si->np) { /* get next sample */ |
36 |
|
|
if (++si->sn >= nsources) |
37 |
|
|
return(0.0); /* no more */ |
38 |
|
|
if (srcsizerat <= FTINY) |
39 |
|
|
nopart(si, org); |
40 |
|
|
else { |
41 |
|
|
for (i = si->sn; source[i].sflags & SVIRTUAL; |
42 |
|
|
i = source[i].sa.sv.sn) |
43 |
|
|
; /* partition source */ |
44 |
|
|
(*sfun[source[i].so->otype].of->partit)(si, org); |
45 |
|
|
} |
46 |
|
|
si->sp = -1; |
47 |
|
|
} |
48 |
|
|
/* get partition */ |
49 |
|
|
cent[0] = cent[1] = cent[2] = 0; |
50 |
|
|
size[0] = size[1] = size[2] = MAXSPART; |
51 |
|
|
parr[0] = 0; parr[1] = si->sp; |
52 |
|
|
if (!skipparts(cent, size, parr, si->spt)) |
53 |
|
|
error(CONSISTENCY, "bad source partition in nextssamp"); |
54 |
|
|
/* compute sample */ |
55 |
|
|
if (dstrsrc > FTINY) { /* jitter sample */ |
56 |
|
|
dimlist[ndims] = si->sn + 8831; |
57 |
|
|
dimlist[ndims+1] = si->sp + 3109; |
58 |
|
|
d = urand(ilhash(dimlist,ndims+2)+samplendx); |
59 |
|
|
if (source[si->sn].sflags & SFLAT) { |
60 |
|
|
multisamp(vpos, 2, d); |
61 |
|
|
vpos[2] = 0.5; |
62 |
|
|
} else |
63 |
|
|
multisamp(vpos, 3, d); |
64 |
|
|
for (i = 0; i < 3; i++) |
65 |
|
|
vpos[i] = dstrsrc * (1. - 2.*vpos[i]) * |
66 |
|
|
(double)size[i]/MAXSPART; |
67 |
|
|
} else |
68 |
|
|
vpos[0] = vpos[1] = vpos[2] = 0.0; |
69 |
|
|
|
70 |
|
|
for (i = 0; i < 3; i++) |
71 |
|
|
vpos[i] += (double)cent[i]/MAXSPART; |
72 |
|
|
/* compute direction */ |
73 |
|
|
for (i = 0; i < 3; i++) |
74 |
|
|
dir[i] = source[si->sn].sloc[i] + |
75 |
|
|
vpos[SU]*source[si->sn].ss[SU][i] + |
76 |
|
|
vpos[SV]*source[si->sn].ss[SV][i] + |
77 |
|
|
vpos[SW]*source[si->sn].ss[SW][i]; |
78 |
|
|
|
79 |
|
|
if (!(source[si->sn].sflags & SDISTANT)) |
80 |
|
|
for (i = 0; i < 3; i++) |
81 |
|
|
dir[i] -= org[i]; |
82 |
|
|
/* compute distance */ |
83 |
|
|
if ((d = normalize(dir)) == 0.0) |
84 |
|
|
goto tryagain; /* at source! */ |
85 |
|
|
|
86 |
|
|
/* compute sample size */ |
87 |
|
|
si->dom = source[si->sn].ss2; |
88 |
|
|
if (source[si->sn].sflags & SFLAT) { |
89 |
|
|
si->dom *= sflatform(si->sn, dir); |
90 |
|
|
if (si->dom <= FTINY) { /* behind source */ |
91 |
greg |
1.3 |
si->np = 0; |
92 |
greg |
1.1 |
goto tryagain; |
93 |
|
|
} |
94 |
|
|
si->dom *= (double)(size[SU]*size[SV])/(MAXSPART*MAXSPART); |
95 |
|
|
} else if (source[si->sn].sflags & SCYL) { |
96 |
|
|
si->dom *= scylform(si->sn, dir); |
97 |
|
|
si->dom *= (double)size[SU]/MAXSPART; |
98 |
|
|
} else { |
99 |
|
|
si->dom *= (double)(size[SU]*size[SV]*size[SW]) / |
100 |
|
|
(MAXSPART*MAXSPART*MAXSPART) ; |
101 |
|
|
} |
102 |
|
|
if (source[si->sn].sflags & SDISTANT) |
103 |
|
|
return(FHUGE); |
104 |
|
|
si->dom /= d*d; |
105 |
|
|
return(d); /* sample OK, return distance */ |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
|
109 |
|
|
skipparts(ct, sz, pp, pt) /* skip to requested partition */ |
110 |
|
|
int ct[3], sz[3]; /* center and size of partition (returned) */ |
111 |
|
|
register int pp[2]; /* current index, number to skip (modified) */ |
112 |
|
|
unsigned char *pt; /* partition array */ |
113 |
|
|
{ |
114 |
|
|
register int p; |
115 |
|
|
/* check this partition */ |
116 |
|
|
p = spart(pt, pp[0]); |
117 |
|
|
pp[0]++; |
118 |
|
|
if (p == S0) /* leaf partition */ |
119 |
|
|
if (pp[1]) { |
120 |
|
|
pp[1]--; |
121 |
|
|
return(0); /* not there yet */ |
122 |
|
|
} else |
123 |
|
|
return(1); /* we've arrived */ |
124 |
|
|
/* else check lower */ |
125 |
|
|
sz[p] >>= 1; |
126 |
|
|
ct[p] -= sz[p]; |
127 |
|
|
if (skipparts(ct, sz, pp, pt)) |
128 |
|
|
return(1); /* return hit */ |
129 |
|
|
/* else check upper */ |
130 |
|
|
ct[p] += sz[p] << 1; |
131 |
|
|
if (skipparts(ct, sz, pp, pt)) |
132 |
|
|
return(1); /* return hit */ |
133 |
|
|
/* else return to starting position */ |
134 |
|
|
ct[p] -= sz[p]; |
135 |
|
|
sz[p] <<= 1; |
136 |
|
|
return(0); /* return miss */ |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
140 |
|
|
nopart(si, ro) /* single source partition */ |
141 |
|
|
register SRCINDEX *si; |
142 |
|
|
FVECT ro; |
143 |
|
|
{ |
144 |
|
|
clrpart(si->spt); |
145 |
|
|
setpart(si->spt, 0, S0); |
146 |
|
|
si->np = 1; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
cylpart(si, ro) /* partition a cylinder */ |
151 |
|
|
SRCINDEX *si; |
152 |
|
|
FVECT ro; |
153 |
|
|
{ |
154 |
|
|
double dist2, safedist2, dist2cent, rad2; |
155 |
|
|
FVECT v; |
156 |
|
|
register SRCREC *sp; |
157 |
|
|
int pi; |
158 |
|
|
/* first check point location */ |
159 |
|
|
clrpart(si->spt); |
160 |
|
|
sp = &source[si->sn]; |
161 |
greg |
1.3 |
rad2 = 1.365 * DOT(sp->ss[SV],sp->ss[SV]); |
162 |
greg |
1.1 |
v[0] = ro[0] - sp->sloc[0]; |
163 |
|
|
v[1] = ro[1] - sp->sloc[1]; |
164 |
|
|
v[2] = ro[2] - sp->sloc[2]; |
165 |
|
|
dist2 = DOT(v,sp->ss[SU]); |
166 |
|
|
safedist2 = DOT(sp->ss[SU],sp->ss[SU]); |
167 |
|
|
dist2 *= dist2 / safedist2; |
168 |
|
|
dist2cent = DOT(v,v); |
169 |
|
|
dist2 = dist2cent - dist2; |
170 |
|
|
if (dist2 <= rad2) { /* point inside extended cylinder */ |
171 |
|
|
si->np = 0; |
172 |
|
|
return; |
173 |
|
|
} |
174 |
|
|
safedist2 *= 4./(srcsizerat*srcsizerat); |
175 |
|
|
if (dist2 <= 4.*rad2 || /* point too close to subdivide? */ |
176 |
|
|
dist2cent >= safedist2) { |
177 |
|
|
setpart(si->spt, 0, S0); |
178 |
|
|
si->np = 1; |
179 |
|
|
return; |
180 |
|
|
} |
181 |
|
|
pi = 0; |
182 |
|
|
si->np = cyl_partit(ro, si->spt, &pi, MAXSPART, |
183 |
|
|
sp->sloc, sp->ss[SU], safedist2); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
static int |
188 |
|
|
cyl_partit(ro, pt, pi, mp, cent, axis, d2) /* slice a cylinder */ |
189 |
|
|
FVECT ro; |
190 |
|
|
unsigned char *pt; |
191 |
|
|
register int *pi; |
192 |
|
|
int mp; |
193 |
|
|
FVECT cent, axis; |
194 |
|
|
double d2; |
195 |
|
|
{ |
196 |
|
|
FVECT newct, newax; |
197 |
|
|
int npl, npu; |
198 |
|
|
|
199 |
|
|
if (mp < 2 || dist2(ro, cent) >= d2) { /* hit limit? */ |
200 |
|
|
setpart(pt, *pi, S0); |
201 |
|
|
(*pi)++; |
202 |
|
|
return(1); |
203 |
|
|
} |
204 |
|
|
/* subdivide */ |
205 |
|
|
setpart(pt, *pi, SU); |
206 |
|
|
(*pi)++; |
207 |
|
|
newax[0] = .5*axis[0]; |
208 |
|
|
newax[1] = .5*axis[1]; |
209 |
|
|
newax[2] = .5*axis[2]; |
210 |
|
|
d2 *= 0.25; |
211 |
|
|
/* lower half */ |
212 |
|
|
newct[0] = cent[0] - newax[0]; |
213 |
|
|
newct[1] = cent[1] - newax[1]; |
214 |
|
|
newct[2] = cent[2] - newax[2]; |
215 |
greg |
1.2 |
npl = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2); |
216 |
greg |
1.1 |
/* upper half */ |
217 |
|
|
newct[0] = cent[0] + newax[0]; |
218 |
|
|
newct[1] = cent[1] + newax[1]; |
219 |
|
|
newct[2] = cent[2] + newax[2]; |
220 |
greg |
1.2 |
npu = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2); |
221 |
greg |
1.1 |
/* return total */ |
222 |
|
|
return(npl + npu); |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
|
226 |
|
|
flatpart(si, ro) /* partition a flat source */ |
227 |
|
|
register SRCINDEX *si; |
228 |
|
|
FVECT ro; |
229 |
|
|
{ |
230 |
|
|
register double *vp; |
231 |
|
|
double du2, dv2; |
232 |
|
|
int pi; |
233 |
|
|
|
234 |
|
|
vp = source[si->sn].ss[SU]; |
235 |
|
|
du2 = 4./(srcsizerat*srcsizerat) * DOT(vp,vp); |
236 |
|
|
vp = source[si->sn].ss[SV]; |
237 |
|
|
dv2 = 4./(srcsizerat*srcsizerat) * DOT(vp,vp); |
238 |
|
|
clrpart(si->spt); |
239 |
|
|
pi = 0; |
240 |
|
|
si->np = flt_partit(ro, si->spt, &pi, MAXSPART, source[si->sn].sloc, |
241 |
|
|
source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
|
|
static int |
246 |
|
|
flt_partit(ro, pt, pi, mp, cent, u, v, du2, dv2) /* partition flatty */ |
247 |
|
|
FVECT ro; |
248 |
|
|
unsigned char *pt; |
249 |
|
|
register int *pi; |
250 |
|
|
int mp; |
251 |
|
|
FVECT cent, u, v; |
252 |
|
|
double du2, dv2; |
253 |
|
|
{ |
254 |
|
|
double d2; |
255 |
|
|
FVECT newct, newax; |
256 |
|
|
int npl, npu; |
257 |
|
|
|
258 |
|
|
if (mp < 2 || ((d2 = dist2(ro, cent)) >= du2 |
259 |
|
|
&& d2 >= dv2)) { /* hit limit? */ |
260 |
|
|
setpart(pt, *pi, S0); |
261 |
|
|
(*pi)++; |
262 |
|
|
return(1); |
263 |
|
|
} |
264 |
|
|
if (du2 > dv2) { /* subdivide in U */ |
265 |
|
|
setpart(pt, *pi, SU); |
266 |
|
|
(*pi)++; |
267 |
|
|
newax[0] = .5*u[0]; |
268 |
|
|
newax[1] = .5*u[1]; |
269 |
|
|
newax[2] = .5*u[2]; |
270 |
|
|
u = newax; |
271 |
|
|
du2 *= 0.25; |
272 |
|
|
} else { /* subdivide in V */ |
273 |
|
|
setpart(pt, *pi, SV); |
274 |
|
|
(*pi)++; |
275 |
|
|
newax[0] = .5*v[0]; |
276 |
|
|
newax[1] = .5*v[1]; |
277 |
|
|
newax[2] = .5*v[2]; |
278 |
|
|
v = newax; |
279 |
|
|
dv2 *= 0.25; |
280 |
|
|
} |
281 |
|
|
/* lower half */ |
282 |
|
|
newct[0] = cent[0] - newax[0]; |
283 |
|
|
newct[1] = cent[1] - newax[1]; |
284 |
|
|
newct[2] = cent[2] - newax[2]; |
285 |
greg |
1.2 |
npl = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2); |
286 |
greg |
1.1 |
/* upper half */ |
287 |
|
|
newct[0] = cent[0] + newax[0]; |
288 |
|
|
newct[1] = cent[1] + newax[1]; |
289 |
|
|
newct[2] = cent[2] + newax[2]; |
290 |
greg |
1.2 |
npu = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2); |
291 |
greg |
1.1 |
/* return total */ |
292 |
|
|
return(npl + npu); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
|
296 |
|
|
double |
297 |
|
|
scylform(sn, dir) /* compute cosine for cylinder's projection */ |
298 |
|
|
int sn; |
299 |
|
|
register FVECT dir; /* assume normalized */ |
300 |
|
|
{ |
301 |
|
|
register double *dv; |
302 |
|
|
double d; |
303 |
|
|
|
304 |
|
|
dv = source[sn].ss[SU]; |
305 |
|
|
d = DOT(dir, dv); |
306 |
|
|
d *= d / DOT(dv,dv); |
307 |
|
|
return(sqrt(1. - d)); |
308 |
|
|
} |