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.53 by schorsch, Mon Jul 21 22:30:19 2003 UTC vs.
Revision 2.57 by greg, Fri Nov 5 17:36:55 2004 UTC

# Line 14 | Line 14 | static const char      RCSid[] = "$Id$";
14   #include  "platform.h"
15   #include  "ray.h"
16   #include  "otypes.h"
17 + #include  "resolu.h"
18   #include  "ambient.h"
19   #include  "random.h"
20  
# 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)",
# Line 180 | 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 206 | 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 235 | 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 > ambient(                /* compute ambient component for ray */
258 >        COLOR  acol,
259 >        register RAY  *r,
260 >        FVECT  nrm
261 > )
262   {
263          static int  rdepth = 0;                 /* ambient recursion */
264          double  d, l;
# Line 280 | Line 299 | FVECT  nrm;
299                  return;
300   dumbamb:                                        /* return global value */
301          copycolor(acol, ambval);
302 <        if (ambvwt <= 0 | navsum == 0)
302 >        if ((ambvwt <= 0) | (navsum == 0))
303                  return;
304          l = bright(ambval);                     /* average in computations */
305          if (l > FTINY) {
# Line 295 | Line 314 | dumbamb:                                       /* return global value */
314   }
315  
316  
317 < double
318 < sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
319 < COLOR  acol;
320 < register RAY  *r;
321 < FVECT  rn;
322 < int  al;
323 < AMBTREE  *at;
324 < FVECT  c0;
325 < double  s;
317 > extern double
318 > sumambient(     /* get interpolated ambient value */
319 >        COLOR  acol,
320 >        register RAY  *r,
321 >        FVECT  rn,
322 >        int  al,
323 >        AMBTREE  *at,
324 >        FVECT  c0,
325 >        double  s
326 > )
327   {
328          double  d, e1, e2, wt, wsum;
329          COLOR  ct;
# Line 408 | Line 428 | double s;
428   }
429  
430  
431 < double
432 < makeambient(acol, r, rn, al)    /* make a new ambient value */
433 < COLOR  acol;
434 < register RAY  *r;
435 < FVECT  rn;
436 < int  al;
431 > extern double
432 > makeambient(    /* make a new ambient value */
433 >        COLOR  acol,
434 >        register RAY  *r,
435 >        FVECT  rn,
436 >        int  al
437 > )
438   {
439          AMBVAL  amb;
440          FVECT   gp, gd;
# Line 440 | Line 461 | int  al;
461   }
462  
463  
464 < void
465 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
466 < COLOR  cr;
467 < register AMBVAL  *ap;
468 < FVECT  pv, nv;
464 > extern void
465 > extambient(             /* extrapolate value at pv, nv */
466 >        COLOR  cr,
467 >        register AMBVAL  *ap,
468 >        FVECT  pv,
469 >        FVECT  nv
470 > )
471   {
472          FVECT  v1;
473          register int  i;
# Line 467 | Line 490 | FVECT  pv, nv;
490  
491  
492   static void
493 < initambfile(creat)              /* initialize ambient file */
494 < int  creat;
493 > initambfile(            /* initialize ambient file */
494 >        int  creat
495 > )
496   {
497          extern char  *progname, *octname;
498          static char  *mybuf = NULL;
# Line 503 | Line 527 | int  creat;
527  
528  
529   static void
530 < avsave(av)                              /* insert and save an ambient value */
531 < AMBVAL  *av;
530 > avsave(                         /* insert and save an ambient value */
531 >        AMBVAL  *av
532 > )
533   {
534          avinsert(avstore(av));
535          if (ambfp == NULL)
# Line 521 | Line 546 | writerr:
546  
547  
548   static AMBVAL *
549 < avstore(aval)                           /* allocate memory and store aval */
550 < register AMBVAL  *aval;
549 > avstore(                                /* allocate memory and store aval */
550 >        register AMBVAL  *aval
551 > )
552   {
553          register AMBVAL  *av;
554          double  d;
# Line 548 | Line 574 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
574  
575  
576   static AMBTREE *
577 < newambtree()                            /* allocate 8 ambient tree structs */
577 > newambtree(void)                                /* allocate 8 ambient tree structs */
578   {
579          register AMBTREE  *atp, *upperlim;
580  
# Line 570 | Line 596 | newambtree()                           /* allocate 8 ambient tree structs */
596  
597  
598   static void
599 < freeambtree(atp)                        /* free 8 ambient tree structs */
600 < AMBTREE  *atp;
599 > freeambtree(                    /* free 8 ambient tree structs */
600 >        AMBTREE  *atp
601 > )
602   {
603          atp->kid = atfreelist;
604          atfreelist = atp;
# Line 579 | Line 606 | AMBTREE  *atp;
606  
607  
608   static void
609 < avinsert(av)                            /* insert ambient value in our tree */
610 < register AMBVAL  *av;
609 > avinsert(                               /* insert ambient value in our tree */
610 >        void *av
611 > )
612   {
613          register AMBTREE  *at;
614          register AMBVAL  *ap;
# Line 590 | Line 618 | register AMBVAL         *av;
618          int  branch;
619          register int  i;
620  
621 <        if (av->rad <= FTINY)
621 >        if (((AMBVAL*)av)->rad <= FTINY)
622                  error(CONSISTENCY, "zero ambient radius in avinsert");
623          at = &atrunk;
624          VCOPY(ck0, thescene.cuorg);
625          s = thescene.cusize;
626 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
626 >        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
627                  if (at->kid == NULL)
628                          if ((at->kid = newambtree()) == NULL)
629                                  error(SYSTEM, "out of memory in avinsert");
630                  s *= 0.5;
631                  branch = 0;
632                  for (i = 0; i < 3; i++)
633 <                        if (av->pos[i] > ck0[i] + s) {
633 >                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
634                                  ck0[i] += s;
635                                  branch |= 1 << i;
636                          }
# Line 610 | Line 638 | register AMBVAL         *av;
638          }
639          avh.next = at->alist;           /* order by increasing level */
640          for (ap = &avh; ap->next != NULL; ap = ap->next)
641 <                if (ap->next->lvl >= av->lvl)
641 >                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
642                          break;
643 <        av->next = ap->next;
644 <        ap->next = av;
643 >        ((AMBVAL*)av)->next = ap->next;
644 >        ap->next = (AMBVAL*)av;
645          at->alist = avh.next;
646   }
647  
648  
649   static void
650 < unloadatree(at, f)                      /* unload an ambient value tree */
651 < register AMBTREE  *at;
652 < void    (*f)();
650 > unloadatree(                    /* unload an ambient value tree */
651 >        register AMBTREE  *at,
652 >        unloadtf_t *f
653 > )
654   {
655          register AMBVAL  *av;
656          register int  i;
# Line 646 | Line 675 | static struct avl {
675   static AMBVAL   **avlist2;              /* memory positions for sorting */
676   static int      i_avlist;               /* index for lists */
677  
678 + static int alatcmp(const void *av1, const void *av2);
679  
680 < static int
681 < av2list(av)
682 < register AMBVAL *av;
680 > static void
681 > av2list(
682 >        void *av
683 > )
684   {
685   #ifdef DEBUG
686          if (i_avlist >= nambvals)
687                  error(CONSISTENCY, "too many ambient values in av2list1");
688   #endif
689 <        avlist1[i_avlist].p = avlist2[i_avlist] = av;
690 <        avlist1[i_avlist++].t = av->latick;
689 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
690 >        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
691   }
692  
693  
694   static int
695 < alatcmp(av1, av2)                       /* compare ambient values for MRA */
696 < struct avl      *av1, *av2;
695 > alatcmp(                        /* compare ambient values for MRA */
696 >        const void *av1,
697 >        const void *av2
698 > )
699   {
700 <        register long  lc = av2->t - av1->t;
700 >        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
701          return(lc<0 ? -1 : lc>0 ? 1 : 0);
702   }
703  
# Line 676 | Line 709 | struct avl     *av1, *av2;
709   * assumes pointers differ by exact struct size increments.
710   */
711   static int
712 < aposcmp(avp1, avp2)                     /* compare ambient value positions */
713 < const void      *avp1, *avp2;
712 > aposcmp(                        /* compare ambient value positions */
713 >        const void      *avp1,
714 >        const void      *avp2
715 > )
716   {
717          register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
718          if (diff < 0)
# Line 687 | Line 722 | const void     *avp1, *avp2;
722  
723   #if 1
724   static int
725 < avlmemi(avaddr)                         /* find list position from address */
726 < AMBVAL  *avaddr;
725 > avlmemi(                                /* find list position from address */
726 >        AMBVAL  *avaddr
727 > )
728   {
729          register AMBVAL  **avlpp;
730  
# Line 705 | Line 741 | AMBVAL *avaddr;
741  
742  
743   static void
744 < sortambvals(always)                     /* resort ambient values */
745 < int     always;
744 > sortambvals(                    /* resort ambient values */
745 >        int     always
746 > )
747   {
748          AMBTREE  oldatrunk;
749          AMBVAL  tav, *tap, *pnext;
# Line 804 | Line 841 | int    always;
841   #ifdef  F_SETLKW
842  
843   static void
844 < aflock(typ)                     /* lock/unlock ambient file */
845 < int  typ;
844 > aflock(                 /* lock/unlock ambient file */
845 >        int  typ
846 > )
847   {
848          static struct flock  fls;       /* static so initialized to zeroes */
849  
# Line 815 | Line 853 | int  typ;
853   }
854  
855  
856 < int
857 < ambsync()                       /* synchronize ambient file */
856 > extern int
857 > ambsync(void)                   /* synchronize ambient file */
858   {
859          long  flen;
860          AMBVAL  avs;
# Line 829 | Line 867 | ambsync()                      /* synchronize ambient file */
867                                  /* gain exclusive access */
868          aflock(F_WRLCK);
869                                  /* see if file has grown */
870 <        if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0)
870 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
871                  goto seekerr;
872 <        if (n = flen - lastpos) {               /* file has grown */
872 >        if ( (n = flen - lastpos) ) {           /* file has grown */
873                  if (ambinp == NULL) {           /* use duplicate filedes */
874                          ambinp = fdopen(dup(fileno(ambfp)), "r");
875                          if (ambinp == NULL)
# Line 852 | Line 890 | ambsync()                      /* synchronize ambient file */
890                  }
891                  /*** seek always as safety measure
892                  if (n) ***/                     /* alignment */
893 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0)
893 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
894                                  goto seekerr;
895          }
896   #ifdef  DEBUG
# Line 865 | Line 903 | ambsync()                      /* synchronize ambient file */
903   #endif
904   syncend:
905          n = fflush(ambfp);                      /* calls write() at last */
906 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0)
906 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
907                  goto seekerr;
908          aflock(F_UNLCK);                        /* release file */
909          nunflshed = 0;
910          return(n);
911   seekerr:
912          error(SYSTEM, "seek failed in ambsync");
913 +        return -1; /* pro forma return */
914   }
915  
916   #else
917  
918 < int
919 < ambsync()                       /* flush ambient file */
918 > extern int
919 > ambsync(void)                   /* flush ambient file */
920   {
921          if (nunflshed == 0)
922                  return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines