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.25 by greg, Thu Jun 2 11:45:27 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;
62 >                minarad = 0;
63                  maxarad = thescene.cusize / 2.0;
64          } else {
65                  minarad = thescene.cusize / ar;
66 <                maxarad = 16.0 * minarad;               /* heuristic */
66 >                maxarad = 16 * minarad;                 /* heuristic */
67                  if (maxarad > thescene.cusize / 2.0)
68                          maxarad = thescene.cusize / 2.0;
69          }
70 <        if (maxarad <= FTINY)
71 <                maxarad = .001;
70 >        if (minarad <= FTINY)
71 >                minarad = 10*FTINY;
72 >        if (maxarad <= minarad)
73 >                maxarad = 64 * minarad;
74   }
75  
76  
77 < resetambacc(newa)                       /* change ambient accuracy setting */
77 > setambacc(newa)                         /* set ambient accuracy */
78   double  newa;
79   {
80 +        static double  oldambacc = -1.0;
81          AMBTREE  oldatrunk;
82  
83 <        if (fabs(newa - ambacc) < 0.01)
83 >        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
84 >        if (oldambacc < -FTINY)
85 >                oldambacc = ambacc;     /* do nothing first call */
86 >        if (fabs(newa - oldambacc) < 0.01)
87                  return;                 /* insignificant -- don't bother */
82        ambacc = newa;
88          if (ambacc <= FTINY)
89                  return;                 /* cannot build new tree */
90                                          /* else need to rebuild tree */
91          copystruct(&oldatrunk, &atrunk);
92          atrunk.alist = NULL;
93          atrunk.kid = NULL;
94 <        loadtree(&oldatrunk);
94 >        loadatree(&oldatrunk);
95 >        oldambacc = ambacc;             /* remeber setting for next call */
96   }
97  
98  
# Line 97 | Line 103 | char  *afile;
103          AMBVAL  amb;
104                                                  /* init ambient limits */
105          setambres(ambres);
106 +        setambacc(ambacc);
107          if (afile == NULL)
108                  return;
109          if (ambacc <= FTINY) {
110 <                sprintf(errmsg, "zero ambient accuracy so \"%s\" not loaded",
110 >                sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
111                                  afile);
112                  error(WARNING, errmsg);
113                  return;
# Line 110 | Line 117 | char  *afile;
117                  initambfile(0);
118                  headlen = ftell(ambfp);
119                  while (readambval(&amb, ambfp))
120 <                        avinsert(avstore(&amb), &atrunk,
114 <                                        thescene.cuorg, thescene.cusize);
120 >                        avinsert(avstore(&amb));
121                                                  /* align */
122                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
123          } else if ((ambfp = fopen(afile, "w+")) != NULL)
# Line 210 | Line 216 | double s;
216                  /*
217                   *  Ambient level test.
218                   */
219 <                if (av->lvl > al || av->weight < r->rweight-FTINY)
219 >                if (av->lvl > al)       /* list sorted, so this works */
220 >                        break;
221 >                if (av->weight < r->rweight-FTINY)
222                          continue;
223                  /*
224                   *  Ambient radius test.
# Line 343 | Line 351 | int  creat;
351   #ifdef MSDOS
352          setmode(fileno(ambfp), O_BINARY);
353   #endif
354 <        setbuf(ambfp, bmalloc(BUFSIZ));
354 >        setbuf(ambfp, bmalloc(BUFSIZ+8));
355          if (creat) {                    /* new file */
356 +                newheader("RADIANCE", ambfp);
357                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
358                                  progname, colval(ambval,RED),
359                                  colval(ambval,GRN), colval(ambval,BLU),
# Line 365 | Line 374 | static
374   avsave(av)                              /* insert and save an ambient value */
375   AMBVAL  *av;
376   {
377 <        avinsert(avstore(av), &atrunk, thescene.cuorg, thescene.cusize);
377 >        avinsert(avstore(av));
378          if (ambfp == NULL)
379                  return;
380          if (writambval(av, ambfp) < 0)
# Line 393 | Line 402 | register AMBVAL  *aval;
402  
403  
404   static
405 < avinsert(av, at, c0, s)                 /* insert ambient value in a tree */
405 > avinsert(av)                            /* insert ambient value in our tree */
406   register AMBVAL  *av;
398 register AMBTREE  *at;
399 FVECT  c0;
400 double  s;
407   {
408 +        register AMBTREE  *at;
409 +        register AMBVAL  *ap;
410 +        AMBVAL  avh;
411          FVECT  ck0;
412 +        double  s;
413          int  branch;
414          register int  i;
415  
416          if (av->rad <= FTINY)
417                  error(CONSISTENCY, "zero ambient radius in avinsert");
418 <        VCOPY(ck0, c0);
418 >        at = &atrunk;
419 >        VCOPY(ck0, thescene.cuorg);
420 >        s = thescene.cusize;
421          while (s*(OCTSCALE/2) > av->rad*ambacc) {
422                  if (at->kid == NULL)
423                          if ((at->kid = newambtree()) == NULL)
# Line 419 | Line 431 | double s;
431                          }
432                  at = at->kid + branch;
433          }
434 <        av->next = at->alist;
435 <        at->alist = av;
434 >        avh.next = at->alist;           /* order by increasing level */
435 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
436 >                if (ap->next->lvl >= av->lvl)
437 >                        break;
438 >        av->next = ap->next;
439 >        ap->next = av;
440 >        at->alist = avh.next;
441   }
442  
443  
444   static
445 < loadtree(at)                            /* move tree to main store */
445 > loadatree(at)                           /* move tree to main store */
446   register AMBTREE  *at;
447   {
448          register AMBVAL  *av;
# Line 433 | Line 450 | register AMBTREE  *at;
450                                          /* transfer values at this node */
451          for (av = at->alist; av != NULL; av = at->alist) {
452                  at->alist = av->next;
453 <                avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
453 >                avinsert(av);
454          }
455 +        if (at->kid == NULL)
456 +                return;
457          for (i = 0; i < 8; i++)         /* transfer and free children */
458 <                loadtree(at->kid+i);
458 >                loadatree(at->kid+i);
459          freeambtree(at->kid);
460   }
461  
# Line 472 | Line 491 | ambsync()                      /* synchronize ambient file */
491          aflock(F_WRLCK);
492                                  /* see if file has grown */
493          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
494 <                error(SYSTEM, "cannot seek on ambient file");
494 >                goto seekerr;
495          if (n = flen - lastpos) {               /* file has grown */
496                  if (ambinp == NULL) {           /* use duplicate filedes */
497                          ambinp = fdopen(dup(fileno(ambfp)), "r");
# Line 480 | Line 499 | ambsync()                      /* synchronize ambient file */
499                                  error(SYSTEM, "fdopen failed in ambsync");
500                  }
501                  if (fseek(ambinp, lastpos, 0) < 0)
502 <                        error(SYSTEM, "fseek failed in ambsync");
502 >                        goto seekerr;
503                  while (n >= AMBVALSIZ) {        /* load contributed values */
504                          readambval(&avs, ambinp);
505 <                        avinsert(avstore(&avs), &atrunk,
487 <                                        thescene.cuorg, thescene.cusize);
505 >                        avinsert(avstore(&avs));
506                          n -= AMBVALSIZ;
507                  }
508 <                if (n)                          /* alignment */
509 <                        lseek(fileno(ambfp), flen-n, 0);
508 >                /*** seek always as safety measure
509 >                if (n) ***/                     /* alignment */
510 >                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
511 >                                goto seekerr;
512          }
513 + #ifdef  DEBUG
514 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
515 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
516 +                                ambfp->_ptr - ambfp->_base,
517 +                                nunflshed*AMBVALSIZ);
518 +                error(CONSISTENCY, errmsg);
519 +        }
520 + #endif
521   syncend:
522          n = fflush(ambfp);                      /* calls write() at last */
523 <        lastpos = lseek(fileno(ambfp), 0L, 1);
523 >        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
524 >                goto seekerr;
525          aflock(F_UNLCK);                        /* release file */
526          nunflshed = 0;
527          return(n);
528 + seekerr:
529 +        error(SYSTEM, "seek failed in ambsync");
530   }
531  
532   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines