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 1.9 by greg, Fri Jan 19 00:00:05 1990 UTC vs.
Revision 2.22 by greg, Fri Aug 6 17:24:24 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 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 19 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "otypes.h"
16  
17 + #include  "ambient.h"
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 */
26 + }  AMBTREE;                     /* ambient octree */
27 +
28   extern CUBE  thescene;          /* contains space boundaries */
29  
30 < extern COLOR  ambval;           /* global ambient component */
31 < extern double  ambacc;          /* ambient accuracy */
30 < extern int  ambres;             /* ambient resolution */
31 < extern int  ambdiv;             /* number of divisions for calculation */
32 < extern int  ambssamp;           /* number of super-samples */
33 < extern int  ambounce;           /* number of ambient bounces */
34 < extern char  *amblist[];        /* ambient include/exclude list */
35 < extern int  ambincl;            /* include == 1, exclude == 0 */
30 > #define  MAXASET        511     /* maximum number of elements in ambient set */
31 > OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
32  
33 < OBJECT  ambset[256]={0};        /* ambient include/exclude set */
33 > double  maxarad;                /* maximum ambient radius */
34 > double  minarad;                /* minimum ambient radius */
35  
36 < double  maxarad;                /* maximum ambient radius */
40 < double  minarad;                /* minimum ambient radius */
36 > static AMBTREE  atrunk;         /* our ambient trunk node */
37  
38 < typedef struct ambval {
39 <        FVECT  pos;             /* position in space */
44 <        FVECT  dir;             /* normal direction */
45 <        int  lvl;               /* recursion level of parent ray */
46 <        float  weight;          /* weight of parent ray */
47 <        COLOR  val;             /* computed ambient value */
48 <        float  rad;             /* validity radius */
49 <        struct ambval  *next;   /* next in list */
50 < }  AMBVAL;                      /* ambient value */
38 > static FILE  *ambfp = NULL;     /* ambient file pointer */
39 > static int  nunflshed = 0;      /* number of unflushed ambient values */
40  
41 < typedef struct ambtree {
53 <        AMBVAL  *alist;         /* ambient value list */
54 <        struct ambtree  *kid;   /* 8 child nodes */
55 < }  AMBTREE;                     /* ambient octree */
41 > #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
42  
43 < typedef struct {
58 <        float  k;               /* error contribution per sample */
59 <        COLOR  v;               /* ray sum */
60 <        int  n;                 /* number of samples */
61 <        short  t, p;            /* theta, phi indices */
62 < }  AMBSAMP;                     /* ambient sample */
43 > #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
44  
45 < static AMBTREE  atrunk;         /* our ambient trunk node */
45 > #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46 > #define  freeambtree(t) free((char *)(t))
47  
48 < static FILE  *ambfp = NULL;     /* ambient file pointer */
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  
68 #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
55  
56 < #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
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;
63 >                maxarad = thescene.cusize / 2.0;
64 >        } else {
65 >                minarad = thescene.cusize / ar;
66 >                maxarad = 16.0 * minarad;               /* heuristic */
67 >                if (maxarad > thescene.cusize / 2.0)
68 >                        maxarad = thescene.cusize / 2.0;
69 >        }
70 >        if (maxarad <= FTINY)
71 >                maxarad = .001;
72 > }
73  
72 double  sumambient(), doambient(), makeambient();
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  ftell();
101 <        OBJECT  obj;
102 <        AMBVAL  amb;
103 <
104 <        maxarad = thescene.cusize / 2.0;                /* maximum radius */
105 <                                                        /* minimum radius */
106 <        minarad = ambres > 0 ? thescene.cusize/ambres : 0.0;
107 <
108 <                                        /* open ambient file */
109 <        if (afile != NULL)
110 <                if ((ambfp = fopen(afile, "r+")) != NULL) {
111 <                        while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1)
112 <                                avinsert(&amb, &atrunk, thescene.cuorg,
113 <                                                thescene.cusize);
114 <                                                        /* align */
115 <                        fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1);
116 <                } else if ((ambfp = fopen(afile, "w")) == NULL) {
117 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
118 <                                        afile);
119 <                        error(SYSTEM, errmsg);
120 <                }
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 ((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 <        register OBJREC  *o = objptr(obj);
135 >        static int  hitlimit = 0;
136 >        register OBJREC  *o = objptr(obj);
137          register char  **amblp;
138  
139 <        if (!ismodifier(o->otype))
139 >        if (hitlimit || !ismodifier(o->otype))
140                  return;
141          for (amblp = amblist; *amblp != NULL; amblp++)
142                  if (!strcmp(o->oname, *amblp)) {
143 +                        if (ambset[0] >= MAXASET) {
144 +                                error(WARNING, "too many modifiers in ambient list");
145 +                                hitlimit++;
146 +                                return;         /* should this be fatal? */
147 +                        }
148                          insertelem(ambset, obj);
149                          return;
150                  }
# Line 120 | Line 156 | COLOR  acol;
156   register RAY  *r;
157   {
158          static int  rdepth = 0;                 /* ambient recursion */
159 <        double  wsum;
159 >        double  d;
160  
125        rdepth++;                               /* increment level */
126
161          if (ambdiv <= 0)                        /* no ambient calculation */
162                  goto dumbamb;
163                                                  /* check number of bounces */
164 <        if (rdepth > ambounce)
164 >        if (rdepth >= ambounce)
165                  goto dumbamb;
166                                                  /* check ambient list */
167          if (ambincl != -1 && r->ro != NULL &&
# Line 135 | Line 169 | register RAY  *r;
169                  goto dumbamb;
170  
171          if (ambacc <= FTINY) {                  /* no ambient storage */
172 <                if (doambient(acol, r) == 0.0)
172 >                rdepth++;
173 >                d = doambient(acol, r, r->rweight, NULL, NULL);
174 >                rdepth--;
175 >                if (d == 0.0)
176                          goto dumbamb;
177 <                goto done;
177 >                return;
178          }
179                                                  /* get ambient value */
180          setcolor(acol, 0.0, 0.0, 0.0);
181 <        wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize);
182 <        if (wsum > FTINY)
183 <                scalecolor(acol, 1.0/wsum);
184 <        else if (makeambient(acol, r) == 0.0)
185 <                goto dumbamb;
186 <        goto done;
187 <
181 >        d = sumambient(acol, r, rdepth,
182 >                        &atrunk, thescene.cuorg, thescene.cusize);
183 >        if (d > FTINY)
184 >                scalecolor(acol, 1.0/d);
185 >        else {
186 >                d = makeambient(acol, r, rdepth++);
187 >                rdepth--;
188 >        }
189 >        if (d > FTINY)
190 >                return;
191   dumbamb:                                        /* return global value */
192          copycolor(acol, ambval);
153 done:                                           /* must finish here! */
154        rdepth--;
193   }
194  
195  
196   double
197 < sumambient(acol, r, at, c0, s)          /* get interpolated ambient value */
197 > sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
198   COLOR  acol;
199   register RAY  *r;
200 < AMBTREE  *at;
200 > int  al;
201 > AMBTREE  *at;
202   FVECT  c0;
203 < double  s;
203 > double  s;
204   {
205 <        extern double  sqrt();
167 <        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 <                 *  Ray strength test.
215 >                 *  Ambient level test.
216                   */
217 <                if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY)
217 >                if (av->lvl > al || av->weight < r->rweight-FTINY)
218                          continue;
219                  /*
220                   *  Ambient radius test.
# Line 203 | Line 241 | double  s;
241                  for (j = 0; j < 3; j++)
242                          d += (r->rop[j] - av->pos[j]) *
243                                          (av->dir[j] + r->ron[j]);
244 <                if (d < -minarad)
244 >                if (d*0.5 < -minarad*ambacc-.001)
245                          continue;
246                  /*
247                   *  Jittering final test reduces image artifacts.
248                   */
249                  wt = sqrt(e1) + sqrt(e2);
250 <                wt *= .9 + .2*frandom();
250 >                wt *= .9 + .2*urand(9015+samplendx);
251                  if (wt > ambacc)
252                          continue;
253                  if (wt <= 1e-3)
# Line 217 | Line 255 | double  s;
255                  else
256                          wt = 1.0 / wt;
257                  wsum += wt;
258 <                copycolor(ct, av->val);
258 >                extambient(ct, av, r->rop, r->ron);
259                  scalecolor(ct, wt);
260                  addcolor(acol, ct);
261          }
# Line 236 | Line 274 | double  s;
274                                  break;
275                  }
276                  if (j == 3)
277 <                        wsum += sumambient(acol, r, at->kid+i, ck0, s);
277 >                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
278          }
279          return(wsum);
280   }
281  
282  
283   double
284 < makeambient(acol, r)            /* make a new ambient value */
284 > makeambient(acol, r, al)        /* make a new ambient value */
285   COLOR  acol;
286   register RAY  *r;
287 + int  al;
288   {
289 <        AMBVAL  amb;
290 <
291 <        amb.rad = doambient(acol, r);           /* compute ambient */
289 >        AMBVAL  amb;
290 >        FVECT   gp, gd;
291 >                                                /* compute weight */
292 >        amb.weight = pow(AVGREFL, (double)al);
293 >        if (r->rweight < 0.2*amb.weight)        /* heuristic */
294 >                amb.weight = r->rweight;
295 >                                                /* compute ambient */
296 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
297          if (amb.rad == 0.0)
298                  return(0.0);
299                                                  /* store it */
300          VCOPY(amb.pos, r->rop);
301          VCOPY(amb.dir, r->ron);
302 <        amb.lvl = r->rlvl;
259 <        amb.weight = r->rweight;
302 >        amb.lvl = al;
303          copycolor(amb.val, acol);
304 +        VCOPY(amb.gpos, gp);
305 +        VCOPY(amb.gdir, gd);
306                                                  /* insert into tree */
307 <        avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize);
263 <        avsave(&amb);                           /* write to file */
307 >        avsave(&amb);                           /* and save to file */
308          return(amb.rad);
309   }
310  
311  
312 < double
313 < doambient(acol, r)                      /* compute ambient component */
314 < COLOR  acol;
315 < register RAY  *r;
312 > extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
313 > COLOR  cr;
314 > register AMBVAL  *ap;
315 > FVECT  pv, nv;
316   {
317 <        extern int  ambcmp();
318 <        extern double  sin(), cos(), sqrt();
319 <        double  phi, xd, yd, zd;
276 <        double  b, b2;
277 <        register AMBSAMP  *div;
278 <        AMBSAMP  dnew;
279 <        RAY  ar;
280 <        FVECT  ux, uy;
281 <        double  arad;
282 <        int  ndivs, nt, np, ns, ne, i, j;
283 <        register int  k;
317 >        FVECT  v1, v2;
318 >        register int  i;
319 >        double  d;
320  
321 <        setcolor(acol, 0.0, 0.0, 0.0);
322 <                                        /* set number of divisions */
323 <        nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5;
324 <        np = 2 * nt;
325 <        ndivs = nt * np;
326 <                                        /* check first */
327 <        if (ndivs == 0 || rayorigin(&ar, r, AMBIENT, 0.5) < 0)
328 <                return(0.0);
329 <                                        /* set number of super-samples */
330 <        ns = ambssamp * r->rweight + 0.5;
331 <        if (ns > 0) {
296 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
297 <                if (div == NULL)
298 <                        error(SYSTEM, "out of memory in doambient");
321 >        d = 1.0;                        /* zeroeth order */
322 >                                        /* gradient due to translation */
323 >        for (i = 0; i < 3; i++)
324 >                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
325 >                                        /* gradient due to rotation */
326 >        VCOPY(v1, ap->dir);
327 >        fcross(v2, v1, nv);
328 >        d += DOT(ap->gdir, v2);
329 >        if (d <= 0.0) {
330 >                setcolor(cr, 0.0, 0.0, 0.0);
331 >                return;
332          }
333 <                                        /* make axes */
334 <        uy[0] = uy[1] = uy[2] = 0.0;
302 <        for (k = 0; k < 3; k++)
303 <                if (r->ron[k] < 0.6 && r->ron[k] > -0.6)
304 <                        break;
305 <        uy[k] = 1.0;
306 <        fcross(ux, r->ron, uy);
307 <        normalize(ux);
308 <        fcross(uy, ux, r->ron);
309 <                                                /* sample divisions */
310 <        arad = 0.0;
311 <        ne = 0;
312 <        for (i = 0; i < nt; i++)
313 <                for (j = 0; j < np; j++) {
314 <                        rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
315 <                        zd = sqrt((i+frandom())/nt);
316 <                        phi = 2.0*PI * (j+frandom())/np;
317 <                        xd = cos(phi) * zd;
318 <                        yd = sin(phi) * zd;
319 <                        zd = sqrt(1.0 - zd*zd);
320 <                        for (k = 0; k < 3; k++)
321 <                                ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
322 <                        rayvalue(&ar);
323 <                        if (ar.rot < FHUGE)
324 <                                arad += 1.0 / ar.rot;
325 <                        if (ns > 0) {                   /* save division */
326 <                                div[ne].k = 0.0;
327 <                                copycolor(div[ne].v, ar.rcol);
328 <                                div[ne].n = 0;
329 <                                div[ne].t = i; div[ne].p = j;
330 <                                                        /* sum errors */
331 <                                b = bright(ar.rcol);
332 <                                if (i > 0) {            /* from above */
333 <                                        b2 = bright(div[ne-np].v) - b;
334 <                                        b2 *= b2 * 0.25;
335 <                                        div[ne].k += b2;
336 <                                        div[ne].n++;
337 <                                        div[ne-np].k += b2;
338 <                                        div[ne-np].n++;
339 <                                }
340 <                                if (j > 0) {            /* from behind */
341 <                                        b2 = bright(div[ne-1].v) - b;
342 <                                        b2 *= b2 * 0.25;
343 <                                        div[ne].k += b2;
344 <                                        div[ne].n++;
345 <                                        div[ne-1].k += b2;
346 <                                        div[ne-1].n++;
347 <                                }
348 <                                if (j == np-1) {        /* around */
349 <                                        b2 = bright(div[ne-(np-1)].v) - b;
350 <                                        b2 *= b2 * 0.25;
351 <                                        div[ne].k += b2;
352 <                                        div[ne].n++;
353 <                                        div[ne-(np-1)].k += b2;
354 <                                        div[ne-(np-1)].n++;
355 <                                }
356 <                                ne++;
357 <                        } else
358 <                                addcolor(acol, ar.rcol);
359 <                }
360 <        for (k = 0; k < ne; k++) {              /* compute errors */
361 <                if (div[k].n > 1)
362 <                        div[k].k /= div[k].n;
363 <                div[k].n = 1;
364 <        }
365 <                                                /* sort the divisions */
366 <        qsort(div, ne, sizeof(AMBSAMP), ambcmp);
367 <                                                /* skim excess */
368 <        while (ne > ns) {
369 <                ne--;
370 <                addcolor(acol, div[ne].v);
371 <        }
372 <                                                /* super-sample */
373 <        for (i = ns; i > 0; i--) {
374 <                rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
375 <                zd = sqrt((div[0].t+frandom())/nt);
376 <                phi = 2.0*PI * (div[0].p+frandom())/np;
377 <                xd = cos(phi) * zd;
378 <                yd = sin(phi) * zd;
379 <                zd = sqrt(1.0 - zd*zd);
380 <                for (k = 0; k < 3; k++)
381 <                        ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
382 <                rayvalue(&ar);
383 <                if (ar.rot < FHUGE)
384 <                        arad += 1.0 / ar.rot;
385 <                                                /* recompute error */
386 <                copycolor(dnew.v, div[0].v);
387 <                addcolor(dnew.v, ar.rcol);
388 <                dnew.n = div[0].n + 1;
389 <                dnew.t = div[0].t; dnew.p = div[0].p;
390 <                b2 = bright(dnew.v)/dnew.n - bright(ar.rcol);
391 <                b2 = b2*b2 + div[0].k*(div[0].n*div[0].n);
392 <                dnew.k = b2/(dnew.n*dnew.n);
393 <                                                /* reinsert */
394 <                for (k = 0; k < ne-1 && dnew.k < div[k+1].k; k++)
395 <                        copystruct(&div[k], &div[k+1]);
396 <                copystruct(&div[k], &dnew);
397 <
398 <                if (ne >= i) {          /* extract darkest division */
399 <                        ne--;
400 <                        if (div[ne].n > 1)
401 <                                scalecolor(div[ne].v, 1.0/div[ne].n);
402 <                        addcolor(acol, div[ne].v);
403 <                }
404 <        }
405 <        scalecolor(acol, 1.0/ndivs);
406 <        if (arad <= FTINY)
407 <                arad = FHUGE;
408 <        else
409 <                arad = (ndivs+ns) / arad / sqrt(r->rweight);
410 <        if (arad > maxarad)
411 <                arad = maxarad;
412 <        else if (arad < minarad)
413 <                arad = minarad;
414 <        if (ns > 0)
415 <                free((char *)div);
416 <        return(arad);
333 >        copycolor(cr, ap->val);
334 >        scalecolor(cr, d);
335   }
336  
337  
338 < static int
339 < ambcmp(d1, d2)                          /* decreasing order */
340 < AMBSAMP  *d1, *d2;
338 > static
339 > initambfile(creat)              /* initialize ambient file */
340 > int  creat;
341   {
342 <        if (d1->k < d2->k)
343 <                return(1);
344 <        if (d1->k > d2->k)
345 <                return(-1);
346 <        return(0);
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+8));
351 >        if (creat) {                    /* new file */
352 >                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
353 >                                progname, colval(ambval,RED),
354 >                                colval(ambval,GRN), colval(ambval,BLU),
355 >                                ambounce, ambacc);
356 >                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
357 >                                ambdiv, ambssamp, ambres,
358 >                                octname==NULL ? "" : octname);
359 >                fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
360 >                fputformat(AMBFMT, ambfp);
361 >                putc('\n', ambfp);
362 >                putambmagic(ambfp);
363 >        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
364 >                error(USER, "bad ambient file");
365   }
366  
367  
368   static
369 < avsave(av)                              /* save an ambient value */
370 < AMBVAL  *av;
369 > avsave(av)                              /* insert and save an ambient value */
370 > AMBVAL  *av;
371   {
372 < #ifdef  AMBFLUSH
437 <        static int  nunflshed = 0;
438 < #endif
372 >        avinsert(avstore(av));
373          if (ambfp == NULL)
374                  return;
375 <        if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1)
375 >        if (writambval(av, ambfp) < 0)
376                  goto writerr;
377 < #ifdef  AMBFLUSH
378 <        if (++nunflshed >= AMBFLUSH) {
445 <                if (fflush(ambfp) == EOF)
377 >        if (++nunflshed >= AMBFLUSH)
378 >                if (ambsync() == EOF)
379                          goto writerr;
447                nunflshed = 0;
448        }
449 #endif
380          return;
381   writerr:
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;
459 < register AMBTREE  *at;
460 < FVECT  c0;
461 < 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;
465        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 484 | Line 426 | double  s;
426          }
427          av->next = at->alist;
428          at->alist = av;
487        return;
488 memerr:
489        error(SYSTEM, "out of memory in avinsert");
429   }
430 +
431 +
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 +
451 + #ifdef  F_SETLKW
452 +
453 + static
454 + aflock(typ)                     /* lock/unlock ambient file */
455 + int  typ;
456 + {
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 +
464 +
465 + int
466 + ambsync()                       /* synchronize ambient file */
467 + {
468 +        static FILE  *ambinp = NULL;
469 +        static long  lastpos = -1;
470 +        long  flen;
471 +        AMBVAL  avs;
472 +        register int  n;
473 +
474 +        if (nunflshed == 0)
475 +                return(0);
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 +                goto seekerr;
483 +        if (n = flen - lastpos) {               /* file has grown */
484 +                if (ambinp == NULL) {           /* use duplicate filedes */
485 +                        ambinp = fdopen(dup(fileno(ambfp)), "r");
486 +                        if (ambinp == NULL)
487 +                                error(SYSTEM, "fdopen failed in ambsync");
488 +                }
489 +                if (fseek(ambinp, lastpos, 0) < 0)
490 +                        goto seekerr;
491 +                while (n >= AMBVALSIZ) {        /* load contributed values */
492 +                        readambval(&avs, ambinp);
493 +                        avinsert(avstore(&avs));
494 +                        n -= AMBVALSIZ;
495 +                }
496 +                /*** seek always as safety measure
497 +                if (n) ***/                     /* alignment */
498 +                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
499 +                                goto seekerr;
500 +        }
501 + #ifdef  DEBUG
502 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
503 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
504 +                                ambfp->_ptr - ambfp->_base,
505 +                                nunflshed*AMBVALSIZ);
506 +                error(CONSISTENCY, errmsg);
507 +        }
508 + #endif
509 + syncend:
510 +        n = fflush(ambfp);                      /* calls write() at last */
511 +        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
512 +                goto seekerr;
513 +        aflock(F_UNLCK);                        /* release file */
514 +        nunflshed = 0;
515 +        return(n);
516 + seekerr:
517 +        error(SYSTEM, "seek failed in ambsync");
518 + }
519 +
520 + #else
521 +
522 + int
523 + ambsync()                       /* flush ambient file */
524 + {
525 +        if (nunflshed == 0)
526 +                return(0);
527 +        nunflshed = 0;
528 +        return(fflush(ambfp));
529 + }
530 +
531 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines