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.58 by greg, Tue Apr 19 01:15:06 2005 UTC vs.
Revision 2.69 by greg, Mon Jun 27 22:10:37 2011 UTC

# Line 25 | Line 25 | static const char      RCSid[] = "$Id$";
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27   #ifndef  MAXASET
28 < #define  MAXASET        2047    /* maximum number of elements in ambient set */
28 > #define  MAXASET        4095    /* maximum number of elements in ambient set */
29   #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
# Line 140 | Line 140 | extern void
140   setambient(void)                                /* initialize calculation */
141   {
142          int     readonly = 0;
143 <        long  pos, flen;
143 >        long    flen;
144          AMBVAL  amb;
145                                                  /* make sure we're fresh */
146          ambdone();
# Line 160 | Line 160 | setambient(void)                               /* initialize calculation */
160                  readonly = (ambfp = fopen(ambfile, "r")) != NULL;
161          if (ambfp != NULL) {
162                  initambfile(0);                 /* file exists */
163 <                pos = ftell(ambfp);
163 >                lastpos = ftell(ambfp);
164                  while (readambval(&amb, ambfp))
165                          avinsert(avstore(&amb));
166                  nambshare = nambvals;           /* share loaded values */
# Line 174 | Line 174 | setambient(void)                               /* initialize calculation */
174                          return;                 /* avoid ambsync() */
175                  }
176                                                  /* align file pointer */
177 <                pos += (long)nambvals*AMBVALSIZ;
177 >                lastpos += (long)nambvals*AMBVALSIZ;
178                  flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
179 <                if (flen != pos) {
179 >                if (flen != lastpos) {
180                          sprintf(errmsg,
181                          "ignoring last %ld values in ambient file (corrupted)",
182 <                                        (flen - pos)/AMBVALSIZ);
182 >                                        (flen - lastpos)/AMBVALSIZ);
183                          error(WARNING, errmsg);
184 <                        fseek(ambfp, pos, 0);
184 >                        fseek(ambfp, lastpos, SEEK_SET);
185   #ifndef _WIN32 /* XXX we need a replacement for that one */
186 <                        ftruncate(fileno(ambfp), (off_t)pos);
186 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
187   #endif
188                  }
189          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190                  initambfile(1);                 /* else create new file */
191 +                fflush(ambfp);
192 +                lastpos = ftell(ambfp);
193          } else {
194                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
195                  error(SYSTEM, errmsg);
196          }
197 <        nunflshed++;    /* lie */
198 <        ambsync();
197 > #ifdef  F_SETLKW
198 >        aflock(F_UNLCK);                        /* release file */
199 > #endif
200   }
201  
202  
# Line 275 | Line 278 | multambient(           /* compute ambient component & multiply
278                  goto dumbamb;
279  
280          if (ambacc <= FTINY) {                  /* no ambient storage */
281 +                copycolor(acol, aval);
282                  rdepth++;
283 <                d = doambient(acol, r, aval, intens(aval)*r->rweight,
280 <                                                        NULL, NULL);
283 >                d = doambient(acol, r, r->rweight, NULL, NULL);
284                  rdepth--;
285                  if (d <= FTINY)
286                          goto dumbamb;
287 <                multcolor(aval, acol);
287 >                copycolor(aval, acol);
288                  return;
289          }
290  
291          if (tracktime)                          /* sort to minimize thrashing */
292                  sortambvals(0);
293 <                                                /* get ambient value */
293 >                                                /* interpolate ambient value */
294          setcolor(acol, 0.0, 0.0, 0.0);
295          d = sumambient(acol, r, nrm, rdepth,
296                          &atrunk, thescene.cuorg, thescene.cusize);
297          if (d > FTINY) {
298 <                scalecolor(acol, 1.0/d);
298 >                d = 1.0/d;
299 >                scalecolor(acol, d);
300                  multcolor(aval, acol);
301                  return;
302          }
303          rdepth++;                               /* need to cache new value */
304 <        d = makeambient(acol, r, aval, nrm, rdepth-1);
304 >        d = makeambient(acol, r, nrm, rdepth-1);
305          rdepth--;
306          if (d > FTINY) {
307                  multcolor(aval, acol);          /* got new value */
# Line 351 | Line 355 | sumambient(    /* get interpolated ambient value */
355                   */
356                  if (av->lvl > al)       /* list sorted, so this works */
357                          break;
358 <                if (av->weight < r->rweight-FTINY)
358 >                if (av->weight < 0.9*r->rweight)
359                          continue;
360                  /*
361                   *  Ambient radius test.
# Line 430 | Line 434 | sumambient(    /* get interpolated ambient value */
434                                  break;
435                  }
436                  if (j == 3)
437 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
437 >                        wsum += sumambient(acol, r, rn, al,
438 >                                                at->kid+i, ck0, s);
439          }
440          return(wsum);
441   }
442  
443  
444   extern double
445 < makeambient(    /* make a new ambient value */
445 > makeambient(            /* make a new ambient value for storage */
446          COLOR  acol,
447          RAY  *r,
443        COLOR  ac,
448          FVECT  rn,
449          int  al
450   )
451   {
452          AMBVAL  amb;
449        double  coef;
453          FVECT   gp, gd;
454 <                                                /* compute weight */
455 <        amb.weight = pow(AVGREFL, (double)al);
456 <        coef = intens(ac)*r->rweight;
457 <        if (coef < 0.1*amb.weight)              /* heuristic */
458 <                amb.weight = coef;
454 >        int     i;
455 >
456 >        amb.weight = 1.0;                       /* compute weight */
457 >        for (i = al; i-- > 0; )
458 >                amb.weight *= AVGREFL;
459 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
460 >                amb.weight = 1.25*r->rweight;
461 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
462                                                  /* compute ambient */
463 <        amb.rad = doambient(acol, r, ac, amb.weight, gp, gd);
464 <        if (amb.rad <= FTINY)
463 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
464 >        if (amb.rad <= FTINY) {
465 >                setcolor(acol, 0.0, 0.0, 0.0);
466                  return(0.0);
467 <                                                /* store it */
467 >        }
468 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
469 >                                                /* store value */
470          VCOPY(amb.pos, r->rop);
471          VCOPY(amb.dir, r->ron);
472          amb.lvl = al;
# Line 502 | Line 511 | extambient(            /* extrapolate value at pv, nv */
511  
512   static void
513   initambfile(            /* initialize ambient file */
514 <        int  creat
514 >        int  cre8
515   )
516   {
517          extern char  *progname, *octname;
518          static char  *mybuf = NULL;
519  
520   #ifdef  F_SETLKW
521 <        aflock(creat ? F_WRLCK : F_RDLCK);
521 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
522   #endif
523          SET_FILE_BINARY(ambfp);
524          if (mybuf == NULL)
525                  mybuf = (char *)bmalloc(BUFSIZ+8);
526          setbuf(ambfp, mybuf);
527 <        if (creat) {                    /* new file */
527 >        if (cre8) {                     /* new file */
528                  newheader("RADIANCE", ambfp);
529                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
530                                  progname, colval(ambval,RED),
# Line 524 | Line 533 | initambfile(           /* initialize ambient file */
533                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
534                                  ambdiv, ambssamp, ambres);
535                  if (octname != NULL)
536 <                        printargs(1, &octname, ambfp);
537 <                else
529 <                        fputc('\n', ambfp);
536 >                        fputs(octname, ambfp);
537 >                fputc('\n', ambfp);
538                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
539                  fputnow(ambfp);
540                  fputformat(AMBFMT, ambfp);
541 <                putc('\n', ambfp);
541 >                fputc('\n', ambfp);
542                  putambmagic(ambfp);
543          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
544                  error(USER, "bad ambient file");
# Line 858 | Line 866 | aflock(                        /* lock/unlock ambient file */
866   {
867          static struct flock  fls;       /* static so initialized to zeroes */
868  
869 +        if (typ == fls.l_type)          /* already called? */
870 +                return;
871          fls.l_type = typ;
872          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
873                  error(SYSTEM, "cannot (un)lock ambient file");
# Line 871 | Line 881 | ambsync(void)                  /* synchronize ambient file */
881          AMBVAL  avs;
882          register int  n;
883  
884 <        if (nunflshed == 0)
884 >        if (ambfp == NULL)      /* no ambient file? */
885                  return(0);
886 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
887 <                goto syncend;
878 <                                /* gain exclusive access */
879 <        aflock(F_WRLCK);
886 >                                /* gain appropriate access */
887 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
888                                  /* see if file has grown */
889          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
890                  goto seekerr;
891 <        if ( (n = flen - lastpos) ) {           /* file has grown */
891 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
892                  if (ambinp == NULL) {           /* use duplicate filedes */
893                          ambinp = fdopen(dup(fileno(ambfp)), "r");
894                          if (ambinp == NULL)
895                                  error(SYSTEM, "fdopen failed in ambsync");
896                  }
897 <                if (fseek(ambinp, lastpos, 0) < 0)
897 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
898                          goto seekerr;
899                  while (n >= AMBVALSIZ) {        /* load contributed values */
900                          if (!readambval(&avs, ambinp)) {
# Line 899 | Line 907 | ambsync(void)                  /* synchronize ambient file */
907                          avinsert(avstore(&avs));
908                          n -= AMBVALSIZ;
909                  }
910 +                lastpos = flen - n;
911                  /*** seek always as safety measure
912                  if (n) ***/                     /* alignment */
913 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
913 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
914                                  goto seekerr;
915          }
907 #ifdef  DEBUG
908        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
909                sprintf(errmsg, "ambient file buffer at %d rather than %d",
910                                ambfp->_ptr - ambfp->_base,
911                                nunflshed*AMBVALSIZ);
912                error(CONSISTENCY, errmsg);
913        }
914 #endif
915 syncend:
916          n = fflush(ambfp);                      /* calls write() at last */
917 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
917 >        if (n != EOF)
918 >                lastpos += (long)nunflshed*AMBVALSIZ;
919 >        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
920                  goto seekerr;
921 +                
922          aflock(F_UNLCK);                        /* release file */
923          nunflshed = 0;
924          return(n);
# Line 929 | Line 932 | seekerr:
932   extern int
933   ambsync(void)                   /* flush ambient file */
934   {
935 <        if (nunflshed == 0)
935 >        if (ambfp == NULL)
936                  return(0);
937          nunflshed = 0;
938          return(fflush(ambfp));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines