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.18 by greg, Fri Jan 29 12:02:23 1993 UTC vs.
Revision 2.20 by greg, Thu Aug 5 10:02:00 1993 UTC

# Line 43 | Line 43 | static int  nunflshed = 0;     /* number of unflushed ambi
43   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
44  
45   #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46 + #define  freeambtree(t) free((char *)(t))
47  
48   extern long  ftell(), lseek();
49 < static int  initambfile(), avsave(), avinsert();
49 > static int  initambfile(), avsave(), avinsert(), loadtree();
50 > static AMBVAL  *avstore();
51 > #ifdef  F_SETLKW
52 > static  aflock();
53 > #endif
54  
55  
56   setambres(ar)                           /* set ambient resolution */
57   int  ar;
58   {
59 +        ambres = ar;                    /* may be done already */
60                                                  /* set min & max radii */
61          if (ar <= 0) {
62                  minarad = 0.0;
# Line 66 | Line 72 | int  ar;
72   }
73  
74  
75 + resetambacc(newa)                       /* change ambient accuracy setting */
76 + double  newa;
77 + {
78 +        AMBTREE  oldatrunk;
79 +
80 +        if (fabs(newa - ambacc) < 0.01)
81 +                return;                 /* insignificant -- don't bother */
82 +        ambacc = newa;
83 +        if (ambacc <= FTINY)
84 +                return;                 /* cannot build new tree */
85 +                                        /* else need to rebuild tree */
86 +        copystruct(&oldatrunk, &atrunk);
87 +        atrunk.alist = NULL;
88 +        atrunk.kid = NULL;
89 +        loadtree(&oldatrunk);
90 + }
91 +
92 +
93   setambient(afile)                       /* initialize calculation */
94   char  *afile;
95   {
# Line 73 | Line 97 | char  *afile;
97          AMBVAL  amb;
98                                                  /* init ambient limits */
99          setambres(ambres);
100 +        if (afile == NULL)
101 +                return;
102 +        if (ambacc <= FTINY) {
103 +                sprintf(errmsg, "zero ambient accuracy so \"%s\" not loaded",
104 +                                afile);
105 +                error(WARNING, errmsg);
106 +                return;
107 +        }
108                                                  /* open ambient file */
109 <        if (afile != NULL) {
110 <                if ((ambfp = fopen(afile, "r+")) != NULL) {
111 <                        initambfile(0);
112 <                        headlen = ftell(ambfp);
113 <                        while (readambval(&amb, ambfp))
114 <                                avinsert(&amb, &atrunk, thescene.cuorg,
115 <                                                thescene.cusize);
116 <                                                        /* align */
117 <                        fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
118 <                } else if ((ambfp = fopen(afile, "w+")) != NULL)
119 <                        initambfile(1);
120 <                else {
121 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
90 <                                        afile);
91 <                        error(SYSTEM, errmsg);
92 <                }
93 <                nunflshed++;    /* lie */
94 <                ambsync();
109 >        if ((ambfp = fopen(afile, "r+")) != NULL) {
110 >                initambfile(0);
111 >                headlen = ftell(ambfp);
112 >                while (readambval(&amb, ambfp))
113 >                        avinsert(avstore(&amb), &atrunk,
114 >                                        thescene.cuorg, thescene.cusize);
115 >                                                /* align */
116 >                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
117 >        } else if ((ambfp = fopen(afile, "w+")) != NULL)
118 >                initambfile(1);
119 >        else {
120 >                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
121 >                error(SYSTEM, errmsg);
122          }
123 +        nunflshed++;    /* lie */
124 +        ambsync();
125   }
126  
127  
# Line 308 | Line 337 | int  creat;
337   {
338          extern char  *progname, *octname, VersionID[];
339  
340 + #ifdef  F_SETLKW
341 +        aflock(creat ? F_WRLCK : F_RDLCK);
342 + #endif
343   #ifdef MSDOS
344          setmode(fileno(ambfp), O_BINARY);
345   #endif
# Line 333 | Line 365 | static
365   avsave(av)                              /* insert and save an ambient value */
366   AMBVAL  *av;
367   {
368 <        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
368 >        avinsert(avstore(av), &atrunk, thescene.cuorg, thescene.cusize);
369          if (ambfp == NULL)
370                  return;
371          if (writambval(av, ambfp) < 0)
# Line 347 | Line 379 | writerr:
379   }
380  
381  
382 + static AMBVAL *
383 + avstore(aval)                           /* allocate memory and store aval */
384 + register AMBVAL  *aval;
385 + {
386 +        register AMBVAL  *av;
387 +
388 +        if ((av = newambval()) == NULL)
389 +                error(SYSTEM, "out of memory in avstore");
390 +        copystruct(av, aval);
391 +        return(av);
392 + }
393 +
394 +
395   static
396 < avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
397 < AMBVAL  *aval;
396 > avinsert(av, at, c0, s)                 /* insert ambient value in a tree */
397 > register AMBVAL  *av;
398   register AMBTREE  *at;
399   FVECT  c0;
400   double  s;
401   {
402          FVECT  ck0;
403          int  branch;
359        register AMBVAL  *av;
404          register int  i;
405  
406 <        if ((av = newambval()) == NULL)
407 <                goto memerr;
364 <        copystruct(av, aval);
406 >        if (av->rad <= FTINY)
407 >                error(CONSISTENCY, "zero ambient radius in avinsert");
408          VCOPY(ck0, c0);
409          while (s*(OCTSCALE/2) > av->rad*ambacc) {
410                  if (at->kid == NULL)
411                          if ((at->kid = newambtree()) == NULL)
412 <                                goto memerr;
412 >                                error(SYSTEM, "out of memory in avinsert");
413                  s *= 0.5;
414                  branch = 0;
415                  for (i = 0; i < 3; i++)
# Line 378 | Line 421 | double s;
421          }
422          av->next = at->alist;
423          at->alist = av;
381        return;
382 memerr:
383        error(SYSTEM, "out of memory in avinsert");
424   }
425  
426  
427 + static
428 + loadtree(at)                            /* move tree to main store */
429 + register AMBTREE  *at;
430 + {
431 +        register AMBVAL  *av;
432 +        register int  i;
433 +                                        /* transfer values at this node */
434 +        for (av = at->alist; av != NULL; av = at->alist) {
435 +                at->alist = av->next;
436 +                avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
437 +        }
438 +        for (i = 0; i < 8; i++)         /* transfer and free children */
439 +                loadtree(at->kid+i);
440 +        freeambtree(at->kid);
441 + }
442 +
443 +
444   #ifdef  F_SETLKW
445  
446 + static
447 + aflock(typ)                     /* lock/unlock ambient file */
448 + int  typ;
449 + {
450 +        static struct flock  fls;       /* static so initialized to zeroes */
451 +
452 +        fls.l_type = typ;
453 +        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
454 +                error(SYSTEM, "cannot (un)lock ambient file");
455 + }
456 +
457 +
458   int
459   ambsync()                       /* synchronize ambient file */
460   {
461          static FILE  *ambinp = NULL;
462          static long  lastpos = -1;
394        struct flock  fls;
463          long  flen;
464          AMBVAL  avs;
465          register int  n;
466  
467          if (nunflshed == 0)
468                  return(0);
469 <                                /* gain exclusive access */
402 <        fls.l_type = F_WRLCK;
403 <        fls.l_whence = 0;
404 <        fls.l_start = 0L;
405 <        fls.l_len = 0L;
406 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
407 <                error(SYSTEM, "cannot lock ambient file");
408 <        if (lastpos < 0)        /* initializing */
469 >        if (lastpos < 0)        /* initializing (locked in initambfile) */
470                  goto syncend;
471 +                                /* gain exclusive access */
472 +        aflock(F_WRLCK);
473                                  /* see if file has grown */
474          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
475                  error(SYSTEM, "cannot seek on ambient file");
# Line 420 | Line 483 | ambsync()                      /* synchronize ambient file */
483                          error(SYSTEM, "fseek failed in ambsync");
484                  while (n >= AMBVALSIZ) {        /* load contributed values */
485                          readambval(&avs, ambinp);
486 <                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
486 >                        avinsert(avstore(&avs), &atrunk,
487 >                                        thescene.cuorg, thescene.cusize);
488                          n -= AMBVALSIZ;
489                  }
490                  if (n)                          /* alignment */
# Line 429 | Line 493 | ambsync()                      /* synchronize ambient file */
493   syncend:
494          n = fflush(ambfp);                      /* calls write() at last */
495          lastpos = lseek(fileno(ambfp), 0L, 1);
496 <        fls.l_type = F_UNLCK;                   /* release file */
433 <        fcntl(fileno(ambfp), F_SETLKW, &fls);
496 >        aflock(F_UNLCK);                        /* release file */
497          nunflshed = 0;
498          return(n);
499   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines