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

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.86 by greg, Wed May 7 01:16:03 2014 UTC vs.
Revision 2.102 by greg, Sun Apr 24 16:21:32 2016 UTC

# Line 1 | Line 1
1 #ifndef lint
1   static const char       RCSid[] = "$Id$";
3 #endif
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4   *
# Line 17 | Line 15 | static const char      RCSid[] = "$Id$";
15   #include  "resolu.h"
16   #include  "ambient.h"
17   #include  "random.h"
18 + #include  "pmapamb.h"
19  
20   #ifndef  OCTSCALE
21   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
# Line 77 | Line 76 | static long  lastpos = -1;             /* last flush position */
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
80 #define  freeav(av)     free((void *)av);
79  
80   static void initambfile(int creat);
81   static void avsave(AMBVAL *av);
# Line 109 | Line 107 | setambres(                             /* set ambient resolution */
107                                                  /* set min & max radii */
108          if (ar <= 0) {
109                  minarad = 0;
110 <                maxarad = thescene.cusize*0.5;
110 >                maxarad = thescene.cusize*0.2;
111          } else {
112                  minarad = thescene.cusize / ar;
113                  maxarad = 64.0 * minarad;               /* heuristic */
114 <                if (maxarad > thescene.cusize*0.5)
115 <                        maxarad = thescene.cusize*0.5;
114 >                if (maxarad > thescene.cusize*0.2)
115 >                        maxarad = thescene.cusize*0.2;
116          }
117          if (minarad <= FTINY)
118                  minarad = 10.0*FTINY;
# Line 165 | Line 163 | setambient(void)                               /* initialize calculation */
163                  initambfile(0);                 /* file exists */
164                  lastpos = ftell(ambfp);
165                  while (readambval(&amb, ambfp))
166 <                        avinsert(avstore(&amb));
166 >                        avstore(&amb);
167                  nambshare = nambvals;           /* share loaded values */
168                  if (readonly) {
169                          sprintf(errmsg,
# Line 185 | Line 183 | setambient(void)                               /* initialize calculation */
183                                          (flen - lastpos)/AMBVALSIZ);
184                          error(WARNING, errmsg);
185                          fseek(ambfp, lastpos, SEEK_SET);
188 #ifndef _WIN32 /* XXX we need a replacement for that one */
186                          ftruncate(fileno(ambfp), (off_t)lastpos);
190 #endif
187                  }
188          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
189                  initambfile(1);                 /* else create new file */
# Line 197 | Line 193 | setambient(void)                               /* initialize calculation */
193                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
194                  error(SYSTEM, errmsg);
195          }
200 #ifdef getc_unlocked
201        flockfile(ambfp);                       /* application-level lock */
202 #endif
196   #ifdef  F_SETLKW
197          aflock(F_UNLCK);                        /* release file */
198   #endif
# Line 220 | Line 213 | ambdone(void)                  /* close ambient file and free memory
213                  lastpos = -1;
214          }
215                                          /* free ambient tree */
216 <        unloadatree(&atrunk, &avfree);
216 >        unloadatree(&atrunk, avfree);
217                                          /* reset state variables */
218          avsum = 0.;
219          navsum = 0;
# Line 263 | Line 256 | ambnotify(                     /* record new modifier */
256  
257   /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
258  
259 < #ifdef NEWAMB
259 > #ifndef OLDAMB
260  
261   #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
262  
# Line 271 | Line 264 | static int     plugaleak(RAY *r, AMBVAL *ap, FVECT anorm,
264   static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
265                                  AMBTREE *at, FVECT c0, double s);
266   static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
267 < static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
267 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
268                                  FVECT uvw[3]);
269  
270   void
# Line 282 | Line 275 | multambient(           /* compute ambient component & multiply
275   )
276   {
277          static int  rdepth = 0;                 /* ambient recursion */
278 <        COLOR   acol;
278 >        COLOR   acol, caustic;
279          int     ok;
280          double  d, l;
281  
282 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
283 +         * ambient. Return as all ambient components accounted for, else
284 +         * continue. */
285 +        if (ambPmap(aval, r, rdepth))
286 +                return;
287 +
288 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
289 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
290 +         * with RADIANCE ambient calculation */
291 +        copycolor(caustic, aval);
292 +        ambPmapCaustic(caustic, r, rdepth);
293 +        
294          if (ambdiv <= 0)                        /* no ambient calculation */
295                  goto dumbamb;
296                                                  /* check number of bounces */
# Line 305 | Line 310 | multambient(           /* compute ambient component & multiply
310                  if (!ok)
311                          goto dumbamb;
312                  copycolor(aval, acol);
313 +
314 +                /* PMAP: add in caustic */
315 +                addcolor(aval, caustic);
316                  return;
317          }
318  
# Line 314 | Line 322 | multambient(           /* compute ambient component & multiply
322          setcolor(acol, 0.0, 0.0, 0.0);
323          d = sumambient(acol, r, nrm, rdepth,
324                          &atrunk, thescene.cuorg, thescene.cusize);
325 +                        
326          if (d > FTINY) {
327                  d = 1.0/d;
328                  scalecolor(acol, d);
329                  multcolor(aval, acol);
330 +
331 +                /* PMAP: add in caustic */
332 +                addcolor(aval, caustic);
333                  return;
334          }
335 +        
336          rdepth++;                               /* need to cache new value */
337          ok = makeambient(acol, r, nrm, rdepth-1);
338          rdepth--;
339 +        
340          if (ok) {
341                  multcolor(aval, acol);          /* computed new value */
342 +
343 +                /* PMAP: add in caustic */
344 +                addcolor(aval, caustic);
345                  return;
346          }
347 +        
348   dumbamb:                                        /* return global value */
349          if ((ambvwt <= 0) | (navsum == 0)) {
350                  multcolor(aval, ambval);
351 +                
352 +                /* PMAP: add in caustic */
353 +                addcolor(aval, caustic);
354                  return;
355          }
356 <        l = bright(ambval);                     /* average in computations */
356 >        
357 >        l = bright(ambval);                     /* average in computations */  
358          if (l > FTINY) {
359                  d = (log(l)*(double)ambvwt + avsum) /
360                                  (double)(ambvwt + navsum);
# Line 443 | Line 465 | sumambient(            /* get interpolated ambient value */
465                  /*
466                   *  Ambient level test
467                   */
468 <                if (av->lvl > al)       /* list sorted, so this works */
468 >                if (av->lvl > al ||     /* list sorted, so this works */
469 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
470                          break;
448                if (av->weight < 0.9*r->rweight)
449                        continue;
471                  /*
472                   *  Direction test using unperturbed normal
473                   */
# Line 487 | Line 508 | sumambient(            /* get interpolated ambient value */
508                  /*
509                   *  Extrapolate value and compute final weight (hat function)
510                   */
511 <                extambient(ct, av, r->rop, rn, uvw);
511 >                if (!extambient(ct, av, r->rop, rn, uvw))
512 >                        continue;
513                  d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
514                          tfunc(ambacc, sqrt(delta_t2), 0.0);
515                  scalecolor(ct, d);
# Line 538 | Line 560 | makeambient(           /* make a new ambient value for storage
560   }
561  
562  
563 < static void
563 > static int
564   extambient(             /* extrapolate value at pv, nv */
565          COLOR  cr,
566          AMBVAL   *ap,
# Line 547 | Line 569 | extambient(            /* extrapolate value at pv, nv */
569          FVECT  uvw[3]
570   )
571   {
572 +        const double    min_d = 0.05;
573          static FVECT    my_uvw[3];
574          FVECT           v1;
575          int             i;
# Line 566 | Line 589 | extambient(            /* extrapolate value at pv, nv */
589          for (i = 3; i--; )
590                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
591          
592 <        if (d <= 0.0) {
593 <                setcolor(cr, 0.0, 0.0, 0.0);
571 <                return;
572 <        }
592 >        if (d < min_d)                  /* should not use if we can avoid it */
593 >                d = min_d;
594          copycolor(cr, ap->val);
595          scalecolor(cr, d);
596 +        return(d > min_d);
597   }
598  
599  
# Line 608 | Line 630 | avinsert(                              /* insert ambient value in our tree */
630          }
631          avh.next = at->alist;           /* order by increasing level */
632          for (ap = &avh; ap->next != NULL; ap = ap->next)
633 <                if (ap->next->lvl >= av->lvl)
633 >                if ( ap->next->lvl > av->lvl ||
634 >                                (ap->next->lvl == av->lvl) &
635 >                                (ap->next->weight <= av->weight) )
636                          break;
637          av->next = ap->next;
638          ap->next = (AMBVAL*)av;
# Line 632 | Line 656 | multambient(           /* compute ambient component & multiply
656   )
657   {
658          static int  rdepth = 0;                 /* ambient recursion */
659 <        COLOR   acol;
659 >        COLOR   acol, caustic;
660          double  d, l;
661  
662 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
663 +         * as all ambient components accounted for */
664 +        if (ambPmap(aval, r, rdepth))
665 +                return;
666 +
667 +        /* PMAP: Otherwise factor in ambient from caustic photon map
668 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
669 +         * continue with RADIANCE ambient calculation */
670 +        copycolor(caustic, aval);
671 +        ambPmapCaustic(caustic, r, rdepth);
672 +        
673          if (ambdiv <= 0)                        /* no ambient calculation */
674                  goto dumbamb;
675                                                  /* check number of bounces */
# Line 652 | Line 687 | multambient(           /* compute ambient component & multiply
687                  rdepth--;
688                  if (d <= FTINY)
689                          goto dumbamb;
690 <                copycolor(aval, acol);
690 >                copycolor(aval, acol);          
691 >        
692 >           /* PMAP: add in caustic */
693 >                addcolor(aval, caustic);        
694                  return;
695          }
696  
# Line 662 | Line 700 | multambient(           /* compute ambient component & multiply
700          setcolor(acol, 0.0, 0.0, 0.0);
701          d = sumambient(acol, r, nrm, rdepth,
702                          &atrunk, thescene.cuorg, thescene.cusize);
703 +                        
704          if (d > FTINY) {
705                  d = 1.0/d;
706                  scalecolor(acol, d);
707                  multcolor(aval, acol);
708 +                
709 +                /* PMAP: add in caustic */
710 +                addcolor(aval, caustic);        
711                  return;
712          }
713 +        
714          rdepth++;                               /* need to cache new value */
715          d = makeambient(acol, r, nrm, rdepth-1);
716          rdepth--;
717 +        
718          if (d > FTINY) {
719                  multcolor(aval, acol);          /* got new value */
720 +
721 +                /* PMAP: add in caustic */
722 +                addcolor(aval, caustic);                        
723                  return;
724          }
725 +        
726   dumbamb:                                        /* return global value */
727          if ((ambvwt <= 0) | (navsum == 0)) {
728                  multcolor(aval, ambval);
729 +
730 +                /* PMAP: add in caustic */
731 +                addcolor(aval, caustic);        
732                  return;
733          }
734 +        
735          l = bright(ambval);                     /* average in computations */
736          if (l > FTINY) {
737                  d = (log(l)*(double)ambvwt + avsum) /
# Line 721 | Line 773 | sumambient(    /* get interpolated ambient value */
773                  /*
774                   *  Ambient level test.
775                   */
776 <                if (av->lvl > al)       /* list sorted, so this works */
776 >                if (av->lvl > al ||     /* list sorted, so this works */
777 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
778                          break;
726                if (av->weight < 0.9*r->rweight)
727                        continue;
779                  /*
780                   *  Ambient radius test.
781                   */
# Line 906 | Line 957 | avinsert(                              /* insert ambient value in our tree */
957          }
958          avh.next = at->alist;           /* order by increasing level */
959          for (ap = &avh; ap->next != NULL; ap = ap->next)
960 <                if (ap->next->lvl >= av->lvl)
960 >                if ( ap->next->lvl > av->lvl ||
961 >                                (ap->next->lvl == av->lvl) &
962 >                                (ap->next->weight <= av->weight) )
963                          break;
964          av->next = ap->next;
965          ap->next = (AMBVAL*)av;
# Line 958 | Line 1011 | avsave(                                /* insert and save an ambient value */
1011          AMBVAL  *av
1012   )
1013   {
1014 <        avinsert(avstore(av));
1014 >        avstore(av);
1015          if (ambfp == NULL)
1016                  return;
1017          if (writambval(av, ambfp) < 0)
# Line 973 | Line 1026 | writerr:
1026  
1027  
1028   static AMBVAL *
1029 < avstore(                                /* allocate memory and store aval */
1029 > avstore(                                /* allocate memory and save aval */
1030          AMBVAL  *aval
1031   )
1032   {
# Line 991 | Line 1044 | avstore(                               /* allocate memory and store aval */
1044                  avsum += log(d);
1045                  navsum++;
1046          }
1047 +        avinsert(av);                   /* insert in our cache tree */
1048          return(av);
1049   }
1050  
# Line 1017 | Line 1071 | newambtree(void)                               /* allocate 8 ambient tree structs
1071          }
1072          atp = atfreelist;
1073          atfreelist = atp->kid;
1074 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1074 >        memset(atp, 0, 8*sizeof(AMBTREE));
1075          return(atp);
1076   }
1077  
# Line 1043 | Line 1097 | unloadatree(                   /* unload an ambient value tree */
1097                                          /* transfer values at this node */
1098          for (av = at->alist; av != NULL; av = at->alist) {
1099                  at->alist = av->next;
1100 +                av->next = NULL;
1101                  (*f)(av);
1102          }
1103          if (at->kid == NULL)
# Line 1120 | Line 1175 | avlmemi(                               /* find list position from address */
1175   {
1176          AMBVAL  **avlpp;
1177  
1178 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1179 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
1178 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1179 >                        nambvals, sizeof(AMBVAL *), aposcmp);
1180          if (avlpp == NULL)
1181                  error(CONSISTENCY, "address not found in avlmemi");
1182          return(avlpp - avlist2);
# Line 1164 | Line 1219 | sortambvals(                   /* resort ambient values */
1219          }
1220          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1221                  if (avlist2 != NULL)
1222 <                        free((void *)avlist2);
1222 >                        free(avlist2);
1223                  if (always) {           /* rebuild without sorting */
1224                          oldatrunk = atrunk;
1225                          atrunk.alist = NULL;
1226                          atrunk.kid = NULL;
1227 <                        unloadatree(&oldatrunk, &avinsert);
1227 >                        unloadatree(&oldatrunk, avinsert);
1228                  }
1229          } else {                        /* sort memory by last access time */
1230                  /*
# Line 1186 | Line 1241 | sortambvals(                   /* resort ambient values */
1241                  eputs(errmsg);
1242   #endif
1243                  i_avlist = 0;
1244 <                unloadatree(&atrunk, &av2list); /* empty current tree */
1244 >                unloadatree(&atrunk, av2list);  /* empty current tree */
1245   #ifdef DEBUG
1246                  if (i_avlist < nambvals)
1247                          error(CONSISTENCY, "missing ambient values in sortambvals");
1248   #endif
1249 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp);
1250 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp);
1249 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1250 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1251                  for (i = 0; i < nambvals; i++) {
1252                          if (avlist1[i].p == NULL)
1253                                  continue;
# Line 1208 | Line 1263 | sortambvals(                   /* resort ambient values */
1263                          avinsert(avlist2[j]);
1264                          avlist1[j].p = NULL;
1265                  }
1266 <                free((void *)avlist1);
1267 <                free((void *)avlist2);
1266 >                free(avlist1);
1267 >                free(avlist2);
1268                                                  /* compute new sort interval */
1269                  sortintvl = ambclock - lastsort;
1270                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1273 | Line 1328 | ambsync(void)                  /* synchronize ambient file */
1328                                  error(WARNING, errmsg);
1329                                  break;
1330                          }
1331 <                        avinsert(avstore(&avs));
1331 >                        avstore(&avs);
1332                          n -= AMBVALSIZ;
1333                  }
1334                  lastpos = flen - n;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines