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.8 by greg, Fri Jan 12 11:30:59 1990 UTC vs.
Revision 2.98 by greg, Sun Aug 23 00:17:12 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines