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.8 by greg, Thu Jul 16 14:03:27 1992 UTC vs.
Revision 2.24 by greg, Thu Mar 24 13:41:04 1994 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 */
45 < static long  ambheadlen;        /* length of ambient file header */
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 71 | 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 <        AMBVAL  amb;
100 >        long  headlen;
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 <                        while (readambval(&amb, ambfp))
118 <                                avinsert(&amb, &atrunk, thescene.cuorg,
119 <                                                thescene.cusize);
120 <                                                        /* align */
121 <                        fseek(ambfp, -((ftell(ambfp)-ambheadlen)%AMBVALSIZ), 1);
122 <                } else if ((ambfp = fopen(afile, "w")) != NULL)
123 <                        initambfile(1);
124 <                else {
125 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
93 <                                        afile);
94 <                        error(SYSTEM, errmsg);
95 <                }
96 < }
97 <
98 <
99 < initambfile(creat)              /* initialize ambient file */
100 < int  creat;
101 < {
102 <        extern char  *progname, *octname, VersionID[];
103 <
104 <        setbuf(ambfp, bmalloc(BUFSIZ));
105 <        if (creat) {                    /* new file */
106 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
107 <                                progname, colval(ambval,RED),
108 <                                colval(ambval,GRN), colval(ambval,BLU),
109 <                                ambounce, ambacc);
110 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
111 <                                ambdiv, ambssamp, ambres,
112 <                                octname==NULL ? "" : octname);
113 <                fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
114 <                fputformat(AMBFMT, ambfp);
115 <                putc('\n', ambfp);
116 <                putambmagic(ambfp);
117 <                fflush(ambfp);
118 <        } else if (checkheader(ambfp, AMBFMT, NULL) < 0
119 <                        || !hasambmagic(ambfp)) {
120 <                sprintf(errmsg, "\"%s\" is not an ambient file", afname);
121 <                error(USER, errmsg);
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 <        ambheadlen = ftell(ambfp);
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 151 | 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 193 | 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();
201 <        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 282 | 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 307 | 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 332 | Line 338 | FVECT  pv, nv;
338  
339  
340   static
341 < avsave(av)                              /* insert and save an ambient value */
342 < AMBVAL  *av;
341 > initambfile(creat)              /* initialize ambient file */
342 > int  creat;
343   {
344 <        static int  nunflshed = 0;
344 >        extern char  *progname, *octname, VersionID[];
345  
346 <        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
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 >                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),
358 >                                ambounce, ambacc);
359 >                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
360 >                                ambdiv, ambssamp, ambres,
361 >                                octname==NULL ? "" : octname);
362 >                fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
363 >                fputformat(AMBFMT, ambfp);
364 >                putc('\n', ambfp);
365 >                putambmagic(ambfp);
366 >        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
367 >                error(USER, "bad ambient file");
368 > }
369 >
370 >
371 > static
372 > avsave(av)                              /* insert and save an ambient value */
373 > AMBVAL  *av;
374 > {
375 >        avinsert(avstore(av));
376          if (ambfp == NULL)
377                  return;
378          if (writambval(av, ambfp) < 0)
379                  goto writerr;
380 <        if (++nunflshed >= AMBFLUSH) {
380 >        if (++nunflshed >= AMBFLUSH)
381                  if (ambsync() == EOF)
382                          goto writerr;
348                nunflshed = 0;
349        }
383          return;
384   writerr:
385          error(SYSTEM, "error writing ambient file");
386   }
387  
388  
389 + static AMBVAL *
390 + avstore(aval)                           /* allocate memory and store aval */
391 + register AMBVAL  *aval;
392 + {
393 +        register AMBVAL  *av;
394 +
395 +        if ((av = newambval()) == NULL)
396 +                error(SYSTEM, "out of memory in avstore");
397 +        copystruct(av, aval);
398 +        return(av);
399 + }
400 +
401 +
402   static
403 < avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
404 < AMBVAL  *aval;
359 < register AMBTREE  *at;
360 < FVECT  c0;
361 < double  s;
403 > avinsert(av)                            /* insert ambient value in our tree */
404 > register AMBVAL  *av;
405   {
406 +        register AMBTREE  *at;
407 +        register AMBVAL  *ap;
408 +        AMBVAL  avh;
409          FVECT  ck0;
410 +        double  s;
411          int  branch;
365        register AMBVAL  *av;
412          register int  i;
413  
414 <        if ((av = newambval()) == NULL)
415 <                goto memerr;
416 <        copystruct(av, aval);
417 <        VCOPY(ck0, c0);
414 >        if (av->rad <= FTINY)
415 >                error(CONSISTENCY, "zero ambient radius in avinsert");
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)
422 <                                goto memerr;
422 >                                error(SYSTEM, "out of memory in avinsert");
423                  s *= 0.5;
424                  branch = 0;
425                  for (i = 0; i < 3; i++)
# Line 382 | Line 429 | double  s;
429                          }
430                  at = at->kid + branch;
431          }
432 <        av->next = at->alist;
433 <        at->alist = av;
434 <        return;
435 < memerr:
436 <        error(SYSTEM, "out of memory in avinsert");
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 < #include  <fcntl.h>
442 > static
443 > loadatree(at)                           /* move tree to main store */
444 > register AMBTREE  *at;
445 > {
446 >        register AMBVAL  *av;
447 >        register int  i;
448 >                                        /* transfer values at this node */
449 >        for (av = at->alist; av != NULL; av = at->alist) {
450 >                at->alist = av->next;
451 >                avinsert(av);
452 >        }
453 >        if (at->kid == NULL)
454 >                return;
455 >        for (i = 0; i < 8; i++)         /* transfer and free children */
456 >                loadatree(at->kid+i);
457 >        freeambtree(at->kid);
458 > }
459  
460  
461 + #ifdef  F_SETLKW
462 +
463   static
464 + aflock(typ)                     /* lock/unlock ambient file */
465 + int  typ;
466 + {
467 +        static struct flock  fls;       /* static so initialized to zeroes */
468 +
469 +        fls.l_type = typ;
470 +        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
471 +                error(SYSTEM, "cannot (un)lock ambient file");
472 + }
473 +
474 +
475 + int
476   ambsync()                       /* synchronize ambient file */
477   {
478          static FILE  *ambinp = NULL;
479 <        struct flock  fls;
480 <        AMBVAL  avs;
481 <        long  lastpos, flen;
479 >        static long  lastpos = -1;
480 >        long  flen;
481 >        AMBVAL  avs;
482          register int  n;
483 +
484 +        if (nunflshed == 0)
485 +                return(0);
486 +        if (lastpos < 0)        /* initializing (locked in initambfile) */
487 +                goto syncend;
488                                  /* gain exclusive access */
489 <        fls.l_type = F_WRLCK;
406 <        fls.l_whence = 0;
407 <        fls.l_start = 0L;
408 <        fls.l_len = 0L;
409 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
410 <                error(SYSTEM, "cannot lock ambient file");
489 >        aflock(F_WRLCK);
490                                  /* see if file has grown */
491 <        lastpos = lseek(fileno(ambfp), 0L, 2);  /* may move pointer */
492 <        flen = lseek(fileno(ambfp), 0L, 1);     /* new(?) file length */
493 <        if (n = (flen - lastpos)%AMBVALSIZ)     /* assure alignment */
494 <                lseek(fileno(ambfp), flen -= n, 0);
495 <        if (n = (flen - lastpos)/AMBVALSIZ) {   /* file has grown */
496 <                if (ambinp == NULL && (ambinp = fopen(afname, "r")) == NULL)
497 <                        error(SYSTEM, "cannot reopen ambient file");
498 <                fseek(ambinp, lastpos, 0);      /* go to previous position */
499 <                while (n--) {                   /* load contributed values */
491 >        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
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");
496 >                        if (ambinp == NULL)
497 >                                error(SYSTEM, "fdopen failed in ambsync");
498 >                }
499 >                if (fseek(ambinp, lastpos, 0) < 0)
500 >                        goto seekerr;
501 >                while (n >= AMBVALSIZ) {        /* load contributed values */
502                          readambval(&avs, ambinp);
503 <                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
503 >                        avinsert(avstore(&avs));
504 >                        n -= AMBVALSIZ;
505                  }
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 <        fls.l_type = F_UNLCK;                   /* release file */
522 <        fcntl(fileno(ambfp), F_SETLKW, &fls);
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
531 +
532 + int
533 + ambsync()                       /* flush ambient file */
534 + {
535 +        if (nunflshed == 0)
536 +                return(0);
537 +        nunflshed = 0;
538 +        return(fflush(ambfp));
539 + }
540 +
541 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines