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.49 by greg, Thu May 15 05:13:35 2003 UTC vs.
Revision 2.60 by greg, Sat May 28 22:27:54 2005 UTC

# Line 9 | Line 9 | static const char      RCSid[] = "$Id$";
9  
10   #include "copyright.h"
11  
12 < #include  "ray.h"
12 > #include <string.h>
13  
14 + #include  "platform.h"
15 + #include  "ray.h"
16   #include  "otypes.h"
17 <
17 > #include  "resolu.h"
18   #include  "ambient.h"
17
19   #include  "random.h"
20  
21   #ifndef  OCTSCALE
# Line 23 | Line 24 | static const char      RCSid[] = "$Id$";
24  
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27 < #define  MAXASET        511     /* maximum number of elements in ambient set */
27 > #ifndef  MAXASET
28 > #define  MAXASET        2047    /* maximum number of elements in ambient set */
29 > #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
32   double  maxarad;                /* maximum ambient radius */
# Line 75 | Line 78 | static long  lastpos = -1;             /* last flush position */
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79   #define  freeav(av)     free((void *)av);
80  
81 < static void  initambfile(), avsave(), avinsert(), sortambvals(), unloadatree();
82 < static int  avlmemi();
83 < static AMBVAL  *avstore();
81 > static void initambfile(int creat);
82 > static void avsave(AMBVAL *av);
83 > static AMBVAL *avstore(AMBVAL  *aval);
84 > static AMBTREE *newambtree(void);
85 > static void freeambtree(AMBTREE  *atp);
86 >
87 > typedef void unloadtf_t(void *);
88 > static unloadtf_t avinsert;
89 > static unloadtf_t av2list;
90 > static void unloadatree(AMBTREE  *at, unloadtf_t *f);
91 >
92 > static int aposcmp(const void *avp1, const void *avp2);
93 > static int avlmemi(AMBVAL *avaddr);
94 > static void sortambvals(int always);
95 >
96   #ifdef  F_SETLKW
97 < static void  aflock();
97 > static void aflock(int  typ);
98   #endif
99  
100  
101 < void
102 < setambres(ar)                           /* set ambient resolution */
103 < int  ar;
101 > extern void
102 > setambres(                              /* set ambient resolution */
103 >        int  ar
104 > )
105   {
106          ambres = ar < 0 ? 0 : ar;               /* may be done already */
107                                                  /* set min & max radii */
# Line 105 | Line 121 | int  ar;
121   }
122  
123  
124 < void
125 < setambacc(newa)                         /* set ambient accuracy */
126 < double  newa;
124 > extern void
125 > setambacc(                              /* set ambient accuracy */
126 >        double  newa
127 > )
128   {
129          double  ambdiff;
130  
# Line 119 | Line 136 | double  newa;
136   }
137  
138  
139 < void
140 < setambient()                            /* initialize calculation */
139 > extern void
140 > setambient(void)                                /* initialize calculation */
141   {
142          int     readonly = 0;
143          long  pos, flen;
# Line 158 | Line 175 | setambient()                           /* initialize calculation */
175                  }
176                                                  /* align file pointer */
177                  pos += (long)nambvals*AMBVALSIZ;
178 <                flen = lseek(fileno(ambfp), (off_t)0L, 2);
178 >                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
179                  if (flen != pos) {
180                          sprintf(errmsg,
181                          "ignoring last %ld values in ambient file (corrupted)",
182                                          (flen - pos)/AMBVALSIZ);
183                          error(WARNING, errmsg);
184                          fseek(ambfp, pos, 0);
185 + #ifndef _WIN32 /* XXX we need a replacement for that one */
186                          ftruncate(fileno(ambfp), (off_t)pos);
187 + #endif
188                  }
189          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190                  initambfile(1);                 /* else create new file */
# Line 178 | Line 197 | setambient()                           /* initialize calculation */
197   }
198  
199  
200 < void
201 < ambdone()                       /* close ambient file and free memory */
200 > extern void
201 > ambdone(void)                   /* close ambient file and free memory */
202   {
203          if (ambfp != NULL) {            /* close ambient file */
204                  ambsync();
# Line 204 | Line 223 | ambdone()                      /* close ambient file and free memory */
223   }
224  
225  
226 < void
227 < ambnotify(obj)                  /* record new modifier */
228 < OBJECT  obj;
226 > extern void
227 > ambnotify(                      /* record new modifier */
228 >        OBJECT  obj
229 > )
230   {
231          static int  hitlimit = 0;
232          register OBJREC  *o;
# Line 233 | Line 253 | OBJECT obj;
253   }
254  
255  
256 < void
257 < ambient(acol, r, nrm)           /* compute ambient component for ray */
258 < COLOR  acol;
259 < register RAY  *r;
260 < FVECT  nrm;
256 > extern void
257 > multambient(            /* compute ambient component & multiply by coef. */
258 >        COLOR  aval,
259 >        register RAY  *r,
260 >        FVECT  nrm
261 > )
262   {
263          static int  rdepth = 0;                 /* ambient recursion */
264 +        COLOR   acol;
265          double  d, l;
266  
267          if (ambdiv <= 0)                        /* no ambient calculation */
# Line 254 | Line 276 | FVECT  nrm;
276  
277          if (ambacc <= FTINY) {                  /* no ambient storage */
278                  rdepth++;
279 <                d = doambient(acol, r, r->rweight, NULL, NULL);
279 >                d = doambient(acol, r, aval, intens(aval)*r->rweight,
280 >                                                        NULL, NULL);
281                  rdepth--;
282                  if (d <= FTINY)
283                          goto dumbamb;
284 +                multcolor(aval, acol);
285                  return;
286          }
287  
# Line 265 | Line 289 | FVECT  nrm;
289                  sortambvals(0);
290                                                  /* get ambient value */
291          setcolor(acol, 0.0, 0.0, 0.0);
292 <        d = sumambient(acol, r, nrm, rdepth,
292 >        d = sumambient(acol, r, intens(aval)*r->rweight, nrm, rdepth,
293                          &atrunk, thescene.cuorg, thescene.cusize);
294          if (d > FTINY) {
295                  scalecolor(acol, 1.0/d);
296 +                multcolor(aval, acol);
297                  return;
298          }
299          rdepth++;                               /* need to cache new value */
300 <        d = makeambient(acol, r, nrm, rdepth-1);
300 >        d = makeambient(acol, r, aval, nrm, rdepth-1);
301          rdepth--;
302 <        if (d > FTINY)
302 >        if (d > FTINY) {
303 >                multcolor(aval, acol);          /* got new value */
304                  return;
305 +        }
306   dumbamb:                                        /* return global value */
307 <        copycolor(acol, ambval);
308 <        if (ambvwt <= 0 | navsum == 0)
307 >        if ((ambvwt <= 0) | (navsum == 0)) {
308 >                multcolor(aval, ambval);
309                  return;
310 +        }
311          l = bright(ambval);                     /* average in computations */
312          if (l > FTINY) {
313                  d = (log(l)*(double)ambvwt + avsum) /
314                                  (double)(ambvwt + navsum);
315                  d = exp(d) / l;
316 <                scalecolor(acol, d);            /* apply color of ambval */
316 >                scalecolor(aval, d);
317 >                multcolor(aval, ambval);        /* apply color of ambval */
318          } else {
319                  d = exp( avsum / (double)navsum );
320 <                setcolor(acol, d, d, d);        /* neutral color */
320 >                scalecolor(aval, d);            /* neutral color */
321          }
322   }
323  
324  
325 < double
326 < sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
327 < COLOR  acol;
328 < register RAY  *r;
329 < FVECT  rn;
330 < int  al;
331 < AMBTREE  *at;
332 < FVECT  c0;
333 < double  s;
325 > extern double
326 > sumambient(     /* get interpolated ambient value */
327 >        COLOR  acol,
328 >        register RAY  *r,
329 >        double  aw,
330 >        FVECT  rn,
331 >        int  al,
332 >        AMBTREE  *at,
333 >        FVECT  c0,
334 >        double  s
335 > )
336   {
337          double  d, e1, e2, wt, wsum;
338          COLOR  ct;
# Line 321 | Line 352 | double s;
352                   */
353                  if (av->lvl > al)       /* list sorted, so this works */
354                          break;
355 <                if (av->weight < r->rweight-FTINY)
355 >                if (av->weight < 0.85*aw)
356                          continue;
357                  /*
358                   *  Ambient radius test.
# Line 400 | Line 431 | double s;
431                                  break;
432                  }
433                  if (j == 3)
434 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
434 >                        wsum += sumambient(acol, r, aw, rn, al,
435 >                                                at->kid+i, ck0, s);
436          }
437          return(wsum);
438   }
439  
440  
441 < double
442 < makeambient(acol, r, rn, al)    /* make a new ambient value */
443 < COLOR  acol;
444 < register RAY  *r;
445 < FVECT  rn;
446 < int  al;
441 > extern double
442 > makeambient(            /* make a new ambient value */
443 >        COLOR  acol,
444 >        RAY  *r,
445 >        COLOR  ac,
446 >        FVECT  rn,
447 >        int  al
448 > )
449   {
450          AMBVAL  amb;
451 +        double  awt;
452          FVECT   gp, gd;
453 <                                                /* compute weight */
454 <        amb.weight = pow(AVGREFL, (double)al);
455 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
456 <                amb.weight = r->rweight;
453 >        int     i;
454 >
455 >        amb.weight = AVGREFL;                   /* compute weight */
456 >        for (i = al; i-- >= 0; )
457 >                amb.weight *= AVGREFL;
458 >        awt = intens(ac)*r->rweight;
459 >        if (awt < 0.07*amb.weight)              /* heuristic override */
460 >                amb.weight = 1.2*awt;
461                                                  /* compute ambient */
462 <        amb.rad = doambient(acol, r, amb.weight, gp, gd);
462 >        amb.rad = doambient(acol, r, ac, amb.weight, gp, gd);
463          if (amb.rad <= FTINY)
464                  return(0.0);
465                                                  /* store it */
# Line 438 | Line 477 | int  al;
477   }
478  
479  
480 < void
481 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
482 < COLOR  cr;
483 < register AMBVAL  *ap;
484 < FVECT  pv, nv;
480 > extern void
481 > extambient(             /* extrapolate value at pv, nv */
482 >        COLOR  cr,
483 >        register AMBVAL  *ap,
484 >        FVECT  pv,
485 >        FVECT  nv
486 > )
487   {
488          FVECT  v1;
489          register int  i;
# Line 465 | Line 506 | FVECT  pv, nv;
506  
507  
508   static void
509 < initambfile(creat)              /* initialize ambient file */
510 < int  creat;
509 > initambfile(            /* initialize ambient file */
510 >        int  creat
511 > )
512   {
513          extern char  *progname, *octname;
514          static char  *mybuf = NULL;
# Line 474 | Line 516 | int  creat;
516   #ifdef  F_SETLKW
517          aflock(creat ? F_WRLCK : F_RDLCK);
518   #endif
519 < #ifdef MSDOS
478 <        setmode(fileno(ambfp), O_BINARY);
479 < #endif
519 >        SET_FILE_BINARY(ambfp);
520          if (mybuf == NULL)
521                  mybuf = (char *)bmalloc(BUFSIZ+8);
522          setbuf(ambfp, mybuf);
# Line 503 | Line 543 | int  creat;
543  
544  
545   static void
546 < avsave(av)                              /* insert and save an ambient value */
547 < AMBVAL  *av;
546 > avsave(                         /* insert and save an ambient value */
547 >        AMBVAL  *av
548 > )
549   {
550          avinsert(avstore(av));
551          if (ambfp == NULL)
# Line 521 | Line 562 | writerr:
562  
563  
564   static AMBVAL *
565 < avstore(aval)                           /* allocate memory and store aval */
566 < register AMBVAL  *aval;
565 > avstore(                                /* allocate memory and store aval */
566 >        register AMBVAL  *aval
567 > )
568   {
569          register AMBVAL  *av;
570          double  d;
571  
572          if ((av = newambval()) == NULL)
573                  error(SYSTEM, "out of memory in avstore");
574 <        copystruct(av, aval);
574 >        *av = *aval;
575          av->latick = ambclock;
576          av->next = NULL;
577          nambvals++;
# Line 548 | Line 590 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
590  
591  
592   static AMBTREE *
593 < newambtree()                            /* allocate 8 ambient tree structs */
593 > newambtree(void)                                /* allocate 8 ambient tree structs */
594   {
595          register AMBTREE  *atp, *upperlim;
596  
# Line 564 | Line 606 | newambtree()                           /* allocate 8 ambient tree structs */
606          }
607          atp = atfreelist;
608          atfreelist = atp->kid;
609 <        bzero((char *)atp, 8*sizeof(AMBTREE));
609 >        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
610          return(atp);
611   }
612  
613  
614   static void
615 < freeambtree(atp)                        /* free 8 ambient tree structs */
616 < AMBTREE  *atp;
615 > freeambtree(                    /* free 8 ambient tree structs */
616 >        AMBTREE  *atp
617 > )
618   {
619          atp->kid = atfreelist;
620          atfreelist = atp;
# Line 579 | Line 622 | AMBTREE  *atp;
622  
623  
624   static void
625 < avinsert(av)                            /* insert ambient value in our tree */
626 < register AMBVAL  *av;
625 > avinsert(                               /* insert ambient value in our tree */
626 >        void *av
627 > )
628   {
629          register AMBTREE  *at;
630          register AMBVAL  *ap;
# Line 590 | Line 634 | register AMBVAL         *av;
634          int  branch;
635          register int  i;
636  
637 <        if (av->rad <= FTINY)
637 >        if (((AMBVAL*)av)->rad <= FTINY)
638                  error(CONSISTENCY, "zero ambient radius in avinsert");
639          at = &atrunk;
640          VCOPY(ck0, thescene.cuorg);
641          s = thescene.cusize;
642 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
642 >        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
643                  if (at->kid == NULL)
644                          if ((at->kid = newambtree()) == NULL)
645                                  error(SYSTEM, "out of memory in avinsert");
646                  s *= 0.5;
647                  branch = 0;
648                  for (i = 0; i < 3; i++)
649 <                        if (av->pos[i] > ck0[i] + s) {
649 >                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
650                                  ck0[i] += s;
651                                  branch |= 1 << i;
652                          }
# Line 610 | Line 654 | register AMBVAL         *av;
654          }
655          avh.next = at->alist;           /* order by increasing level */
656          for (ap = &avh; ap->next != NULL; ap = ap->next)
657 <                if (ap->next->lvl >= av->lvl)
657 >                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
658                          break;
659 <        av->next = ap->next;
660 <        ap->next = av;
659 >        ((AMBVAL*)av)->next = ap->next;
660 >        ap->next = (AMBVAL*)av;
661          at->alist = avh.next;
662   }
663  
664  
665   static void
666 < unloadatree(at, f)                      /* unload an ambient value tree */
667 < register AMBTREE  *at;
668 < void    (*f)();
666 > unloadatree(                    /* unload an ambient value tree */
667 >        register AMBTREE  *at,
668 >        unloadtf_t *f
669 > )
670   {
671          register AMBVAL  *av;
672          register int  i;
# Line 646 | Line 691 | static struct avl {
691   static AMBVAL   **avlist2;              /* memory positions for sorting */
692   static int      i_avlist;               /* index for lists */
693  
694 + static int alatcmp(const void *av1, const void *av2);
695  
696 < static int
697 < av2list(av)
698 < register AMBVAL *av;
696 > static void
697 > av2list(
698 >        void *av
699 > )
700   {
701   #ifdef DEBUG
702          if (i_avlist >= nambvals)
703                  error(CONSISTENCY, "too many ambient values in av2list1");
704   #endif
705 <        avlist1[i_avlist].p = avlist2[i_avlist] = av;
706 <        avlist1[i_avlist++].t = av->latick;
705 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
706 >        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
707   }
708  
709  
710   static int
711 < alatcmp(av1, av2)                       /* compare ambient values for MRA */
712 < struct avl      *av1, *av2;
711 > alatcmp(                        /* compare ambient values for MRA */
712 >        const void *av1,
713 >        const void *av2
714 > )
715   {
716 <        register long  lc = av2->t - av1->t;
716 >        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
717          return(lc<0 ? -1 : lc>0 ? 1 : 0);
718   }
719  
# Line 676 | Line 725 | struct avl     *av1, *av2;
725   * assumes pointers differ by exact struct size increments.
726   */
727   static int
728 < aposcmp(avp1, avp2)                     /* compare ambient value positions */
729 < const void      *avp1, *avp2;
728 > aposcmp(                        /* compare ambient value positions */
729 >        const void      *avp1,
730 >        const void      *avp2
731 > )
732   {
733          register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
734          if (diff < 0)
# Line 687 | Line 738 | const void     *avp1, *avp2;
738  
739   #if 1
740   static int
741 < avlmemi(avaddr)                         /* find list position from address */
742 < AMBVAL  *avaddr;
741 > avlmemi(                                /* find list position from address */
742 >        AMBVAL  *avaddr
743 > )
744   {
745          register AMBVAL  **avlpp;
746  
# Line 705 | Line 757 | AMBVAL *avaddr;
757  
758  
759   static void
760 < sortambvals(always)                     /* resort ambient values */
761 < int     always;
760 > sortambvals(                    /* resort ambient values */
761 >        int     always
762 > )
763   {
764          AMBTREE  oldatrunk;
765          AMBVAL  tav, *tap, *pnext;
# Line 741 | Line 794 | int    always;
794                  if (avlist2 != NULL)
795                          free((void *)avlist2);
796                  if (always) {           /* rebuild without sorting */
797 <                        copystruct(&oldatrunk, &atrunk);
797 >                        oldatrunk = atrunk;
798                          atrunk.alist = NULL;
799                          atrunk.kid = NULL;
800                          unloadatree(&oldatrunk, avinsert);
# Line 772 | Line 825 | int    always;
825                          if (avlist1[i].p == NULL)
826                                  continue;
827                          tap = avlist2[i];
828 <                        copystruct(&tav, tap);
828 >                        tav = *tap;
829                          for (j = i; (pnext = avlist1[j].p) != tap;
830                                          j = avlmemi(pnext)) {
831 <                                copystruct(avlist2[j], pnext);
831 >                                *(avlist2[j]) = *pnext;
832                                  avinsert(avlist2[j]);
833                                  avlist1[j].p = NULL;
834                          }
835 <                        copystruct(avlist2[j], &tav);
835 >                        *(avlist2[j]) = tav;
836                          avinsert(avlist2[j]);
837                          avlist1[j].p = NULL;
838                  }
# Line 804 | Line 857 | int    always;
857   #ifdef  F_SETLKW
858  
859   static void
860 < aflock(typ)                     /* lock/unlock ambient file */
861 < int  typ;
860 > aflock(                 /* lock/unlock ambient file */
861 >        int  typ
862 > )
863   {
864          static struct flock  fls;       /* static so initialized to zeroes */
865  
# Line 815 | Line 869 | int  typ;
869   }
870  
871  
872 < int
873 < ambsync()                       /* synchronize ambient file */
872 > extern int
873 > ambsync(void)                   /* synchronize ambient file */
874   {
875          long  flen;
876          AMBVAL  avs;
# Line 829 | Line 883 | ambsync()                      /* synchronize ambient file */
883                                  /* gain exclusive access */
884          aflock(F_WRLCK);
885                                  /* see if file has grown */
886 <        if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0)
886 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
887                  goto seekerr;
888 <        if (n = flen - lastpos) {               /* file has grown */
888 >        if ( (n = flen - lastpos) ) {           /* file has grown */
889                  if (ambinp == NULL) {           /* use duplicate filedes */
890                          ambinp = fdopen(dup(fileno(ambfp)), "r");
891                          if (ambinp == NULL)
# Line 852 | Line 906 | ambsync()                      /* synchronize ambient file */
906                  }
907                  /*** seek always as safety measure
908                  if (n) ***/                     /* alignment */
909 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0)
909 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
910                                  goto seekerr;
911          }
912   #ifdef  DEBUG
# Line 865 | Line 919 | ambsync()                      /* synchronize ambient file */
919   #endif
920   syncend:
921          n = fflush(ambfp);                      /* calls write() at last */
922 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0)
922 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
923                  goto seekerr;
924          aflock(F_UNLCK);                        /* release file */
925          nunflshed = 0;
926          return(n);
927   seekerr:
928          error(SYSTEM, "seek failed in ambsync");
929 +        return -1; /* pro forma return */
930   }
931  
932   #else
933  
934 < int
935 < ambsync()                       /* flush ambient file */
934 > extern int
935 > ambsync(void)                   /* flush ambient file */
936   {
937          if (nunflshed == 0)
938                  return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines