1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Source sampling routines |
6 |
+ |
* |
7 |
+ |
* External symbols declared in source.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include "standard.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "object.h" |
12 |
> |
#include "ray.h" |
13 |
|
|
14 |
|
#include "source.h" |
15 |
|
|
16 |
|
#include "random.h" |
17 |
|
|
18 |
|
|
19 |
< |
extern int dimlist[]; /* dimension list for distribution */ |
21 |
< |
extern int ndims; /* number of dimensions so far */ |
22 |
< |
extern int samplendx; /* index for this sample */ |
19 |
> |
static int cyl_partit(), flt_partit(); |
20 |
|
|
21 |
|
|
22 |
|
double |
23 |
< |
nextssamp(org, dir, si) /* compute sample for source, rtn. distance */ |
24 |
< |
FVECT org, dir; /* origin is read only, direction is set */ |
23 |
> |
nextssamp(r, si) /* compute sample for source, rtn. distance */ |
24 |
> |
register RAY *r; /* origin is read, direction is set */ |
25 |
|
register SRCINDEX *si; /* source index (modified to current) */ |
26 |
|
{ |
27 |
|
int cent[3], size[3], parr[2]; |
28 |
|
FVECT vpos; |
29 |
|
double d; |
30 |
|
register int i; |
31 |
< |
tryagain: |
31 |
> |
nextsample: |
32 |
|
while (++si->sp >= si->np) { /* get next sample */ |
33 |
|
if (++si->sn >= nsources) |
34 |
|
return(0.0); /* no more */ |
35 |
< |
if (srcsizerat <= FTINY) |
36 |
< |
nopart(si, org); |
35 |
> |
if (source[si->sn].sflags & SSKIP) |
36 |
> |
si->np = 0; |
37 |
> |
else if (srcsizerat <= FTINY) |
38 |
> |
nopart(si, r); |
39 |
|
else { |
40 |
|
for (i = si->sn; source[i].sflags & SVIRTUAL; |
41 |
|
i = source[i].sa.sv.sn) |
42 |
|
; /* partition source */ |
43 |
< |
(*sfun[source[i].so->otype].of->partit)(si, org); |
43 |
> |
(*sfun[source[i].so->otype].of->partit)(si, r); |
44 |
|
} |
45 |
|
si->sp = -1; |
46 |
|
} |
57 |
|
d = urand(ilhash(dimlist,ndims+2)+samplendx); |
58 |
|
if (source[si->sn].sflags & SFLAT) { |
59 |
|
multisamp(vpos, 2, d); |
60 |
< |
vpos[2] = 0.5; |
60 |
> |
vpos[SW] = 0.5; |
61 |
|
} else |
62 |
|
multisamp(vpos, 3, d); |
63 |
|
for (i = 0; i < 3; i++) |
68 |
|
|
69 |
|
for (i = 0; i < 3; i++) |
70 |
|
vpos[i] += (double)cent[i]/MAXSPART; |
71 |
+ |
/* avoid circular aiming failures */ |
72 |
+ |
if ((source[si->sn].sflags & SCIR) && (si->np > 1 || dstrsrc > 0.7)) { |
73 |
+ |
FVECT trim; |
74 |
+ |
if (source[si->sn].sflags & (SFLAT|SDISTANT)) { |
75 |
+ |
d = 1.12837917; /* correct setflatss() */ |
76 |
+ |
trim[SU] = d*sqrt(1.0 - 0.5*vpos[SV]*vpos[SV]); |
77 |
+ |
trim[SV] = d*sqrt(1.0 - 0.5*vpos[SU]*vpos[SU]); |
78 |
+ |
trim[SW] = 0.0; |
79 |
+ |
} else { |
80 |
+ |
trim[SW] = trim[SU] = vpos[SU]*vpos[SU]; |
81 |
+ |
d = vpos[SV]*vpos[SV]; |
82 |
+ |
if (d > trim[SW]) trim[SW] = d; |
83 |
+ |
trim[SU] += d; |
84 |
+ |
d = vpos[SW]*vpos[SW]; |
85 |
+ |
if (d > trim[SW]) trim[SW] = d; |
86 |
+ |
trim[SU] += d; |
87 |
+ |
d = 1.0/0.7236; /* correct sphsetsrc() */ |
88 |
+ |
trim[SW] = trim[SV] = trim[SU] = |
89 |
+ |
d*sqrt(trim[SW]/trim[SU]); |
90 |
+ |
} |
91 |
+ |
for (i = 0; i < 3; i++) |
92 |
+ |
vpos[i] *= trim[i]; |
93 |
+ |
} |
94 |
|
/* compute direction */ |
95 |
|
for (i = 0; i < 3; i++) |
96 |
< |
dir[i] = source[si->sn].sloc[i] + |
96 |
> |
r->rdir[i] = source[si->sn].sloc[i] + |
97 |
|
vpos[SU]*source[si->sn].ss[SU][i] + |
98 |
|
vpos[SV]*source[si->sn].ss[SV][i] + |
99 |
|
vpos[SW]*source[si->sn].ss[SW][i]; |
100 |
|
|
101 |
|
if (!(source[si->sn].sflags & SDISTANT)) |
102 |
|
for (i = 0; i < 3; i++) |
103 |
< |
dir[i] -= org[i]; |
103 |
> |
r->rdir[i] -= r->rorg[i]; |
104 |
|
/* compute distance */ |
105 |
< |
if ((d = normalize(dir)) == 0.0) |
106 |
< |
goto tryagain; /* at source! */ |
105 |
> |
if ((d = normalize(r->rdir)) == 0.0) |
106 |
> |
goto nextsample; /* at source! */ |
107 |
|
|
108 |
|
/* compute sample size */ |
87 |
– |
si->dom = source[si->sn].ss2; |
109 |
|
if (source[si->sn].sflags & SFLAT) { |
110 |
< |
si->dom *= sflatform(si->sn, dir); |
111 |
< |
if (si->dom <= FTINY) { /* behind source */ |
91 |
< |
si->sp = si->np; |
92 |
< |
goto tryagain; |
93 |
< |
} |
94 |
< |
si->dom *= (double)(size[SU]*size[SV])/(MAXSPART*MAXSPART); |
110 |
> |
si->dom = sflatform(si->sn, r->rdir); |
111 |
> |
si->dom *= size[SU]*size[SV]/(MAXSPART*(double)MAXSPART); |
112 |
|
} else if (source[si->sn].sflags & SCYL) { |
113 |
< |
si->dom *= scylform(si->sn, dir); |
114 |
< |
si->dom *= (double)size[SU]/MAXSPART; |
113 |
> |
si->dom = scylform(si->sn, r->rdir); |
114 |
> |
si->dom *= size[SU]/(double)MAXSPART; |
115 |
|
} else { |
116 |
< |
si->dom *= (double)(size[SU]*size[SV]*size[SW]) / |
117 |
< |
(MAXSPART*MAXSPART*MAXSPART) ; |
116 |
> |
si->dom = size[SU]*size[SV]*(double)size[SW] / |
117 |
> |
(MAXSPART*MAXSPART*(double)MAXSPART) ; |
118 |
|
} |
119 |
< |
if (source[si->sn].sflags & SDISTANT) |
119 |
> |
if (source[si->sn].sflags & SDISTANT) { |
120 |
> |
si->dom *= source[si->sn].ss2; |
121 |
|
return(FHUGE); |
122 |
< |
si->dom /= d*d; |
122 |
> |
} |
123 |
> |
if (si->dom <= 1e-4) |
124 |
> |
goto nextsample; /* behind source? */ |
125 |
> |
si->dom *= source[si->sn].ss2/(d*d); |
126 |
|
return(d); /* sample OK, return distance */ |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
+ |
int |
131 |
|
skipparts(ct, sz, pp, pt) /* skip to requested partition */ |
132 |
|
int ct[3], sz[3]; /* center and size of partition (returned) */ |
133 |
|
register int pp[2]; /* current index, number to skip (modified) */ |
137 |
|
/* check this partition */ |
138 |
|
p = spart(pt, pp[0]); |
139 |
|
pp[0]++; |
140 |
< |
if (p == S0) /* leaf partition */ |
140 |
> |
if (p == S0) { /* leaf partition */ |
141 |
|
if (pp[1]) { |
142 |
|
pp[1]--; |
143 |
|
return(0); /* not there yet */ |
144 |
|
} else |
145 |
|
return(1); /* we've arrived */ |
146 |
+ |
} |
147 |
|
/* else check lower */ |
148 |
|
sz[p] >>= 1; |
149 |
|
ct[p] -= sz[p]; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
< |
nopart(si, ro) /* single source partition */ |
163 |
> |
void |
164 |
> |
nopart(si, r) /* single source partition */ |
165 |
|
register SRCINDEX *si; |
166 |
< |
FVECT ro; |
166 |
> |
RAY *r; |
167 |
|
{ |
168 |
|
clrpart(si->spt); |
169 |
|
setpart(si->spt, 0, S0); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
< |
cylpart(si, ro) /* partition a cylinder */ |
174 |
> |
void |
175 |
> |
cylpart(si, r) /* partition a cylinder */ |
176 |
|
SRCINDEX *si; |
177 |
< |
FVECT ro; |
177 |
> |
register RAY *r; |
178 |
|
{ |
179 |
|
double dist2, safedist2, dist2cent, rad2; |
180 |
|
FVECT v; |
182 |
|
int pi; |
183 |
|
/* first check point location */ |
184 |
|
clrpart(si->spt); |
185 |
< |
sp = &source[si->sn]; |
186 |
< |
rad2 = 1.273 * DOT(sp->ss[SV],sp->ss[SV]); |
187 |
< |
v[0] = ro[0] - sp->sloc[0]; |
188 |
< |
v[1] = ro[1] - sp->sloc[1]; |
189 |
< |
v[2] = ro[2] - sp->sloc[2]; |
185 |
> |
sp = source + si->sn; |
186 |
> |
rad2 = 1.365 * DOT(sp->ss[SV],sp->ss[SV]); |
187 |
> |
v[0] = r->rorg[0] - sp->sloc[0]; |
188 |
> |
v[1] = r->rorg[1] - sp->sloc[1]; |
189 |
> |
v[2] = r->rorg[2] - sp->sloc[2]; |
190 |
|
dist2 = DOT(v,sp->ss[SU]); |
191 |
|
safedist2 = DOT(sp->ss[SU],sp->ss[SU]); |
192 |
|
dist2 *= dist2 / safedist2; |
196 |
|
si->np = 0; |
197 |
|
return; |
198 |
|
} |
199 |
< |
safedist2 *= 4./(srcsizerat*srcsizerat); |
200 |
< |
if (dist2 <= 4.*rad2 || /* point too close to subdivide? */ |
201 |
< |
dist2cent >= safedist2) { |
199 |
> |
safedist2 *= 4.*r->rweight*r->rweight/(srcsizerat*srcsizerat); |
200 |
> |
if (dist2 <= 4.*rad2 || /* point too close to subdivide */ |
201 |
> |
dist2cent >= safedist2) { /* or too far */ |
202 |
|
setpart(si->spt, 0, S0); |
203 |
|
si->np = 1; |
204 |
|
return; |
205 |
|
} |
206 |
|
pi = 0; |
207 |
< |
si->np = cyl_partit(ro, si->spt, &pi, MAXSPART, |
207 |
> |
si->np = cyl_partit(r->rorg, si->spt, &pi, MAXSPART, |
208 |
|
sp->sloc, sp->ss[SU], safedist2); |
209 |
|
} |
210 |
|
|
237 |
|
newct[0] = cent[0] - newax[0]; |
238 |
|
newct[1] = cent[1] - newax[1]; |
239 |
|
newct[2] = cent[2] - newax[2]; |
240 |
< |
npl = cyl_partit(ro, pt, pi, mp*3/4, newct, newax, d2); |
240 |
> |
npl = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2); |
241 |
|
/* upper half */ |
242 |
|
newct[0] = cent[0] + newax[0]; |
243 |
|
newct[1] = cent[1] + newax[1]; |
244 |
|
newct[2] = cent[2] + newax[2]; |
245 |
< |
npu = cyl_partit(ro, pt, pi, mp-npl, newct, newax, d2); |
245 |
> |
npu = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2); |
246 |
|
/* return total */ |
247 |
|
return(npl + npu); |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
< |
flatpart(si, ro) /* partition a flat source */ |
251 |
> |
void |
252 |
> |
flatpart(si, r) /* partition a flat source */ |
253 |
|
register SRCINDEX *si; |
254 |
< |
FVECT ro; |
254 |
> |
register RAY *r; |
255 |
|
{ |
256 |
< |
register double *vp; |
256 |
> |
register RREAL *vp; |
257 |
> |
FVECT v; |
258 |
|
double du2, dv2; |
259 |
|
int pi; |
260 |
|
|
261 |
+ |
clrpart(si->spt); |
262 |
+ |
vp = source[si->sn].sloc; |
263 |
+ |
v[0] = r->rorg[0] - vp[0]; |
264 |
+ |
v[1] = r->rorg[1] - vp[1]; |
265 |
+ |
v[2] = r->rorg[2] - vp[2]; |
266 |
+ |
vp = source[si->sn].snorm; |
267 |
+ |
if (DOT(v,vp) <= 0.) { /* behind source */ |
268 |
+ |
si->np = 0; |
269 |
+ |
return; |
270 |
+ |
} |
271 |
+ |
dv2 = 2.*r->rweight/srcsizerat; |
272 |
+ |
dv2 *= dv2; |
273 |
|
vp = source[si->sn].ss[SU]; |
274 |
< |
du2 = 4./(srcsizerat*srcsizerat) * DOT(vp,vp); |
274 |
> |
du2 = dv2 * DOT(vp,vp); |
275 |
|
vp = source[si->sn].ss[SV]; |
276 |
< |
dv2 = 4./(srcsizerat*srcsizerat) * DOT(vp,vp); |
238 |
< |
clrpart(si->spt); |
276 |
> |
dv2 *= DOT(vp,vp); |
277 |
|
pi = 0; |
278 |
< |
si->np = flt_partit(ro, si->spt, &pi, MAXSPART, source[si->sn].sloc, |
278 |
> |
si->np = flt_partit(r->rorg, si->spt, &pi, MAXSPART, |
279 |
> |
source[si->sn].sloc, |
280 |
|
source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2); |
281 |
|
} |
282 |
|
|
321 |
|
newct[0] = cent[0] - newax[0]; |
322 |
|
newct[1] = cent[1] - newax[1]; |
323 |
|
newct[2] = cent[2] - newax[2]; |
324 |
< |
npl = flt_partit(ro, pt, pi, mp*3/4, newct, u, v, du2, dv2); |
324 |
> |
npl = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2); |
325 |
|
/* upper half */ |
326 |
|
newct[0] = cent[0] + newax[0]; |
327 |
|
newct[1] = cent[1] + newax[1]; |
328 |
|
newct[2] = cent[2] + newax[2]; |
329 |
< |
npu = flt_partit(ro, pt, pi, mp-npl, newct, u, v, du2, dv2); |
329 |
> |
npu = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2); |
330 |
|
/* return total */ |
331 |
|
return(npl + npu); |
332 |
|
} |
337 |
|
int sn; |
338 |
|
register FVECT dir; /* assume normalized */ |
339 |
|
{ |
340 |
< |
register double *dv; |
340 |
> |
register RREAL *dv; |
341 |
|
double d; |
342 |
|
|
343 |
|
dv = source[sn].ss[SU]; |