| 8 |
|
* Source sampling routines |
| 9 |
|
*/ |
| 10 |
|
|
| 11 |
< |
#include "standard.h" |
| 11 |
> |
#include "ray.h" |
| 12 |
|
|
| 13 |
– |
#include "object.h" |
| 14 |
– |
|
| 13 |
|
#include "source.h" |
| 14 |
|
|
| 15 |
|
#include "random.h" |
| 16 |
|
|
| 17 |
|
|
| 18 |
< |
extern int dimlist[]; /* dimension list for distribution */ |
| 21 |
< |
extern int ndims; /* number of dimensions so far */ |
| 22 |
< |
extern int samplendx; /* index for this sample */ |
| 18 |
> |
static int cyl_partit(), flt_partit(); |
| 19 |
|
|
| 20 |
|
|
| 21 |
|
double |
| 22 |
< |
nextssamp(org, dir, si) /* compute sample for source, rtn. distance */ |
| 23 |
< |
FVECT org, dir; /* origin is read only, direction is set */ |
| 22 |
> |
nextssamp(r, si) /* compute sample for source, rtn. distance */ |
| 23 |
> |
register RAY *r; /* origin is read, direction is set */ |
| 24 |
|
register SRCINDEX *si; /* source index (modified to current) */ |
| 25 |
|
{ |
| 26 |
|
int cent[3], size[3], parr[2]; |
| 27 |
|
FVECT vpos; |
| 28 |
|
double d; |
| 29 |
|
register int i; |
| 30 |
< |
tryagain: |
| 30 |
> |
nextsample: |
| 31 |
|
while (++si->sp >= si->np) { /* get next sample */ |
| 32 |
|
if (++si->sn >= nsources) |
| 33 |
|
return(0.0); /* no more */ |
| 34 |
< |
if (srcsizerat <= FTINY) |
| 35 |
< |
nopart(si, org); |
| 34 |
> |
if (source[si->sn].sflags & SSKIP) |
| 35 |
> |
si->np = 0; |
| 36 |
> |
else if (srcsizerat <= FTINY) |
| 37 |
> |
nopart(si, r); |
| 38 |
|
else { |
| 39 |
|
for (i = si->sn; source[i].sflags & SVIRTUAL; |
| 40 |
|
i = source[i].sa.sv.sn) |
| 41 |
|
; /* partition source */ |
| 42 |
< |
(*sfun[source[i].so->otype].of->partit)(si, org); |
| 42 |
> |
(*sfun[source[i].so->otype].of->partit)(si, r); |
| 43 |
|
} |
| 44 |
|
si->sp = -1; |
| 45 |
|
} |
| 69 |
|
vpos[i] += (double)cent[i]/MAXSPART; |
| 70 |
|
/* compute direction */ |
| 71 |
|
for (i = 0; i < 3; i++) |
| 72 |
< |
dir[i] = source[si->sn].sloc[i] + |
| 72 |
> |
r->rdir[i] = source[si->sn].sloc[i] + |
| 73 |
|
vpos[SU]*source[si->sn].ss[SU][i] + |
| 74 |
|
vpos[SV]*source[si->sn].ss[SV][i] + |
| 75 |
|
vpos[SW]*source[si->sn].ss[SW][i]; |
| 76 |
|
|
| 77 |
|
if (!(source[si->sn].sflags & SDISTANT)) |
| 78 |
|
for (i = 0; i < 3; i++) |
| 79 |
< |
dir[i] -= org[i]; |
| 79 |
> |
r->rdir[i] -= r->rorg[i]; |
| 80 |
|
/* compute distance */ |
| 81 |
< |
if ((d = normalize(dir)) == 0.0) |
| 82 |
< |
goto tryagain; /* at source! */ |
| 81 |
> |
if ((d = normalize(r->rdir)) == 0.0) |
| 82 |
> |
goto nextsample; /* at source! */ |
| 83 |
|
|
| 84 |
|
/* compute sample size */ |
| 85 |
|
si->dom = source[si->sn].ss2; |
| 86 |
|
if (source[si->sn].sflags & SFLAT) { |
| 87 |
< |
si->dom *= sflatform(si->sn, dir); |
| 88 |
< |
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); |
| 87 |
> |
si->dom *= sflatform(si->sn, r->rdir); |
| 88 |
> |
si->dom *= size[SU]*size[SV]/(MAXSPART*(double)MAXSPART); |
| 89 |
|
} else if (source[si->sn].sflags & SCYL) { |
| 90 |
< |
si->dom *= scylform(si->sn, dir); |
| 91 |
< |
si->dom *= (double)size[SU]/MAXSPART; |
| 90 |
> |
si->dom *= scylform(si->sn, r->rdir); |
| 91 |
> |
si->dom *= size[SU]/(double)MAXSPART; |
| 92 |
|
} else { |
| 93 |
< |
si->dom *= (double)(size[SU]*size[SV]*size[SW]) / |
| 94 |
< |
(MAXSPART*MAXSPART*MAXSPART) ; |
| 93 |
> |
si->dom *= size[SU]*size[SV]*(double)size[SW] / |
| 94 |
> |
(MAXSPART*MAXSPART*(double)MAXSPART) ; |
| 95 |
|
} |
| 96 |
|
if (source[si->sn].sflags & SDISTANT) |
| 97 |
|
return(FHUGE); |
| 98 |
+ |
if (si->dom <= 1e-4) |
| 99 |
+ |
goto nextsample; /* behind source? */ |
| 100 |
|
si->dom /= d*d; |
| 101 |
|
return(d); /* sample OK, return distance */ |
| 102 |
|
} |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
|
| 136 |
< |
nopart(si, ro) /* single source partition */ |
| 136 |
> |
nopart(si, r) /* single source partition */ |
| 137 |
|
register SRCINDEX *si; |
| 138 |
< |
FVECT ro; |
| 138 |
> |
RAY *r; |
| 139 |
|
{ |
| 140 |
|
clrpart(si->spt); |
| 141 |
|
setpart(si->spt, 0, S0); |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
< |
cylpart(si, ro) /* partition a cylinder */ |
| 146 |
> |
cylpart(si, r) /* partition a cylinder */ |
| 147 |
|
SRCINDEX *si; |
| 148 |
< |
FVECT ro; |
| 148 |
> |
register RAY *r; |
| 149 |
|
{ |
| 150 |
|
double dist2, safedist2, dist2cent, rad2; |
| 151 |
|
FVECT v; |
| 153 |
|
int pi; |
| 154 |
|
/* first check point location */ |
| 155 |
|
clrpart(si->spt); |
| 156 |
< |
sp = &source[si->sn]; |
| 157 |
< |
rad2 = 1.273 * DOT(sp->ss[SV],sp->ss[SV]); |
| 158 |
< |
v[0] = ro[0] - sp->sloc[0]; |
| 159 |
< |
v[1] = ro[1] - sp->sloc[1]; |
| 160 |
< |
v[2] = ro[2] - sp->sloc[2]; |
| 156 |
> |
sp = source + si->sn; |
| 157 |
> |
rad2 = 1.365 * DOT(sp->ss[SV],sp->ss[SV]); |
| 158 |
> |
v[0] = r->rorg[0] - sp->sloc[0]; |
| 159 |
> |
v[1] = r->rorg[1] - sp->sloc[1]; |
| 160 |
> |
v[2] = r->rorg[2] - sp->sloc[2]; |
| 161 |
|
dist2 = DOT(v,sp->ss[SU]); |
| 162 |
|
safedist2 = DOT(sp->ss[SU],sp->ss[SU]); |
| 163 |
|
dist2 *= dist2 / safedist2; |
| 167 |
|
si->np = 0; |
| 168 |
|
return; |
| 169 |
|
} |
| 170 |
< |
safedist2 *= 4./(srcsizerat*srcsizerat); |
| 171 |
< |
if (dist2 <= 4.*rad2 || /* point too close to subdivide? */ |
| 172 |
< |
dist2cent >= safedist2) { |
| 170 |
> |
safedist2 *= 4.*r->rweight*r->rweight/(srcsizerat*srcsizerat); |
| 171 |
> |
if (dist2 <= 4.*rad2 || /* point too close to subdivide */ |
| 172 |
> |
dist2cent >= safedist2) { /* or too far */ |
| 173 |
|
setpart(si->spt, 0, S0); |
| 174 |
|
si->np = 1; |
| 175 |
|
return; |
| 176 |
|
} |
| 177 |
|
pi = 0; |
| 178 |
< |
si->np = cyl_partit(ro, si->spt, &pi, MAXSPART, |
| 178 |
> |
si->np = cyl_partit(r->rorg, si->spt, &pi, MAXSPART, |
| 179 |
|
sp->sloc, sp->ss[SU], safedist2); |
| 180 |
|
} |
| 181 |
|
|
| 208 |
|
newct[0] = cent[0] - newax[0]; |
| 209 |
|
newct[1] = cent[1] - newax[1]; |
| 210 |
|
newct[2] = cent[2] - newax[2]; |
| 211 |
< |
npl = cyl_partit(ro, pt, pi, mp*3/4, newct, newax, d2); |
| 211 |
> |
npl = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2); |
| 212 |
|
/* upper half */ |
| 213 |
|
newct[0] = cent[0] + newax[0]; |
| 214 |
|
newct[1] = cent[1] + newax[1]; |
| 215 |
|
newct[2] = cent[2] + newax[2]; |
| 216 |
< |
npu = cyl_partit(ro, pt, pi, mp-npl, newct, newax, d2); |
| 216 |
> |
npu = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2); |
| 217 |
|
/* return total */ |
| 218 |
|
return(npl + npu); |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
|
| 222 |
< |
flatpart(si, ro) /* partition a flat source */ |
| 222 |
> |
flatpart(si, r) /* partition a flat source */ |
| 223 |
|
register SRCINDEX *si; |
| 224 |
< |
FVECT ro; |
| 224 |
> |
register RAY *r; |
| 225 |
|
{ |
| 226 |
< |
register double *vp; |
| 226 |
> |
register FLOAT *vp; |
| 227 |
> |
FVECT v; |
| 228 |
|
double du2, dv2; |
| 229 |
|
int pi; |
| 230 |
|
|
| 231 |
+ |
clrpart(si->spt); |
| 232 |
+ |
vp = source[si->sn].sloc; |
| 233 |
+ |
v[0] = r->rorg[0] - vp[0]; |
| 234 |
+ |
v[1] = r->rorg[1] - vp[1]; |
| 235 |
+ |
v[2] = r->rorg[2] - vp[2]; |
| 236 |
+ |
vp = source[si->sn].snorm; |
| 237 |
+ |
if (DOT(v,vp) <= FTINY) { /* behind source */ |
| 238 |
+ |
si->np = 0; |
| 239 |
+ |
return; |
| 240 |
+ |
} |
| 241 |
+ |
dv2 = 2.*r->rweight/srcsizerat; |
| 242 |
+ |
dv2 *= dv2; |
| 243 |
|
vp = source[si->sn].ss[SU]; |
| 244 |
< |
du2 = 4./(srcsizerat*srcsizerat) * DOT(vp,vp); |
| 244 |
> |
du2 = dv2 * DOT(vp,vp); |
| 245 |
|
vp = source[si->sn].ss[SV]; |
| 246 |
< |
dv2 = 4./(srcsizerat*srcsizerat) * DOT(vp,vp); |
| 238 |
< |
clrpart(si->spt); |
| 246 |
> |
dv2 *= DOT(vp,vp); |
| 247 |
|
pi = 0; |
| 248 |
< |
si->np = flt_partit(ro, si->spt, &pi, MAXSPART, source[si->sn].sloc, |
| 248 |
> |
si->np = flt_partit(r->rorg, si->spt, &pi, MAXSPART, |
| 249 |
> |
source[si->sn].sloc, |
| 250 |
|
source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2); |
| 251 |
|
} |
| 252 |
|
|
| 291 |
|
newct[0] = cent[0] - newax[0]; |
| 292 |
|
newct[1] = cent[1] - newax[1]; |
| 293 |
|
newct[2] = cent[2] - newax[2]; |
| 294 |
< |
npl = flt_partit(ro, pt, pi, mp*3/4, newct, u, v, du2, dv2); |
| 294 |
> |
npl = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2); |
| 295 |
|
/* upper half */ |
| 296 |
|
newct[0] = cent[0] + newax[0]; |
| 297 |
|
newct[1] = cent[1] + newax[1]; |
| 298 |
|
newct[2] = cent[2] + newax[2]; |
| 299 |
< |
npu = flt_partit(ro, pt, pi, mp-npl, newct, u, v, du2, dv2); |
| 299 |
> |
npu = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2); |
| 300 |
|
/* return total */ |
| 301 |
|
return(npl + npu); |
| 302 |
|
} |
| 307 |
|
int sn; |
| 308 |
|
register FVECT dir; /* assume normalized */ |
| 309 |
|
{ |
| 310 |
< |
register double *dv; |
| 310 |
> |
register FLOAT *dv; |
| 311 |
|
double d; |
| 312 |
|
|
| 313 |
|
dv = source[sn].ss[SU]; |