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.43 by gregl, Sat Aug 30 10:17:01 1997 UTC vs.
Revision 2.106 by greg, Thu Nov 8 00:54:07 2018 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1996 Regents of the University of California */
2 <
3 < #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
5 < #endif
6 <
1 > static const char       RCSid[] = "$Id$";
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4 + *
5 + *  Declarations of external symbols in ambient.h
6   */
7  
8 < #include  "ray.h"
8 > #include "copyright.h"
9  
10 < #include  "octree.h"
10 > #include <string.h>
11  
12 + #include  "platform.h"
13 + #include  "ray.h"
14   #include  "otypes.h"
15 <
15 > #include  "otspecial.h"
16 > #include  "resolu.h"
17   #include  "ambient.h"
18
18   #include  "random.h"
19 + #include  "pmapamb.h"
20  
21   #ifndef  OCTSCALE
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23   #endif
24  
25 typedef struct ambtree {
26        AMBVAL  *alist;         /* ambient value list */
27        struct ambtree  *kid;   /* 8 child nodes */
28 }  AMBTREE;                     /* ambient octree */
29
30 extern CUBE  thescene;          /* contains space boundaries */
31
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27 < #define  MAXASET        511     /* maximum number of elements in ambient set */
27 > #ifndef  MAXASET
28 > #define  MAXASET        4095    /* maximum number of elements in ambient set */
29 > #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
32   double  maxarad;                /* maximum ambient radius */
# Line 43 | Line 38 | static FILE  *ambfp = NULL;    /* ambient file pointer */
38   static int  nunflshed = 0;      /* number of unflushed ambient values */
39  
40   #ifndef SORT_THRESH
41 < #ifdef BIGMEM
42 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
41 > #ifdef SMLMEM
42 > #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
43   #else
44 < #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
44 > #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
45   #endif
46   #endif
47   #ifndef SORT_INTVL
# Line 56 | Line 51 | static int  nunflshed = 0;     /* number of unflushed ambi
51   #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52   #endif
53  
54 +
55   static double  avsum = 0.;              /* computed ambient value sum (log) */
56   static unsigned int  navsum = 0;        /* number of values in avsum */
57   static unsigned int  nambvals = 0;      /* total number of indirect values */
# Line 63 | Line 59 | static unsigned int  nambshare = 0;    /* number of value
59   static unsigned long  ambclock = 0;     /* ambient access clock */
60   static unsigned long  lastsort = 0;     /* time of last value sort */
61   static long  sortintvl = SORT_INTVL;    /* time until next sort */
62 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
63 + static long  lastpos = -1;              /* last flush position */
64  
65   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
66          /*
# Line 78 | Line 76 | static long  sortintvl = SORT_INTVL;   /* time until nex
76  
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
79 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
80  
81 < extern long  ftell(), lseek();
82 < static int  initambfile(), avsave(), avinsert(), sortambvals(), avlmemi();
83 < static AMBVAL  *avstore();
81 > static void initambfile(int creat);
82 > static void avsave(AMBVAL *av);
83 > static AMBVAL *avstore(AMBVAL  *aval);
84 > static AMBTREE *newambtree(void);
85 > static void freeambtree(AMBTREE  *atp);
86 >
87 > typedef void unloadtf_t(AMBVAL *);
88 > static unloadtf_t avinsert;
89 > static unloadtf_t av2list;
90 > static unloadtf_t avfree;
91 > static void unloadatree(AMBTREE  *at, unloadtf_t *f);
92 >
93 > static int aposcmp(const void *avp1, const void *avp2);
94 > static int avlmemi(AMBVAL *avaddr);
95 > static void sortambvals(int always);
96 >
97   #ifdef  F_SETLKW
98 < static  aflock();
98 > static void aflock(int  typ);
99   #endif
100  
101  
102 < setambres(ar)                           /* set ambient resolution */
103 < int  ar;
102 > void
103 > setambres(                              /* set ambient resolution */
104 >        int  ar
105 > )
106   {
107          ambres = ar < 0 ? 0 : ar;               /* may be done already */
108                                                  /* set min & max radii */
109          if (ar <= 0) {
110                  minarad = 0;
111 <                maxarad = thescene.cusize / 2.0;
111 >                maxarad = thescene.cusize*0.2;
112          } else {
113                  minarad = thescene.cusize / ar;
114 <                maxarad = 64 * minarad;                 /* heuristic */
115 <                if (maxarad > thescene.cusize / 2.0)
116 <                        maxarad = thescene.cusize / 2.0;
114 >                maxarad = 64.0 * minarad;               /* heuristic */
115 >                if (maxarad > thescene.cusize*0.2)
116 >                        maxarad = thescene.cusize*0.2;
117          }
118          if (minarad <= FTINY)
119 <                minarad = 10*FTINY;
119 >                minarad = 10.0*FTINY;
120          if (maxarad <= minarad)
121 <                maxarad = 64 * minarad;
121 >                maxarad = 64.0 * minarad;
122   }
123  
124  
125 < setambacc(newa)                         /* set ambient accuracy */
126 < double  newa;
125 > void
126 > setambacc(                              /* set ambient accuracy */
127 >        double  newa
128 > )
129   {
130 <        double  ambdiff;
131 <
132 <        if (newa < 0.0)
133 <                newa = 0.0;
134 <        ambdiff = fabs(newa - ambacc);
135 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
136 <                sortambvals(1);                 /* rebuild tree */
130 >        static double   olda;           /* remember previous setting here */
131 >        
132 >        newa *= (newa > 0);
133 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
134 >                ambacc = newa;
135 >                if (nambvals > 0)
136 >                        sortambvals(1);         /* rebuild tree */
137 >        }
138   }
139  
140  
141 < setambient(afile)                       /* initialize calculation */
142 < char  *afile;
141 > void
142 > setambient(void)                                /* initialize calculation */
143   {
144 <        long  pos, flen;
144 >        int     readonly = 0;
145 >        long    flen;
146          AMBVAL  amb;
147 +                                                /* make sure we're fresh */
148 +        ambdone();
149                                                  /* init ambient limits */
150          setambres(ambres);
151          setambacc(ambacc);
152 <        if (afile == NULL)
152 >        if (ambfile == NULL || !ambfile[0])
153                  return;
154          if (ambacc <= FTINY) {
155                  sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
156 <                                afile);
156 >                                ambfile);
157                  error(WARNING, errmsg);
158                  return;
159          }
160                                                  /* open ambient file */
161 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
162 <                initambfile(0);
163 <                pos = ftell(ambfp);
161 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
162 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
163 >        if (ambfp != NULL) {
164 >                initambfile(0);                 /* file exists */
165 >                lastpos = ftell(ambfp);
166                  while (readambval(&amb, ambfp))
167 <                        avinsert(avstore(&amb));
168 <                                                /* align */
169 <                pos += (long)nambvals*AMBVALSIZ;
149 <                flen = lseek(fileno(ambfp), 0L, 2);
150 <                if (flen != pos) {
167 >                        avstore(&amb);
168 >                nambshare = nambvals;           /* share loaded values */
169 >                if (readonly) {
170                          sprintf(errmsg,
171 +                                "loaded %u values from read-only ambient file",
172 +                                        nambvals);
173 +                        error(WARNING, errmsg);
174 +                        fclose(ambfp);          /* close file so no writes */
175 +                        ambfp = NULL;
176 +                        return;                 /* avoid ambsync() */
177 +                }
178 +                                                /* align file pointer */
179 +                lastpos += (long)nambvals*AMBVALSIZ;
180 +                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
181 +                if (flen != lastpos) {
182 +                        sprintf(errmsg,
183                          "ignoring last %ld values in ambient file (corrupted)",
184 <                                        (flen - pos)/AMBVALSIZ);
184 >                                        (flen - lastpos)/AMBVALSIZ);
185                          error(WARNING, errmsg);
186 <                        fseek(ambfp, pos, 0);
187 <                        ftruncate(fileno(ambfp), pos);
186 >                        fseek(ambfp, lastpos, SEEK_SET);
187 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
188                  }
189 <                nambshare = nambvals;
190 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
191 <                initambfile(1);
192 <        else {
193 <                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
189 >        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190 >                initambfile(1);                 /* else create new file */
191 >                fflush(ambfp);
192 >                lastpos = ftell(ambfp);
193 >        } else {
194 >                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
195                  error(SYSTEM, errmsg);
196          }
197 <        nunflshed++;    /* lie */
198 <        ambsync();
197 > #ifdef  F_SETLKW
198 >        aflock(F_UNLCK);                        /* release file */
199 > #endif
200   }
201  
202  
203 < ambnotify(obj)                  /* record new modifier */
204 < OBJECT  obj;
203 > void
204 > ambdone(void)                   /* close ambient file and free memory */
205   {
206 +        if (ambfp != NULL) {            /* close ambient file */
207 +                ambsync();
208 +                fclose(ambfp);
209 +                ambfp = NULL;
210 +                if (ambinp != NULL) {  
211 +                        fclose(ambinp);
212 +                        ambinp = NULL;
213 +                }
214 +                lastpos = -1;
215 +        }
216 +                                        /* free ambient tree */
217 +        unloadatree(&atrunk, avfree);
218 +                                        /* reset state variables */
219 +        avsum = 0.;
220 +        navsum = 0;
221 +        nambvals = 0;
222 +        nambshare = 0;
223 +        ambclock = 0;
224 +        lastsort = 0;
225 +        sortintvl = SORT_INTVL;
226 + }
227 +
228 +
229 + void
230 + ambnotify(                      /* record new modifier */
231 +        OBJECT  obj
232 + )
233 + {
234          static int  hitlimit = 0;
235 <        register OBJREC  *o = objptr(obj);
236 <        register char  **amblp;
235 >        OBJREC   *o;
236 >        char  **amblp;
237  
238 +        if (obj == OVOID) {             /* starting over */
239 +                ambset[0] = 0;
240 +                hitlimit = 0;
241 +                return;
242 +        }
243 +        o = objptr(obj);
244          if (hitlimit || !ismodifier(o->otype))
245                  return;
246          for (amblp = amblist; *amblp != NULL; amblp++)
# Line 188 | Line 255 | OBJECT obj;
255                  }
256   }
257  
258 + /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
259  
260 < ambient(acol, r, nrm)           /* compute ambient component for ray */
261 < COLOR  acol;
262 < register RAY  *r;
263 < FVECT  nrm;
260 > #ifndef OLDAMB
261 >
262 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263 >
264 > static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266 >                                AMBTREE *at, FVECT c0, double s);
267 > static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269 >                                FVECT uvw[3]);
270 >
271 > void
272 > multambient(            /* compute ambient component & multiply by coef. */
273 >        COLOR  aval,
274 >        RAY  *r,
275 >        FVECT  nrm
276 > )
277   {
278          static int  rdepth = 0;                 /* ambient recursion */
279 +        COLOR   acol, caustic;
280 +        int     i, ok;
281          double  d, l;
282  
283 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
284 +         * ambient. Return as all ambient components accounted for, else
285 +         * continue. */
286 +        if (ambPmap(aval, r, rdepth))
287 +                return;
288 +
289 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
290 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
291 +         * with RADIANCE ambient calculation */
292 +        copycolor(caustic, aval);
293 +        ambPmapCaustic(caustic, r, rdepth);
294 +        
295          if (ambdiv <= 0)                        /* no ambient calculation */
296                  goto dumbamb;
297                                                  /* check number of bounces */
# Line 208 | Line 303 | FVECT  nrm;
303                  goto dumbamb;
304  
305          if (ambacc <= FTINY) {                  /* no ambient storage */
306 +                FVECT   uvd[2];
307 +                float   dgrad[2], *dgp = NULL;
308 +
309 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
310 +                        dgp = dgrad;            /* compute rotational grad. */
311 +                copycolor(acol, aval);
312                  rdepth++;
313 +                ok = doambient(acol, r, r->rweight,
314 +                                uvd, NULL, NULL, dgp, NULL);
315 +                rdepth--;
316 +                if (!ok)
317 +                        goto dumbamb;
318 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
319 +                        FVECT   v1;
320 +                        VCROSS(v1, r->ron, nrm);
321 +                        d = 1.0;
322 +                        for (i = 3; i--; )
323 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
324 +                        if (d >= 0.05)
325 +                                scalecolor(acol, d);
326 +                }
327 +                copycolor(aval, acol);
328 +
329 +                /* PMAP: add in caustic */
330 +                addcolor(aval, caustic);
331 +                return;
332 +        }
333 +
334 +        if (tracktime)                          /* sort to minimize thrashing */
335 +                sortambvals(0);
336 +                                                /* interpolate ambient value */
337 +        setcolor(acol, 0.0, 0.0, 0.0);
338 +        d = sumambient(acol, r, nrm, rdepth,
339 +                        &atrunk, thescene.cuorg, thescene.cusize);
340 +                        
341 +        if (d > FTINY) {
342 +                d = 1.0/d;
343 +                scalecolor(acol, d);
344 +                multcolor(aval, acol);
345 +
346 +                /* PMAP: add in caustic */
347 +                addcolor(aval, caustic);
348 +                return;
349 +        }
350 +        
351 +        rdepth++;                               /* need to cache new value */
352 +        ok = makeambient(acol, r, nrm, rdepth-1);
353 +        rdepth--;
354 +        
355 +        if (ok) {
356 +                multcolor(aval, acol);          /* computed new value */
357 +
358 +                /* PMAP: add in caustic */
359 +                addcolor(aval, caustic);
360 +                return;
361 +        }
362 +        
363 + dumbamb:                                        /* return global value */
364 +        if ((ambvwt <= 0) | (navsum == 0)) {
365 +                multcolor(aval, ambval);
366 +                
367 +                /* PMAP: add in caustic */
368 +                addcolor(aval, caustic);
369 +                return;
370 +        }
371 +        
372 +        l = bright(ambval);                     /* average in computations */  
373 +        if (l > FTINY) {
374 +                d = (log(l)*(double)ambvwt + avsum) /
375 +                                (double)(ambvwt + navsum);
376 +                d = exp(d) / l;
377 +                scalecolor(aval, d);
378 +                multcolor(aval, ambval);        /* apply color of ambval */
379 +        } else {
380 +                d = exp( avsum / (double)navsum );
381 +                scalecolor(aval, d);            /* neutral color */
382 +        }
383 + }
384 +
385 +
386 + /* Plug a potential leak where ambient cache value is occluded */
387 + static int
388 + plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
389 + {
390 +        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
391 +        RAY             rtst;
392 +        FVECT           vdif;
393 +        double          normdot, ndotd, nadotd;
394 +        double          a, b, c, t[2];
395 +
396 +        ang += 2.*PI*(ang < 0);                 /* check direction flags */
397 +        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
398 +                return(0);
399 +        /*
400 +         * Generate test ray, targeting 20 degrees above sample point plane
401 +         * along surface normal from cache position.  This should be high
402 +         * enough to miss local geometry we don't really care about.
403 +         */
404 +        VSUB(vdif, ap->pos, r->rop);
405 +        normdot = DOT(anorm, r->ron);
406 +        ndotd = DOT(vdif, r->ron);
407 +        nadotd = DOT(vdif, anorm);
408 +        a = normdot*normdot - cost70sq;
409 +        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
410 +        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
411 +        if (quadratic(t, a, b, c) != 2)
412 +                return(1);                      /* should rarely happen */
413 +        if (t[1] <= FTINY)
414 +                return(0);                      /* should fail behind test */
415 +        rayorigin(&rtst, SHADOW, r, NULL);
416 +        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
417 +        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
418 +        while (localhit(&rtst, &thescene)) {    /* check for occluder */
419 +                OBJREC  *m = findmaterial(rtst.ro);
420 +                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
421 +                                (rtst.clipset == NULL ||
422 +                                        !inset(rtst.clipset, rtst.ro->omod)))
423 +                        return(1);              /* plug light leak */
424 +                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
425 +                rtst.rmax -= rtst.rot;
426 +                rayclear(&rtst);
427 +        }
428 +        return(0);                              /* seems we're OK */
429 + }
430 +
431 +
432 + static double
433 + sumambient(             /* get interpolated ambient value */
434 +        COLOR  acol,
435 +        RAY  *r,
436 +        FVECT  rn,
437 +        int  al,
438 +        AMBTREE  *at,
439 +        FVECT  c0,
440 +        double  s
441 + )
442 + {                       /* initial limit is 10 degrees plus ambacc radians */
443 +        const double    minangle = 10.0 * PI/180.;
444 +        double          maxangle = minangle + ambacc;
445 +        double          wsum = 0.0;
446 +        FVECT           ck0;
447 +        int             i, j;
448 +        AMBVAL          *av;
449 +
450 +        if (at->kid != NULL) {          /* sum children first */                                
451 +                s *= 0.5;
452 +                for (i = 0; i < 8; i++) {
453 +                        for (j = 0; j < 3; j++) {
454 +                                ck0[j] = c0[j];
455 +                                if (1<<j & i)
456 +                                        ck0[j] += s;
457 +                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
458 +                                        break;
459 +                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
460 +                                        break;
461 +                        }
462 +                        if (j == 3)
463 +                                wsum += sumambient(acol, r, rn, al,
464 +                                                        at->kid+i, ck0, s);
465 +                }
466 +                                        /* good enough? */
467 +                if (wsum >= 0.05 && s > minarad*10.0)
468 +                        return(wsum);
469 +        }
470 +                                        /* adjust maximum angle */
471 +        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
472 +                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
473 +                                        /* sum this node */
474 +        for (av = at->alist; av != NULL; av = av->next) {
475 +                double  u, v, d, delta_r2, delta_t2;
476 +                COLOR   ct;
477 +                FVECT   uvw[3];
478 +                                        /* record access */
479 +                if (tracktime)
480 +                        av->latick = ambclock;
481 +                /*
482 +                 *  Ambient level test
483 +                 */
484 +                if (av->lvl > al ||     /* list sorted, so this works */
485 +                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
486 +                        break;
487 +                /*
488 +                 *  Direction test using unperturbed normal
489 +                 */
490 +                decodedir(uvw[2], av->ndir);
491 +                d = DOT(uvw[2], r->ron);
492 +                if (d <= 0.0)           /* >= 90 degrees */
493 +                        continue;
494 +                delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
495 +                if (delta_r2 >= maxangle*maxangle)
496 +                        continue;
497 +                /*
498 +                 *  Modified ray behind test
499 +                 */
500 +                VSUB(ck0, r->rop, av->pos);
501 +                d = DOT(ck0, uvw[2]);
502 +                if (d < -minarad*ambacc-.001)
503 +                        continue;
504 +                d /= av->rad[0];
505 +                delta_t2 = d*d;
506 +                if (delta_t2 >= ambacc*ambacc)
507 +                        continue;
508 +                /*
509 +                 *  Elliptical radii test based on Hessian
510 +                 */
511 +                decodedir(uvw[0], av->udir);
512 +                VCROSS(uvw[1], uvw[2], uvw[0]);
513 +                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
514 +                delta_t2 += d*d;
515 +                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
516 +                delta_t2 += d*d;
517 +                if (delta_t2 >= ambacc*ambacc)
518 +                        continue;
519 +                /*
520 +                 *  Test for potential light leak
521 +                 */
522 +                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
523 +                        continue;
524 +                /*
525 +                 *  Extrapolate value and compute final weight (hat function)
526 +                 */
527 +                if (!extambient(ct, av, r->rop, rn, uvw))
528 +                        continue;
529 +                d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
530 +                        tfunc(ambacc, sqrt(delta_t2), 0.0);
531 +                scalecolor(ct, d);
532 +                addcolor(acol, ct);
533 +                wsum += d;
534 +        }
535 +        return(wsum);
536 + }
537 +
538 +
539 + static int
540 + makeambient(            /* make a new ambient value for storage */
541 +        COLOR  acol,
542 +        RAY  *r,
543 +        FVECT  rn,
544 +        int  al
545 + )
546 + {
547 +        AMBVAL  amb;
548 +        FVECT   uvw[3];
549 +        int     i;
550 +
551 +        amb.weight = 1.0;                       /* compute weight */
552 +        for (i = al; i-- > 0; )
553 +                amb.weight *= AVGREFL;
554 +        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
555 +                amb.weight = 1.25*r->rweight;
556 +        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
557 +                                                /* compute ambient */
558 +        i = doambient(acol, r, amb.weight,
559 +                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
560 +        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
561 +        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
562 +                return(i);
563 +                                                /* store value */
564 +        VCOPY(amb.pos, r->rop);
565 +        amb.ndir = encodedir(r->ron);
566 +        amb.udir = encodedir(uvw[0]);
567 +        amb.lvl = al;
568 +        copycolor(amb.val, acol);
569 +                                                /* insert into tree */
570 +        avsave(&amb);                           /* and save to file */
571 +        if (rn != r->ron) {                     /* texture */
572 +                VCOPY(uvw[2], r->ron);
573 +                extambient(acol, &amb, r->rop, rn, uvw);
574 +        }
575 +        return(1);
576 + }
577 +
578 +
579 + static int
580 + extambient(             /* extrapolate value at pv, nv */
581 +        COLOR  cr,
582 +        AMBVAL   *ap,
583 +        FVECT  pv,
584 +        FVECT  nv,
585 +        FVECT  uvw[3]
586 + )
587 + {
588 +        const double    min_d = 0.05;
589 +        static FVECT    my_uvw[3];
590 +        FVECT           v1;
591 +        int             i;
592 +        double          d = 1.0;        /* zeroeth order */
593 +
594 +        if (uvw == NULL) {              /* need local coordinates? */
595 +                decodedir(my_uvw[2], ap->ndir);
596 +                decodedir(my_uvw[0], ap->udir);
597 +                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
598 +                uvw = my_uvw;
599 +        }
600 +        for (i = 3; i--; )              /* gradient due to translation */
601 +                d += (pv[i] - ap->pos[i]) *
602 +                        (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
603 +
604 +        VCROSS(v1, uvw[2], nv);         /* gradient due to rotation */
605 +        for (i = 3; i--; )
606 +                d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
607 +        
608 +        if (d < min_d)                  /* should not use if we can avoid it */
609 +                d = min_d;
610 +        copycolor(cr, ap->val);
611 +        scalecolor(cr, d);
612 +        return(d > min_d);
613 + }
614 +
615 +
616 + static void
617 + avinsert(                               /* insert ambient value in our tree */
618 +        AMBVAL *av
619 + )
620 + {
621 +        AMBTREE  *at;
622 +        AMBVAL  *ap;
623 +        AMBVAL  avh;
624 +        FVECT  ck0;
625 +        double  s;
626 +        int  branch;
627 +        int  i;
628 +
629 +        if (av->rad[0] <= FTINY)
630 +                error(CONSISTENCY, "zero ambient radius in avinsert");
631 +        at = &atrunk;
632 +        VCOPY(ck0, thescene.cuorg);
633 +        s = thescene.cusize;
634 +        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
635 +                if (at->kid == NULL)
636 +                        if ((at->kid = newambtree()) == NULL)
637 +                                error(SYSTEM, "out of memory in avinsert");
638 +                s *= 0.5;
639 +                branch = 0;
640 +                for (i = 0; i < 3; i++)
641 +                        if (av->pos[i] > ck0[i] + s) {
642 +                                ck0[i] += s;
643 +                                branch |= 1 << i;
644 +                        }
645 +                at = at->kid + branch;
646 +        }
647 +        avh.next = at->alist;           /* order by increasing level */
648 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
649 +                if ( ap->next->lvl > av->lvl ||
650 +                                (ap->next->lvl == av->lvl) &
651 +                                (ap->next->weight <= av->weight) )
652 +                        break;
653 +        av->next = ap->next;
654 +        ap->next = (AMBVAL*)av;
655 +        at->alist = avh.next;
656 + }
657 +
658 +
659 + #else /* ! NEWAMB */
660 +
661 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
662 +                                AMBTREE *at, FVECT c0, double s);
663 + static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
664 + static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
665 +
666 +
667 + void
668 + multambient(            /* compute ambient component & multiply by coef. */
669 +        COLOR  aval,
670 +        RAY  *r,
671 +        FVECT  nrm
672 + )
673 + {
674 +        static int  rdepth = 0;                 /* ambient recursion */
675 +        COLOR   acol, caustic;
676 +        double  d, l;
677 +
678 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
679 +         * as all ambient components accounted for */
680 +        if (ambPmap(aval, r, rdepth))
681 +                return;
682 +
683 +        /* PMAP: Otherwise factor in ambient from caustic photon map
684 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
685 +         * continue with RADIANCE ambient calculation */
686 +        copycolor(caustic, aval);
687 +        ambPmapCaustic(caustic, r, rdepth);
688 +        
689 +        if (ambdiv <= 0)                        /* no ambient calculation */
690 +                goto dumbamb;
691 +                                                /* check number of bounces */
692 +        if (rdepth >= ambounce)
693 +                goto dumbamb;
694 +                                                /* check ambient list */
695 +        if (ambincl != -1 && r->ro != NULL &&
696 +                        ambincl != inset(ambset, r->ro->omod))
697 +                goto dumbamb;
698 +
699 +        if (ambacc <= FTINY) {                  /* no ambient storage */
700 +                copycolor(acol, aval);
701 +                rdepth++;
702                  d = doambient(acol, r, r->rweight, NULL, NULL);
703                  rdepth--;
704                  if (d <= FTINY)
705                          goto dumbamb;
706 +                copycolor(aval, acol);          
707 +        
708 +           /* PMAP: add in caustic */
709 +                addcolor(aval, caustic);        
710                  return;
711          }
712  
713          if (tracktime)                          /* sort to minimize thrashing */
714                  sortambvals(0);
715 <                                                /* get ambient value */
715 >                                                /* interpolate ambient value */
716          setcolor(acol, 0.0, 0.0, 0.0);
717          d = sumambient(acol, r, nrm, rdepth,
718                          &atrunk, thescene.cuorg, thescene.cusize);
719 +                        
720          if (d > FTINY) {
721 <                scalecolor(acol, 1.0/d);
721 >                d = 1.0/d;
722 >                scalecolor(acol, d);
723 >                multcolor(aval, acol);
724 >                
725 >                /* PMAP: add in caustic */
726 >                addcolor(aval, caustic);        
727                  return;
728          }
729 +        
730          rdepth++;                               /* need to cache new value */
731          d = makeambient(acol, r, nrm, rdepth-1);
732          rdepth--;
733 <        if (d > FTINY)
733 >        
734 >        if (d > FTINY) {
735 >                multcolor(aval, acol);          /* got new value */
736 >
737 >                /* PMAP: add in caustic */
738 >                addcolor(aval, caustic);                        
739                  return;
740 +        }
741 +        
742   dumbamb:                                        /* return global value */
743 <        copycolor(acol, ambval);
744 <        if (ambvwt <= 0 | navsum == 0)
743 >        if ((ambvwt <= 0) | (navsum == 0)) {
744 >                multcolor(aval, ambval);
745 >
746 >                /* PMAP: add in caustic */
747 >                addcolor(aval, caustic);        
748                  return;
749 +        }
750 +        
751          l = bright(ambval);                     /* average in computations */
752          if (l > FTINY) {
753                  d = (log(l)*(double)ambvwt + avsum) /
754                                  (double)(ambvwt + navsum);
755                  d = exp(d) / l;
756 <                scalecolor(acol, d);            /* apply color of ambval */
756 >                scalecolor(aval, d);
757 >                multcolor(aval, ambval);        /* apply color of ambval */
758          } else {
759                  d = exp( avsum / (double)navsum );
760 <                setcolor(acol, d, d, d);        /* neutral color */
760 >                scalecolor(aval, d);            /* neutral color */
761          }
762   }
763  
764  
765 < double
766 < sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
767 < COLOR  acol;
768 < register RAY  *r;
769 < FVECT  rn;
770 < int  al;
771 < AMBTREE  *at;
772 < FVECT  c0;
773 < double  s;
765 > static double
766 > sumambient(     /* get interpolated ambient value */
767 >        COLOR  acol,
768 >        RAY  *r,
769 >        FVECT  rn,
770 >        int  al,
771 >        AMBTREE  *at,
772 >        FVECT  c0,
773 >        double  s
774 > )
775   {
776          double  d, e1, e2, wt, wsum;
777          COLOR  ct;
778          FVECT  ck0;
779          int  i;
780 <        register int  j;
781 <        register AMBVAL  *av;
780 >        int  j;
781 >        AMBVAL   *av;
782  
783          wsum = 0.0;
784                                          /* do this node */
785          for (av = at->alist; av != NULL; av = av->next) {
786 +                double  rn_dot = -2.0;
787                  if (tracktime)
788                          av->latick = ambclock;
789                  /*
790                   *  Ambient level test.
791                   */
792 <                if (av->lvl > al)       /* list sorted, so this works */
792 >                if (av->lvl > al ||     /* list sorted, so this works */
793 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
794                          break;
278                if (av->weight < r->rweight-FTINY)
279                        continue;
795                  /*
796                   *  Ambient radius test.
797                   */
798 <                d = av->pos[0] - r->rop[0];
799 <                e1 = d * d;
285 <                d = av->pos[1] - r->rop[1];
286 <                e1 += d * d;
287 <                d = av->pos[2] - r->rop[2];
288 <                e1 += d * d;
289 <                e1 /= av->rad * av->rad;
798 >                VSUB(ck0, av->pos, r->rop);
799 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
800                  if (e1 > ambacc*ambacc*1.21)
801                          continue;
802                  /*
803 <                 *  Normal direction test.
803 >                 *  Direction test using closest normal.
804                   */
805 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
806 <                if (e2 < 0.0) e2 = 0.0;
807 <                if (e1 + e2 > ambacc*ambacc*1.21)
805 >                d = DOT(av->dir, r->ron);
806 >                if (rn != r->ron) {
807 >                        rn_dot = DOT(av->dir, rn);
808 >                        if (rn_dot > 1.0-FTINY)
809 >                                rn_dot = 1.0-FTINY;
810 >                        if (rn_dot >= d-FTINY) {
811 >                                d = rn_dot;
812 >                                rn_dot = -2.0;
813 >                        }
814 >                }
815 >                e2 = (1.0 - d) * r->rweight;
816 >                if (e2 < 0.0)
817 >                        e2 = 0.0;
818 >                else if (e1 + e2 > ambacc*ambacc*1.21)
819                          continue;
820                  /*
821                   *  Ray behind test.
# Line 308 | Line 829 | double s;
829                  /*
830                   *  Jittering final test reduces image artifacts.
831                   */
832 <                wt = sqrt(e1) + sqrt(e2);
832 >                e1 = sqrt(e1);
833 >                e2 = sqrt(e2);
834 >                wt = e1 + e2;
835                  if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
836                          continue;
837 +                /*
838 +                 *  Recompute directional error using perturbed normal
839 +                 */
840 +                if (rn_dot > 0.0) {
841 +                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
842 +                        wt = e1 + e2;
843 +                }
844                  if (wt <= 1e-3)
845                          wt = 1e3;
846                  else
# Line 335 | Line 865 | double s;
865                                  break;
866                  }
867                  if (j == 3)
868 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
868 >                        wsum += sumambient(acol, r, rn, al,
869 >                                                at->kid+i, ck0, s);
870          }
871          return(wsum);
872   }
873  
874  
875 < double
876 < makeambient(acol, r, rn, al)    /* make a new ambient value */
877 < COLOR  acol;
878 < register RAY  *r;
879 < FVECT  rn;
880 < int  al;
875 > static double
876 > makeambient(            /* make a new ambient value for storage */
877 >        COLOR  acol,
878 >        RAY  *r,
879 >        FVECT  rn,
880 >        int  al
881 > )
882   {
883          AMBVAL  amb;
884          FVECT   gp, gd;
885 <                                                /* compute weight */
886 <        amb.weight = pow(AVGREFL, (double)al);
887 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
888 <                amb.weight = r->rweight;
885 >        int     i;
886 >
887 >        amb.weight = 1.0;                       /* compute weight */
888 >        for (i = al; i-- > 0; )
889 >                amb.weight *= AVGREFL;
890 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
891 >                amb.weight = 1.25*r->rweight;
892 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
893                                                  /* compute ambient */
894          amb.rad = doambient(acol, r, amb.weight, gp, gd);
895 <        if (amb.rad <= FTINY)
895 >        if (amb.rad <= FTINY) {
896 >                setcolor(acol, 0.0, 0.0, 0.0);
897                  return(0.0);
898 <                                                /* store it */
898 >        }
899 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
900 >                                                /* store value */
901          VCOPY(amb.pos, r->rop);
902          VCOPY(amb.dir, r->ron);
903          amb.lvl = al;
# Line 373 | Line 912 | int  al;
912   }
913  
914  
915 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
916 < COLOR  cr;
917 < register AMBVAL  *ap;
918 < FVECT  pv, nv;
915 > static void
916 > extambient(             /* extrapolate value at pv, nv */
917 >        COLOR  cr,
918 >        AMBVAL   *ap,
919 >        FVECT  pv,
920 >        FVECT  nv
921 > )
922   {
923 <        FVECT  v1, v2;
924 <        register int  i;
923 >        FVECT  v1;
924 >        int  i;
925          double  d;
926  
927          d = 1.0;                        /* zeroeth order */
# Line 387 | Line 929 | FVECT  pv, nv;
929          for (i = 0; i < 3; i++)
930                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
931                                          /* gradient due to rotation */
932 <        VCOPY(v1, ap->dir);
933 <        fcross(v2, v1, nv);
392 <        d += DOT(ap->gdir, v2);
932 >        VCROSS(v1, ap->dir, nv);
933 >        d += DOT(ap->gdir, v1);
934          if (d <= 0.0) {
935                  setcolor(cr, 0.0, 0.0, 0.0);
936                  return;
# Line 399 | Line 940 | FVECT  pv, nv;
940   }
941  
942  
943 < static
944 < initambfile(creat)              /* initialize ambient file */
945 < int  creat;
943 > static void
944 > avinsert(                               /* insert ambient value in our tree */
945 >        AMBVAL *av
946 > )
947   {
948 <        extern char  *progname, *octname, VersionID[];
948 >        AMBTREE  *at;
949 >        AMBVAL  *ap;
950 >        AMBVAL  avh;
951 >        FVECT  ck0;
952 >        double  s;
953 >        int  branch;
954 >        int  i;
955  
956 +        if (av->rad <= FTINY)
957 +                error(CONSISTENCY, "zero ambient radius in avinsert");
958 +        at = &atrunk;
959 +        VCOPY(ck0, thescene.cuorg);
960 +        s = thescene.cusize;
961 +        while (s*(OCTSCALE/2) > av->rad*ambacc) {
962 +                if (at->kid == NULL)
963 +                        if ((at->kid = newambtree()) == NULL)
964 +                                error(SYSTEM, "out of memory in avinsert");
965 +                s *= 0.5;
966 +                branch = 0;
967 +                for (i = 0; i < 3; i++)
968 +                        if (av->pos[i] > ck0[i] + s) {
969 +                                ck0[i] += s;
970 +                                branch |= 1 << i;
971 +                        }
972 +                at = at->kid + branch;
973 +        }
974 +        avh.next = at->alist;           /* order by increasing level */
975 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
976 +                if ( ap->next->lvl > av->lvl ||
977 +                                (ap->next->lvl == av->lvl) &
978 +                                (ap->next->weight <= av->weight) )
979 +                        break;
980 +        av->next = ap->next;
981 +        ap->next = (AMBVAL*)av;
982 +        at->alist = avh.next;
983 + }
984 +
985 + #endif  /* ! NEWAMB */
986 +
987 + /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
988 +
989 + static void
990 + initambfile(            /* initialize ambient file */
991 +        int  cre8
992 + )
993 + {
994 +        extern char  *progname, *octname;
995 +        static char  *mybuf = NULL;
996 +
997   #ifdef  F_SETLKW
998 <        aflock(creat ? F_WRLCK : F_RDLCK);
998 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
999   #endif
1000 < #ifdef MSDOS
1001 <        setmode(fileno(ambfp), O_BINARY);
1002 < #endif
1003 <        setbuf(ambfp, bmalloc(BUFSIZ+8));
1004 <        if (creat) {                    /* new file */
1000 >        SET_FILE_BINARY(ambfp);
1001 >        if (mybuf == NULL)
1002 >                mybuf = (char *)bmalloc(BUFSIZ+8);
1003 >        setbuf(ambfp, mybuf);
1004 >        if (cre8) {                     /* new file */
1005                  newheader("RADIANCE", ambfp);
1006                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
1007                                  progname, colval(ambval,RED),
1008                                  colval(ambval,GRN), colval(ambval,BLU),
1009                                  ambvwt, ambounce, ambacc);
1010 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
1011 <                                ambdiv, ambssamp, ambres,
1012 <                                octname==NULL ? "" : octname);
1010 >                fprintf(ambfp, "-ad %d -as %d -ar %d ",
1011 >                                ambdiv, ambssamp, ambres);
1012 >                if (octname != NULL)
1013 >                        fputs(octname, ambfp);
1014 >                fputc('\n', ambfp);
1015                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
1016 +                fputnow(ambfp);
1017                  fputformat(AMBFMT, ambfp);
1018 <                putc('\n', ambfp);
1018 >                fputc('\n', ambfp);
1019                  putambmagic(ambfp);
1020          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
1021                  error(USER, "bad ambient file");
1022   }
1023  
1024  
1025 < static
1026 < avsave(av)                              /* insert and save an ambient value */
1027 < AMBVAL  *av;
1025 > static void
1026 > avsave(                         /* insert and save an ambient value */
1027 >        AMBVAL  *av
1028 > )
1029   {
1030 <        avinsert(avstore(av));
1030 >        avstore(av);
1031          if (ambfp == NULL)
1032                  return;
1033          if (writambval(av, ambfp) < 0)
# Line 444 | Line 1037 | AMBVAL *av;
1037                          goto writerr;
1038          return;
1039   writerr:
1040 <        error(SYSTEM, "error writing ambient file");
1040 >        error(SYSTEM, "error writing to ambient file");
1041   }
1042  
1043  
1044   static AMBVAL *
1045 < avstore(aval)                           /* allocate memory and store aval */
1046 < register AMBVAL  *aval;
1045 > avstore(                                /* allocate memory and save aval */
1046 >        AMBVAL  *aval
1047 > )
1048   {
1049 <        register AMBVAL  *av;
1049 >        AMBVAL  *av;
1050          double  d;
1051  
1052          if ((av = newambval()) == NULL)
1053                  error(SYSTEM, "out of memory in avstore");
1054 <        copystruct(av, aval);
1054 >        *av = *aval;
1055          av->latick = ambclock;
1056          av->next = NULL;
1057          nambvals++;
# Line 466 | Line 1060 | register AMBVAL  *aval;
1060                  avsum += log(d);
1061                  navsum++;
1062          }
1063 +        avinsert(av);                   /* insert in our cache tree */
1064          return(av);
1065   }
1066  
# Line 475 | Line 1070 | register AMBVAL  *aval;
1070   static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
1071  
1072  
1073 < static
1074 < AMBTREE *
480 < newambtree()                            /* allocate 8 ambient tree structs */
1073 > static AMBTREE *
1074 > newambtree(void)                                /* allocate 8 ambient tree structs */
1075   {
1076 <        register AMBTREE  *atp, *upperlim;
1076 >        AMBTREE  *atp, *upperlim;
1077  
1078          if (atfreelist == NULL) {       /* get more nodes */
1079 <                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
1079 >                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
1080                  if (atfreelist == NULL)
1081                          return(NULL);
1082                                          /* link new free list */
# Line 493 | Line 1087 | newambtree()                           /* allocate 8 ambient tree structs */
1087          }
1088          atp = atfreelist;
1089          atfreelist = atp->kid;
1090 <        bzero((char *)atp, 8*sizeof(AMBTREE));
1090 >        memset(atp, 0, 8*sizeof(AMBTREE));
1091          return(atp);
1092   }
1093  
1094  
1095 < static
1096 < freeambtree(atp)                        /* free 8 ambient tree structs */
1097 < AMBTREE  *atp;
1095 > static void
1096 > freeambtree(                    /* free 8 ambient tree structs */
1097 >        AMBTREE  *atp
1098 > )
1099   {
1100          atp->kid = atfreelist;
1101          atfreelist = atp;
1102   }
1103  
1104  
1105 < static
1106 < avinsert(av)                            /* insert ambient value in our tree */
1107 < register AMBVAL  *av;
1105 > static void
1106 > unloadatree(                    /* unload an ambient value tree */
1107 >        AMBTREE  *at,
1108 >        unloadtf_t *f
1109 > )
1110   {
1111 <        register AMBTREE  *at;
1112 <        register AMBVAL  *ap;
516 <        AMBVAL  avh;
517 <        FVECT  ck0;
518 <        double  s;
519 <        int  branch;
520 <        register int  i;
521 <
522 <        if (av->rad <= FTINY)
523 <                error(CONSISTENCY, "zero ambient radius in avinsert");
524 <        at = &atrunk;
525 <        VCOPY(ck0, thescene.cuorg);
526 <        s = thescene.cusize;
527 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
528 <                if (at->kid == NULL)
529 <                        if ((at->kid = newambtree()) == NULL)
530 <                                error(SYSTEM, "out of memory in avinsert");
531 <                s *= 0.5;
532 <                branch = 0;
533 <                for (i = 0; i < 3; i++)
534 <                        if (av->pos[i] > ck0[i] + s) {
535 <                                ck0[i] += s;
536 <                                branch |= 1 << i;
537 <                        }
538 <                at = at->kid + branch;
539 <        }
540 <        avh.next = at->alist;           /* order by increasing level */
541 <        for (ap = &avh; ap->next != NULL; ap = ap->next)
542 <                if (ap->next->lvl >= av->lvl)
543 <                        break;
544 <        av->next = ap->next;
545 <        ap->next = av;
546 <        at->alist = avh.next;
547 < }
548 <
549 <
550 < static
551 < unloadatree(at, f)                      /* unload an ambient value tree */
552 < register AMBTREE  *at;
553 < int     (*f)();
554 < {
555 <        register AMBVAL  *av;
556 <        register int  i;
1111 >        AMBVAL  *av;
1112 >        int  i;
1113                                          /* transfer values at this node */
1114          for (av = at->alist; av != NULL; av = at->alist) {
1115                  at->alist = av->next;
1116 +                av->next = NULL;
1117                  (*f)(av);
1118          }
1119          if (at->kid == NULL)
# Line 575 | Line 1132 | static struct avl {
1132   static AMBVAL   **avlist2;              /* memory positions for sorting */
1133   static int      i_avlist;               /* index for lists */
1134  
1135 + static int alatcmp(const void *av1, const void *av2);
1136  
1137 < static
1138 < av2list(av)
581 < register AMBVAL *av;
1137 > static void
1138 > avfree(AMBVAL *av)
1139   {
1140 +        free(av);
1141 + }
1142 +
1143 + static void
1144 + av2list(
1145 +        AMBVAL *av
1146 + )
1147 + {
1148   #ifdef DEBUG
1149          if (i_avlist >= nambvals)
1150                  error(CONSISTENCY, "too many ambient values in av2list1");
1151   #endif
1152 <        avlist1[i_avlist].p = avlist2[i_avlist] = av;
1152 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1153          avlist1[i_avlist++].t = av->latick;
1154   }
1155  
1156  
1157   static int
1158 < alatcmp(av1, av2)                       /* compare ambient values for MRA */
1159 < struct avl      *av1, *av2;
1158 > alatcmp(                        /* compare ambient values for MRA */
1159 >        const void *av1,
1160 >        const void *av2
1161 > )
1162   {
1163 <        register long  lc = av2->t - av1->t;
1163 >        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1164          return(lc<0 ? -1 : lc>0 ? 1 : 0);
1165   }
1166  
1167  
1168 + /* GW NOTE 2002/10/3:
1169 + * I used to compare AMBVAL pointers, but found that this was the
1170 + * cause of a serious consistency error with gcc, since the optimizer
1171 + * uses some dangerous trick in pointer subtraction that
1172 + * assumes pointers differ by exact struct size increments.
1173 + */
1174   static int
1175 < aposcmp(avp1, avp2)                     /* compare ambient value positions */
1176 < AMBVAL  **avp1, **avp2;
1175 > aposcmp(                        /* compare ambient value positions */
1176 >        const void      *avp1,
1177 >        const void      *avp2
1178 > )
1179   {
1180 <        return(*avp1 - *avp2);
1180 >        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1181 >        if (diff < 0)
1182 >                return(-1);
1183 >        return(diff > 0);
1184   }
1185  
1186  
609 #if 1
1187   static int
1188 < avlmemi(avaddr)                         /* find list position from address */
1189 < AMBVAL  *avaddr;
1188 > avlmemi(                                /* find list position from address */
1189 >        AMBVAL  *avaddr
1190 > )
1191   {
1192 <        register AMBVAL  **avlpp;
1192 >        AMBVAL  **avlpp;
1193  
1194 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1194 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1195                          nambvals, sizeof(AMBVAL *), aposcmp);
1196          if (avlpp == NULL)
1197                  error(CONSISTENCY, "address not found in avlmemi");
1198          return(avlpp - avlist2);
1199   }
622 #else
623 #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
624                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
625 #endif
1200  
1201  
1202 < static
1203 < sortambvals(always)                     /* resort ambient values */
1204 < int     always;
1202 > static void
1203 > sortambvals(                    /* resort ambient values */
1204 >        int     always
1205 > )
1206   {
1207          AMBTREE  oldatrunk;
1208          AMBVAL  tav, *tap, *pnext;
1209 <        register int    i, j;
1209 >        int     i, j;
1210                                          /* see if it's time yet */
1211          if (!always && (ambclock++ < lastsort+sortintvl ||
1212                          nambvals < SORT_THRESH))
# Line 660 | Line 1235 | int    always;
1235          }
1236          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1237                  if (avlist2 != NULL)
1238 <                        free((char *)avlist2);
1238 >                        free(avlist2);
1239                  if (always) {           /* rebuild without sorting */
1240 <                        copystruct(&oldatrunk, &atrunk);
1240 >                        oldatrunk = atrunk;
1241                          atrunk.alist = NULL;
1242                          atrunk.kid = NULL;
1243                          unloadatree(&oldatrunk, avinsert);
# Line 687 | Line 1262 | int    always;
1262                  if (i_avlist < nambvals)
1263                          error(CONSISTENCY, "missing ambient values in sortambvals");
1264   #endif
1265 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1266 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1265 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1266 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1267                  for (i = 0; i < nambvals; i++) {
1268                          if (avlist1[i].p == NULL)
1269                                  continue;
1270                          tap = avlist2[i];
1271 <                        copystruct(&tav, tap);
1271 >                        tav = *tap;
1272                          for (j = i; (pnext = avlist1[j].p) != tap;
1273                                          j = avlmemi(pnext)) {
1274 <                                copystruct(avlist2[j], pnext);
1274 >                                *(avlist2[j]) = *pnext;
1275                                  avinsert(avlist2[j]);
1276                                  avlist1[j].p = NULL;
1277                          }
1278 <                        copystruct(avlist2[j], &tav);
1278 >                        *(avlist2[j]) = tav;
1279                          avinsert(avlist2[j]);
1280                          avlist1[j].p = NULL;
1281                  }
1282 <                free((char *)avlist1);
1283 <                free((char *)avlist2);
1282 >                free(avlist1);
1283 >                free(avlist2);
1284                                                  /* compute new sort interval */
1285                  sortintvl = ambclock - lastsort;
1286                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 724 | Line 1299 | int    always;
1299  
1300   #ifdef  F_SETLKW
1301  
1302 < static
1303 < aflock(typ)                     /* lock/unlock ambient file */
1304 < int  typ;
1302 > static void
1303 > aflock(                 /* lock/unlock ambient file */
1304 >        int  typ
1305 > )
1306   {
1307          static struct flock  fls;       /* static so initialized to zeroes */
1308  
1309 +        if (typ == fls.l_type)          /* already called? */
1310 +                return;
1311          fls.l_type = typ;
1312          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1313                  error(SYSTEM, "cannot (un)lock ambient file");
# Line 737 | Line 1315 | int  typ;
1315  
1316  
1317   int
1318 < ambsync()                       /* synchronize ambient file */
1318 > ambsync(void)                   /* synchronize ambient file */
1319   {
742        static FILE  *ambinp = NULL;
743        static long  lastpos = -1;
1320          long  flen;
1321          AMBVAL  avs;
1322 <        register int  n;
1322 >        int  n;
1323  
1324 <        if (nunflshed == 0)
1324 >        if (ambfp == NULL)      /* no ambient file? */
1325                  return(0);
1326 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
1327 <                goto syncend;
752 <                                /* gain exclusive access */
753 <        aflock(F_WRLCK);
1326 >                                /* gain appropriate access */
1327 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
1328                                  /* see if file has grown */
1329 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
1329 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1330                  goto seekerr;
1331 <        if (n = flen - lastpos) {               /* file has grown */
1332 <                if (ambinp == NULL) {           /* use duplicate filedes */
1333 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1331 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
1332 >                if (ambinp == NULL) {           /* get new file pointer */
1333 >                        ambinp = fopen(ambfile, "rb");
1334                          if (ambinp == NULL)
1335 <                                error(SYSTEM, "fdopen failed in ambsync");
1335 >                                error(SYSTEM, "fopen failed in ambsync");
1336                  }
1337 <                if (fseek(ambinp, lastpos, 0) < 0)
1337 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1338                          goto seekerr;
1339                  while (n >= AMBVALSIZ) {        /* load contributed values */
1340                          if (!readambval(&avs, ambinp)) {
1341                                  sprintf(errmsg,
1342 <                                "ambient file corrupted near character %ld",
1343 <                                                flen - n);
1342 >                        "ambient file \"%s\" corrupted near character %ld",
1343 >                                                ambfile, flen - n);
1344                                  error(WARNING, errmsg);
1345                                  break;
1346                          }
1347 <                        avinsert(avstore(&avs));
1347 >                        avstore(&avs);
1348                          n -= AMBVALSIZ;
1349                  }
1350 <                /*** seek always as safety measure
1351 <                if (n) ***/                     /* alignment */
1352 <                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
779 <                                goto seekerr;
1350 >                lastpos = flen - n;             /* check alignment */
1351 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1352 >                        goto seekerr;
1353          }
781 #ifdef  DEBUG
782        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
783                sprintf(errmsg, "ambient file buffer at %d rather than %d",
784                                ambfp->_ptr - ambfp->_base,
785                                nunflshed*AMBVALSIZ);
786                error(CONSISTENCY, errmsg);
787        }
788 #endif
789 syncend:
1354          n = fflush(ambfp);                      /* calls write() at last */
1355 <        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
792 <                goto seekerr;
1355 >        lastpos += (long)nunflshed*AMBVALSIZ;
1356          aflock(F_UNLCK);                        /* release file */
1357          nunflshed = 0;
1358          return(n);
1359   seekerr:
1360          error(SYSTEM, "seek failed in ambsync");
1361 +        return(EOF);    /* pro forma return */
1362   }
1363  
1364 < #else
1364 > #else   /* ! F_SETLKW */
1365  
1366   int
1367 < ambsync()                       /* flush ambient file */
1367 > ambsync(void)                   /* flush ambient file */
1368   {
1369 <        if (nunflshed == 0)
1369 >        if (ambfp == NULL)
1370                  return(0);
1371          nunflshed = 0;
1372          return(fflush(ambfp));
1373   }
1374  
1375 < #endif
1375 > #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines