ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsamp.c
Revision: 2.9
Committed: Thu Jun 26 00:58:10 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.8: +3 -3 lines
Log Message:
Abstracted process and path handling for Windows.
Renamed FLOAT to RREAL because of conflict on Windows.
Added conditional compiles for some signal handlers.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: srcsamp.c,v 2.8 2003/02/25 02:47:23 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[2] = 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 /* compute direction */
72 for (i = 0; i < 3; i++)
73 r->rdir[i] = source[si->sn].sloc[i] +
74 vpos[SU]*source[si->sn].ss[SU][i] +
75 vpos[SV]*source[si->sn].ss[SV][i] +
76 vpos[SW]*source[si->sn].ss[SW][i];
77
78 if (!(source[si->sn].sflags & SDISTANT))
79 for (i = 0; i < 3; i++)
80 r->rdir[i] -= r->rorg[i];
81 /* compute distance */
82 if ((d = normalize(r->rdir)) == 0.0)
83 goto nextsample; /* at source! */
84
85 /* compute sample size */
86 if (source[si->sn].sflags & SFLAT) {
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, r->rdir);
91 si->dom *= size[SU]/(double)MAXSPART;
92 } else {
93 si->dom = size[SU]*size[SV]*(double)size[SW] /
94 (MAXSPART*MAXSPART*(double)MAXSPART) ;
95 }
96 if (source[si->sn].sflags & SDISTANT) {
97 si->dom *= source[si->sn].ss2;
98 return(FHUGE);
99 }
100 if (si->dom <= 1e-4)
101 goto nextsample; /* behind source? */
102 si->dom *= source[si->sn].ss2/(d*d);
103 return(d); /* sample OK, return distance */
104 }
105
106
107 int
108 skipparts(ct, sz, pp, pt) /* skip to requested partition */
109 int ct[3], sz[3]; /* center and size of partition (returned) */
110 register int pp[2]; /* current index, number to skip (modified) */
111 unsigned char *pt; /* partition array */
112 {
113 register int p;
114 /* check this partition */
115 p = spart(pt, pp[0]);
116 pp[0]++;
117 if (p == S0) /* leaf partition */
118 if (pp[1]) {
119 pp[1]--;
120 return(0); /* not there yet */
121 } else
122 return(1); /* we've arrived */
123 /* else check lower */
124 sz[p] >>= 1;
125 ct[p] -= sz[p];
126 if (skipparts(ct, sz, pp, pt))
127 return(1); /* return hit */
128 /* else check upper */
129 ct[p] += sz[p] << 1;
130 if (skipparts(ct, sz, pp, pt))
131 return(1); /* return hit */
132 /* else return to starting position */
133 ct[p] -= sz[p];
134 sz[p] <<= 1;
135 return(0); /* return miss */
136 }
137
138
139 void
140 nopart(si, r) /* single source partition */
141 register SRCINDEX *si;
142 RAY *r;
143 {
144 clrpart(si->spt);
145 setpart(si->spt, 0, S0);
146 si->np = 1;
147 }
148
149
150 void
151 cylpart(si, r) /* partition a cylinder */
152 SRCINDEX *si;
153 register RAY *r;
154 {
155 double dist2, safedist2, dist2cent, rad2;
156 FVECT v;
157 register SRCREC *sp;
158 int pi;
159 /* first check point location */
160 clrpart(si->spt);
161 sp = source + si->sn;
162 rad2 = 1.365 * DOT(sp->ss[SV],sp->ss[SV]);
163 v[0] = r->rorg[0] - sp->sloc[0];
164 v[1] = r->rorg[1] - sp->sloc[1];
165 v[2] = r->rorg[2] - sp->sloc[2];
166 dist2 = DOT(v,sp->ss[SU]);
167 safedist2 = DOT(sp->ss[SU],sp->ss[SU]);
168 dist2 *= dist2 / safedist2;
169 dist2cent = DOT(v,v);
170 dist2 = dist2cent - dist2;
171 if (dist2 <= rad2) { /* point inside extended cylinder */
172 si->np = 0;
173 return;
174 }
175 safedist2 *= 4.*r->rweight*r->rweight/(srcsizerat*srcsizerat);
176 if (dist2 <= 4.*rad2 || /* point too close to subdivide */
177 dist2cent >= safedist2) { /* or too far */
178 setpart(si->spt, 0, S0);
179 si->np = 1;
180 return;
181 }
182 pi = 0;
183 si->np = cyl_partit(r->rorg, si->spt, &pi, MAXSPART,
184 sp->sloc, sp->ss[SU], safedist2);
185 }
186
187
188 static int
189 cyl_partit(ro, pt, pi, mp, cent, axis, d2) /* slice a cylinder */
190 FVECT ro;
191 unsigned char *pt;
192 register int *pi;
193 int mp;
194 FVECT cent, axis;
195 double d2;
196 {
197 FVECT newct, newax;
198 int npl, npu;
199
200 if (mp < 2 || dist2(ro, cent) >= d2) { /* hit limit? */
201 setpart(pt, *pi, S0);
202 (*pi)++;
203 return(1);
204 }
205 /* subdivide */
206 setpart(pt, *pi, SU);
207 (*pi)++;
208 newax[0] = .5*axis[0];
209 newax[1] = .5*axis[1];
210 newax[2] = .5*axis[2];
211 d2 *= 0.25;
212 /* lower half */
213 newct[0] = cent[0] - newax[0];
214 newct[1] = cent[1] - newax[1];
215 newct[2] = cent[2] - newax[2];
216 npl = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2);
217 /* upper half */
218 newct[0] = cent[0] + newax[0];
219 newct[1] = cent[1] + newax[1];
220 newct[2] = cent[2] + newax[2];
221 npu = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2);
222 /* return total */
223 return(npl + npu);
224 }
225
226
227 void
228 flatpart(si, r) /* partition a flat source */
229 register SRCINDEX *si;
230 register RAY *r;
231 {
232 register RREAL *vp;
233 FVECT v;
234 double du2, dv2;
235 int pi;
236
237 clrpart(si->spt);
238 vp = source[si->sn].sloc;
239 v[0] = r->rorg[0] - vp[0];
240 v[1] = r->rorg[1] - vp[1];
241 v[2] = r->rorg[2] - vp[2];
242 vp = source[si->sn].snorm;
243 if (DOT(v,vp) <= FTINY) { /* behind source */
244 si->np = 0;
245 return;
246 }
247 dv2 = 2.*r->rweight/srcsizerat;
248 dv2 *= dv2;
249 vp = source[si->sn].ss[SU];
250 du2 = dv2 * DOT(vp,vp);
251 vp = source[si->sn].ss[SV];
252 dv2 *= DOT(vp,vp);
253 pi = 0;
254 si->np = flt_partit(r->rorg, si->spt, &pi, MAXSPART,
255 source[si->sn].sloc,
256 source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2);
257 }
258
259
260 static int
261 flt_partit(ro, pt, pi, mp, cent, u, v, du2, dv2) /* partition flatty */
262 FVECT ro;
263 unsigned char *pt;
264 register int *pi;
265 int mp;
266 FVECT cent, u, v;
267 double du2, dv2;
268 {
269 double d2;
270 FVECT newct, newax;
271 int npl, npu;
272
273 if (mp < 2 || ((d2 = dist2(ro, cent)) >= du2
274 && d2 >= dv2)) { /* hit limit? */
275 setpart(pt, *pi, S0);
276 (*pi)++;
277 return(1);
278 }
279 if (du2 > dv2) { /* subdivide in U */
280 setpart(pt, *pi, SU);
281 (*pi)++;
282 newax[0] = .5*u[0];
283 newax[1] = .5*u[1];
284 newax[2] = .5*u[2];
285 u = newax;
286 du2 *= 0.25;
287 } else { /* subdivide in V */
288 setpart(pt, *pi, SV);
289 (*pi)++;
290 newax[0] = .5*v[0];
291 newax[1] = .5*v[1];
292 newax[2] = .5*v[2];
293 v = newax;
294 dv2 *= 0.25;
295 }
296 /* lower half */
297 newct[0] = cent[0] - newax[0];
298 newct[1] = cent[1] - newax[1];
299 newct[2] = cent[2] - newax[2];
300 npl = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2);
301 /* upper half */
302 newct[0] = cent[0] + newax[0];
303 newct[1] = cent[1] + newax[1];
304 newct[2] = cent[2] + newax[2];
305 npu = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2);
306 /* return total */
307 return(npl + npu);
308 }
309
310
311 double
312 scylform(sn, dir) /* compute cosine for cylinder's projection */
313 int sn;
314 register FVECT dir; /* assume normalized */
315 {
316 register RREAL *dv;
317 double d;
318
319 dv = source[sn].ss[SU];
320 d = DOT(dir, dv);
321 d *= d / DOT(dv,dv);
322 return(sqrt(1. - d));
323 }