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.6 by greg, Wed Jun 7 10:29:05 1989 UTC vs.
Revision 1.22 by greg, Tue Nov 13 14:32:30 1990 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 286 | 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 301 | 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 >        if (nsources <= 0)
314 >                return;
315 >        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
316 >        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
317 >        if (srccnt == NULL || cntord == NULL)
318                  error(SYSTEM, "out of memory in direct");
319 +                                                /* compute number to check */
320 +        nshadcheck = pow((double)nsources, shadcert) + .5;
321 +                                                /* modify threshold */
322 +        ourthresh = shadthresh / r->rweight;
323                                                  /* potential contributions */
324          for (sn = 0; sn < nsources; sn++) {
325 <                srccnt[sn].sno = sn;
326 <                setcolor(srccnt[sn].val, 0.0, 0.0, 0.0);
315 <                srccnt[sn].brt = 0.0;
325 >                cntord[sn].sno = sn;
326 >                cntord[sn].brt = 0.0;
327                                                  /* get source ray */
328                  if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
329                          continue;
330                  VCOPY(srccnt[sn].dir, sr.rdir);
331                                                  /* compute coefficient */
332                  (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
333 <                srccnt[sn].brt = bright(srccnt[sn].val);
334 <                if (srccnt[sn].brt <= FTINY)
333 >                cntord[sn].brt = bright(srccnt[sn].val);
334 >                if (cntord[sn].brt <= 0.0)
335                          continue;
336                                                  /* compute intersection */
337                  if (!( source[sn].sflags & SDISTANT ?
# Line 329 | Line 340 | char  *p;                      /* data for f */
340                                  (source[sn].so, &sr) ))
341                          continue;
342                                                  /* compute contribution */
343 <                rayshade(&sr, sr.ro->omod);
343 >                raycont(&sr);
344                  multcolor(srccnt[sn].val, sr.rcol);
345 <                srccnt[sn].brt = bright(srccnt[sn].val);
345 >                cntord[sn].brt = bright(srccnt[sn].val);
346          }
347                                                  /* sort contributions */
348 <        qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp);
349 <        hit2 = test2 = 0.0;
348 >        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
349 >        {                                       /* find last */
350 >                register int  l, m;
351 >
352 >                sn = 0; ncnts = l = nsources;
353 >                while ((m = (sn + ncnts) >> 1) != l) {
354 >                        if (cntord[m].brt > 0.0)
355 >                                sn = m;
356 >                        else
357 >                                ncnts = m;
358 >                        l = m;
359 >                }
360 >        }
361 >                                                /* accumulate tail */
362 >        for (sn = ncnts-1; sn > 0; sn--)
363 >                cntord[sn-1].brt += cntord[sn].brt;
364 >                                                /* start with prob=.5 */
365 >        hit2 = 0.5; test2 = 1.0;
366                                                  /* test for shadows */
367 <        for (sn = 0; sn < nsources; sn++) {
367 >        for (sn = 0; sn < ncnts; sn++) {
368                                                  /* check threshold */
369 <                if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight)
369 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
370 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
371 >                                ourthresh*bright(r->rcol))
372                          break;
373                                                  /* get statistics */
374 <                hwt = (double)source[srccnt[sn].sno].nhits /
375 <                                (double)source[srccnt[sn].sno].ntests;
374 >                hwt = (double)source[cntord[sn].sno].nhits /
375 >                                (double)source[cntord[sn].sno].ntests;
376                  test2 += hwt;
377 <                source[srccnt[sn].sno].ntests++;
377 >                source[cntord[sn].sno].ntests++;
378                                                  /* test for hit */
379                  rayorigin(&sr, r, SHADOW, 1.0);
380 <                VCOPY(sr.rdir, srccnt[sn].dir);
380 >                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
381 >                sr.rsrc = cntord[sn].sno;
382                  if (localhit(&sr, &thescene) &&
383 <                                sr.ro != source[srccnt[sn].sno].so) {
383 >                                sr.ro != source[cntord[sn].sno].so) {
384                                                  /* check for transmission */
385 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
356 <                                raytrans(&sr);          /* object is clipped */
357 <                        else
358 <                                rayshade(&sr, sr.ro->omod);
385 >                        raycont(&sr);
386                          if (bright(sr.rcol) <= FTINY)
387                                  continue;       /* missed! */
388 <                        (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
389 <                        multcolor(srccnt[sn].val, sr.rcol);
388 >                        (*f)(srccnt[cntord[sn].sno].val, p,
389 >                                        srccnt[cntord[sn].sno].dir,
390 >                                        srccnt[cntord[sn].sno].dom);
391 >                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
392                  }
393                                                  /* add contribution if hit */
394 <                addcolor(r->rcol, srccnt[sn].val);
394 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
395                  hit2 += hwt;
396 <                source[srccnt[sn].sno].nhits++;
396 >                source[cntord[sn].sno].nhits++;
397          }
398 <        if (test2 > FTINY)              /* weighted hit rate */
399 <                hwt = hit2 / test2;
371 <        else
372 <                hwt = 0.0;
398 >                                        /* weighted hit rate */
399 >        hwt = hit2 / test2;
400   #ifdef DEBUG
401 <        fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt);
401 >        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
402 >                        sn, ncnts-sn, hwt);
403 >        eputs(errmsg);
404   #endif
405                                          /* add in untested sources */
406 <        for ( ; sn < nsources; sn++) {
407 <                if (srccnt[sn].brt <= 0.0)
408 <                        break;
409 <                dtmp = hwt * (double)source[srccnt[sn].sno].nhits /
410 <                                (double)source[srccnt[sn].sno].ntests;
382 <                scalecolor(srccnt[sn].val, dtmp);
383 <                addcolor(r->rcol, srccnt[sn].val);
406 >        for ( ; sn < ncnts; sn++) {
407 >                prob = hwt * (double)source[cntord[sn].sno].nhits /
408 >                                (double)source[cntord[sn].sno].ntests;
409 >                scalecolor(srccnt[cntord[sn].sno].val, prob);
410 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
411          }
412                  
413          free(srccnt);
414 +        free(cntord);
415   }
416  
417  
# Line 403 | Line 431 | m_light(m, r)                  /* ray hit a light source */
431   register OBJREC  *m;
432   register RAY  *r;
433   {
406                                                /* check for behind */
407        if (r->rod < 0.0)
408                return;
434                                                  /* check for over-counting */
435          if (wrongsource(m, r) || badambient(m, r))
436                  return;
# Line 419 | Line 444 | register RAY  *r;
444  
445                                                  /* otherwise treat as source */
446          } else {
447 +                                                /* check for behind */
448 +                if (r->rod < 0.0)
449 +                        return;
450                                                  /* get distribution pattern */
451                  raytexture(r, m->omod);
452                                                  /* get source color */
# Line 427 | Line 455 | register RAY  *r;
455                                    m->oargs.farg[2]);
456                                                  /* modify value */
457                  multcolor(r->rcol, r->pcol);
458 +                                                /* assign distance */
459 +                r->rt = r->rot;
460          }
461   }
462  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines