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.48 by greg, Tue Feb 25 02:47:22 2003 UTC vs.
Revision 2.58 by greg, Tue Apr 19 01:15:06 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 35 | Line 38 | static FILE  *ambfp = NULL;    /* ambient file pointer */
38   static int  nunflshed = 0;      /* number of unflushed ambient values */
39  
40   #ifndef SORT_THRESH
41 < #ifdef BIGMEM
39 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
40 < #else
41 > #ifdef SMLMEM
42   #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
43 + #else
44 + #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
45   #endif
46   #endif
47   #ifndef SORT_INTVL
# 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 269 | Line 293 | FVECT  nrm;
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 >        FVECT  rn,
330 >        int  al,
331 >        AMBTREE  *at,
332 >        FVECT  c0,
333 >        double  s
334 > )
335   {
336          double  d, e1, e2, wt, wsum;
337          COLOR  ct;
# Line 406 | Line 436 | double s;
436   }
437  
438  
439 < double
440 < makeambient(acol, r, rn, al)    /* make a new ambient value */
441 < COLOR  acol;
442 < register RAY  *r;
443 < FVECT  rn;
444 < int  al;
439 > extern double
440 > makeambient(    /* make a new ambient value */
441 >        COLOR  acol,
442 >        RAY  *r,
443 >        COLOR  ac,
444 >        FVECT  rn,
445 >        int  al
446 > )
447   {
448          AMBVAL  amb;
449 +        double  coef;
450          FVECT   gp, gd;
451                                                  /* compute weight */
452          amb.weight = pow(AVGREFL, (double)al);
453 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
454 <                amb.weight = r->rweight;
453 >        coef = intens(ac)*r->rweight;
454 >        if (coef < 0.1*amb.weight)              /* heuristic */
455 >                amb.weight = coef;
456                                                  /* compute ambient */
457 <        amb.rad = doambient(acol, r, amb.weight, gp, gd);
457 >        amb.rad = doambient(acol, r, ac, amb.weight, gp, gd);
458          if (amb.rad <= FTINY)
459                  return(0.0);
460                                                  /* store it */
# Line 438 | Line 472 | int  al;
472   }
473  
474  
475 < void
476 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
477 < COLOR  cr;
478 < register AMBVAL  *ap;
479 < FVECT  pv, nv;
475 > extern void
476 > extambient(             /* extrapolate value at pv, nv */
477 >        COLOR  cr,
478 >        register AMBVAL  *ap,
479 >        FVECT  pv,
480 >        FVECT  nv
481 > )
482   {
483          FVECT  v1;
484          register int  i;
# Line 465 | Line 501 | FVECT  pv, nv;
501  
502  
503   static void
504 < initambfile(creat)              /* initialize ambient file */
505 < int  creat;
504 > initambfile(            /* initialize ambient file */
505 >        int  creat
506 > )
507   {
508          extern char  *progname, *octname;
509          static char  *mybuf = NULL;
# Line 474 | Line 511 | int  creat;
511   #ifdef  F_SETLKW
512          aflock(creat ? F_WRLCK : F_RDLCK);
513   #endif
514 < #ifdef MSDOS
478 <        setmode(fileno(ambfp), O_BINARY);
479 < #endif
514 >        SET_FILE_BINARY(ambfp);
515          if (mybuf == NULL)
516                  mybuf = (char *)bmalloc(BUFSIZ+8);
517          setbuf(ambfp, mybuf);
# Line 503 | Line 538 | int  creat;
538  
539  
540   static void
541 < avsave(av)                              /* insert and save an ambient value */
542 < AMBVAL  *av;
541 > avsave(                         /* insert and save an ambient value */
542 >        AMBVAL  *av
543 > )
544   {
545          avinsert(avstore(av));
546          if (ambfp == NULL)
# Line 521 | Line 557 | writerr:
557  
558  
559   static AMBVAL *
560 < avstore(aval)                           /* allocate memory and store aval */
561 < register AMBVAL  *aval;
560 > avstore(                                /* allocate memory and store aval */
561 >        register AMBVAL  *aval
562 > )
563   {
564          register AMBVAL  *av;
565          double  d;
566  
567          if ((av = newambval()) == NULL)
568                  error(SYSTEM, "out of memory in avstore");
569 <        copystruct(av, aval);
569 >        *av = *aval;
570          av->latick = ambclock;
571          av->next = NULL;
572          nambvals++;
# Line 548 | Line 585 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
585  
586  
587   static AMBTREE *
588 < newambtree()                            /* allocate 8 ambient tree structs */
588 > newambtree(void)                                /* allocate 8 ambient tree structs */
589   {
590          register AMBTREE  *atp, *upperlim;
591  
# Line 564 | Line 601 | newambtree()                           /* allocate 8 ambient tree structs */
601          }
602          atp = atfreelist;
603          atfreelist = atp->kid;
604 <        bzero((char *)atp, 8*sizeof(AMBTREE));
604 >        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
605          return(atp);
606   }
607  
608  
609   static void
610 < freeambtree(atp)                        /* free 8 ambient tree structs */
611 < AMBTREE  *atp;
610 > freeambtree(                    /* free 8 ambient tree structs */
611 >        AMBTREE  *atp
612 > )
613   {
614          atp->kid = atfreelist;
615          atfreelist = atp;
# Line 579 | Line 617 | AMBTREE  *atp;
617  
618  
619   static void
620 < avinsert(av)                            /* insert ambient value in our tree */
621 < register AMBVAL  *av;
620 > avinsert(                               /* insert ambient value in our tree */
621 >        void *av
622 > )
623   {
624          register AMBTREE  *at;
625          register AMBVAL  *ap;
# Line 590 | Line 629 | register AMBVAL         *av;
629          int  branch;
630          register int  i;
631  
632 <        if (av->rad <= FTINY)
632 >        if (((AMBVAL*)av)->rad <= FTINY)
633                  error(CONSISTENCY, "zero ambient radius in avinsert");
634          at = &atrunk;
635          VCOPY(ck0, thescene.cuorg);
636          s = thescene.cusize;
637 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
637 >        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
638                  if (at->kid == NULL)
639                          if ((at->kid = newambtree()) == NULL)
640                                  error(SYSTEM, "out of memory in avinsert");
641                  s *= 0.5;
642                  branch = 0;
643                  for (i = 0; i < 3; i++)
644 <                        if (av->pos[i] > ck0[i] + s) {
644 >                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
645                                  ck0[i] += s;
646                                  branch |= 1 << i;
647                          }
# Line 610 | Line 649 | register AMBVAL         *av;
649          }
650          avh.next = at->alist;           /* order by increasing level */
651          for (ap = &avh; ap->next != NULL; ap = ap->next)
652 <                if (ap->next->lvl >= av->lvl)
652 >                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
653                          break;
654 <        av->next = ap->next;
655 <        ap->next = av;
654 >        ((AMBVAL*)av)->next = ap->next;
655 >        ap->next = (AMBVAL*)av;
656          at->alist = avh.next;
657   }
658  
659  
660   static void
661 < unloadatree(at, f)                      /* unload an ambient value tree */
662 < register AMBTREE  *at;
663 < void    (*f)();
661 > unloadatree(                    /* unload an ambient value tree */
662 >        register AMBTREE  *at,
663 >        unloadtf_t *f
664 > )
665   {
666          register AMBVAL  *av;
667          register int  i;
# Line 646 | Line 686 | static struct avl {
686   static AMBVAL   **avlist2;              /* memory positions for sorting */
687   static int      i_avlist;               /* index for lists */
688  
689 + static int alatcmp(const void *av1, const void *av2);
690  
691 < static int
692 < av2list(av)
693 < register AMBVAL *av;
691 > static void
692 > av2list(
693 >        void *av
694 > )
695   {
696   #ifdef DEBUG
697          if (i_avlist >= nambvals)
698                  error(CONSISTENCY, "too many ambient values in av2list1");
699   #endif
700 <        avlist1[i_avlist].p = avlist2[i_avlist] = av;
701 <        avlist1[i_avlist++].t = av->latick;
700 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
701 >        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
702   }
703  
704  
705   static int
706 < alatcmp(av1, av2)                       /* compare ambient values for MRA */
707 < struct avl      *av1, *av2;
706 > alatcmp(                        /* compare ambient values for MRA */
707 >        const void *av1,
708 >        const void *av2
709 > )
710   {
711 <        register long  lc = av2->t - av1->t;
711 >        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
712          return(lc<0 ? -1 : lc>0 ? 1 : 0);
713   }
714  
# Line 676 | Line 720 | struct avl     *av1, *av2;
720   * assumes pointers differ by exact struct size increments.
721   */
722   static int
723 < aposcmp(avp1, avp2)                     /* compare ambient value positions */
724 < const void      *avp1, *avp2;
723 > aposcmp(                        /* compare ambient value positions */
724 >        const void      *avp1,
725 >        const void      *avp2
726 > )
727   {
728          register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
729          if (diff < 0)
# Line 687 | Line 733 | const void     *avp1, *avp2;
733  
734   #if 1
735   static int
736 < avlmemi(avaddr)                         /* find list position from address */
737 < AMBVAL  *avaddr;
736 > avlmemi(                                /* find list position from address */
737 >        AMBVAL  *avaddr
738 > )
739   {
740          register AMBVAL  **avlpp;
741  
# Line 705 | Line 752 | AMBVAL *avaddr;
752  
753  
754   static void
755 < sortambvals(always)                     /* resort ambient values */
756 < int     always;
755 > sortambvals(                    /* resort ambient values */
756 >        int     always
757 > )
758   {
759          AMBTREE  oldatrunk;
760          AMBVAL  tav, *tap, *pnext;
# Line 741 | Line 789 | int    always;
789                  if (avlist2 != NULL)
790                          free((void *)avlist2);
791                  if (always) {           /* rebuild without sorting */
792 <                        copystruct(&oldatrunk, &atrunk);
792 >                        oldatrunk = atrunk;
793                          atrunk.alist = NULL;
794                          atrunk.kid = NULL;
795                          unloadatree(&oldatrunk, avinsert);
# Line 772 | Line 820 | int    always;
820                          if (avlist1[i].p == NULL)
821                                  continue;
822                          tap = avlist2[i];
823 <                        copystruct(&tav, tap);
823 >                        tav = *tap;
824                          for (j = i; (pnext = avlist1[j].p) != tap;
825                                          j = avlmemi(pnext)) {
826 <                                copystruct(avlist2[j], pnext);
826 >                                *(avlist2[j]) = *pnext;
827                                  avinsert(avlist2[j]);
828                                  avlist1[j].p = NULL;
829                          }
830 <                        copystruct(avlist2[j], &tav);
830 >                        *(avlist2[j]) = tav;
831                          avinsert(avlist2[j]);
832                          avlist1[j].p = NULL;
833                  }
# Line 804 | Line 852 | int    always;
852   #ifdef  F_SETLKW
853  
854   static void
855 < aflock(typ)                     /* lock/unlock ambient file */
856 < int  typ;
855 > aflock(                 /* lock/unlock ambient file */
856 >        int  typ
857 > )
858   {
859          static struct flock  fls;       /* static so initialized to zeroes */
860  
# Line 815 | Line 864 | int  typ;
864   }
865  
866  
867 < int
868 < ambsync()                       /* synchronize ambient file */
867 > extern int
868 > ambsync(void)                   /* synchronize ambient file */
869   {
870          long  flen;
871          AMBVAL  avs;
# Line 829 | Line 878 | ambsync()                      /* synchronize ambient file */
878                                  /* gain exclusive access */
879          aflock(F_WRLCK);
880                                  /* see if file has grown */
881 <        if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0)
881 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
882                  goto seekerr;
883 <        if (n = flen - lastpos) {               /* file has grown */
883 >        if ( (n = flen - lastpos) ) {           /* file has grown */
884                  if (ambinp == NULL) {           /* use duplicate filedes */
885                          ambinp = fdopen(dup(fileno(ambfp)), "r");
886                          if (ambinp == NULL)
# Line 852 | Line 901 | ambsync()                      /* synchronize ambient file */
901                  }
902                  /*** seek always as safety measure
903                  if (n) ***/                     /* alignment */
904 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0)
904 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
905                                  goto seekerr;
906          }
907   #ifdef  DEBUG
# Line 865 | Line 914 | ambsync()                      /* synchronize ambient file */
914   #endif
915   syncend:
916          n = fflush(ambfp);                      /* calls write() at last */
917 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0)
917 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
918                  goto seekerr;
919          aflock(F_UNLCK);                        /* release file */
920          nunflshed = 0;
921          return(n);
922   seekerr:
923          error(SYSTEM, "seek failed in ambsync");
924 +        return -1; /* pro forma return */
925   }
926  
927   #else
928  
929 < int
930 < ambsync()                       /* flush ambient file */
929 > extern int
930 > ambsync(void)                   /* flush ambient file */
931   {
932          if (nunflshed == 0)
933                  return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines