ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsamp.c
(Generate patch)

Comparing ray/src/rt/srcsamp.c (file contents):
Revision 2.12 by greg, Sat Dec 6 01:08:53 2008 UTC vs.
Revision 2.21 by greg, Sat Nov 9 15:21:32 2024 UTC

# Line 16 | Line 16 | static const char      RCSid[] = "$Id$";
16   #include  "random.h"
17  
18  
19 < static int  cyl_partit(), flt_partit();
19 > int
20 > srcskip(                        /* pre-emptive test for source to skip */
21 >        int  sn,
22 >        RAY  *r
23 > )
24 > {
25 >        SRCREC  *sp = source + sn;
26  
27 +        if (sp->sflags & SSKIP)
28 +                return(1);
29  
30 +        if ((sp->sflags & (SPROX|SDISTANT)) != SPROX)
31 +                return(0);
32 +
33 +        return(dist2(r->rorg, sp->sloc) >
34 +                        (sp->sl.prox + sp->srad)*(sp->sl.prox + sp->srad));
35 + }
36 +
37   double
38 < nextssamp(r, si)                /* compute sample for source, rtn. distance */
39 < register RAY  *r;               /* origin is read, direction is set */
40 < register SRCINDEX  *si;         /* source index (modified to current) */
38 > nextssamp(                      /* compute sample for source, rtn. distance */
39 >        RAY  *r,                /* origin is read, direction is set */
40 >        SRCINDEX  *si           /* source index (modified to current) */
41 > )
42   {
43          int  cent[3], size[3], parr[2];
44 <        FVECT  vpos;
44 >        SRCREC  *srcp;
45 >        double  vpos[3];
46          double  d;
47 <        register int  i;
47 >        int  i;
48   nextsample:
49          while (++si->sp >= si->np) {    /* get next sample */
50                  if (++si->sn >= nsources)
51                          return(0.0);    /* no more */
52 <                if (source[si->sn].sflags & SSKIP)
52 >                if (srcskip(si->sn, r))
53                          si->np = 0;
54                  else if (srcsizerat <= FTINY)
55                          nopart(si, r);
# Line 51 | Line 68 | nextsample:
68          if (!skipparts(cent, size, parr, si->spt))
69                  error(CONSISTENCY, "bad source partition in nextssamp");
70                                          /* compute sample */
71 +        srcp = source + si->sn;
72          if (dstrsrc > FTINY) {                  /* jitter sample */
73                  dimlist[ndims] = si->sn + 8831;
74                  dimlist[ndims+1] = si->sp + 3109;
75                  d = urand(ilhash(dimlist,ndims+2)+samplendx);
76 <                if (source[si->sn].sflags & SFLAT) {
76 >                if (srcp->sflags & SFLAT) {
77                          multisamp(vpos, 2, d);
78                          vpos[SW] = 0.5;
79                  } else
80                          multisamp(vpos, 3, d);
81                  for (i = 0; i < 3; i++)
82                          vpos[i] = dstrsrc * (1. - 2.*vpos[i]) *
83 <                                        (double)size[i]/MAXSPART;
83 >                                        (double)size[i]*(1.0/MAXSPART);
84          } else
85                  vpos[0] = vpos[1] = vpos[2] = 0.0;
86  
87 <        for (i = 0; i < 3; i++)
70 <                vpos[i] += (double)cent[i]/MAXSPART;
87 >        VSUM(vpos, vpos, cent, 1.0/MAXSPART);
88                                          /* avoid circular aiming failures */
89 <        if (source[si->sn].sflags & SCIR) {
89 >        if ((srcp->sflags & SCIR) && (si->np > 1) | (dstrsrc > 0.7)) {
90                  FVECT   trim;
91 <                double  d;
75 <                if (source[si->sn].sflags & (SFLAT|SDISTANT)) {
91 >                if (srcp->sflags & (SFLAT|SDISTANT)) {
92                          d = 1.12837917;         /* correct setflatss() */
93                          trim[SU] = d*sqrt(1.0 - 0.5*vpos[SV]*vpos[SV]);
94                          trim[SV] = d*sqrt(1.0 - 0.5*vpos[SU]*vpos[SU]);
# Line 85 | Line 101 | nextsample:
101                          d = vpos[SW]*vpos[SW];
102                          if (d > trim[SW]) trim[SW] = d;
103                          trim[SU] += d;
104 <                        d = 1.0/0.7236;         /* correct sphsetsrc() */
105 <                        trim[SW] = trim[SV] = trim[SU] =
106 <                                        d*sqrt(trim[SW]/trim[SU]);
104 >                        if (trim[SU] > FTINY*FTINY) {
105 >                                d = 1.0/0.7236; /* correct sphsetsrc() */
106 >                                trim[SW] = trim[SV] = trim[SU] =
107 >                                                d*sqrt(trim[SW]/trim[SU]);
108 >                        } else
109 >                                trim[SW] = trim[SV] = trim[SU] = 0.0;
110                  }
111                  for (i = 0; i < 3; i++)
112                          vpos[i] *= trim[i];
113          }
114                                          /* compute direction */
115          for (i = 0; i < 3; i++)
116 <                r->rdir[i] = source[si->sn].sloc[i] +
117 <                                vpos[SU]*source[si->sn].ss[SU][i] +
118 <                                vpos[SV]*source[si->sn].ss[SV][i] +
119 <                                vpos[SW]*source[si->sn].ss[SW][i];
116 >                r->rdir[i] = srcp->sloc[i] +
117 >                                vpos[SU]*srcp->ss[SU][i] +
118 >                                vpos[SV]*srcp->ss[SV][i] +
119 >                                vpos[SW]*srcp->ss[SW][i];
120  
121 <        if (!(source[si->sn].sflags & SDISTANT))
122 <                for (i = 0; i < 3; i++)
104 <                        r->rdir[i] -= r->rorg[i];
121 >        if (!(srcp->sflags & SDISTANT))
122 >                VSUB(r->rdir, r->rdir, r->rorg);
123                                          /* compute distance */
124          if ((d = normalize(r->rdir)) == 0.0)
125                  goto nextsample;                /* at source! */
126  
127                                          /* compute sample size */
128 <        if (source[si->sn].sflags & SFLAT) {
128 >        if (srcp->sflags & SFLAT) {
129                  si->dom = sflatform(si->sn, r->rdir);
130 <                si->dom *= size[SU]*size[SV]/(MAXSPART*(double)MAXSPART);
131 <        } else if (source[si->sn].sflags & SCYL) {
130 >                si->dom *= size[SU]*size[SV]*(1.0/MAXSPART/MAXSPART);
131 >        } else if (srcp->sflags & SCYL) {
132                  si->dom = scylform(si->sn, r->rdir);
133 <                si->dom *= size[SU]/(double)MAXSPART;
133 >                si->dom *= size[SU]*(1.0/MAXSPART);
134          } else {
135 <                si->dom = size[SU]*size[SV]*(double)size[SW] /
136 <                                (MAXSPART*MAXSPART*(double)MAXSPART) ;
135 >                si->dom = size[SU]*size[SV]*(double)size[SW] *
136 >                                (1.0/MAXSPART/MAXSPART/MAXSPART) ;
137          }
138 <        if (source[si->sn].sflags & SDISTANT) {
139 <                si->dom *= source[si->sn].ss2;
138 >        if (srcp->sflags & SDISTANT) {
139 >                si->dom *= srcp->ss2;
140                  return(FHUGE);
141          }
142          if (si->dom <= 1e-4)
143                  goto nextsample;                /* behind source? */
144 <        si->dom *= source[si->sn].ss2/(d*d);
144 >        si->dom *= srcp->ss2/(d*d);
145          return(d);              /* sample OK, return distance */
146   }
147  
148  
149   int
150 < skipparts(ct, sz, pp, pt)               /* skip to requested partition */
151 < int  ct[3], sz[3];              /* center and size of partition (returned) */
152 < register int  pp[2];            /* current index, number to skip (modified) */
153 < unsigned char  *pt;             /* partition array */
150 > skipparts(                      /* skip to requested partition */
151 >        int  ct[3],
152 >        int  sz[3],             /* center and size of partition (returned) */
153 >        int  pp[2],             /* current index, number to skip (modified) */
154 >        unsigned char  *pt      /* partition array */
155 > )
156   {
157 <        register int  p;
157 >        int  p;
158                                          /* check this partition */
159          p = spart(pt, pp[0]);
160          pp[0]++;
# Line 162 | Line 182 | unsigned char  *pt;            /* partition array */
182  
183  
184   void
185 < nopart(si, r)                   /* single source partition */
186 < register SRCINDEX  *si;
187 < RAY  *r;
185 > nopart(                         /* single source partition */
186 >        SRCINDEX  *si,
187 >        RAY  *r
188 > )
189   {
190          clrpart(si->spt);
191          setpart(si->spt, 0, S0);
# Line 172 | Line 193 | RAY  *r;
193   }
194  
195  
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
196   static int
197 < cyl_partit(ro, pt, pi, mp, cent, axis, d2)      /* slice a cylinder */
198 < FVECT  ro;
199 < unsigned char  *pt;
200 < register int  *pi;
201 < int  mp;
202 < FVECT  cent, axis;
203 < double  d2;
197 > cyl_partit(                             /* slice a cylinder */
198 >        FVECT  ro,
199 >        unsigned char  *pt,
200 >        int  *pi,
201 >        int  mp,
202 >        FVECT  cent,
203 >        FVECT  axis,
204 >        double  d2
205 > )
206   {
207          FVECT  newct, newax;
208          int  npl, npu;
# Line 250 | Line 235 | double  d2;
235  
236  
237   void
238 < flatpart(si, r)                         /* partition a flat source */
239 < register SRCINDEX  *si;
240 < register RAY  *r;
238 > cylpart(                        /* partition a cylinder */
239 >        SRCINDEX  *si,
240 >        RAY  *r
241 > )
242   {
243 <        register RREAL  *vp;
243 >        double  dist2, safedist2, dist2cent, rad2;
244          FVECT  v;
245 <        double  du2, dv2;
245 >        SRCREC  *sp;
246          int  pi;
247 <
247 >                                        /* first check point location */
248          clrpart(si->spt);
249 <        vp = source[si->sn].sloc;
250 <        v[0] = r->rorg[0] - vp[0];
251 <        v[1] = r->rorg[1] - vp[1];
252 <        v[2] = r->rorg[2] - vp[2];
253 <        vp = source[si->sn].snorm;
254 <        if (DOT(v,vp) <= 0.) {          /* behind source */
249 >        sp = source + si->sn;
250 >        rad2 = 1.365 * DOT(sp->ss[SV],sp->ss[SV]);
251 >        v[0] = r->rorg[0] - sp->sloc[0];
252 >        v[1] = r->rorg[1] - sp->sloc[1];
253 >        v[2] = r->rorg[2] - sp->sloc[2];
254 >        dist2 = DOT(v,sp->ss[SU]);
255 >        safedist2 = DOT(sp->ss[SU],sp->ss[SU]);
256 >        dist2 *= dist2 / safedist2;
257 >        dist2cent = DOT(v,v);
258 >        dist2 = dist2cent - dist2;
259 >        if (dist2 <= rad2) {            /* point inside extended cylinder */
260                  si->np = 0;
261                  return;
262          }
263 <        dv2 = 2.*r->rweight/srcsizerat;
264 <        dv2 *= dv2;
265 <        vp = source[si->sn].ss[SU];
266 <        du2 = dv2 * DOT(vp,vp);
267 <        vp = source[si->sn].ss[SV];
268 <        dv2 *= DOT(vp,vp);
263 >        safedist2 *= 4.*r->rweight*r->rweight/(srcsizerat*srcsizerat);
264 >        if (dist2 <= 4.*rad2 ||         /* point too close to subdivide */
265 >                        dist2cent >= safedist2) {       /* or too far */
266 >                setpart(si->spt, 0, S0);
267 >                si->np = 1;
268 >                return;
269 >        }
270          pi = 0;
271 <        si->np = flt_partit(r->rorg, si->spt, &pi, MAXSPART,
272 <                source[si->sn].sloc,
281 <                source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2);
271 >        si->np = cyl_partit(r->rorg, si->spt, &pi, MAXSPART,
272 >                        sp->sloc, sp->ss[SU], safedist2);
273   }
274  
275  
276   static int
277 < flt_partit(ro, pt, pi, mp, cent, u, v, du2, dv2)        /* partition flatty */
278 < FVECT  ro;
279 < unsigned char  *pt;
280 < register int  *pi;
281 < int  mp;
282 < FVECT  cent, u, v;
283 < double  du2, dv2;
277 > flt_partit(                             /* partition flatty */
278 >        FVECT  ro,
279 >        unsigned char  *pt,
280 >        int  *pi,
281 >        int  mp,
282 >        FVECT  cent,
283 >        FVECT  u,
284 >        FVECT  v,
285 >        double  du2,
286 >        double  dv2
287 > )
288   {
289          double  d2;
290          FVECT  newct, newax;
# Line 333 | Line 328 | double  du2, dv2;
328   }
329  
330  
331 + void
332 + flatpart(                               /* partition a flat source */
333 +        SRCINDEX  *si,
334 +        RAY  *r
335 + )
336 + {
337 +        RREAL  *vp;
338 +        FVECT  v;
339 +        double  du2, dv2;
340 +        int  pi;
341 +
342 +        clrpart(si->spt);
343 +        vp = source[si->sn].sloc;
344 +        v[0] = r->rorg[0] - vp[0];
345 +        v[1] = r->rorg[1] - vp[1];
346 +        v[2] = r->rorg[2] - vp[2];
347 +        vp = source[si->sn].snorm;
348 +        if (DOT(v,vp) <= 0.) {          /* behind source */
349 +                si->np = 0;
350 +                return;
351 +        }
352 +        dv2 = 2.*r->rweight/srcsizerat;
353 +        dv2 *= dv2;
354 +        vp = source[si->sn].ss[SU];
355 +        du2 = dv2 * DOT(vp,vp);
356 +        vp = source[si->sn].ss[SV];
357 +        dv2 *= DOT(vp,vp);
358 +        pi = 0;
359 +        si->np = flt_partit(r->rorg, si->spt, &pi, MAXSPART,
360 +                source[si->sn].sloc,
361 +                source[si->sn].ss[SU], source[si->sn].ss[SV], du2, dv2);
362 + }
363 +
364 +
365   double
366 < scylform(sn, dir)               /* compute cosine for cylinder's projection */
367 < int  sn;
368 < register FVECT  dir;            /* assume normalized */
366 > scylform(                       /* compute cosine for cylinder's projection */
367 >        int  sn,
368 >        FVECT  dir              /* assume normalized */
369 > )
370   {
371 <        register RREAL  *dv;
371 >        RREAL  *dv;
372          double  d;
373  
374          dv = source[sn].ss[SU];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines