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.11 by greg, Thu Jul 16 19:44:46 1992 UTC vs.
Revision 2.19 by greg, Wed Aug 4 14:22:11 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ LBL";
6  
7   /*
8   *  ambient.c - routines dealing with ambient (inter-reflected) component.
9 *
10 *     5/9/86
9   */
10  
11   #include  "ray.h"
# Line 20 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
22  
23   typedef struct ambtree {
24 <        AMBVAL  *alist;         /* ambient value list */
25 <        struct ambtree  *kid;   /* 8 child nodes */
24 >        AMBVAL  *alist;         /* ambient value list */
25 >        struct ambtree  *kid;   /* 8 child nodes */
26   }  AMBTREE;                     /* ambient octree */
27  
28   extern CUBE  thescene;          /* contains space boundaries */
29  
30 < #define  MAXASET        511     /* maximum number of elements in ambient set */
31 < OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
30 > #define  MAXASET        511     /* maximum number of elements in ambient set */
31 > OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
32  
33 < double  maxarad;                /* maximum ambient radius */
34 < double  minarad;                /* minimum ambient radius */
33 > double  maxarad;                /* maximum ambient radius */
34 > double  minarad;                /* minimum ambient radius */
35  
36 < static AMBTREE  atrunk;         /* our ambient trunk node */
36 > static AMBTREE  atrunk;         /* our ambient trunk node */
37  
38   static FILE  *ambfp = NULL;     /* ambient file pointer */
39 + static int  nunflshed = 0;      /* number of unflushed ambient values */
40  
41 < #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
41 > #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
42  
43 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
43 > #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
44  
45 < #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
45 > #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46  
47   extern long  ftell(), lseek();
48 < static int  initambfile(), avsave(), avinsert(), ambsync();
48 > static int  initambfile(), avsave(), avinsert();
49 > #ifdef  F_SETLKW
50 > static  aflock();
51 > #endif
52  
53  
54   setambres(ar)                           /* set ambient resolution */
# Line 71 | Line 73 | setambient(afile)                      /* initialize calculation */
73   char  *afile;
74   {
75          long  headlen;
76 <        AMBVAL  amb;
76 >        AMBVAL  amb;
77                                                  /* init ambient limits */
78          setambres(ambres);
79 +        if (afile == NULL)
80 +                return;
81                                                  /* open ambient file */
82 <        if (afile != NULL)
83 <                if ((ambfp = fopen(afile, "r+")) != NULL) {
84 <                        initambfile(0);
85 <                        headlen = ftell(ambfp);
86 <                        while (readambval(&amb, ambfp))
87 <                                avinsert(&amb, &atrunk, thescene.cuorg,
88 <                                                thescene.cusize);
89 <                                                        /* align */
90 <                        fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
91 <                } else if ((ambfp = fopen(afile, "w+")) != NULL)
92 <                        initambfile(1);
93 <                else {
94 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
95 <                                        afile);
96 <                        error(SYSTEM, errmsg);
97 <                }
82 >        if ((ambfp = fopen(afile, "r+")) != NULL) {
83 >                initambfile(0);
84 >                headlen = ftell(ambfp);
85 >                while (readambval(&amb, ambfp))
86 >                        avinsert(&amb, &atrunk, thescene.cuorg,
87 >                                        thescene.cusize);
88 >                                                /* align */
89 >                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
90 >        } else if ((ambfp = fopen(afile, "w+")) != NULL)
91 >                initambfile(1);
92 >        else {
93 >                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
94 >                error(SYSTEM, errmsg);
95 >        }
96 >        nunflshed++;    /* lie */
97 >        ambsync();
98   }
99  
100  
101   ambnotify(obj)                  /* record new modifier */
102 < OBJECT  obj;
102 > OBJECT  obj;
103   {
104          static int  hitlimit = 0;
105 <        register OBJREC  *o = objptr(obj);
105 >        register OBJREC  *o = objptr(obj);
106          register char  **amblp;
107  
108          if (hitlimit || !ismodifier(o->otype))
# Line 121 | Line 125 | COLOR  acol;
125   register RAY  *r;
126   {
127          static int  rdepth = 0;                 /* ambient recursion */
128 <        double  d;
128 >        double  d;
129  
130          if (ambdiv <= 0)                        /* no ambient calculation */
131                  goto dumbamb;
# Line 163 | Line 167 | sumambient(acol, r, al, at, c0, s)     /* get interpolated
167   COLOR  acol;
168   register RAY  *r;
169   int  al;
170 < AMBTREE  *at;
170 > AMBTREE  *at;
171   FVECT  c0;
172 < double  s;
172 > double  s;
173   {
174 <        extern double  sqrt();
171 <        double  d, e1, e2, wt, wsum;
174 >        double  d, e1, e2, wt, wsum;
175          COLOR  ct;
176          FVECT  ck0;
177          int  i;
178          register int  j;
179 <        register AMBVAL  *av;
179 >        register AMBVAL  *av;
180                                          /* do this node */
181          wsum = 0.0;
182          for (av = at->alist; av != NULL; av = av->next) {
# Line 252 | Line 255 | COLOR  acol;
255   register RAY  *r;
256   int  al;
257   {
258 <        AMBVAL  amb;
258 >        AMBVAL  amb;
259          FVECT   gp, gd;
260                                                  /* compute weight */
261          amb.weight = pow(AVGREFL, (double)al);
# Line 277 | Line 280 | int  al;
280  
281   extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
282   COLOR  cr;
283 < register AMBVAL  *ap;
283 > register AMBVAL  *ap;
284   FVECT  pv, nv;
285   {
286          FVECT  v1, v2;
287          register int  i;
288 <        double  d;
288 >        double  d;
289  
290          d = 1.0;                        /* zeroeth order */
291                                          /* gradient due to translation */
# Line 307 | Line 310 | int  creat;
310   {
311          extern char  *progname, *octname, VersionID[];
312  
313 + #ifdef  F_SETLKW
314 +        aflock(creat ? F_WRLCK : F_RDLCK);
315 + #endif
316 + #ifdef MSDOS
317 +        setmode(fileno(ambfp), O_BINARY);
318 + #endif
319          setbuf(ambfp, bmalloc(BUFSIZ));
320          if (creat) {                    /* new file */
321                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
# Line 320 | Line 329 | int  creat;
329                  fputformat(AMBFMT, ambfp);
330                  putc('\n', ambfp);
331                  putambmagic(ambfp);
323                fflush(ambfp);
324 #ifndef  NIX
325                sync();                 /* protect against NFS buffering */
326 #endif
332          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
333                  error(USER, "bad ambient file");
334   }
# Line 331 | Line 336 | int  creat;
336  
337   static
338   avsave(av)                              /* insert and save an ambient value */
339 < AMBVAL  *av;
339 > AMBVAL  *av;
340   {
336        static int  nunflshed = 0;
337
341          avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
342          if (ambfp == NULL)
343                  return;
344          if (writambval(av, ambfp) < 0)
345                  goto writerr;
346 <        if (++nunflshed >= AMBFLUSH) {
346 >        if (++nunflshed >= AMBFLUSH)
347                  if (ambsync() == EOF)
348                          goto writerr;
346                nunflshed = 0;
347        }
349          return;
350   writerr:
351          error(SYSTEM, "error writing ambient file");
# Line 353 | Line 354 | writerr:
354  
355   static
356   avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
357 < AMBVAL  *aval;
357 > AMBVAL  *aval;
358   register AMBTREE  *at;
359   FVECT  c0;
360 < double  s;
360 > double  s;
361   {
362          FVECT  ck0;
363          int  branch;
364 <        register AMBVAL  *av;
364 >        register AMBVAL  *av;
365          register int  i;
366  
367          if ((av = newambval()) == NULL)
# Line 388 | Line 389 | memerr:
389   }
390  
391  
392 < #ifdef  NIX
392 > #ifdef  F_SETLKW
393  
394   static
395 < ambsync()                       /* flush ambient file */
395 > aflock(typ)                     /* lock/unlock ambient file */
396 > int  typ;
397   {
398 <        return(fflush(ambfp));
398 >        static struct flock  fls;       /* static so initialized to zeroes */
399 >
400 >        fls.l_type = typ;
401 >        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
402 >                error(SYSTEM, "cannot (un)lock ambient file");
403   }
404  
399 #else
405  
406 < #include  <fcntl.h>
402 < #include  <sys/types.h>
403 < #include  <sys/stat.h>
404 <
405 < static
406 > int
407   ambsync()                       /* synchronize ambient file */
408   {
409          static FILE  *ambinp = NULL;
410 <        struct flock  fls;
411 <        struct stat  sts;
412 <        AMBVAL  avs;
412 <        long  lastpos, flen;
410 >        static long  lastpos = -1;
411 >        long  flen;
412 >        AMBVAL  avs;
413          register int  n;
414 +
415 +        if (nunflshed == 0)
416 +                return(0);
417 +        if (lastpos < 0)        /* initializing (locked in initambfile) */
418 +                goto syncend;
419                                  /* gain exclusive access */
420 <        fls.l_type = F_WRLCK;
416 <        fls.l_whence = 0;
417 <        fls.l_start = 0L;
418 <        fls.l_len = 0L;
419 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
420 <                error(SYSTEM, "cannot lock ambient file");
420 >        aflock(F_WRLCK);
421                                  /* see if file has grown */
422 <        lastpos = lseek(fileno(ambfp), 0L, 1);  /* get previous position */
423 <        if (fstat(fileno(ambfp), &sts) < 0)     /* get current length */
424 <                error(SYSTEM, "cannot stat ambient file");
425 <        flen = sts.st_size;
426 <        if (n = (flen - lastpos)/AMBVALSIZ) {   /* file has grown */
427 <                if (ambinp == NULL)             /* use duplicate file */
422 >        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
423 >                error(SYSTEM, "cannot seek on ambient file");
424 >        if (n = flen - lastpos) {               /* file has grown */
425 >                if (ambinp == NULL) {           /* use duplicate filedes */
426                          ambinp = fdopen(dup(fileno(ambfp)), "r");
427 <                while (n--) {                   /* load contributed values */
427 >                        if (ambinp == NULL)
428 >                                error(SYSTEM, "fdopen failed in ambsync");
429 >                }
430 >                if (fseek(ambinp, lastpos, 0) < 0)
431 >                        error(SYSTEM, "fseek failed in ambsync");
432 >                while (n >= AMBVALSIZ) {        /* load contributed values */
433                          readambval(&avs, ambinp);
434                          avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
435 <                }                               /* moves shared file pointer */
436 <                if (n = (flen - lastpos)%AMBVALSIZ)     /* alignment */
435 >                        n -= AMBVALSIZ;
436 >                }
437 >                if (n)                          /* alignment */
438                          lseek(fileno(ambfp), flen-n, 0);
439          }
440 + syncend:
441          n = fflush(ambfp);                      /* calls write() at last */
442 <        fls.l_type = F_UNLCK;                   /* release file */
443 <        fcntl(fileno(ambfp), F_SETLKW, &fls);
442 >        lastpos = lseek(fileno(ambfp), 0L, 1);
443 >        aflock(F_UNLCK);                        /* release file */
444 >        nunflshed = 0;
445          return(n);
446 + }
447 +
448 + #else
449 +
450 + int
451 + ambsync()                       /* flush ambient file */
452 + {
453 +        if (nunflshed == 0)
454 +                return(0);
455 +        nunflshed = 0;
456 +        return(fflush(ambfp));
457   }
458  
459   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines