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.16 by greg, Fri Jan 22 09:51:13 1993 UTC vs.
Revision 2.21 by greg, Thu Aug 5 16:25:05 1993 UTC

# Line 35 | Line 35 | double minarad;                /* minimum ambient radius */
35  
36   static AMBTREE  atrunk;         /* our ambient trunk node */
37  
38 static char  *ambfname = NULL;  /* ambient file name */
38   static FILE  *ambfp = NULL;     /* ambient file pointer */
39   static int  nunflshed = 0;      /* number of unflushed ambient values */
40  
# Line 44 | 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(), loadatree();
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 < 0 ? 0 : ar;               /* may be done already */
60                                                  /* set min & max radii */
61          if (ar <= 0) {
62                  minarad = 0.0;
# Line 67 | Line 72 | int  ar;
72   }
73  
74  
75 + setambacc(newa)                         /* set ambient accuracy */
76 + double  newa;
77 + {
78 +        static double  oldambacc = -1.0;
79 +        AMBTREE  oldatrunk;
80 +
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 */
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 +        loadatree(&oldatrunk);
93 +        oldambacc = ambacc;             /* remeber setting for next call */
94 + }
95 +
96 +
97   setambient(afile)                       /* initialize calculation */
98   char  *afile;
99   {
# Line 74 | 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 opened",
109 +                                afile);
110 +                error(WARNING, errmsg);
111 +                return;
112 +        }
113                                                  /* open ambient file */
114 <        if ((ambfname = afile) != NULL) {
115 <                if ((ambfp = fopen(afile, "r+")) != NULL) {
116 <                        initambfile(0);
117 <                        headlen = ftell(ambfp);
118 <                        while (readambval(&amb, ambfp))
119 <                                avinsert(&amb, &atrunk, thescene.cuorg,
120 <                                                thescene.cusize);
121 <                                                        /* align */
122 <                        fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
123 <                } else if ((ambfp = fopen(afile, "w+")) != NULL)
124 <                        initambfile(1);
125 <                else {
90 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
91 <                                        afile);
92 <                        error(SYSTEM, errmsg);
93 <                }
94 <                nunflshed++;    /* lie */
95 <                ambsync();
114 >        if ((ambfp = fopen(afile, "r+")) != NULL) {
115 >                initambfile(0);
116 >                headlen = ftell(ambfp);
117 >                while (readambval(&amb, ambfp))
118 >                        avinsert(avstore(&amb));
119 >                                                /* align */
120 >                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
121 >        } else if ((ambfp = fopen(afile, "w+")) != NULL)
122 >                initambfile(1);
123 >        else {
124 >                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
125 >                error(SYSTEM, errmsg);
126          }
127 +        nunflshed++;    /* lie */
128 +        ambsync();
129   }
130  
131  
# Line 309 | Line 341 | int  creat;
341   {
342          extern char  *progname, *octname, VersionID[];
343  
344 + #ifdef  F_SETLKW
345 +        aflock(creat ? F_WRLCK : F_RDLCK);
346 + #endif
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 325 | Line 360 | int  creat;
360                  fputformat(AMBFMT, ambfp);
361                  putc('\n', ambfp);
362                  putambmagic(ambfp);
363 <        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) {
364 <                sprintf(errmsg, "bad ambient file \"%s\"", ambfname);
330 <                error(USER, errmsg);
331 <        }
363 >        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
364 >                error(USER, "bad ambient file");
365   }
366  
367  
# Line 336 | Line 369 | static
369   avsave(av)                              /* insert and save an ambient value */
370   AMBVAL  *av;
371   {
372 <        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
372 >        avinsert(avstore(av));
373          if (ambfp == NULL)
374                  return;
375          if (writambval(av, ambfp) < 0)
# Line 346 | Line 379 | AMBVAL *av;
379                          goto writerr;
380          return;
381   writerr:
382 <        sprintf(errmsg, "error writing ambient file \"%s\"", ambfname);
350 <        error(SYSTEM, errmsg);
382 >        error(SYSTEM, "error writing ambient file");
383   }
384  
385  
386 + static AMBVAL *
387 + avstore(aval)                           /* allocate memory and store aval */
388 + register AMBVAL  *aval;
389 + {
390 +        register AMBVAL  *av;
391 +
392 +        if ((av = newambval()) == NULL)
393 +                error(SYSTEM, "out of memory in avstore");
394 +        copystruct(av, aval);
395 +        return(av);
396 + }
397 +
398 +
399   static
400 < avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
401 < AMBVAL  *aval;
357 < register AMBTREE  *at;
358 < FVECT  c0;
359 < double  s;
400 > avinsert(av)                            /* insert ambient value in our tree */
401 > register AMBVAL  *av;
402   {
403 +        register AMBTREE  *at;
404          FVECT  ck0;
405 +        double  s;
406          int  branch;
363        register AMBVAL  *av;
407          register int  i;
408  
409 <        if ((av = newambval()) == NULL)
410 <                goto memerr;
411 <        copystruct(av, aval);
412 <        VCOPY(ck0, c0);
409 >        if (av->rad <= FTINY)
410 >                error(CONSISTENCY, "zero ambient radius in avinsert");
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)
417 <                                goto memerr;
417 >                                error(SYSTEM, "out of memory in avinsert");
418                  s *= 0.5;
419                  branch = 0;
420                  for (i = 0; i < 3; i++)
# Line 382 | Line 426 | double s;
426          }
427          av->next = at->alist;
428          at->alist = av;
385        return;
386 memerr:
387        error(SYSTEM, "out of memory in avinsert");
429   }
430  
431  
432 < #ifdef  NIX
432 > static
433 > loadatree(at)                           /* move tree to main store */
434 > register AMBTREE  *at;
435 > {
436 >        register AMBVAL  *av;
437 >        register int  i;
438 >                                        /* transfer values at this node */
439 >        for (av = at->alist; av != NULL; av = at->alist) {
440 >                at->alist = av->next;
441 >                avinsert(av);
442 >        }
443 >        if (at->kid == NULL)
444 >                return;
445 >        for (i = 0; i < 8; i++)         /* transfer and free children */
446 >                loadatree(at->kid+i);
447 >        freeambtree(at->kid);
448 > }
449  
450 < int
451 < ambsync()                       /* flush ambient file */
450 >
451 > #ifdef  F_SETLKW
452 >
453 > static
454 > aflock(typ)                     /* lock/unlock ambient file */
455 > int  typ;
456   {
457 <        if (nunflshed == 0)
458 <                return(0);
459 <        nunflshed = 0;
460 <        return(fflush(ambfp));
457 >        static struct flock  fls;       /* static so initialized to zeroes */
458 >
459 >        fls.l_type = typ;
460 >        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
461 >                error(SYSTEM, "cannot (un)lock ambient file");
462   }
463  
402 #else
464  
465   int
466   ambsync()                       /* synchronize ambient file */
467   {
468          static FILE  *ambinp = NULL;
469          static long  lastpos = -1;
409        struct flock  fls;
470          long  flen;
471          AMBVAL  avs;
472          register int  n;
473  
474          if (nunflshed == 0)
475                  return(0);
476 <                                /* gain exclusive access */
417 <        fls.l_type = F_WRLCK;
418 <        fls.l_whence = 0;
419 <        fls.l_start = 0L;
420 <        fls.l_len = 0L;
421 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
422 <                error(SYSTEM, "cannot lock ambient file");
423 <        if (lastpos < 0)        /* initializing */
476 >        if (lastpos < 0)        /* initializing (locked in initambfile) */
477                  goto syncend;
478 +                                /* gain exclusive access */
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) {
485 <                        ambinp = fopen(ambfname, "r");
484 >                if (ambinp == NULL) {           /* use duplicate filedes */
485 >                        ambinp = fdopen(dup(fileno(ambfp)), "r");
486                          if (ambinp == NULL)
487 <                                error(SYSTEM, "fopen failed in ambsync");
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(&avs,&atrunk,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);
511 <        fls.l_type = F_UNLCK;                   /* release file */
512 <        fcntl(fileno(ambfp), F_SETLKW, &fls);
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
520 +
521 + int
522 + ambsync()                       /* flush ambient file */
523 + {
524 +        if (nunflshed == 0)
525 +                return(0);
526 +        nunflshed = 0;
527 +        return(fflush(ambfp));
528   }
529  
530   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines