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

Comparing ray/src/rt/source.c (file contents):
Revision 1.5 by greg, Wed Jun 7 08:38:38 1989 UTC vs.
Revision 1.12 by greg, Tue Jun 13 10:57:52 1989 UTC

# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27  
28   extern double  dstrsrc;                 /* source distribution amount */
29   extern double  shadthresh;              /* relative shadow threshold */
30 + extern double  shadcert;                /* shadow testing certainty */
31  
32   SRCREC  *source = NULL;                 /* our list of sources */
33   int  nsources = 0;                      /* the number of sources */
# Line 48 | Line 49 | marksources()                  /* find and mark source objects */
49  
50                  if (m->otype != MAT_LIGHT &&
51                                  m->otype != MAT_ILLUM &&
52 <                                m->otype != MAT_GLOW)
52 >                                m->otype != MAT_GLOW &&
53 >                                m->otype != MAT_SPOT)
54                          continue;
55          
56 <                if (m->oargs.nfargs != 3)
56 >                if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
57 >                                m->otype == MAT_SPOT ? 7 : 3))
58                          objerror(m, USER, "bad # arguments");
59  
60 <                if (m->otype == MAT_GLOW && o->otype != OBJ_SOURCE)
60 >                if (m->otype == MAT_GLOW &&
61 >                                o->otype != OBJ_SOURCE &&
62 >                                m->oargs.farg[3] <= FTINY)
63                          continue;                       /* don't bother */
64  
65                  if (source == NULL)
# Line 67 | Line 72 | marksources()                  /* find and mark source objects */
72  
73                  newsource(&source[nsources], o);
74  
75 <                if (m->otype == MAT_GLOW)
76 <                        source[nsources].sflags |= SSKIP;
77 <
75 >                if (m->otype == MAT_GLOW) {
76 >                        source[nsources].sflags |= SPROX;
77 >                        source[nsources].sl.prox = m->oargs.farg[3];
78 >                        if (o->otype == OBJ_SOURCE)
79 >                                source[nsources].sflags |= SSKIP;
80 >                } else if (m->otype == MAT_SPOT) {
81 >                        source[nsources].sflags |= SSPOT;
82 >                        source[nsources].sl.s = makespot(m);
83 >                }
84                  nsources++;
85          }
86   }
# Line 87 | Line 98 | register OBJREC  *so;
98          register int  i;
99          
100          src->sflags = 0;
101 <        src->nhits = src->ntests = 1;   /* start with hit probability = 1 */
101 >        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
102          src->so = so;
103  
104          switch (so->otype) {
# Line 139 | Line 150 | register OBJREC  *so;
150   }
151  
152  
153 + SPOT *
154 + makespot(m)                     /* make a spotlight */
155 + register OBJREC  *m;
156 + {
157 +        extern double  cos();
158 +        register SPOT  *ns;
159 +
160 +        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
161 +                error(SYSTEM, "out of memory in makespot");
162 +        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
163 +        VCOPY(ns->aim, m->oargs.farg+4);
164 +        if ((ns->flen = normalize(ns->aim)) == 0.0)
165 +                objerror(m, USER, "zero focus vector");
166 +        return(ns);
167 + }
168 +
169 +
170   double
171   srcray(sr, r, sn)               /* send a ray to a source, return domega */
172   register RAY  *sr;              /* returned source ray */
# Line 199 | Line 227 | register int  sn;              /* source number */
227                  return(source[sn].ss2);
228  
229          else {
230 +                                                /* check proximity */
231 +                if (source[sn].sflags & SPROX &&
232 +                                d > source[sn].sl.prox)
233 +                        return(0.0);
234  
235                  if (norm != NULL)
236                          ddot /= d;
237                  else
238                          ddot = 1.0;
239 +                                                /* check angle */
240 +                if (source[sn].sflags & SSPOT) {
241 +                        if (source[sn].sl.s->siz < 2.0*PI *
242 +                                (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
243 +                                return(0.0);
244 +                        d += source[sn].sl.s->flen;
245 +                }
246                                                  /* return domega */
247                  return(ddot*source[sn].ss2/(d*d));
248          }
# Line 248 | Line 287 | register RAY  *r;
287  
288   static int
289   cntcmp(sc1, sc2)                        /* contribution compare (descending) */
290 < register CONTRIB  *sc1, *sc2;
290 > register CNTPTR  *sc1, *sc2;
291   {
292          if (sc1->brt > sc2->brt)
293                  return(-1);
# Line 263 | Line 302 | RAY  *r;                       /* ray that hit surface */
302   int  (*f)();                    /* direct component coefficient function */
303   char  *p;                       /* data for f */
304   {
305 +        extern double  pow();
306          register int  sn;
307          register CONTRIB  *srccnt;
308 <        double  dtmp, hwt, test2, hit2;
308 >        register CNTPTR  *cntord;
309 >        int  nshadcheck, ncnts;
310 >        double  prob, ourthresh, hwt, test2, hit2;
311          RAY  sr;
312  
313 <        if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL)
313 >        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
314 >        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
315 >        if (srccnt == NULL || cntord == NULL)
316                  error(SYSTEM, "out of memory in direct");
317 +                                                /* compute number to check */
318 +        nshadcheck = pow((double)nsources, shadcert) + .5;
319 +                                                /* modify threshold */
320 +        ourthresh = shadthresh / r->rweight;
321                                                  /* potential contributions */
322          for (sn = 0; sn < nsources; sn++) {
323 <                srccnt[sn].sno = sn;
324 <                setcolor(srccnt[sn].val, 0.0, 0.0, 0.0);
323 >                cntord[sn].sno = sn;
324 >                cntord[sn].brt = 0.0;
325                                                  /* get source ray */
326                  if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
327                          continue;
328                  VCOPY(srccnt[sn].dir, sr.rdir);
329                                                  /* compute coefficient */
330                  (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
331 <                srccnt[sn].brt = bright(srccnt[sn].val);
332 <                if (srccnt[sn].brt <= FTINY)
331 >                cntord[sn].brt = bright(srccnt[sn].val);
332 >                if (cntord[sn].brt <= FTINY)
333                          continue;
334                                                  /* compute intersection */
335                  if (!( source[sn].sflags & SDISTANT ?
# Line 292 | Line 340 | char  *p;                      /* data for f */
340                                                  /* compute contribution */
341                  rayshade(&sr, sr.ro->omod);
342                  multcolor(srccnt[sn].val, sr.rcol);
343 <                srccnt[sn].brt = bright(srccnt[sn].val);
343 >                cntord[sn].brt = bright(srccnt[sn].val);
344          }
345                                                  /* sort contributions */
346 <        qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp);
347 <        hit2 = test2 = 0.0;
346 >        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
347 >                                                /* find last */
348 >        sn = 0; ncnts = nsources;
349 >        while (sn < ncnts-1) {
350 >                register int  m;
351 >                m = (sn + ncnts) >> 1;
352 >                if (cntord[m].brt > 0.0)
353 >                        sn = m;
354 >                else
355 >                        ncnts = m;
356 >        }
357 >                                                /* accumulate tail */
358 >        for (sn = ncnts-1; sn > 0; sn--)
359 >                cntord[sn-1].brt += cntord[sn].brt;
360 >                                                /* start with prob=.5 */
361 >        hit2 = 0.5; test2 = 1.0;
362                                                  /* test for shadows */
363 <        for (sn = 0; sn < nsources; sn++) {
363 >        for (sn = 0; sn < ncnts; sn++) {
364                                                  /* check threshold */
365 <                if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight)
365 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
366 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
367 >                                ourthresh*bright(r->rcol))
368                          break;
369                                                  /* get statistics */
370 <                hwt = (double)source[srccnt[sn].sno].nhits /
371 <                                (double)source[srccnt[sn].sno].ntests;
370 >                hwt = (double)source[cntord[sn].sno].nhits /
371 >                                (double)source[cntord[sn].sno].ntests;
372                  test2 += hwt;
373 <                source[srccnt[sn].sno].ntests++;
373 >                source[cntord[sn].sno].ntests++;
374                                                  /* test for hit */
375                  rayorigin(&sr, r, SHADOW, 1.0);
376 <                VCOPY(sr.rdir, srccnt[sn].dir);
376 >                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
377                  if (localhit(&sr, &thescene) &&
378 <                                sr.ro != source[srccnt[sn].sno].so) {
378 >                                sr.ro != source[cntord[sn].sno].so) {
379                                                  /* check for transmission */
380                          if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
381                                  raytrans(&sr);          /* object is clipped */
# Line 319 | Line 383 | char  *p;                      /* data for f */
383                                  rayshade(&sr, sr.ro->omod);
384                          if (bright(sr.rcol) <= FTINY)
385                                  continue;       /* missed! */
386 <                        (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
387 <                        multcolor(srccnt[sn].val, sr.rcol);
386 >                        (*f)(srccnt[cntord[sn].sno].val, p,
387 >                                        srccnt[cntord[sn].sno].dir,
388 >                                        srccnt[cntord[sn].sno].dom);
389 >                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
390                  }
391                                                  /* add contribution if hit */
392 <                addcolor(r->rcol, srccnt[sn].val);
392 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
393                  hit2 += hwt;
394 <                source[srccnt[sn].sno].nhits++;
394 >                source[cntord[sn].sno].nhits++;
395          }
396 <        if (test2 > FTINY)              /* weighted hit rate */
397 <                hwt = hit2 / test2;
332 <        else
333 <                hwt = 0.0;
396 >                                        /* weighted hit rate */
397 >        hwt = hit2 / test2;
398   #ifdef DEBUG
399 <        fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt);
399 >        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
400 >                        sn, ncnts-sn, hwt);
401 >        eputs(errmsg);
402   #endif
403                                          /* add in untested sources */
404 <        for ( ; sn < nsources; sn++) {
405 <                if (srccnt[sn].brt <= 0.0)
406 <                        break;
407 <                dtmp = hwt * (double)source[srccnt[sn].sno].nhits /
408 <                                (double)source[srccnt[sn].sno].ntests;
343 <                scalecolor(srccnt[sn].val, dtmp);
344 <                addcolor(r->rcol, srccnt[sn].val);
404 >        for ( ; sn < ncnts; sn++) {
405 >                prob = hwt * (double)source[cntord[sn].sno].nhits /
406 >                                (double)source[cntord[sn].sno].ntests;
407 >                scalecolor(srccnt[cntord[sn].sno].val, prob);
408 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
409          }
410                  
411          free(srccnt);
412 +        free(cntord);
413   }
414  
415  
# Line 353 | Line 418 | char  *p;                      /* data for f */
418                                  source[r->rsrc].so!=r->ro)
419  
420   #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
421 <                                !(r->rtype&REFLECTED))  /* hack! */
421 >                                !(r->rtype&REFLECTED) &&        /* hack! */\
422 >                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
423  
424   #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
425                                  !(r->rsrc>=0&&source[r->rsrc].so==r->ro))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines