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.9 by greg, Thu Jul 16 15:53:42 1992 UTC vs.
Revision 2.23 by greg, Sat Dec 18 10:37:01 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ LBL";
6  
7   /*
8   *  ambient.c - routines dealing with ambient (inter-reflected) component.
9 *
10 *  The macro AMBFLUSH (if defined) is the number of ambient values
11 *      to wait before flushing to the ambient file.
12 *
13 *     5/9/86
9   */
10  
11   #include  "ray.h"
# Line 23 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
22  
23   typedef struct ambtree {
24 <        AMBVAL  *alist;         /* ambient value list */
25 <        struct ambtree  *kid;   /* 8 child nodes */
24 >        AMBVAL  *alist;         /* ambient value list */
25 >        struct ambtree  *kid;   /* 8 child nodes */
26   }  AMBTREE;                     /* ambient octree */
27  
28   extern CUBE  thescene;          /* contains space boundaries */
29  
30 < #define  MAXASET        511     /* maximum number of elements in ambient set */
31 < OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
30 > #define  MAXASET        511     /* maximum number of elements in ambient set */
31 > OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
32  
33 < double  maxarad;                /* maximum ambient radius */
34 < double  minarad;                /* minimum ambient radius */
33 > double  maxarad;                /* maximum ambient radius */
34 > double  minarad;                /* minimum ambient radius */
35  
36 < static AMBTREE  atrunk;         /* our ambient trunk node */
36 > static AMBTREE  atrunk;         /* our ambient trunk node */
37  
38   static FILE  *ambfp = NULL;     /* ambient file pointer */
39 < static char  *afname;           /* ambient file name */
39 > static int  nunflshed = 0;      /* number of unflushed ambient values */
40  
41 < #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
41 > #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
42  
43 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
43 > #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
44  
45 < #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
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(), 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 70 | 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   {
100          long  headlen;
101 <        AMBVAL  amb;
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 ((afname = 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 {
126 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
127 <                                        afile);
128 <                        error(SYSTEM, errmsg);
96 <                }
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  
132   ambnotify(obj)                  /* record new modifier */
133 < OBJECT  obj;
133 > OBJECT  obj;
134   {
135          static int  hitlimit = 0;
136 <        register OBJREC  *o = objptr(obj);
136 >        register OBJREC  *o = objptr(obj);
137          register char  **amblp;
138  
139          if (hitlimit || !ismodifier(o->otype))
# Line 124 | Line 156 | COLOR  acol;
156   register RAY  *r;
157   {
158          static int  rdepth = 0;                 /* ambient recursion */
159 <        double  d;
159 >        double  d;
160  
161          if (ambdiv <= 0)                        /* no ambient calculation */
162                  goto dumbamb;
# Line 166 | Line 198 | sumambient(acol, r, al, at, c0, s)     /* get interpolated
198   COLOR  acol;
199   register RAY  *r;
200   int  al;
201 < AMBTREE  *at;
201 > AMBTREE  *at;
202   FVECT  c0;
203 < double  s;
203 > double  s;
204   {
205 <        extern double  sqrt();
174 <        double  d, e1, e2, wt, wsum;
205 >        double  d, e1, e2, wt, wsum;
206          COLOR  ct;
207          FVECT  ck0;
208          int  i;
209          register int  j;
210 <        register AMBVAL  *av;
210 >        register AMBVAL  *av;
211                                          /* do this node */
212          wsum = 0.0;
213          for (av = at->alist; av != NULL; av = av->next) {
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 255 | Line 288 | COLOR  acol;
288   register RAY  *r;
289   int  al;
290   {
291 <        AMBVAL  amb;
291 >        AMBVAL  amb;
292          FVECT   gp, gd;
293                                                  /* compute weight */
294          amb.weight = pow(AVGREFL, (double)al);
# Line 280 | Line 313 | int  al;
313  
314   extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
315   COLOR  cr;
316 < register AMBVAL  *ap;
316 > register AMBVAL  *ap;
317   FVECT  pv, nv;
318   {
319          FVECT  v1, v2;
320          register int  i;
321 <        double  d;
321 >        double  d;
322  
323          d = 1.0;                        /* zeroeth order */
324                                          /* gradient due to translation */
# Line 310 | Line 343 | int  creat;
343   {
344          extern char  *progname, *octname, VersionID[];
345  
346 <        setbuf(ambfp, bmalloc(BUFSIZ));
346 > #ifdef  F_SETLKW
347 >        aflock(creat ? F_WRLCK : F_RDLCK);
348 > #endif
349 > #ifdef MSDOS
350 >        setmode(fileno(ambfp), O_BINARY);
351 > #endif
352 >        setbuf(ambfp, bmalloc(BUFSIZ+8));
353          if (creat) {                    /* new file */
354                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
355                                  progname, colval(ambval,RED),
# Line 323 | Line 362 | int  creat;
362                  fputformat(AMBFMT, ambfp);
363                  putc('\n', ambfp);
364                  putambmagic(ambfp);
365 <                fflush(ambfp);
366 <        } else if (checkheader(ambfp, AMBFMT, NULL) < 0
328 <                        || !hasambmagic(ambfp)) {
329 <                sprintf(errmsg, "\"%s\" is not an ambient file", afname);
330 <                error(USER, errmsg);
331 <        }
365 >        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
366 >                error(USER, "bad ambient file");
367   }
368  
369  
370   static
371   avsave(av)                              /* insert and save an ambient value */
372 < AMBVAL  *av;
372 > AMBVAL  *av;
373   {
374 <        static int  nunflshed = 0;
340 <
341 <        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
374 >        avinsert(avstore(av));
375          if (ambfp == NULL)
376                  return;
377          if (writambval(av, ambfp) < 0)
378                  goto writerr;
379 <        if (++nunflshed >= AMBFLUSH) {
379 >        if (++nunflshed >= AMBFLUSH)
380                  if (ambsync() == EOF)
381                          goto writerr;
349                nunflshed = 0;
350        }
382          return;
383   writerr:
384          error(SYSTEM, "error writing ambient file");
385   }
386  
387  
388 + static AMBVAL *
389 + avstore(aval)                           /* allocate memory and store aval */
390 + register AMBVAL  *aval;
391 + {
392 +        register AMBVAL  *av;
393 +
394 +        if ((av = newambval()) == NULL)
395 +                error(SYSTEM, "out of memory in avstore");
396 +        copystruct(av, aval);
397 +        return(av);
398 + }
399 +
400 +
401   static
402 < avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
403 < AMBVAL  *aval;
360 < register AMBTREE  *at;
361 < FVECT  c0;
362 < double  s;
402 > avinsert(av)                            /* insert ambient value in our tree */
403 > register AMBVAL  *av;
404   {
405 +        register AMBTREE  *at;
406 +        register AMBVAL  *ap;
407 +        AMBVAL  avh;
408          FVECT  ck0;
409 +        double  s;
410          int  branch;
366        register AMBVAL  *av;
411          register int  i;
412  
413 <        if ((av = newambval()) == NULL)
414 <                goto memerr;
415 <        copystruct(av, aval);
416 <        VCOPY(ck0, c0);
413 >        if (av->rad <= FTINY)
414 >                error(CONSISTENCY, "zero ambient radius in avinsert");
415 >        at = &atrunk;
416 >        VCOPY(ck0, thescene.cuorg);
417 >        s = thescene.cusize;
418          while (s*(OCTSCALE/2) > av->rad*ambacc) {
419                  if (at->kid == NULL)
420                          if ((at->kid = newambtree()) == NULL)
421 <                                goto memerr;
421 >                                error(SYSTEM, "out of memory in avinsert");
422                  s *= 0.5;
423                  branch = 0;
424                  for (i = 0; i < 3; i++)
# Line 383 | Line 428 | double  s;
428                          }
429                  at = at->kid + branch;
430          }
431 <        av->next = at->alist;
432 <        at->alist = av;
433 <        return;
434 < memerr:
435 <        error(SYSTEM, "out of memory in avinsert");
431 >        avh.next = at->alist;           /* order by increasing level */
432 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
433 >                if (ap->next->lvl >= av->lvl)
434 >                        break;
435 >        av->next = ap->next;
436 >        ap->next = av;
437 >        at->alist = avh.next;
438   }
439  
440  
441 < #include  <fcntl.h>
441 > static
442 > loadatree(at)                           /* move tree to main store */
443 > register AMBTREE  *at;
444 > {
445 >        register AMBVAL  *av;
446 >        register int  i;
447 >                                        /* transfer values at this node */
448 >        for (av = at->alist; av != NULL; av = at->alist) {
449 >                at->alist = av->next;
450 >                avinsert(av);
451 >        }
452 >        if (at->kid == NULL)
453 >                return;
454 >        for (i = 0; i < 8; i++)         /* transfer and free children */
455 >                loadatree(at->kid+i);
456 >        freeambtree(at->kid);
457 > }
458  
459  
460 + #ifdef  F_SETLKW
461 +
462   static
463 + aflock(typ)                     /* lock/unlock ambient file */
464 + int  typ;
465 + {
466 +        static struct flock  fls;       /* static so initialized to zeroes */
467 +
468 +        fls.l_type = typ;
469 +        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
470 +                error(SYSTEM, "cannot (un)lock ambient file");
471 + }
472 +
473 +
474 + int
475   ambsync()                       /* synchronize ambient file */
476   {
477          static FILE  *ambinp = NULL;
478 <        struct flock  fls;
479 <        AMBVAL  avs;
480 <        long  lastpos, flen;
478 >        static long  lastpos = -1;
479 >        long  flen;
480 >        AMBVAL  avs;
481          register int  n;
482 +
483 +        if (nunflshed == 0)
484 +                return(0);
485 +        if (lastpos < 0)        /* initializing (locked in initambfile) */
486 +                goto syncend;
487                                  /* gain exclusive access */
488 <        fls.l_type = F_WRLCK;
407 <        fls.l_whence = 0;
408 <        fls.l_start = 0L;
409 <        fls.l_len = 0L;
410 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
411 <                error(SYSTEM, "cannot lock ambient file");
488 >        aflock(F_WRLCK);
489                                  /* see if file has grown */
490 <        lastpos = lseek(fileno(ambfp), 0L, 1);  /* get previous position */
491 <        flen = lseek(fileno(ambfp), 0L, 2);     /* new(?) file length */
492 <        if (n = (flen - lastpos)%AMBVALSIZ)     /* assure alignment */
493 <                lseek(fileno(ambfp), (long)-n, 1);
494 <        if (n = (flen - lastpos)/AMBVALSIZ) {   /* file has grown */
495 <                if (ambinp == NULL && (ambinp = fopen(afname, "r")) == NULL)
496 <                        error(SYSTEM, "cannot reopen ambient file");
497 <                fseek(ambinp, lastpos, 0);      /* go to previous position */
498 <                while (n--) {                   /* load contributed values */
490 >        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
491 >                goto seekerr;
492 >        if (n = flen - lastpos) {               /* file has grown */
493 >                if (ambinp == NULL) {           /* use duplicate filedes */
494 >                        ambinp = fdopen(dup(fileno(ambfp)), "r");
495 >                        if (ambinp == NULL)
496 >                                error(SYSTEM, "fdopen failed in ambsync");
497 >                }
498 >                if (fseek(ambinp, lastpos, 0) < 0)
499 >                        goto seekerr;
500 >                while (n >= AMBVALSIZ) {        /* load contributed values */
501                          readambval(&avs, ambinp);
502 <                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
502 >                        avinsert(avstore(&avs));
503 >                        n -= AMBVALSIZ;
504                  }
505 +                /*** seek always as safety measure
506 +                if (n) ***/                     /* alignment */
507 +                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
508 +                                goto seekerr;
509          }
510 + #ifdef  DEBUG
511 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
512 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
513 +                                ambfp->_ptr - ambfp->_base,
514 +                                nunflshed*AMBVALSIZ);
515 +                error(CONSISTENCY, errmsg);
516 +        }
517 + #endif
518 + syncend:
519          n = fflush(ambfp);                      /* calls write() at last */
520 <        fls.l_type = F_UNLCK;                   /* release file */
521 <        fcntl(fileno(ambfp), F_SETLKW, &fls);
520 >        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
521 >                goto seekerr;
522 >        aflock(F_UNLCK);                        /* release file */
523 >        nunflshed = 0;
524          return(n);
525 + seekerr:
526 +        error(SYSTEM, "seek failed in ambsync");
527   }
528 +
529 + #else
530 +
531 + int
532 + ambsync()                       /* flush ambient file */
533 + {
534 +        if (nunflshed == 0)
535 +                return(0);
536 +        nunflshed = 0;
537 +        return(fflush(ambfp));
538 + }
539 +
540 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines