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.20 by greg, Thu Aug 5 10:02:00 1993 UTC vs.
Revision 2.21 by greg, Thu Aug 5 16:25:05 1993 UTC

# Line 46 | Line 46 | static int  nunflshed = 0;     /* number of unflushed ambi
46   #define  freeambtree(t) free((char *)(t))
47  
48   extern long  ftell(), lseek();
49 < static int  initambfile(), avsave(), avinsert(), loadtree();
49 > static int  initambfile(), avsave(), avinsert(), loadatree();
50   static AMBVAL  *avstore();
51   #ifdef  F_SETLKW
52   static  aflock();
# Line 56 | Line 56 | static  aflock();
56   setambres(ar)                           /* set ambient resolution */
57   int  ar;
58   {
59 <        ambres = ar;                    /* may be done already */
59 >        ambres = ar < 0 ? 0 : ar;               /* may be done already */
60                                                  /* set min & max radii */
61          if (ar <= 0) {
62                  minarad = 0.0;
# Line 72 | Line 72 | int  ar;
72   }
73  
74  
75 < resetambacc(newa)                       /* change ambient accuracy setting */
75 > setambacc(newa)                         /* set ambient accuracy */
76   double  newa;
77   {
78 +        static double  oldambacc = -1.0;
79          AMBTREE  oldatrunk;
80  
81 <        if (fabs(newa - ambacc) < 0.01)
81 >        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
82 >        if (oldambacc < -FTINY)
83 >                oldambacc = ambacc;     /* do nothing first call */
84 >        if (fabs(newa - oldambacc) < 0.01)
85                  return;                 /* insignificant -- don't bother */
82        ambacc = newa;
86          if (ambacc <= FTINY)
87                  return;                 /* cannot build new tree */
88                                          /* else need to rebuild tree */
89          copystruct(&oldatrunk, &atrunk);
90          atrunk.alist = NULL;
91          atrunk.kid = NULL;
92 <        loadtree(&oldatrunk);
92 >        loadatree(&oldatrunk);
93 >        oldambacc = ambacc;             /* remeber setting for next call */
94   }
95  
96  
# Line 97 | Line 101 | char  *afile;
101          AMBVAL  amb;
102                                                  /* init ambient limits */
103          setambres(ambres);
104 +        setambacc(ambacc);
105          if (afile == NULL)
106                  return;
107          if (ambacc <= FTINY) {
108 <                sprintf(errmsg, "zero ambient accuracy so \"%s\" not loaded",
108 >                sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
109                                  afile);
110                  error(WARNING, errmsg);
111                  return;
# Line 110 | Line 115 | char  *afile;
115                  initambfile(0);
116                  headlen = ftell(ambfp);
117                  while (readambval(&amb, ambfp))
118 <                        avinsert(avstore(&amb), &atrunk,
114 <                                        thescene.cuorg, thescene.cusize);
118 >                        avinsert(avstore(&amb));
119                                                  /* align */
120                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
121          } else if ((ambfp = fopen(afile, "w+")) != NULL)
# Line 343 | Line 347 | int  creat;
347   #ifdef MSDOS
348          setmode(fileno(ambfp), O_BINARY);
349   #endif
350 <        setbuf(ambfp, bmalloc(BUFSIZ));
350 >        setbuf(ambfp, bmalloc(BUFSIZ+8));
351          if (creat) {                    /* new file */
352                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
353                                  progname, colval(ambval,RED),
# Line 365 | Line 369 | static
369   avsave(av)                              /* insert and save an ambient value */
370   AMBVAL  *av;
371   {
372 <        avinsert(avstore(av), &atrunk, thescene.cuorg, thescene.cusize);
372 >        avinsert(avstore(av));
373          if (ambfp == NULL)
374                  return;
375          if (writambval(av, ambfp) < 0)
# Line 393 | Line 397 | register AMBVAL  *aval;
397  
398  
399   static
400 < avinsert(av, at, c0, s)                 /* insert ambient value in a tree */
400 > avinsert(av)                            /* insert ambient value in our tree */
401   register AMBVAL  *av;
398 register AMBTREE  *at;
399 FVECT  c0;
400 double  s;
402   {
403 +        register AMBTREE  *at;
404          FVECT  ck0;
405 +        double  s;
406          int  branch;
407          register int  i;
408  
409          if (av->rad <= FTINY)
410                  error(CONSISTENCY, "zero ambient radius in avinsert");
411 <        VCOPY(ck0, c0);
411 >        at = &atrunk;
412 >        VCOPY(ck0, thescene.cuorg);
413 >        s = thescene.cusize;
414          while (s*(OCTSCALE/2) > av->rad*ambacc) {
415                  if (at->kid == NULL)
416                          if ((at->kid = newambtree()) == NULL)
# Line 425 | Line 430 | double s;
430  
431  
432   static
433 < loadtree(at)                            /* move tree to main store */
433 > loadatree(at)                           /* move tree to main store */
434   register AMBTREE  *at;
435   {
436          register AMBVAL  *av;
# Line 433 | Line 438 | register AMBTREE  *at;
438                                          /* transfer values at this node */
439          for (av = at->alist; av != NULL; av = at->alist) {
440                  at->alist = av->next;
441 <                avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
441 >                avinsert(av);
442          }
443 +        if (at->kid == NULL)
444 +                return;
445          for (i = 0; i < 8; i++)         /* transfer and free children */
446 <                loadtree(at->kid+i);
446 >                loadatree(at->kid+i);
447          freeambtree(at->kid);
448   }
449  
# Line 472 | Line 479 | ambsync()                      /* synchronize ambient file */
479          aflock(F_WRLCK);
480                                  /* see if file has grown */
481          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
482 <                error(SYSTEM, "cannot seek on ambient file");
482 >                goto seekerr;
483          if (n = flen - lastpos) {               /* file has grown */
484                  if (ambinp == NULL) {           /* use duplicate filedes */
485                          ambinp = fdopen(dup(fileno(ambfp)), "r");
# Line 480 | Line 487 | ambsync()                      /* synchronize ambient file */
487                                  error(SYSTEM, "fdopen failed in ambsync");
488                  }
489                  if (fseek(ambinp, lastpos, 0) < 0)
490 <                        error(SYSTEM, "fseek failed in ambsync");
490 >                        goto seekerr;
491                  while (n >= AMBVALSIZ) {        /* load contributed values */
492                          readambval(&avs, ambinp);
493 <                        avinsert(avstore(&avs), &atrunk,
487 <                                        thescene.cuorg, thescene.cusize);
493 >                        avinsert(avstore(&avs));
494                          n -= AMBVALSIZ;
495                  }
496                  if (n)                          /* alignment */
497 <                        lseek(fileno(ambfp), flen-n, 0);
497 >                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
498 >                                goto seekerr;
499          }
500 + #ifdef  DEBUG
501 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
502 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
503 +                                ambfp->_ptr - ambfp->_base,
504 +                                nunflshed*AMBVALSIZ);
505 +                error(CONSISTENCY, errmsg);
506 +        }
507 + #endif
508   syncend:
509          n = fflush(ambfp);                      /* calls write() at last */
510 <        lastpos = lseek(fileno(ambfp), 0L, 1);
510 >        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
511 >                goto seekerr;
512          aflock(F_UNLCK);                        /* release file */
513          nunflshed = 0;
514          return(n);
515 + seekerr:
516 +        error(SYSTEM, "seek failed in ambsync");
517   }
518  
519   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines