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.46 by gwlarson, Sun Jan 10 12:45:19 1999 UTC vs.
Revision 2.47 by greg, Sat Feb 22 02:07:28 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  ambient.c - routines dealing with ambient (inter-reflected) component.
6 + *
7 + *  Declarations of external symbols in ambient.h
8   */
9  
10 + /* ====================================================================
11 + * The Radiance Software License, Version 1.0
12 + *
13 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 + *
16 + * Redistribution and use in source and binary forms, with or without
17 + * modification, are permitted provided that the following conditions
18 + * are met:
19 + *
20 + * 1. Redistributions of source code must retain the above copyright
21 + *         notice, this list of conditions and the following disclaimer.
22 + *
23 + * 2. Redistributions in binary form must reproduce the above copyright
24 + *       notice, this list of conditions and the following disclaimer in
25 + *       the documentation and/or other materials provided with the
26 + *       distribution.
27 + *
28 + * 3. The end-user documentation included with the redistribution,
29 + *           if any, must include the following acknowledgment:
30 + *             "This product includes Radiance software
31 + *                 (http://radsite.lbl.gov/)
32 + *                 developed by the Lawrence Berkeley National Laboratory
33 + *               (http://www.lbl.gov/)."
34 + *       Alternately, this acknowledgment may appear in the software itself,
35 + *       if and wherever such third-party acknowledgments normally appear.
36 + *
37 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 + *       and "The Regents of the University of California" must
39 + *       not be used to endorse or promote products derived from this
40 + *       software without prior written permission. For written
41 + *       permission, please contact [email protected].
42 + *
43 + * 5. Products derived from this software may not be called "Radiance",
44 + *       nor may "Radiance" appear in their name, without prior written
45 + *       permission of Lawrence Berkeley National Laboratory.
46 + *
47 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 + * SUCH DAMAGE.
59 + * ====================================================================
60 + *
61 + * This software consists of voluntary contributions made by many
62 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 + * information on Lawrence Berkeley National Laboratory, please see
64 + * <http://www.lbl.gov/>.
65 + */
66 +
67   #include  "ray.h"
68  
13 #include  "octree.h"
14
69   #include  "otypes.h"
70  
71   #include  "ambient.h"
# Line 22 | Line 76 | static char SCCSid[] = "$SunId$ LBL";
76   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
77   #endif
78  
25 typedef struct ambtree {
26        AMBVAL  *alist;         /* ambient value list */
27        struct ambtree  *kid;   /* 8 child nodes */
28 }  AMBTREE;                     /* ambient octree */
29
30 extern CUBE  thescene;          /* contains space boundaries */
31
79   extern char  *shm_boundary;     /* memory sharing boundary */
80  
81   #define  MAXASET        511     /* maximum number of elements in ambient set */
# Line 63 | Line 110 | static unsigned int  nambshare = 0;    /* number of value
110   static unsigned long  ambclock = 0;     /* ambient access clock */
111   static unsigned long  lastsort = 0;     /* time of last value sort */
112   static long  sortintvl = SORT_INTVL;    /* time until next sort */
113 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
114 + static long  lastpos = -1;              /* last flush position */
115  
116   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
117          /*
# Line 78 | Line 127 | static long  sortintvl = SORT_INTVL;   /* time until nex
127  
128   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
129  
130 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
130 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
131 > #define  freeav(av)     free((void *)av);
132  
133 < static int  initambfile(), avsave(), avinsert(), sortambvals(), avlmemi();
133 > static void  initambfile(), avsave(), avinsert(), sortambvals(), unloadatree();
134 > static int  avlmemi();
135   static AMBVAL  *avstore();
136   #ifdef  F_SETLKW
137 < static  aflock();
137 > static void  aflock();
138   #endif
139  
140  
141 + void
142   setambres(ar)                           /* set ambient resolution */
143   int  ar;
144   {
# Line 108 | Line 160 | int  ar;
160   }
161  
162  
163 + void
164   setambacc(newa)                         /* set ambient accuracy */
165   double  newa;
166   {
# Line 121 | Line 174 | double  newa;
174   }
175  
176  
177 < setambient(afile)                       /* initialize calculation */
178 < char  *afile;
177 > void
178 > setambient()                            /* initialize calculation */
179   {
180          int     readonly = 0;
181          long  pos, flen;
182          AMBVAL  amb;
183 +                                                /* make sure we're fresh */
184 +        ambdone();
185                                                  /* init ambient limits */
186          setambres(ambres);
187          setambacc(ambacc);
188 <        if (afile == NULL)
188 >        if (ambfile == NULL || !ambfile[0])
189                  return;
190          if (ambacc <= FTINY) {
191                  sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
192 <                                afile);
192 >                                ambfile);
193                  error(WARNING, errmsg);
194                  return;
195          }
196                                                  /* open ambient file */
197 <        if ((ambfp = fopen(afile, "r+")) == NULL)
198 <                readonly = (ambfp = fopen(afile, "r")) != NULL;
197 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
198 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
199          if (ambfp != NULL) {
200                  initambfile(0);                 /* file exists */
201                  pos = ftell(ambfp);
# Line 158 | Line 213 | char  *afile;
213                  }
214                                                  /* align file pointer */
215                  pos += (long)nambvals*AMBVALSIZ;
216 <                flen = lseek(fileno(ambfp), 0L, 2);
216 >                flen = lseek(fileno(ambfp), (off_t)0L, 2);
217                  if (flen != pos) {
218                          sprintf(errmsg,
219                          "ignoring last %ld values in ambient file (corrupted)",
220                                          (flen - pos)/AMBVALSIZ);
221                          error(WARNING, errmsg);
222                          fseek(ambfp, pos, 0);
223 <                        ftruncate(fileno(ambfp), pos);
223 >                        ftruncate(fileno(ambfp), (off_t)pos);
224                  }
225 <        } else if ((ambfp = fopen(afile, "w+")) != NULL) {
225 >        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
226                  initambfile(1);                 /* else create new file */
227          } else {
228 <                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
228 >                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
229                  error(SYSTEM, errmsg);
230          }
231          nunflshed++;    /* lie */
# Line 178 | Line 233 | char  *afile;
233   }
234  
235  
236 + void
237 + ambdone()                       /* close ambient file and free memory */
238 + {
239 +        if (ambfp != NULL) {            /* close ambient file */
240 +                ambsync();
241 +                fclose(ambfp);
242 +                ambfp = NULL;
243 +                if (ambinp != NULL) {  
244 +                        fclose(ambinp);
245 +                        ambinp = NULL;
246 +                }
247 +                lastpos = -1;
248 +        }
249 +                                        /* free ambient tree */
250 +        unloadatree(&atrunk, free);
251 +                                        /* reset state variables */
252 +        avsum = 0.;
253 +        navsum = 0;
254 +        nambvals = 0;
255 +        nambshare = 0;
256 +        ambclock = 0;
257 +        lastsort = 0;
258 +        sortintvl = SORT_INTVL;
259 + }
260 +
261 +
262 + void
263   ambnotify(obj)                  /* record new modifier */
264   OBJECT  obj;
265   {
266          static int  hitlimit = 0;
267 <        register OBJREC  *o = objptr(obj);
267 >        register OBJREC  *o;
268          register char  **amblp;
269  
270 +        if (obj == OVOID) {             /* starting over */
271 +                ambset[0] = 0;
272 +                hitlimit = 0;
273 +                return;
274 +        }
275 +        o = objptr(obj);
276          if (hitlimit || !ismodifier(o->otype))
277                  return;
278          for (amblp = amblist; *amblp != NULL; amblp++)
# Line 200 | Line 288 | OBJECT obj;
288   }
289  
290  
291 + void
292   ambient(acol, r, nrm)           /* compute ambient component for ray */
293   COLOR  acol;
294   register RAY  *r;
# Line 279 | Line 368 | double s;
368          wsum = 0.0;
369                                          /* do this node */
370          for (av = at->alist; av != NULL; av = av->next) {
371 +                double  rn_dot = -2.0;
372                  if (tracktime)
373                          av->latick = ambclock;
374                  /*
# Line 301 | Line 391 | double s;
391                  if (e1 > ambacc*ambacc*1.21)
392                          continue;
393                  /*
394 <                 *  Normal direction test.
394 >                 *  Direction test using closest normal.
395                   */
396 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
396 >                d = DOT(av->dir, r->ron);
397 >                if (rn != r->ron) {
398 >                        rn_dot = DOT(av->dir, rn);
399 >                        if (rn_dot > 1.0-FTINY)
400 >                                rn_dot = 1.0-FTINY;
401 >                        if (rn_dot >= d-FTINY) {
402 >                                d = rn_dot;
403 >                                rn_dot = -2.0;
404 >                        }
405 >                }
406 >                e2 = (1.0 - d) * r->rweight;
407                  if (e2 < 0.0) e2 = 0.0;
408                  if (e1 + e2 > ambacc*ambacc*1.21)
409                          continue;
# Line 319 | Line 419 | double s;
419                  /*
420                   *  Jittering final test reduces image artifacts.
421                   */
422 <                wt = sqrt(e1) + sqrt(e2);
422 >                e1 = sqrt(e1);
423 >                e2 = sqrt(e2);
424 >                wt = e1 + e2;
425                  if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
426                          continue;
427 +                /*
428 +                 *  Recompute directional error using perturbed normal
429 +                 */
430 +                if (rn_dot > 0.0) {
431 +                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
432 +                        wt = e1 + e2;
433 +                }
434                  if (wt <= 1e-3)
435                          wt = 1e3;
436                  else
# Line 384 | Line 493 | int  al;
493   }
494  
495  
496 + void
497   extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
498   COLOR  cr;
499   register AMBVAL  *ap;
# Line 409 | Line 519 | FVECT  pv, nv;
519   }
520  
521  
522 < static
522 > static void
523   initambfile(creat)              /* initialize ambient file */
524   int  creat;
525   {
526 <        extern char  *progname, *octname, VersionID[];
526 >        extern char  *progname, *octname;
527 >        static char  *mybuf = NULL;
528  
529   #ifdef  F_SETLKW
530          aflock(creat ? F_WRLCK : F_RDLCK);
# Line 421 | Line 532 | int  creat;
532   #ifdef MSDOS
533          setmode(fileno(ambfp), O_BINARY);
534   #endif
535 <        setbuf(ambfp, bmalloc(BUFSIZ+8));
535 >        if (mybuf == NULL)
536 >                mybuf = (char *)bmalloc(BUFSIZ+8);
537 >        setbuf(ambfp, mybuf);
538          if (creat) {                    /* new file */
539                  newheader("RADIANCE", ambfp);
540                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
541                                  progname, colval(ambval,RED),
542                                  colval(ambval,GRN), colval(ambval,BLU),
543                                  ambvwt, ambounce, ambacc);
544 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
545 <                                ambdiv, ambssamp, ambres,
546 <                                octname==NULL ? "" : octname);
544 >                fprintf(ambfp, "-ad %d -as %d -ar %d ",
545 >                                ambdiv, ambssamp, ambres);
546 >                if (octname != NULL)
547 >                        printargs(1, &octname, ambfp);
548 >                else
549 >                        fputc('\n', ambfp);
550                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
551 +                fputnow(ambfp);
552                  fputformat(AMBFMT, ambfp);
553                  putc('\n', ambfp);
554                  putambmagic(ambfp);
# Line 440 | Line 557 | int  creat;
557   }
558  
559  
560 < static
560 > static void
561   avsave(av)                              /* insert and save an ambient value */
562   AMBVAL  *av;
563   {
# Line 454 | Line 571 | AMBVAL *av;
571                          goto writerr;
572          return;
573   writerr:
574 <        error(SYSTEM, "error writing ambient file");
574 >        error(SYSTEM, "error writing to ambient file");
575   }
576  
577  
# Line 485 | Line 602 | register AMBVAL  *aval;
602   static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
603  
604  
605 < static
489 < AMBTREE *
605 > static AMBTREE *
606   newambtree()                            /* allocate 8 ambient tree structs */
607   {
608          register AMBTREE  *atp, *upperlim;
609  
610          if (atfreelist == NULL) {       /* get more nodes */
611 <                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
611 >                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
612                  if (atfreelist == NULL)
613                          return(NULL);
614                                          /* link new free list */
# Line 508 | Line 624 | newambtree()                           /* allocate 8 ambient tree structs */
624   }
625  
626  
627 < static
627 > static void
628   freeambtree(atp)                        /* free 8 ambient tree structs */
629   AMBTREE  *atp;
630   {
# Line 517 | Line 633 | AMBTREE  *atp;
633   }
634  
635  
636 < static
636 > static void
637   avinsert(av)                            /* insert ambient value in our tree */
638   register AMBVAL  *av;
639   {
# Line 557 | Line 673 | register AMBVAL         *av;
673   }
674  
675  
676 < static
676 > static void
677   unloadatree(at, f)                      /* unload an ambient value tree */
678   register AMBTREE  *at;
679 < int     (*f)();
679 > void    (*f)();
680   {
681          register AMBVAL  *av;
682          register int  i;
# Line 586 | Line 702 | static AMBVAL  **avlist2;              /* memory positions for sort
702   static int      i_avlist;               /* index for lists */
703  
704  
705 < static
705 > static int
706   av2list(av)
707   register AMBVAL *av;
708   {
# Line 608 | Line 724 | struct avl     *av1, *av2;
724   }
725  
726  
727 + /* GW NOTE 2002/10/3:
728 + * I used to compare AMBVAL pointers, but found that this was the
729 + * cause of a serious consistency error with gcc, since the optimizer
730 + * uses some dangerous trick in pointer subtraction that
731 + * assumes pointers differ by exact struct size increments.
732 + */
733   static int
734   aposcmp(avp1, avp2)                     /* compare ambient value positions */
735 < AMBVAL  **avp1, **avp2;
735 > const void      *avp1, *avp2;
736   {
737 <        return(*avp1 - *avp2);
737 >        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
738 >        if (diff < 0)
739 >                return(-1);
740 >        return(diff > 0);
741   }
742  
618
743   #if 1
744   static int
745   avlmemi(avaddr)                         /* find list position from address */
# Line 635 | Line 759 | AMBVAL *avaddr;
759   #endif
760  
761  
762 < static
762 > static void
763   sortambvals(always)                     /* resort ambient values */
764   int     always;
765   {
# Line 670 | Line 794 | int    always;
794          }
795          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
796                  if (avlist2 != NULL)
797 <                        free((char *)avlist2);
797 >                        free((void *)avlist2);
798                  if (always) {           /* rebuild without sorting */
799                          copystruct(&oldatrunk, &atrunk);
800                          atrunk.alist = NULL;
# Line 714 | Line 838 | int    always;
838                          avinsert(avlist2[j]);
839                          avlist1[j].p = NULL;
840                  }
841 <                free((char *)avlist1);
842 <                free((char *)avlist2);
841 >                free((void *)avlist1);
842 >                free((void *)avlist2);
843                                                  /* compute new sort interval */
844                  sortintvl = ambclock - lastsort;
845                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 734 | Line 858 | int    always;
858  
859   #ifdef  F_SETLKW
860  
861 < static
861 > static void
862   aflock(typ)                     /* lock/unlock ambient file */
863   int  typ;
864   {
# Line 749 | Line 873 | int  typ;
873   int
874   ambsync()                       /* synchronize ambient file */
875   {
752        static FILE  *ambinp = NULL;
753        static long  lastpos = -1;
876          long  flen;
877          AMBVAL  avs;
878          register int  n;
# Line 762 | Line 884 | ambsync()                      /* synchronize ambient file */
884                                  /* gain exclusive access */
885          aflock(F_WRLCK);
886                                  /* see if file has grown */
887 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
887 >        if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0)
888                  goto seekerr;
889          if (n = flen - lastpos) {               /* file has grown */
890                  if (ambinp == NULL) {           /* use duplicate filedes */
# Line 775 | Line 897 | ambsync()                      /* synchronize ambient file */
897                  while (n >= AMBVALSIZ) {        /* load contributed values */
898                          if (!readambval(&avs, ambinp)) {
899                                  sprintf(errmsg,
900 <                                "ambient file corrupted near character %ld",
901 <                                                flen - n);
900 >                        "ambient file \"%s\" corrupted near character %ld",
901 >                                                ambfile, flen - n);
902                                  error(WARNING, errmsg);
903                                  break;
904                          }
# Line 785 | Line 907 | ambsync()                      /* synchronize ambient file */
907                  }
908                  /*** seek always as safety measure
909                  if (n) ***/                     /* alignment */
910 <                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
910 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0)
911                                  goto seekerr;
912          }
913   #ifdef  DEBUG
# Line 798 | Line 920 | ambsync()                      /* synchronize ambient file */
920   #endif
921   syncend:
922          n = fflush(ambfp);                      /* calls write() at last */
923 <        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
923 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0)
924                  goto seekerr;
925          aflock(F_UNLCK);                        /* release file */
926          nunflshed = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines