ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsamp.c
Revision: 2.13
Committed: Sun Dec 7 19:25:23 2008 UTC (15 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +2 -2 lines
Log Message:
Made new sampling code conditional for speed in -dj 0 case

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: srcsamp.c,v 2.12 2008/12/06 01:08:53 greg Exp $";
3 #endif
4 /*
5 * Source sampling routines
6 *
7 * External symbols declared in source.h
8 */
9
10 #include "copyright.h"
11
12 #include "ray.h"
13
14 #include "source.h"
15
16 #include "random.h"
17
18
19 static int cyl_partit(), flt_partit();
20
21
22 double
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 nextsample:
32 while (++si->sp >= si->np) { /* get next sample */
33 if (++si->sn >= nsources)
34 return(0.0); /* no more */
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, r);
44 }
45 si->sp = -1;
46 }
47 /* get partition */
48 cent[0] = cent[1] = cent[2] = 0;
49 size[0] = size[1] = size[2] = MAXSPART;
50 parr[0] = 0; parr[1] = si->sp;
51 if (!skipparts(cent, size, parr, si->spt))
52 error(CONSISTENCY, "bad source partition in nextssamp");
53 /* compute sample */
54 if (dstrsrc > FTINY) { /* jitter sample */
55 dimlist[ndims] = si->sn + 8831;
56 dimlist[ndims+1] = si->sp + 3109;
57 d = urand(ilhash(dimlist,ndims+2)+samplendx);
58 if (source[si->sn].sflags & SFLAT) {
59 multisamp(vpos, 2, d);
60 vpos[SW] = 0.5;
61 } else
62 multisamp(vpos, 3, d);
63 for (i = 0; i < 3; i++)
64 vpos[i] = dstrsrc * (1. - 2.*vpos[i]) *
65 (double)size[i]/MAXSPART;
66 } else
67 vpos[0] = vpos[1] = vpos[2] = 0.0;
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 double d;
75 if (source[si->sn].sflags & (SFLAT|SDISTANT)) {
76 d = 1.12837917; /* correct setflatss() */
77 trim[SU] = d*sqrt(1.0 - 0.5*vpos[SV]*vpos[SV]);
78 trim[SV] = d*sqrt(1.0 - 0.5*vpos[SU]*vpos[SU]);
79 trim[SW] = 0.0;
80 } else {
81 trim[SW] = trim[SU] = vpos[SU]*vpos[SU];
82 d = vpos[SV]*vpos[SV];
83 if (d > trim[SW]) trim[SW] = d;
84 trim[SU] += d;
85 d = vpos[SW]*vpos[SW];
86 if (d > trim[SW]) trim[SW] = d;
87 trim[SU] += d;
88 d = 1.0/0.7236; /* correct sphsetsrc() */
89 trim[SW] = trim[SV] = trim[SU] =
90 d*sqrt(trim[SW]/trim[SU]);
91 }
92 for (i = 0; i < 3; i++)
93 vpos[i] *= trim[i];
94 }
95 /* compute direction */
96 for (i = 0; i < 3; i++)
97 r->rdir[i] = source[si->sn].sloc[i] +
98 vpos[SU]*source[si->sn].ss[SU][i] +
99 vpos[SV]*source[si->sn].ss[SV][i] +
100 vpos[SW]*source[si->sn].ss[SW][i];
101
102 if (!(source[si->sn].sflags & SDISTANT))
103 for (i = 0; i < 3; i++)
104 r->rdir[i] -= r->rorg[i];
105 /* compute distance */
106 if ((d = normalize(r->rdir)) == 0.0)
107 goto nextsample; /* at source! */
108
109 /* compute sample size */
110 if (source[si->sn].sflags & SFLAT) {
111 si->dom = sflatform(si->sn, r->rdir);
112 si->dom *= size[SU]*size[SV]/(MAXSPART*(double)MAXSPART);
113 } else if (source[si->sn].sflags & SCYL) {
114 si->dom = scylform(si->sn, r->rdir);
115 si->dom *= size[SU]/(double)MAXSPART;
116 } else {
117 si->dom = size[SU]*size[SV]*(double)size[SW] /
118 (MAXSPART*MAXSPART*(double)MAXSPART) ;
119 }
120 if (source[si->sn].sflags & SDISTANT) {
121 si->dom *= source[si->sn].ss2;
122 return(FHUGE);
123 }
124 if (si->dom <= 1e-4)
125 goto nextsample; /* behind source? */
126 si->dom *= source[si->sn].ss2/(d*d);
127 return(d); /* sample OK, return distance */
128 }
129
130
131 int
132 skipparts(ct, sz, pp, pt) /* skip to requested partition */
133 int ct[3], sz[3]; /* center and size of partition (returned) */
134 register int pp[2]; /* current index, number to skip (modified) */
135 unsigned char *pt; /* partition array */
136 {
137 register int p;
138 /* check this partition */
139 p = spart(pt, pp[0]);
140 pp[0]++;
141 if (p == S0) { /* leaf partition */
142 if (pp[1]) {
143 pp[1]--;
144 return(0); /* not there yet */
145 } else
146 return(1); /* we've arrived */
147 }
148 /* else check lower */
149 sz[p] >>= 1;
150 ct[p] -= sz[p];
151 if (skipparts(ct, sz, pp, pt))
152 return(1); /* return hit */
153 /* else check upper */
154 ct[p] += sz[p] << 1;
155 if (skipparts(ct, sz, pp, pt))
156 return(1); /* return hit */
157 /* else return to starting position */
158 ct[p] -= sz[p];
159 sz[p] <<= 1;
160 return(0); /* return miss */
161 }
162
163
164 void
165 nopart(si, r) /* single source partition */
166 register SRCINDEX *si;
167 RAY *r;
168 {
169 clrpart(si->spt);
170 setpart(si->spt, 0, S0);
171 si->np = 1;
172 }
173
174
175 void
176 cylpart(si, r) /* partition a cylinder */
177 SRCINDEX *si;
178 register RAY *r;
179 {
180 double dist2, safedist2, dist2cent, rad2;
181 FVECT v;
182 register SRCREC *sp;
183 int pi;
184 /* first check point location */
185 clrpart(si->spt);
186 sp = source + si->sn;
187 rad2 = 1.365 * DOT(sp->ss[SV],sp->ss[SV]);
188 v[0] = r->rorg[0] - sp->sloc[0];
189 v[1] = r->rorg[1] - sp->sloc[1];
190 v[2] = r->rorg[2] - sp->sloc[2];
191 dist2 = DOT(v,sp->ss[SU]);
192 safedist2 = DOT(sp->ss[SU],sp->ss[SU]);
193 dist2 *= dist2 / safedist2;
194 dist2cent = DOT(v,v);
195 dist2 = dist2cent - dist2;
196 if (dist2 <= rad2) { /* point inside extended cylinder */
197 si->np = 0;
198 return;
199 }
200 safedist2 *= 4.*r->rweight*r->rweight/(srcsizerat*srcsizerat);
201 if (dist2 <= 4.*rad2 || /* point too close to subdivide */
202 dist2cent >= safedist2) { /* or too far */
203 setpart(si->spt, 0, S0);
204 si->np = 1;
205 return;
206 }
207 pi = 0;
208 si->np = cyl_partit(r->rorg, si->spt, &pi, MAXSPART,
209 sp->sloc, sp->ss[SU], safedist2);
210 }
211
212
213 static int
214 cyl_partit(ro, pt, pi, mp, cent, axis, d2) /* slice a cylinder */
215 FVECT ro;
216 unsigned char *pt;
217 register int *pi;
218 int mp;
219 FVECT cent, axis;
220 double d2;
221 {
222 FVECT newct, newax;
223 int npl, npu;
224
225 if (mp < 2 || dist2(ro, cent) >= d2) { /* hit limit? */
226 setpart(pt, *pi, S0);
227 (*pi)++;
228 return(1);
229 }
230 /* subdivide */
231 setpart(pt, *pi, SU);
232 (*pi)++;
233 newax[0] = .5*axis[0];
234 newax[1] = .5*axis[1];
235 newax[2] = .5*axis[2];
236 d2 *= 0.25;
237 /* lower half */
238 newct[0] = cent[0] - newax[0];
239 newct[1] = cent[1] - newax[1];
240 newct[2] = cent[2] - newax[2];
241 npl = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2);
242 /* upper half */
243 newct[0] = cent[0] + newax[0];
244 newct[1] = cent[1] + newax[1];
245 newct[2] = cent[2] + newax[2];
246 npu = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2);
247 /* return total */
248 return(npl + npu);
249 }
250
251
252 void
253 flatpart(si, r) /* partition a flat source */
254 register SRCINDEX *si;
255 register RAY *r;
256 {
257 register RREAL *vp;
258 FVECT v;
259 double du2, dv2;
260 int pi;
261
262 clrpart(si->spt);
263 vp = source[si->sn].sloc;
264 v[0] = r->rorg[0] - vp[0];
265 v[1] = r->rorg[1] - vp[1];
266 v[2] = r->rorg[2] - vp[2];
267 vp = source[si->sn].snorm;
268 if (DOT(v,vp) <= 0.) { /* behind source */
269 si->np = 0;
270 return;
271 }
272 dv2 = 2.*r->rweight/srcsizerat;
273 dv2 *= dv2;
274 vp = source[si->sn].ss[SU];
275 du2 = dv2 * DOT(vp,vp);
276 vp = source[si->sn].ss[SV];
277 dv2 *= DOT(vp,vp);
278 pi = 0;
279 si->np = flt_partit(r->rorg, si->spt, &pi, MAXSPART,
280 source[si->sn].sloc,
281 source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2);
282 }
283
284
285 static int
286 flt_partit(ro, pt, pi, mp, cent, u, v, du2, dv2) /* partition flatty */
287 FVECT ro;
288 unsigned char *pt;
289 register int *pi;
290 int mp;
291 FVECT cent, u, v;
292 double du2, dv2;
293 {
294 double d2;
295 FVECT newct, newax;
296 int npl, npu;
297
298 if (mp < 2 || ((d2 = dist2(ro, cent)) >= du2
299 && d2 >= dv2)) { /* hit limit? */
300 setpart(pt, *pi, S0);
301 (*pi)++;
302 return(1);
303 }
304 if (du2 > dv2) { /* subdivide in U */
305 setpart(pt, *pi, SU);
306 (*pi)++;
307 newax[0] = .5*u[0];
308 newax[1] = .5*u[1];
309 newax[2] = .5*u[2];
310 u = newax;
311 du2 *= 0.25;
312 } else { /* subdivide in V */
313 setpart(pt, *pi, SV);
314 (*pi)++;
315 newax[0] = .5*v[0];
316 newax[1] = .5*v[1];
317 newax[2] = .5*v[2];
318 v = newax;
319 dv2 *= 0.25;
320 }
321 /* lower half */
322 newct[0] = cent[0] - newax[0];
323 newct[1] = cent[1] - newax[1];
324 newct[2] = cent[2] - newax[2];
325 npl = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2);
326 /* upper half */
327 newct[0] = cent[0] + newax[0];
328 newct[1] = cent[1] + newax[1];
329 newct[2] = cent[2] + newax[2];
330 npu = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2);
331 /* return total */
332 return(npl + npu);
333 }
334
335
336 double
337 scylform(sn, dir) /* compute cosine for cylinder's projection */
338 int sn;
339 register FVECT dir; /* assume normalized */
340 {
341 register RREAL *dv;
342 double d;
343
344 dv = source[sn].ss[SU];
345 d = DOT(dir, dv);
346 d *= d / DOT(dv,dv);
347 return(sqrt(1. - d));
348 }