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.7 by greg, Wed Jun 7 15:07:31 1989 UTC vs.
Revision 1.24 by greg, Thu Dec 13 10:44:04 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 46 | Line 47 | marksources()                  /* find and mark source objects */
47  
48                  m = objptr(o->omod);
49  
50 <                if (m->otype != MAT_LIGHT &&
50 <                                m->otype != MAT_ILLUM &&
51 <                                m->otype != MAT_GLOW &&
52 <                                m->otype != MAT_SPOT)
50 >                if (!islight(m->otype))
51                          continue;
52          
53                  if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
# Line 286 | Line 284 | register RAY  *r;
284  
285   static int
286   cntcmp(sc1, sc2)                        /* contribution compare (descending) */
287 < register CONTRIB  *sc1, *sc2;
287 > register CNTPTR  *sc1, *sc2;
288   {
289          if (sc1->brt > sc2->brt)
290                  return(-1);
# Line 301 | Line 299 | RAY  *r;                       /* ray that hit surface */
299   int  (*f)();                    /* direct component coefficient function */
300   char  *p;                       /* data for f */
301   {
302 +        extern double  pow();
303          register int  sn;
304          register CONTRIB  *srccnt;
305 <        double  dtmp, hwt, test2, hit2;
305 >        register CNTPTR  *cntord;
306 >        int  nshadcheck, ncnts;
307 >        double  prob, ourthresh, hwt, test2, hit2;
308          RAY  sr;
309  
310 <        if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL)
310 >        if (nsources <= 0)
311 >                return;
312 >        srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
313 >        cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
314 >        if (srccnt == NULL || cntord == NULL)
315                  error(SYSTEM, "out of memory in direct");
316 +                                                /* compute number to check */
317 +        nshadcheck = pow((double)nsources, shadcert) + .5;
318 +                                                /* modify threshold */
319 +        ourthresh = shadthresh / r->rweight;
320                                                  /* potential contributions */
321          for (sn = 0; sn < nsources; sn++) {
322 <                srccnt[sn].sno = sn;
323 <                setcolor(srccnt[sn].val, 0.0, 0.0, 0.0);
315 <                srccnt[sn].brt = 0.0;
322 >                cntord[sn].sno = sn;
323 >                cntord[sn].brt = 0.0;
324                                                  /* get source ray */
325                  if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
326                          continue;
327                  VCOPY(srccnt[sn].dir, sr.rdir);
328                                                  /* compute coefficient */
329                  (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
330 <                srccnt[sn].brt = bright(srccnt[sn].val);
331 <                if (srccnt[sn].brt <= FTINY)
330 >                cntord[sn].brt = bright(srccnt[sn].val);
331 >                if (cntord[sn].brt <= 0.0)
332                          continue;
333                                                  /* compute intersection */
334                  if (!( source[sn].sflags & SDISTANT ?
# Line 329 | Line 337 | char  *p;                      /* data for f */
337                                  (source[sn].so, &sr) ))
338                          continue;
339                                                  /* compute contribution */
340 <                rayshade(&sr, sr.ro->omod);
340 >                raycont(&sr);
341                  multcolor(srccnt[sn].val, sr.rcol);
342 <                srccnt[sn].brt = bright(srccnt[sn].val);
342 >                cntord[sn].brt = bright(srccnt[sn].val);
343          }
344                                                  /* sort contributions */
345 <        qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp);
346 <        hit2 = 0.0; test2 = FTINY;
345 >        qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
346 >        {                                       /* find last */
347 >                register int  l, m;
348 >
349 >                sn = 0; ncnts = l = nsources;
350 >                while ((m = (sn + ncnts) >> 1) != l) {
351 >                        if (cntord[m].brt > 0.0)
352 >                                sn = m;
353 >                        else
354 >                                ncnts = m;
355 >                        l = m;
356 >                }
357 >        }
358 >                                                /* accumulate tail */
359 >        for (sn = ncnts-1; sn > 0; sn--)
360 >                cntord[sn-1].brt += cntord[sn].brt;
361 >                                                /* start with prob=.5 */
362 >        hit2 = 0.5; test2 = 1.0;
363                                                  /* test for shadows */
364 <        for (sn = 0; sn < nsources; sn++) {
364 >        for (sn = 0; sn < ncnts; sn++) {
365                                                  /* check threshold */
366 <                if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight)
366 >                if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
367 >                                cntord[sn].brt-cntord[sn+nshadcheck].brt) <
368 >                                ourthresh*bright(r->rcol))
369                          break;
370                                                  /* get statistics */
371 <                hwt = (double)source[srccnt[sn].sno].nhits /
372 <                                (double)source[srccnt[sn].sno].ntests;
371 >                hwt = (double)source[cntord[sn].sno].nhits /
372 >                                (double)source[cntord[sn].sno].ntests;
373                  test2 += hwt;
374 <                source[srccnt[sn].sno].ntests++;
374 >                source[cntord[sn].sno].ntests++;
375                                                  /* test for hit */
376                  rayorigin(&sr, r, SHADOW, 1.0);
377 <                VCOPY(sr.rdir, srccnt[sn].dir);
377 >                VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
378 >                sr.rsrc = cntord[sn].sno;
379                  if (localhit(&sr, &thescene) &&
380 <                                sr.ro != source[srccnt[sn].sno].so) {
380 >                                sr.ro != source[cntord[sn].sno].so) {
381                                                  /* check for transmission */
382 <                        if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod))
356 <                                raytrans(&sr);          /* object is clipped */
357 <                        else
358 <                                rayshade(&sr, sr.ro->omod);
382 >                        raycont(&sr);
383                          if (bright(sr.rcol) <= FTINY)
384                                  continue;       /* missed! */
385 <                        (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
386 <                        multcolor(srccnt[sn].val, sr.rcol);
385 >                        (*f)(srccnt[cntord[sn].sno].val, p,
386 >                                        srccnt[cntord[sn].sno].dir,
387 >                                        srccnt[cntord[sn].sno].dom);
388 >                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
389                  }
390                                                  /* add contribution if hit */
391 <                addcolor(r->rcol, srccnt[sn].val);
391 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
392                  hit2 += hwt;
393 <                source[srccnt[sn].sno].nhits++;
393 >                source[cntord[sn].sno].nhits++;
394          }
395                                          /* weighted hit rate */
396          hwt = hit2 / test2;
397   #ifdef DEBUG
398 <        {
399 <                int  ntested = sn;
398 >        sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
399 >                        sn, ncnts-sn, hwt);
400 >        eputs(errmsg);
401   #endif
402                                          /* add in untested sources */
403 <        for ( ; sn < nsources; sn++) {
404 <                if (srccnt[sn].brt <= 0.0)
405 <                        break;
406 <                dtmp = hwt * (double)source[srccnt[sn].sno].nhits /
407 <                                (double)source[srccnt[sn].sno].ntests;
381 <                scalecolor(srccnt[sn].val, dtmp);
382 <                addcolor(r->rcol, srccnt[sn].val);
403 >        for ( ; sn < ncnts; sn++) {
404 >                prob = hwt * (double)source[cntord[sn].sno].nhits /
405 >                                (double)source[cntord[sn].sno].ntests;
406 >                scalecolor(srccnt[cntord[sn].sno].val, prob);
407 >                addcolor(r->rcol, srccnt[cntord[sn].sno].val);
408          }
384 #ifdef DEBUG
385        fprintf(stderr, "%d tested, %d untested, %f hit rate\n",
386                        ntested, sn-ntested, hwt);
387        }
388 #endif
409                  
410 <        free(srccnt);
410 >        free((char *)srccnt);
411 >        free((char *)cntord);
412   }
413  
414  
# Line 407 | Line 428 | m_light(m, r)                  /* ray hit a light source */
428   register OBJREC  *m;
429   register RAY  *r;
430   {
410                                                /* check for behind */
411        if (r->rod < 0.0)
412                return;
431                                                  /* check for over-counting */
432          if (wrongsource(m, r) || badambient(m, r))
433                  return;
# Line 423 | Line 441 | register RAY  *r;
441  
442                                                  /* otherwise treat as source */
443          } else {
444 +                                                /* check for behind */
445 +                if (r->rod < 0.0)
446 +                        return;
447                                                  /* get distribution pattern */
448                  raytexture(r, m->omod);
449                                                  /* get source color */
# Line 431 | Line 452 | register RAY  *r;
452                                    m->oargs.farg[2]);
453                                                  /* modify value */
454                  multcolor(r->rcol, r->pcol);
455 +                                                /* assign distance */
456 +                r->rt = r->rot;
457          }
458   }
436
437
438 o_source() {}           /* intersection with a source is done elsewhere */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines