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.24 by greg, Thu Mar 24 13:41:04 1994 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 210 | Line 214 | double s;
214                  /*
215                   *  Ambient level test.
216                   */
217 <                if (av->lvl > al || av->weight < r->rweight-FTINY)
217 >                if (av->lvl > al)       /* list sorted, so this works */
218 >                        break;
219 >                if (av->weight < r->rweight-FTINY)
220                          continue;
221                  /*
222                   *  Ambient radius test.
# Line 343 | Line 349 | int  creat;
349   #ifdef MSDOS
350          setmode(fileno(ambfp), O_BINARY);
351   #endif
352 <        setbuf(ambfp, bmalloc(BUFSIZ));
352 >        setbuf(ambfp, bmalloc(BUFSIZ+8));
353          if (creat) {                    /* new file */
354 +                newheader("RADIANCE", ambfp);
355                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
356                                  progname, colval(ambval,RED),
357                                  colval(ambval,GRN), colval(ambval,BLU),
# Line 365 | Line 372 | static
372   avsave(av)                              /* insert and save an ambient value */
373   AMBVAL  *av;
374   {
375 <        avinsert(avstore(av), &atrunk, thescene.cuorg, thescene.cusize);
375 >        avinsert(avstore(av));
376          if (ambfp == NULL)
377                  return;
378          if (writambval(av, ambfp) < 0)
# Line 393 | Line 400 | register AMBVAL  *aval;
400  
401  
402   static
403 < avinsert(av, at, c0, s)                 /* insert ambient value in a tree */
403 > avinsert(av)                            /* insert ambient value in our tree */
404   register AMBVAL  *av;
398 register AMBTREE  *at;
399 FVECT  c0;
400 double  s;
405   {
406 +        register AMBTREE  *at;
407 +        register AMBVAL  *ap;
408 +        AMBVAL  avh;
409          FVECT  ck0;
410 +        double  s;
411          int  branch;
412          register int  i;
413  
414          if (av->rad <= FTINY)
415                  error(CONSISTENCY, "zero ambient radius in avinsert");
416 <        VCOPY(ck0, c0);
416 >        at = &atrunk;
417 >        VCOPY(ck0, thescene.cuorg);
418 >        s = thescene.cusize;
419          while (s*(OCTSCALE/2) > av->rad*ambacc) {
420                  if (at->kid == NULL)
421                          if ((at->kid = newambtree()) == NULL)
# Line 419 | Line 429 | double s;
429                          }
430                  at = at->kid + branch;
431          }
432 <        av->next = at->alist;
433 <        at->alist = av;
432 >        avh.next = at->alist;           /* order by increasing level */
433 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
434 >                if (ap->next->lvl >= av->lvl)
435 >                        break;
436 >        av->next = ap->next;
437 >        ap->next = av;
438 >        at->alist = avh.next;
439   }
440  
441  
442   static
443 < loadtree(at)                            /* move tree to main store */
443 > loadatree(at)                           /* move tree to main store */
444   register AMBTREE  *at;
445   {
446          register AMBVAL  *av;
# Line 433 | Line 448 | register AMBTREE  *at;
448                                          /* transfer values at this node */
449          for (av = at->alist; av != NULL; av = at->alist) {
450                  at->alist = av->next;
451 <                avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
451 >                avinsert(av);
452          }
453 +        if (at->kid == NULL)
454 +                return;
455          for (i = 0; i < 8; i++)         /* transfer and free children */
456 <                loadtree(at->kid+i);
456 >                loadatree(at->kid+i);
457          freeambtree(at->kid);
458   }
459  
# Line 472 | Line 489 | ambsync()                      /* synchronize ambient file */
489          aflock(F_WRLCK);
490                                  /* see if file has grown */
491          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
492 <                error(SYSTEM, "cannot seek on ambient file");
492 >                goto seekerr;
493          if (n = flen - lastpos) {               /* file has grown */
494                  if (ambinp == NULL) {           /* use duplicate filedes */
495                          ambinp = fdopen(dup(fileno(ambfp)), "r");
# Line 480 | Line 497 | ambsync()                      /* synchronize ambient file */
497                                  error(SYSTEM, "fdopen failed in ambsync");
498                  }
499                  if (fseek(ambinp, lastpos, 0) < 0)
500 <                        error(SYSTEM, "fseek failed in ambsync");
500 >                        goto seekerr;
501                  while (n >= AMBVALSIZ) {        /* load contributed values */
502                          readambval(&avs, ambinp);
503 <                        avinsert(avstore(&avs), &atrunk,
487 <                                        thescene.cuorg, thescene.cusize);
503 >                        avinsert(avstore(&avs));
504                          n -= AMBVALSIZ;
505                  }
506 <                if (n)                          /* alignment */
507 <                        lseek(fileno(ambfp), flen-n, 0);
506 >                /*** seek always as safety measure
507 >                if (n) ***/                     /* alignment */
508 >                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
509 >                                goto seekerr;
510          }
511 + #ifdef  DEBUG
512 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
513 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
514 +                                ambfp->_ptr - ambfp->_base,
515 +                                nunflshed*AMBVALSIZ);
516 +                error(CONSISTENCY, errmsg);
517 +        }
518 + #endif
519   syncend:
520          n = fflush(ambfp);                      /* calls write() at last */
521 <        lastpos = lseek(fileno(ambfp), 0L, 1);
521 >        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
522 >                goto seekerr;
523          aflock(F_UNLCK);                        /* release file */
524          nunflshed = 0;
525          return(n);
526 + seekerr:
527 +        error(SYSTEM, "seek failed in ambsync");
528   }
529  
530   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines