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.56 by schorsch, Tue Mar 30 16:13:00 2004 UTC vs.
Revision 2.102 by greg, Sun Apr 24 16:21:32 2016 UTC

# Line 1 | Line 1
1 #ifndef lint
1   static const char       RCSid[] = "$Id$";
3 #endif
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4   *
# Line 17 | Line 15 | static const char      RCSid[] = "$Id$";
15   #include  "resolu.h"
16   #include  "ambient.h"
17   #include  "random.h"
18 + #include  "pmapamb.h"
19  
20   #ifndef  OCTSCALE
21   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
# Line 24 | Line 23 | static const char      RCSid[] = "$Id$";
23  
24   extern char  *shm_boundary;     /* memory sharing boundary */
25  
26 < #define  MAXASET        511     /* maximum number of elements in ambient set */
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   double  maxarad;                /* maximum ambient radius */
# Line 37 | Line 38 | static int  nunflshed = 0;     /* number of unflushed ambi
38  
39   #ifndef SORT_THRESH
40   #ifdef SMLMEM
41 < #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
41 > #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
42   #else
43 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
43 > #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
44   #endif
45   #endif
46   #ifndef SORT_INTVL
# Line 49 | Line 50 | static int  nunflshed = 0;     /* number of unflushed ambi
50   #define MAX_SORT_INTVL  (SORT_INTVL<<6)
51   #endif
52  
53 +
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 */
# Line 74 | Line 76 | static long  lastpos = -1;             /* last flush position */
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
77 #define  freeav(av)     free((void *)av);
79  
80   static void initambfile(int creat);
81   static void avsave(AMBVAL *av);
# Line 82 | Line 83 | static AMBVAL *avstore(AMBVAL  *aval);
83   static AMBTREE *newambtree(void);
84   static void freeambtree(AMBTREE  *atp);
85  
86 < typedef void unloadtf_t(void *);
86 > typedef void unloadtf_t(AMBVAL *);
87   static unloadtf_t avinsert;
88   static unloadtf_t av2list;
89 + static unloadtf_t avfree;
90   static void unloadatree(AMBTREE  *at, unloadtf_t *f);
91  
92   static int aposcmp(const void *avp1, const void *avp2);
# Line 96 | Line 98 | static void aflock(int  typ);
98   #endif
99  
100  
101 < extern void
101 > void
102   setambres(                              /* set ambient resolution */
103          int  ar
104   )
# Line 105 | Line 107 | setambres(                             /* set ambient resolution */
107                                                  /* set min & max radii */
108          if (ar <= 0) {
109                  minarad = 0;
110 <                maxarad = thescene.cusize / 2.0;
110 >                maxarad = thescene.cusize*0.2;
111          } else {
112                  minarad = thescene.cusize / ar;
113 <                maxarad = 64 * minarad;                 /* heuristic */
114 <                if (maxarad > thescene.cusize / 2.0)
115 <                        maxarad = thescene.cusize / 2.0;
113 >                maxarad = 64.0 * minarad;               /* heuristic */
114 >                if (maxarad > thescene.cusize*0.2)
115 >                        maxarad = thescene.cusize*0.2;
116          }
117          if (minarad <= FTINY)
118 <                minarad = 10*FTINY;
118 >                minarad = 10.0*FTINY;
119          if (maxarad <= minarad)
120 <                maxarad = 64 * minarad;
120 >                maxarad = 64.0 * minarad;
121   }
122  
123  
124 < extern void
124 > void
125   setambacc(                              /* set ambient accuracy */
126          double  newa
127   )
128   {
129 <        double  ambdiff;
130 <
131 <        if (newa < 0.0)
132 <                newa = 0.0;
133 <        ambdiff = fabs(newa - ambacc);
134 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
135 <                sortambvals(1);                 /* rebuild tree */
129 >        static double   olda;           /* remember previous setting here */
130 >        
131 >        newa *= (newa > 0);
132 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
133 >                ambacc = newa;
134 >                if (nambvals > 0)
135 >                        sortambvals(1);         /* rebuild tree */
136 >        }
137   }
138  
139  
140 < extern void
140 > void
141   setambient(void)                                /* initialize calculation */
142   {
143          int     readonly = 0;
144 <        long  pos, flen;
144 >        long    flen;
145          AMBVAL  amb;
146                                                  /* make sure we're fresh */
147          ambdone();
# Line 158 | Line 161 | setambient(void)                               /* initialize calculation */
161                  readonly = (ambfp = fopen(ambfile, "r")) != NULL;
162          if (ambfp != NULL) {
163                  initambfile(0);                 /* file exists */
164 <                pos = ftell(ambfp);
164 >                lastpos = ftell(ambfp);
165                  while (readambval(&amb, ambfp))
166 <                        avinsert(avstore(&amb));
166 >                        avstore(&amb);
167                  nambshare = nambvals;           /* share loaded values */
168                  if (readonly) {
169                          sprintf(errmsg,
# Line 172 | Line 175 | setambient(void)                               /* initialize calculation */
175                          return;                 /* avoid ambsync() */
176                  }
177                                                  /* align file pointer */
178 <                pos += (long)nambvals*AMBVALSIZ;
178 >                lastpos += (long)nambvals*AMBVALSIZ;
179                  flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
180 <                if (flen != pos) {
180 >                if (flen != lastpos) {
181                          sprintf(errmsg,
182                          "ignoring last %ld values in ambient file (corrupted)",
183 <                                        (flen - pos)/AMBVALSIZ);
183 >                                        (flen - lastpos)/AMBVALSIZ);
184                          error(WARNING, errmsg);
185 <                        fseek(ambfp, pos, 0);
186 < #ifndef _WIN32 /* XXX we need a replacement for that one */
184 <                        ftruncate(fileno(ambfp), (off_t)pos);
185 < #endif
185 >                        fseek(ambfp, lastpos, SEEK_SET);
186 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
187                  }
188          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
189                  initambfile(1);                 /* else create new file */
190 +                fflush(ambfp);
191 +                lastpos = ftell(ambfp);
192          } else {
193                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
194                  error(SYSTEM, errmsg);
195          }
196 <        nunflshed++;    /* lie */
197 <        ambsync();
196 > #ifdef  F_SETLKW
197 >        aflock(F_UNLCK);                        /* release file */
198 > #endif
199   }
200  
201  
202 < extern void
202 > void
203   ambdone(void)                   /* close ambient file and free memory */
204   {
205          if (ambfp != NULL) {            /* close ambient file */
# Line 209 | Line 213 | ambdone(void)                  /* close ambient file and free memory
213                  lastpos = -1;
214          }
215                                          /* free ambient tree */
216 <        unloadatree(&atrunk, free);
216 >        unloadatree(&atrunk, avfree);
217                                          /* reset state variables */
218          avsum = 0.;
219          navsum = 0;
# Line 221 | Line 225 | ambdone(void)                  /* close ambient file and free memory
225   }
226  
227  
228 < extern void
228 > void
229   ambnotify(                      /* record new modifier */
230          OBJECT  obj
231   )
232   {
233          static int  hitlimit = 0;
234 <        register OBJREC  *o;
235 <        register char  **amblp;
234 >        OBJREC   *o;
235 >        char  **amblp;
236  
237          if (obj == OVOID) {             /* starting over */
238                  ambset[0] = 0;
# Line 250 | Line 254 | ambnotify(                     /* record new modifier */
254                  }
255   }
256  
257 + /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
258  
259 < extern void
260 < ambient(                /* compute ambient component for ray */
259 > #ifndef OLDAMB
260 >
261 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
262 >
263 > static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
264 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
265 >                                AMBTREE *at, FVECT c0, double s);
266 > static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
267 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
268 >                                FVECT uvw[3]);
269 >
270 > void
271 > multambient(            /* compute ambient component & multiply by coef. */
272 >        COLOR  aval,
273 >        RAY  *r,
274 >        FVECT  nrm
275 > )
276 > {
277 >        static int  rdepth = 0;                 /* ambient recursion */
278 >        COLOR   acol, caustic;
279 >        int     ok;
280 >        double  d, l;
281 >
282 >        /* PMAP: Factor in ambient from photon map, if enabled and ray is
283 >         * ambient. Return as all ambient components accounted for, else
284 >         * continue. */
285 >        if (ambPmap(aval, r, rdepth))
286 >                return;
287 >
288 >        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
289 >         * map, if enabled and ray is primary, else caustic is zero.  Continue
290 >         * with RADIANCE ambient calculation */
291 >        copycolor(caustic, aval);
292 >        ambPmapCaustic(caustic, r, rdepth);
293 >        
294 >        if (ambdiv <= 0)                        /* no ambient calculation */
295 >                goto dumbamb;
296 >                                                /* check number of bounces */
297 >        if (rdepth >= ambounce)
298 >                goto dumbamb;
299 >                                                /* check ambient list */
300 >        if (ambincl != -1 && r->ro != NULL &&
301 >                        ambincl != inset(ambset, r->ro->omod))
302 >                goto dumbamb;
303 >
304 >        if (ambacc <= FTINY) {                  /* no ambient storage */
305 >                copycolor(acol, aval);
306 >                rdepth++;
307 >                ok = doambient(acol, r, r->rweight,
308 >                                NULL, NULL, NULL, NULL, NULL);
309 >                rdepth--;
310 >                if (!ok)
311 >                        goto dumbamb;
312 >                copycolor(aval, acol);
313 >
314 >                /* PMAP: add in caustic */
315 >                addcolor(aval, caustic);
316 >                return;
317 >        }
318 >
319 >        if (tracktime)                          /* sort to minimize thrashing */
320 >                sortambvals(0);
321 >                                                /* interpolate ambient value */
322 >        setcolor(acol, 0.0, 0.0, 0.0);
323 >        d = sumambient(acol, r, nrm, rdepth,
324 >                        &atrunk, thescene.cuorg, thescene.cusize);
325 >                        
326 >        if (d > FTINY) {
327 >                d = 1.0/d;
328 >                scalecolor(acol, d);
329 >                multcolor(aval, acol);
330 >
331 >                /* PMAP: add in caustic */
332 >                addcolor(aval, caustic);
333 >                return;
334 >        }
335 >        
336 >        rdepth++;                               /* need to cache new value */
337 >        ok = makeambient(acol, r, nrm, rdepth-1);
338 >        rdepth--;
339 >        
340 >        if (ok) {
341 >                multcolor(aval, acol);          /* computed new value */
342 >
343 >                /* PMAP: add in caustic */
344 >                addcolor(aval, caustic);
345 >                return;
346 >        }
347 >        
348 > dumbamb:                                        /* return global value */
349 >        if ((ambvwt <= 0) | (navsum == 0)) {
350 >                multcolor(aval, ambval);
351 >                
352 >                /* PMAP: add in caustic */
353 >                addcolor(aval, caustic);
354 >                return;
355 >        }
356 >        
357 >        l = bright(ambval);                     /* average in computations */  
358 >        if (l > FTINY) {
359 >                d = (log(l)*(double)ambvwt + avsum) /
360 >                                (double)(ambvwt + navsum);
361 >                d = exp(d) / l;
362 >                scalecolor(aval, d);
363 >                multcolor(aval, ambval);        /* apply color of ambval */
364 >        } else {
365 >                d = exp( avsum / (double)navsum );
366 >                scalecolor(aval, d);            /* neutral color */
367 >        }
368 > }
369 >
370 >
371 > /* Plug a potential leak where ambient cache value is occluded */
372 > static int
373 > plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
374 > {
375 >        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
376 >        RAY             rtst;
377 >        FVECT           vdif;
378 >        double          normdot, ndotd, nadotd;
379 >        double          a, b, c, t[2];
380 >
381 >        ang += 2.*PI*(ang < 0);                 /* check direction flags */
382 >        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
383 >                return(0);
384 >        /*
385 >         * Generate test ray, targeting 20 degrees above sample point plane
386 >         * along surface normal from cache position.  This should be high
387 >         * enough to miss local geometry we don't really care about.
388 >         */
389 >        VSUB(vdif, ap->pos, r->rop);
390 >        normdot = DOT(anorm, r->ron);
391 >        ndotd = DOT(vdif, r->ron);
392 >        nadotd = DOT(vdif, anorm);
393 >        a = normdot*normdot - cost70sq;
394 >        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
395 >        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
396 >        if (quadratic(t, a, b, c) != 2)
397 >                return(1);                      /* should rarely happen */
398 >        if (t[1] <= FTINY)
399 >                return(0);                      /* should fail behind test */
400 >        rayorigin(&rtst, SHADOW, r, NULL);
401 >        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
402 >        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
403 >        while (localhit(&rtst, &thescene)) {    /* check for occluder */
404 >                if (rtst.ro->omod != OVOID &&
405 >                                (rtst.clipset == NULL ||
406 >                                        !inset(rtst.clipset, rtst.ro->omod)))
407 >                        return(1);              /* plug light leak */
408 >                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
409 >                rtst.rmax -= rtst.rot;
410 >                rayclear(&rtst);
411 >        }
412 >        return(0);                              /* seems we're OK */
413 > }
414 >
415 >
416 > static double
417 > sumambient(             /* get interpolated ambient value */
418          COLOR  acol,
419 <        register RAY  *r,
419 >        RAY  *r,
420 >        FVECT  rn,
421 >        int  al,
422 >        AMBTREE  *at,
423 >        FVECT  c0,
424 >        double  s
425 > )
426 > {                       /* initial limit is 10 degrees plus ambacc radians */
427 >        const double    minangle = 10.0 * PI/180.;
428 >        double          maxangle = minangle + ambacc;
429 >        double          wsum = 0.0;
430 >        FVECT           ck0;
431 >        int             i, j;
432 >        AMBVAL          *av;
433 >
434 >        if (at->kid != NULL) {          /* sum children first */                                
435 >                s *= 0.5;
436 >                for (i = 0; i < 8; i++) {
437 >                        for (j = 0; j < 3; j++) {
438 >                                ck0[j] = c0[j];
439 >                                if (1<<j & i)
440 >                                        ck0[j] += s;
441 >                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
442 >                                        break;
443 >                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
444 >                                        break;
445 >                        }
446 >                        if (j == 3)
447 >                                wsum += sumambient(acol, r, rn, al,
448 >                                                        at->kid+i, ck0, s);
449 >                }
450 >                                        /* good enough? */
451 >                if (wsum >= 0.05 && s > minarad*10.0)
452 >                        return(wsum);
453 >        }
454 >                                        /* adjust maximum angle */
455 >        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
456 >                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
457 >                                        /* sum this node */
458 >        for (av = at->alist; av != NULL; av = av->next) {
459 >                double  u, v, d, delta_r2, delta_t2;
460 >                COLOR   ct;
461 >                FVECT   uvw[3];
462 >                                        /* record access */
463 >                if (tracktime)
464 >                        av->latick = ambclock;
465 >                /*
466 >                 *  Ambient level test
467 >                 */
468 >                if (av->lvl > al ||     /* list sorted, so this works */
469 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
470 >                        break;
471 >                /*
472 >                 *  Direction test using unperturbed normal
473 >                 */
474 >                decodedir(uvw[2], av->ndir);
475 >                d = DOT(uvw[2], r->ron);
476 >                if (d <= 0.0)           /* >= 90 degrees */
477 >                        continue;
478 >                delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
479 >                if (delta_r2 >= maxangle*maxangle)
480 >                        continue;
481 >                /*
482 >                 *  Modified ray behind test
483 >                 */
484 >                VSUB(ck0, r->rop, av->pos);
485 >                d = DOT(ck0, uvw[2]);
486 >                if (d < -minarad*ambacc-.001)
487 >                        continue;
488 >                d /= av->rad[0];
489 >                delta_t2 = d*d;
490 >                if (delta_t2 >= ambacc*ambacc)
491 >                        continue;
492 >                /*
493 >                 *  Elliptical radii test based on Hessian
494 >                 */
495 >                decodedir(uvw[0], av->udir);
496 >                VCROSS(uvw[1], uvw[2], uvw[0]);
497 >                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
498 >                delta_t2 += d*d;
499 >                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
500 >                delta_t2 += d*d;
501 >                if (delta_t2 >= ambacc*ambacc)
502 >                        continue;
503 >                /*
504 >                 *  Test for potential light leak
505 >                 */
506 >                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
507 >                        continue;
508 >                /*
509 >                 *  Extrapolate value and compute final weight (hat function)
510 >                 */
511 >                if (!extambient(ct, av, r->rop, rn, uvw))
512 >                        continue;
513 >                d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
514 >                        tfunc(ambacc, sqrt(delta_t2), 0.0);
515 >                scalecolor(ct, d);
516 >                addcolor(acol, ct);
517 >                wsum += d;
518 >        }
519 >        return(wsum);
520 > }
521 >
522 >
523 > static int
524 > makeambient(            /* make a new ambient value for storage */
525 >        COLOR  acol,
526 >        RAY  *r,
527 >        FVECT  rn,
528 >        int  al
529 > )
530 > {
531 >        AMBVAL  amb;
532 >        FVECT   uvw[3];
533 >        int     i;
534 >
535 >        amb.weight = 1.0;                       /* compute weight */
536 >        for (i = al; i-- > 0; )
537 >                amb.weight *= AVGREFL;
538 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
539 >                amb.weight = 1.25*r->rweight;
540 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
541 >                                                /* compute ambient */
542 >        i = doambient(acol, r, amb.weight,
543 >                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
544 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
545 >        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
546 >                return(i);
547 >                                                /* store value */
548 >        VCOPY(amb.pos, r->rop);
549 >        amb.ndir = encodedir(r->ron);
550 >        amb.udir = encodedir(uvw[0]);
551 >        amb.lvl = al;
552 >        copycolor(amb.val, acol);
553 >                                                /* insert into tree */
554 >        avsave(&amb);                           /* and save to file */
555 >        if (rn != r->ron) {                     /* texture */
556 >                VCOPY(uvw[2], r->ron);
557 >                extambient(acol, &amb, r->rop, rn, uvw);
558 >        }
559 >        return(1);
560 > }
561 >
562 >
563 > static int
564 > extambient(             /* extrapolate value at pv, nv */
565 >        COLOR  cr,
566 >        AMBVAL   *ap,
567 >        FVECT  pv,
568 >        FVECT  nv,
569 >        FVECT  uvw[3]
570 > )
571 > {
572 >        const double    min_d = 0.05;
573 >        static FVECT    my_uvw[3];
574 >        FVECT           v1;
575 >        int             i;
576 >        double          d = 1.0;        /* zeroeth order */
577 >
578 >        if (uvw == NULL) {              /* need local coordinates? */
579 >                decodedir(my_uvw[2], ap->ndir);
580 >                decodedir(my_uvw[0], ap->udir);
581 >                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
582 >                uvw = my_uvw;
583 >        }
584 >        for (i = 3; i--; )              /* gradient due to translation */
585 >                d += (pv[i] - ap->pos[i]) *
586 >                        (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
587 >
588 >        VCROSS(v1, uvw[2], nv);         /* gradient due to rotation */
589 >        for (i = 3; i--; )
590 >                d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
591 >        
592 >        if (d < min_d)                  /* should not use if we can avoid it */
593 >                d = min_d;
594 >        copycolor(cr, ap->val);
595 >        scalecolor(cr, d);
596 >        return(d > min_d);
597 > }
598 >
599 >
600 > static void
601 > avinsert(                               /* insert ambient value in our tree */
602 >        AMBVAL *av
603 > )
604 > {
605 >        AMBTREE  *at;
606 >        AMBVAL  *ap;
607 >        AMBVAL  avh;
608 >        FVECT  ck0;
609 >        double  s;
610 >        int  branch;
611 >        int  i;
612 >
613 >        if (av->rad[0] <= FTINY)
614 >                error(CONSISTENCY, "zero ambient radius in avinsert");
615 >        at = &atrunk;
616 >        VCOPY(ck0, thescene.cuorg);
617 >        s = thescene.cusize;
618 >        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
619 >                if (at->kid == NULL)
620 >                        if ((at->kid = newambtree()) == NULL)
621 >                                error(SYSTEM, "out of memory in avinsert");
622 >                s *= 0.5;
623 >                branch = 0;
624 >                for (i = 0; i < 3; i++)
625 >                        if (av->pos[i] > ck0[i] + s) {
626 >                                ck0[i] += s;
627 >                                branch |= 1 << i;
628 >                        }
629 >                at = at->kid + branch;
630 >        }
631 >        avh.next = at->alist;           /* order by increasing level */
632 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
633 >                if ( ap->next->lvl > av->lvl ||
634 >                                (ap->next->lvl == av->lvl) &
635 >                                (ap->next->weight <= av->weight) )
636 >                        break;
637 >        av->next = ap->next;
638 >        ap->next = (AMBVAL*)av;
639 >        at->alist = avh.next;
640 > }
641 >
642 >
643 > #else /* ! NEWAMB */
644 >
645 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
646 >                                AMBTREE *at, FVECT c0, double s);
647 > static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
648 > static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
649 >
650 >
651 > void
652 > multambient(            /* compute ambient component & multiply by coef. */
653 >        COLOR  aval,
654 >        RAY  *r,
655          FVECT  nrm
656   )
657   {
658          static int  rdepth = 0;                 /* ambient recursion */
659 +        COLOR   acol, caustic;
660          double  d, l;
661  
662 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
663 +         * as all ambient components accounted for */
664 +        if (ambPmap(aval, r, rdepth))
665 +                return;
666 +
667 +        /* PMAP: Otherwise factor in ambient from caustic photon map
668 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
669 +         * continue with RADIANCE ambient calculation */
670 +        copycolor(caustic, aval);
671 +        ambPmapCaustic(caustic, r, rdepth);
672 +        
673          if (ambdiv <= 0)                        /* no ambient calculation */
674                  goto dumbamb;
675                                                  /* check number of bounces */
# Line 272 | Line 681 | ambient(               /* compute ambient component for ray */
681                  goto dumbamb;
682  
683          if (ambacc <= FTINY) {                  /* no ambient storage */
684 +                copycolor(acol, aval);
685                  rdepth++;
686                  d = doambient(acol, r, r->rweight, NULL, NULL);
687                  rdepth--;
688                  if (d <= FTINY)
689                          goto dumbamb;
690 +                copycolor(aval, acol);          
691 +        
692 +           /* PMAP: add in caustic */
693 +                addcolor(aval, caustic);        
694                  return;
695          }
696  
697          if (tracktime)                          /* sort to minimize thrashing */
698                  sortambvals(0);
699 <                                                /* get ambient value */
699 >                                                /* interpolate ambient value */
700          setcolor(acol, 0.0, 0.0, 0.0);
701          d = sumambient(acol, r, nrm, rdepth,
702                          &atrunk, thescene.cuorg, thescene.cusize);
703 +                        
704          if (d > FTINY) {
705 <                scalecolor(acol, 1.0/d);
705 >                d = 1.0/d;
706 >                scalecolor(acol, d);
707 >                multcolor(aval, acol);
708 >                
709 >                /* PMAP: add in caustic */
710 >                addcolor(aval, caustic);        
711                  return;
712          }
713 +        
714          rdepth++;                               /* need to cache new value */
715          d = makeambient(acol, r, nrm, rdepth-1);
716          rdepth--;
717 <        if (d > FTINY)
717 >        
718 >        if (d > FTINY) {
719 >                multcolor(aval, acol);          /* got new value */
720 >
721 >                /* PMAP: add in caustic */
722 >                addcolor(aval, caustic);                        
723                  return;
724 +        }
725 +        
726   dumbamb:                                        /* return global value */
727 <        copycolor(acol, ambval);
728 <        if ((ambvwt <= 0) | (navsum == 0))
727 >        if ((ambvwt <= 0) | (navsum == 0)) {
728 >                multcolor(aval, ambval);
729 >
730 >                /* PMAP: add in caustic */
731 >                addcolor(aval, caustic);        
732                  return;
733 +        }
734 +        
735          l = bright(ambval);                     /* average in computations */
736          if (l > FTINY) {
737                  d = (log(l)*(double)ambvwt + avsum) /
738                                  (double)(ambvwt + navsum);
739                  d = exp(d) / l;
740 <                scalecolor(acol, d);            /* apply color of ambval */
740 >                scalecolor(aval, d);
741 >                multcolor(aval, ambval);        /* apply color of ambval */
742          } else {
743                  d = exp( avsum / (double)navsum );
744 <                setcolor(acol, d, d, d);        /* neutral color */
744 >                scalecolor(aval, d);            /* neutral color */
745          }
746   }
747  
748  
749 < extern double
749 > static double
750   sumambient(     /* get interpolated ambient value */
751          COLOR  acol,
752 <        register RAY  *r,
752 >        RAY  *r,
753          FVECT  rn,
754          int  al,
755          AMBTREE  *at,
# Line 327 | Line 761 | sumambient(    /* get interpolated ambient value */
761          COLOR  ct;
762          FVECT  ck0;
763          int  i;
764 <        register int  j;
765 <        register AMBVAL  *av;
764 >        int  j;
765 >        AMBVAL   *av;
766  
767          wsum = 0.0;
768                                          /* do this node */
# Line 339 | Line 773 | sumambient(    /* get interpolated ambient value */
773                  /*
774                   *  Ambient level test.
775                   */
776 <                if (av->lvl > al)       /* list sorted, so this works */
776 >                if (av->lvl > al ||     /* list sorted, so this works */
777 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
778                          break;
344                if (av->weight < r->rweight-FTINY)
345                        continue;
779                  /*
780                   *  Ambient radius test.
781                   */
782 <                d = av->pos[0] - r->rop[0];
783 <                e1 = d * d;
351 <                d = av->pos[1] - r->rop[1];
352 <                e1 += d * d;
353 <                d = av->pos[2] - r->rop[2];
354 <                e1 += d * d;
355 <                e1 /= av->rad * av->rad;
782 >                VSUB(ck0, av->pos, r->rop);
783 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
784                  if (e1 > ambacc*ambacc*1.21)
785                          continue;
786                  /*
# Line 369 | Line 797 | sumambient(    /* get interpolated ambient value */
797                          }
798                  }
799                  e2 = (1.0 - d) * r->rweight;
800 <                if (e2 < 0.0) e2 = 0.0;
801 <                if (e1 + e2 > ambacc*ambacc*1.21)
800 >                if (e2 < 0.0)
801 >                        e2 = 0.0;
802 >                else if (e1 + e2 > ambacc*ambacc*1.21)
803                          continue;
804                  /*
805                   *  Ray behind test.
# Line 420 | Line 849 | sumambient(    /* get interpolated ambient value */
849                                  break;
850                  }
851                  if (j == 3)
852 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
852 >                        wsum += sumambient(acol, r, rn, al,
853 >                                                at->kid+i, ck0, s);
854          }
855          return(wsum);
856   }
857  
858  
859 < extern double
860 < makeambient(    /* make a new ambient value */
859 > static double
860 > makeambient(            /* make a new ambient value for storage */
861          COLOR  acol,
862 <        register RAY  *r,
862 >        RAY  *r,
863          FVECT  rn,
864          int  al
865   )
866   {
867          AMBVAL  amb;
868          FVECT   gp, gd;
869 <                                                /* compute weight */
870 <        amb.weight = pow(AVGREFL, (double)al);
871 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
872 <                amb.weight = r->rweight;
869 >        int     i;
870 >
871 >        amb.weight = 1.0;                       /* compute weight */
872 >        for (i = al; i-- > 0; )
873 >                amb.weight *= AVGREFL;
874 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
875 >                amb.weight = 1.25*r->rweight;
876 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
877                                                  /* compute ambient */
878          amb.rad = doambient(acol, r, amb.weight, gp, gd);
879 <        if (amb.rad <= FTINY)
879 >        if (amb.rad <= FTINY) {
880 >                setcolor(acol, 0.0, 0.0, 0.0);
881                  return(0.0);
882 <                                                /* store it */
882 >        }
883 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
884 >                                                /* store value */
885          VCOPY(amb.pos, r->rop);
886          VCOPY(amb.dir, r->ron);
887          amb.lvl = al;
# Line 459 | Line 896 | makeambient(   /* make a new ambient value */
896   }
897  
898  
899 < extern void
899 > static void
900   extambient(             /* extrapolate value at pv, nv */
901          COLOR  cr,
902 <        register AMBVAL  *ap,
902 >        AMBVAL   *ap,
903          FVECT  pv,
904          FVECT  nv
905   )
906   {
907          FVECT  v1;
908 <        register int  i;
908 >        int  i;
909          double  d;
910  
911          d = 1.0;                        /* zeroeth order */
# Line 488 | Line 925 | extambient(            /* extrapolate value at pv, nv */
925  
926  
927   static void
928 + avinsert(                               /* insert ambient value in our tree */
929 +        AMBVAL *av
930 + )
931 + {
932 +        AMBTREE  *at;
933 +        AMBVAL  *ap;
934 +        AMBVAL  avh;
935 +        FVECT  ck0;
936 +        double  s;
937 +        int  branch;
938 +        int  i;
939 +
940 +        if (av->rad <= FTINY)
941 +                error(CONSISTENCY, "zero ambient radius in avinsert");
942 +        at = &atrunk;
943 +        VCOPY(ck0, thescene.cuorg);
944 +        s = thescene.cusize;
945 +        while (s*(OCTSCALE/2) > av->rad*ambacc) {
946 +                if (at->kid == NULL)
947 +                        if ((at->kid = newambtree()) == NULL)
948 +                                error(SYSTEM, "out of memory in avinsert");
949 +                s *= 0.5;
950 +                branch = 0;
951 +                for (i = 0; i < 3; i++)
952 +                        if (av->pos[i] > ck0[i] + s) {
953 +                                ck0[i] += s;
954 +                                branch |= 1 << i;
955 +                        }
956 +                at = at->kid + branch;
957 +        }
958 +        avh.next = at->alist;           /* order by increasing level */
959 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
960 +                if ( ap->next->lvl > av->lvl ||
961 +                                (ap->next->lvl == av->lvl) &
962 +                                (ap->next->weight <= av->weight) )
963 +                        break;
964 +        av->next = ap->next;
965 +        ap->next = (AMBVAL*)av;
966 +        at->alist = avh.next;
967 + }
968 +
969 + #endif  /* ! NEWAMB */
970 +
971 + /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
972 +
973 + static void
974   initambfile(            /* initialize ambient file */
975 <        int  creat
975 >        int  cre8
976   )
977   {
978          extern char  *progname, *octname;
979          static char  *mybuf = NULL;
980  
981   #ifdef  F_SETLKW
982 <        aflock(creat ? F_WRLCK : F_RDLCK);
982 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
983   #endif
984          SET_FILE_BINARY(ambfp);
985          if (mybuf == NULL)
986                  mybuf = (char *)bmalloc(BUFSIZ+8);
987          setbuf(ambfp, mybuf);
988 <        if (creat) {                    /* new file */
988 >        if (cre8) {                     /* new file */
989                  newheader("RADIANCE", ambfp);
990                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
991                                  progname, colval(ambval,RED),
# Line 511 | Line 994 | initambfile(           /* initialize ambient file */
994                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
995                                  ambdiv, ambssamp, ambres);
996                  if (octname != NULL)
997 <                        printargs(1, &octname, ambfp);
998 <                else
516 <                        fputc('\n', ambfp);
997 >                        fputs(octname, ambfp);
998 >                fputc('\n', ambfp);
999                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
1000                  fputnow(ambfp);
1001                  fputformat(AMBFMT, ambfp);
1002 <                putc('\n', ambfp);
1002 >                fputc('\n', ambfp);
1003                  putambmagic(ambfp);
1004          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
1005                  error(USER, "bad ambient file");
# Line 529 | Line 1011 | avsave(                                /* insert and save an ambient value */
1011          AMBVAL  *av
1012   )
1013   {
1014 <        avinsert(avstore(av));
1014 >        avstore(av);
1015          if (ambfp == NULL)
1016                  return;
1017          if (writambval(av, ambfp) < 0)
# Line 544 | Line 1026 | writerr:
1026  
1027  
1028   static AMBVAL *
1029 < avstore(                                /* allocate memory and store aval */
1030 <        register AMBVAL  *aval
1029 > avstore(                                /* allocate memory and save aval */
1030 >        AMBVAL  *aval
1031   )
1032   {
1033 <        register AMBVAL  *av;
1033 >        AMBVAL  *av;
1034          double  d;
1035  
1036          if ((av = newambval()) == NULL)
# Line 562 | Line 1044 | avstore(                               /* allocate memory and store aval */
1044                  avsum += log(d);
1045                  navsum++;
1046          }
1047 +        avinsert(av);                   /* insert in our cache tree */
1048          return(av);
1049   }
1050  
# Line 574 | Line 1057 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
1057   static AMBTREE *
1058   newambtree(void)                                /* allocate 8 ambient tree structs */
1059   {
1060 <        register AMBTREE  *atp, *upperlim;
1060 >        AMBTREE  *atp, *upperlim;
1061  
1062          if (atfreelist == NULL) {       /* get more nodes */
1063                  atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
# Line 588 | Line 1071 | newambtree(void)                               /* allocate 8 ambient tree structs
1071          }
1072          atp = atfreelist;
1073          atfreelist = atp->kid;
1074 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1074 >        memset(atp, 0, 8*sizeof(AMBTREE));
1075          return(atp);
1076   }
1077  
# Line 604 | Line 1087 | freeambtree(                   /* free 8 ambient tree structs */
1087  
1088  
1089   static void
607 avinsert(                               /* insert ambient value in our tree */
608        void *av
609 )
610 {
611        register AMBTREE  *at;
612        register AMBVAL  *ap;
613        AMBVAL  avh;
614        FVECT  ck0;
615        double  s;
616        int  branch;
617        register int  i;
618
619        if (((AMBVAL*)av)->rad <= FTINY)
620                error(CONSISTENCY, "zero ambient radius in avinsert");
621        at = &atrunk;
622        VCOPY(ck0, thescene.cuorg);
623        s = thescene.cusize;
624        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
625                if (at->kid == NULL)
626                        if ((at->kid = newambtree()) == NULL)
627                                error(SYSTEM, "out of memory in avinsert");
628                s *= 0.5;
629                branch = 0;
630                for (i = 0; i < 3; i++)
631                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
632                                ck0[i] += s;
633                                branch |= 1 << i;
634                        }
635                at = at->kid + branch;
636        }
637        avh.next = at->alist;           /* order by increasing level */
638        for (ap = &avh; ap->next != NULL; ap = ap->next)
639                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
640                        break;
641        ((AMBVAL*)av)->next = ap->next;
642        ap->next = (AMBVAL*)av;
643        at->alist = avh.next;
644 }
645
646
647 static void
1090   unloadatree(                    /* unload an ambient value tree */
1091 <        register AMBTREE  *at,
1091 >        AMBTREE  *at,
1092          unloadtf_t *f
1093   )
1094   {
1095 <        register AMBVAL  *av;
1096 <        register int  i;
1095 >        AMBVAL  *av;
1096 >        int  i;
1097                                          /* transfer values at this node */
1098          for (av = at->alist; av != NULL; av = at->alist) {
1099                  at->alist = av->next;
1100 +                av->next = NULL;
1101                  (*f)(av);
1102          }
1103          if (at->kid == NULL)
# Line 676 | Line 1119 | static int     i_avlist;               /* index for lists */
1119   static int alatcmp(const void *av1, const void *av2);
1120  
1121   static void
1122 + avfree(AMBVAL *av)
1123 + {
1124 +        free(av);
1125 + }
1126 +
1127 + static void
1128   av2list(
1129 <        void *av
1129 >        AMBVAL *av
1130   )
1131   {
1132   #ifdef DEBUG
# Line 685 | Line 1134 | av2list(
1134                  error(CONSISTENCY, "too many ambient values in av2list1");
1135   #endif
1136          avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1137 <        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
1137 >        avlist1[i_avlist++].t = av->latick;
1138   }
1139  
1140  
# Line 695 | Line 1144 | alatcmp(                       /* compare ambient values for MRA */
1144          const void *av2
1145   )
1146   {
1147 <        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1147 >        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1148          return(lc<0 ? -1 : lc>0 ? 1 : 0);
1149   }
1150  
# Line 712 | Line 1161 | aposcmp(                       /* compare ambient value positions */
1161          const void      *avp2
1162   )
1163   {
1164 <        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
1164 >        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1165          if (diff < 0)
1166                  return(-1);
1167          return(diff > 0);
1168   }
1169  
1170 < #if 1
1170 >
1171   static int
1172   avlmemi(                                /* find list position from address */
1173          AMBVAL  *avaddr
1174   )
1175   {
1176 <        register AMBVAL  **avlpp;
1176 >        AMBVAL  **avlpp;
1177  
1178 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1178 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1179                          nambvals, sizeof(AMBVAL *), aposcmp);
1180          if (avlpp == NULL)
1181                  error(CONSISTENCY, "address not found in avlmemi");
1182          return(avlpp - avlist2);
1183   }
735 #else
736 #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
737                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
738 #endif
1184  
1185  
1186   static void
# Line 745 | Line 1190 | sortambvals(                   /* resort ambient values */
1190   {
1191          AMBTREE  oldatrunk;
1192          AMBVAL  tav, *tap, *pnext;
1193 <        register int    i, j;
1193 >        int     i, j;
1194                                          /* see if it's time yet */
1195          if (!always && (ambclock++ < lastsort+sortintvl ||
1196                          nambvals < SORT_THRESH))
# Line 774 | Line 1219 | sortambvals(                   /* resort ambient values */
1219          }
1220          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1221                  if (avlist2 != NULL)
1222 <                        free((void *)avlist2);
1222 >                        free(avlist2);
1223                  if (always) {           /* rebuild without sorting */
1224                          oldatrunk = atrunk;
1225                          atrunk.alist = NULL;
# Line 801 | Line 1246 | sortambvals(                   /* resort ambient values */
1246                  if (i_avlist < nambvals)
1247                          error(CONSISTENCY, "missing ambient values in sortambvals");
1248   #endif
1249 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1250 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1249 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1250 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1251                  for (i = 0; i < nambvals; i++) {
1252                          if (avlist1[i].p == NULL)
1253                                  continue;
# Line 818 | Line 1263 | sortambvals(                   /* resort ambient values */
1263                          avinsert(avlist2[j]);
1264                          avlist1[j].p = NULL;
1265                  }
1266 <                free((void *)avlist1);
1267 <                free((void *)avlist2);
1266 >                free(avlist1);
1267 >                free(avlist2);
1268                                                  /* compute new sort interval */
1269                  sortintvl = ambclock - lastsort;
1270                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 845 | Line 1290 | aflock(                        /* lock/unlock ambient file */
1290   {
1291          static struct flock  fls;       /* static so initialized to zeroes */
1292  
1293 +        if (typ == fls.l_type)          /* already called? */
1294 +                return;
1295          fls.l_type = typ;
1296          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1297                  error(SYSTEM, "cannot (un)lock ambient file");
1298   }
1299  
1300  
1301 < extern int
1301 > int
1302   ambsync(void)                   /* synchronize ambient file */
1303   {
1304          long  flen;
1305          AMBVAL  avs;
1306 <        register int  n;
1306 >        int  n;
1307  
1308 <        if (nunflshed == 0)
1308 >        if (ambfp == NULL)      /* no ambient file? */
1309                  return(0);
1310 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
1311 <                goto syncend;
865 <                                /* gain exclusive access */
866 <        aflock(F_WRLCK);
1310 >                                /* gain appropriate access */
1311 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
1312                                  /* see if file has grown */
1313          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1314                  goto seekerr;
1315 <        if ( (n = flen - lastpos) ) {           /* file has grown */
1315 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
1316                  if (ambinp == NULL) {           /* use duplicate filedes */
1317                          ambinp = fdopen(dup(fileno(ambfp)), "r");
1318                          if (ambinp == NULL)
1319                                  error(SYSTEM, "fdopen failed in ambsync");
1320                  }
1321 <                if (fseek(ambinp, lastpos, 0) < 0)
1321 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1322                          goto seekerr;
1323                  while (n >= AMBVALSIZ) {        /* load contributed values */
1324                          if (!readambval(&avs, ambinp)) {
# Line 883 | Line 1328 | ambsync(void)                  /* synchronize ambient file */
1328                                  error(WARNING, errmsg);
1329                                  break;
1330                          }
1331 <                        avinsert(avstore(&avs));
1331 >                        avstore(&avs);
1332                          n -= AMBVALSIZ;
1333                  }
1334 +                lastpos = flen - n;
1335                  /*** seek always as safety measure
1336                  if (n) ***/                     /* alignment */
1337 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
1337 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1338                                  goto seekerr;
1339          }
894 #ifdef  DEBUG
895        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
896                sprintf(errmsg, "ambient file buffer at %d rather than %d",
897                                ambfp->_ptr - ambfp->_base,
898                                nunflshed*AMBVALSIZ);
899                error(CONSISTENCY, errmsg);
900        }
901 #endif
902 syncend:
1340          n = fflush(ambfp);                      /* calls write() at last */
1341 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1341 >        if (n != EOF)
1342 >                lastpos += (long)nunflshed*AMBVALSIZ;
1343 >        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1344                  goto seekerr;
1345 +                
1346          aflock(F_UNLCK);                        /* release file */
1347          nunflshed = 0;
1348          return(n);
# Line 911 | Line 1351 | seekerr:
1351          return -1; /* pro forma return */
1352   }
1353  
1354 < #else
1354 > #else   /* ! F_SETLKW */
1355  
1356 < extern int
1356 > int
1357   ambsync(void)                   /* flush ambient file */
1358   {
1359 <        if (nunflshed == 0)
1359 >        if (ambfp == NULL)
1360                  return(0);
1361          nunflshed = 0;
1362          return(fflush(ambfp));
1363   }
1364  
1365 < #endif
1365 > #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines