ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsamp.c
Revision: 2.7
Committed: Sat Feb 22 02:07:29 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +64 -4 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Source sampling routines
6 *
7 * External symbols declared in source.h
8 */
9
10 /* ====================================================================
11 * The Radiance Software License, Version 1.0
12 *
13 * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 * through Lawrence Berkeley National Laboratory. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 *
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 *
28 * 3. The end-user documentation included with the redistribution,
29 * if any, must include the following acknowledgment:
30 * "This product includes Radiance software
31 * (http://radsite.lbl.gov/)
32 * developed by the Lawrence Berkeley National Laboratory
33 * (http://www.lbl.gov/)."
34 * Alternately, this acknowledgment may appear in the software itself,
35 * if and wherever such third-party acknowledgments normally appear.
36 *
37 * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 * and "The Regents of the University of California" must
39 * not be used to endorse or promote products derived from this
40 * software without prior written permission. For written
41 * permission, please contact [email protected].
42 *
43 * 5. Products derived from this software may not be called "Radiance",
44 * nor may "Radiance" appear in their name, without prior written
45 * permission of Lawrence Berkeley National Laboratory.
46 *
47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 * ====================================================================
60 *
61 * This software consists of voluntary contributions made by many
62 * individuals on behalf of Lawrence Berkeley National Laboratory. For more
63 * information on Lawrence Berkeley National Laboratory, please see
64 * <http://www.lbl.gov/>.
65 */
66
67 #include "ray.h"
68
69 #include "source.h"
70
71 #include "random.h"
72
73
74 static int cyl_partit(), flt_partit();
75
76
77 double
78 nextssamp(r, si) /* compute sample for source, rtn. distance */
79 register RAY *r; /* origin is read, direction is set */
80 register SRCINDEX *si; /* source index (modified to current) */
81 {
82 int cent[3], size[3], parr[2];
83 FVECT vpos;
84 double d;
85 register int i;
86 nextsample:
87 while (++si->sp >= si->np) { /* get next sample */
88 if (++si->sn >= nsources)
89 return(0.0); /* no more */
90 if (source[si->sn].sflags & SSKIP)
91 si->np = 0;
92 else if (srcsizerat <= FTINY)
93 nopart(si, r);
94 else {
95 for (i = si->sn; source[i].sflags & SVIRTUAL;
96 i = source[i].sa.sv.sn)
97 ; /* partition source */
98 (*sfun[source[i].so->otype].of->partit)(si, r);
99 }
100 si->sp = -1;
101 }
102 /* get partition */
103 cent[0] = cent[1] = cent[2] = 0;
104 size[0] = size[1] = size[2] = MAXSPART;
105 parr[0] = 0; parr[1] = si->sp;
106 if (!skipparts(cent, size, parr, si->spt))
107 error(CONSISTENCY, "bad source partition in nextssamp");
108 /* compute sample */
109 if (dstrsrc > FTINY) { /* jitter sample */
110 dimlist[ndims] = si->sn + 8831;
111 dimlist[ndims+1] = si->sp + 3109;
112 d = urand(ilhash(dimlist,ndims+2)+samplendx);
113 if (source[si->sn].sflags & SFLAT) {
114 multisamp(vpos, 2, d);
115 vpos[2] = 0.5;
116 } else
117 multisamp(vpos, 3, d);
118 for (i = 0; i < 3; i++)
119 vpos[i] = dstrsrc * (1. - 2.*vpos[i]) *
120 (double)size[i]/MAXSPART;
121 } else
122 vpos[0] = vpos[1] = vpos[2] = 0.0;
123
124 for (i = 0; i < 3; i++)
125 vpos[i] += (double)cent[i]/MAXSPART;
126 /* compute direction */
127 for (i = 0; i < 3; i++)
128 r->rdir[i] = source[si->sn].sloc[i] +
129 vpos[SU]*source[si->sn].ss[SU][i] +
130 vpos[SV]*source[si->sn].ss[SV][i] +
131 vpos[SW]*source[si->sn].ss[SW][i];
132
133 if (!(source[si->sn].sflags & SDISTANT))
134 for (i = 0; i < 3; i++)
135 r->rdir[i] -= r->rorg[i];
136 /* compute distance */
137 if ((d = normalize(r->rdir)) == 0.0)
138 goto nextsample; /* at source! */
139
140 /* compute sample size */
141 if (source[si->sn].sflags & SFLAT) {
142 si->dom = sflatform(si->sn, r->rdir);
143 si->dom *= size[SU]*size[SV]/(MAXSPART*(double)MAXSPART);
144 } else if (source[si->sn].sflags & SCYL) {
145 si->dom = scylform(si->sn, r->rdir);
146 si->dom *= size[SU]/(double)MAXSPART;
147 } else {
148 si->dom = size[SU]*size[SV]*(double)size[SW] /
149 (MAXSPART*MAXSPART*(double)MAXSPART) ;
150 }
151 if (source[si->sn].sflags & SDISTANT) {
152 si->dom *= source[si->sn].ss2;
153 return(FHUGE);
154 }
155 if (si->dom <= 1e-4)
156 goto nextsample; /* behind source? */
157 si->dom *= source[si->sn].ss2/(d*d);
158 return(d); /* sample OK, return distance */
159 }
160
161
162 int
163 skipparts(ct, sz, pp, pt) /* skip to requested partition */
164 int ct[3], sz[3]; /* center and size of partition (returned) */
165 register int pp[2]; /* current index, number to skip (modified) */
166 unsigned char *pt; /* partition array */
167 {
168 register int p;
169 /* check this partition */
170 p = spart(pt, pp[0]);
171 pp[0]++;
172 if (p == S0) /* leaf partition */
173 if (pp[1]) {
174 pp[1]--;
175 return(0); /* not there yet */
176 } else
177 return(1); /* we've arrived */
178 /* else check lower */
179 sz[p] >>= 1;
180 ct[p] -= sz[p];
181 if (skipparts(ct, sz, pp, pt))
182 return(1); /* return hit */
183 /* else check upper */
184 ct[p] += sz[p] << 1;
185 if (skipparts(ct, sz, pp, pt))
186 return(1); /* return hit */
187 /* else return to starting position */
188 ct[p] -= sz[p];
189 sz[p] <<= 1;
190 return(0); /* return miss */
191 }
192
193
194 void
195 nopart(si, r) /* single source partition */
196 register SRCINDEX *si;
197 RAY *r;
198 {
199 clrpart(si->spt);
200 setpart(si->spt, 0, S0);
201 si->np = 1;
202 }
203
204
205 void
206 cylpart(si, r) /* partition a cylinder */
207 SRCINDEX *si;
208 register RAY *r;
209 {
210 double dist2, safedist2, dist2cent, rad2;
211 FVECT v;
212 register SRCREC *sp;
213 int pi;
214 /* first check point location */
215 clrpart(si->spt);
216 sp = source + si->sn;
217 rad2 = 1.365 * DOT(sp->ss[SV],sp->ss[SV]);
218 v[0] = r->rorg[0] - sp->sloc[0];
219 v[1] = r->rorg[1] - sp->sloc[1];
220 v[2] = r->rorg[2] - sp->sloc[2];
221 dist2 = DOT(v,sp->ss[SU]);
222 safedist2 = DOT(sp->ss[SU],sp->ss[SU]);
223 dist2 *= dist2 / safedist2;
224 dist2cent = DOT(v,v);
225 dist2 = dist2cent - dist2;
226 if (dist2 <= rad2) { /* point inside extended cylinder */
227 si->np = 0;
228 return;
229 }
230 safedist2 *= 4.*r->rweight*r->rweight/(srcsizerat*srcsizerat);
231 if (dist2 <= 4.*rad2 || /* point too close to subdivide */
232 dist2cent >= safedist2) { /* or too far */
233 setpart(si->spt, 0, S0);
234 si->np = 1;
235 return;
236 }
237 pi = 0;
238 si->np = cyl_partit(r->rorg, si->spt, &pi, MAXSPART,
239 sp->sloc, sp->ss[SU], safedist2);
240 }
241
242
243 static int
244 cyl_partit(ro, pt, pi, mp, cent, axis, d2) /* slice a cylinder */
245 FVECT ro;
246 unsigned char *pt;
247 register int *pi;
248 int mp;
249 FVECT cent, axis;
250 double d2;
251 {
252 FVECT newct, newax;
253 int npl, npu;
254
255 if (mp < 2 || dist2(ro, cent) >= d2) { /* hit limit? */
256 setpart(pt, *pi, S0);
257 (*pi)++;
258 return(1);
259 }
260 /* subdivide */
261 setpart(pt, *pi, SU);
262 (*pi)++;
263 newax[0] = .5*axis[0];
264 newax[1] = .5*axis[1];
265 newax[2] = .5*axis[2];
266 d2 *= 0.25;
267 /* lower half */
268 newct[0] = cent[0] - newax[0];
269 newct[1] = cent[1] - newax[1];
270 newct[2] = cent[2] - newax[2];
271 npl = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2);
272 /* upper half */
273 newct[0] = cent[0] + newax[0];
274 newct[1] = cent[1] + newax[1];
275 newct[2] = cent[2] + newax[2];
276 npu = cyl_partit(ro, pt, pi, mp/2, newct, newax, d2);
277 /* return total */
278 return(npl + npu);
279 }
280
281
282 void
283 flatpart(si, r) /* partition a flat source */
284 register SRCINDEX *si;
285 register RAY *r;
286 {
287 register FLOAT *vp;
288 FVECT v;
289 double du2, dv2;
290 int pi;
291
292 clrpart(si->spt);
293 vp = source[si->sn].sloc;
294 v[0] = r->rorg[0] - vp[0];
295 v[1] = r->rorg[1] - vp[1];
296 v[2] = r->rorg[2] - vp[2];
297 vp = source[si->sn].snorm;
298 if (DOT(v,vp) <= FTINY) { /* behind source */
299 si->np = 0;
300 return;
301 }
302 dv2 = 2.*r->rweight/srcsizerat;
303 dv2 *= dv2;
304 vp = source[si->sn].ss[SU];
305 du2 = dv2 * DOT(vp,vp);
306 vp = source[si->sn].ss[SV];
307 dv2 *= DOT(vp,vp);
308 pi = 0;
309 si->np = flt_partit(r->rorg, si->spt, &pi, MAXSPART,
310 source[si->sn].sloc,
311 source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2);
312 }
313
314
315 static int
316 flt_partit(ro, pt, pi, mp, cent, u, v, du2, dv2) /* partition flatty */
317 FVECT ro;
318 unsigned char *pt;
319 register int *pi;
320 int mp;
321 FVECT cent, u, v;
322 double du2, dv2;
323 {
324 double d2;
325 FVECT newct, newax;
326 int npl, npu;
327
328 if (mp < 2 || ((d2 = dist2(ro, cent)) >= du2
329 && d2 >= dv2)) { /* hit limit? */
330 setpart(pt, *pi, S0);
331 (*pi)++;
332 return(1);
333 }
334 if (du2 > dv2) { /* subdivide in U */
335 setpart(pt, *pi, SU);
336 (*pi)++;
337 newax[0] = .5*u[0];
338 newax[1] = .5*u[1];
339 newax[2] = .5*u[2];
340 u = newax;
341 du2 *= 0.25;
342 } else { /* subdivide in V */
343 setpart(pt, *pi, SV);
344 (*pi)++;
345 newax[0] = .5*v[0];
346 newax[1] = .5*v[1];
347 newax[2] = .5*v[2];
348 v = newax;
349 dv2 *= 0.25;
350 }
351 /* lower half */
352 newct[0] = cent[0] - newax[0];
353 newct[1] = cent[1] - newax[1];
354 newct[2] = cent[2] - newax[2];
355 npl = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2);
356 /* upper half */
357 newct[0] = cent[0] + newax[0];
358 newct[1] = cent[1] + newax[1];
359 newct[2] = cent[2] + newax[2];
360 npu = flt_partit(ro, pt, pi, mp/2, newct, u, v, du2, dv2);
361 /* return total */
362 return(npl + npu);
363 }
364
365
366 double
367 scylform(sn, dir) /* compute cosine for cylinder's projection */
368 int sn;
369 register FVECT dir; /* assume normalized */
370 {
371 register FLOAT *dv;
372 double d;
373
374 dv = source[sn].ss[SU];
375 d = DOT(dir, dv);
376 d *= d / DOT(dv,dv);
377 return(sqrt(1. - d));
378 }