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.60 by greg, Sat May 28 22:27:54 2005 UTC vs.
Revision 2.73 by greg, Wed Apr 16 20:32:00 2014 UTC

# Line 25 | Line 25 | static const char      RCSid[] = "$Id$";
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27   #ifndef  MAXASET
28 < #define  MAXASET        2047    /* maximum number of elements in ambient set */
28 > #define  MAXASET        4095    /* maximum number of elements in ambient set */
29   #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
# Line 39 | Line 39 | static int  nunflshed = 0;     /* number of unflushed ambi
39  
40   #ifndef SORT_THRESH
41   #ifdef SMLMEM
42 < #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
42 > #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
43   #else
44 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
44 > #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
45   #endif
46   #endif
47   #ifndef SORT_INTVL
# Line 84 | Line 84 | static AMBVAL *avstore(AMBVAL  *aval);
84   static AMBTREE *newambtree(void);
85   static void freeambtree(AMBTREE  *atp);
86  
87 < typedef void unloadtf_t(void *);
87 > typedef void unloadtf_t(AMBVAL *);
88   static unloadtf_t avinsert;
89   static unloadtf_t av2list;
90 + static unloadtf_t avfree;
91   static void unloadatree(AMBTREE  *at, unloadtf_t *f);
92  
93   static int aposcmp(const void *avp1, const void *avp2);
# Line 98 | Line 99 | static void aflock(int  typ);
99   #endif
100  
101  
102 < extern void
102 > void
103   setambres(                              /* set ambient resolution */
104          int  ar
105   )
# Line 121 | Line 122 | setambres(                             /* set ambient resolution */
122   }
123  
124  
125 < extern void
125 > void
126   setambacc(                              /* set ambient accuracy */
127          double  newa
128   )
# Line 136 | Line 137 | setambacc(                             /* set ambient accuracy */
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 160 | 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));
167                  nambshare = nambvals;           /* share loaded values */
# Line 174 | 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);
185 >                        fseek(ambfp, lastpos, SEEK_SET);
186   #ifndef _WIN32 /* XXX we need a replacement for that one */
187 <                        ftruncate(fileno(ambfp), (off_t)pos);
187 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
188   #endif
189                  }
190          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
191                  initambfile(1);                 /* else create new file */
192 +                fflush(ambfp);
193 +                lastpos = ftell(ambfp);
194          } else {
195                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
196                  error(SYSTEM, errmsg);
197          }
198 <        nunflshed++;    /* lie */
199 <        ambsync();
198 > #ifdef getc_unlocked
199 >        flockfile(ambfp);                       /* application-level lock */
200 > #endif
201 > #ifdef  F_SETLKW
202 >        aflock(F_UNLCK);                        /* release file */
203 > #endif
204   }
205  
206  
207 < extern void
207 > void
208   ambdone(void)                   /* close ambient file and free memory */
209   {
210          if (ambfp != NULL) {            /* close ambient file */
# Line 211 | Line 218 | ambdone(void)                  /* close ambient file and free memory
218                  lastpos = -1;
219          }
220                                          /* free ambient tree */
221 <        unloadatree(&atrunk, free);
221 >        unloadatree(&atrunk, &avfree);
222                                          /* reset state variables */
223          avsum = 0.;
224          navsum = 0;
# Line 223 | Line 230 | ambdone(void)                  /* close ambient file and free memory
230   }
231  
232  
233 < extern void
233 > void
234   ambnotify(                      /* record new modifier */
235          OBJECT  obj
236   )
237   {
238          static int  hitlimit = 0;
239 <        register OBJREC  *o;
240 <        register char  **amblp;
239 >        OBJREC   *o;
240 >        char  **amblp;
241  
242          if (obj == OVOID) {             /* starting over */
243                  ambset[0] = 0;
# Line 252 | Line 259 | ambnotify(                     /* record new modifier */
259                  }
260   }
261  
262 + /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
263  
264 < extern void
264 > #ifdef NEWAMB
265 >
266 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
267 >
268 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
269 >                                AMBTREE *at, FVECT c0, double s);
270 > static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
271 > static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
272 >                                FVECT uvw[3]);
273 >
274 > void
275   multambient(            /* compute ambient component & multiply by coef. */
276          COLOR  aval,
277 <        register RAY  *r,
277 >        RAY  *r,
278          FVECT  nrm
279   )
280   {
281          static int  rdepth = 0;                 /* ambient recursion */
282          COLOR   acol;
283 +        int     ok;
284          double  d, l;
285  
286          if (ambdiv <= 0)                        /* no ambient calculation */
# Line 275 | Line 294 | multambient(           /* compute ambient component & multiply
294                  goto dumbamb;
295  
296          if (ambacc <= FTINY) {                  /* no ambient storage */
297 +                copycolor(acol, aval);
298                  rdepth++;
299 <                d = doambient(acol, r, aval, intens(aval)*r->rweight,
280 <                                                        NULL, NULL);
299 >                ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL);
300                  rdepth--;
301 <                if (d <= FTINY)
301 >                if (!ok)
302                          goto dumbamb;
303 +                copycolor(aval, acol);
304 +                return;
305 +        }
306 +
307 +        if (tracktime)                          /* sort to minimize thrashing */
308 +                sortambvals(0);
309 +                                                /* interpolate ambient value */
310 +        setcolor(acol, 0.0, 0.0, 0.0);
311 +        d = sumambient(acol, r, nrm, rdepth,
312 +                        &atrunk, thescene.cuorg, thescene.cusize);
313 +        if (d > FTINY) {
314 +                d = 1.0/d;
315 +                scalecolor(acol, d);
316                  multcolor(aval, acol);
317                  return;
318          }
319 +        rdepth++;                               /* need to cache new value */
320 +        ok = makeambient(acol, r, nrm, rdepth-1);
321 +        rdepth--;
322 +        if (ok) {
323 +                multcolor(aval, acol);          /* computed new value */
324 +                return;
325 +        }
326 + dumbamb:                                        /* return global value */
327 +        if ((ambvwt <= 0) | (navsum == 0)) {
328 +                multcolor(aval, ambval);
329 +                return;
330 +        }
331 +        l = bright(ambval);                     /* average in computations */
332 +        if (l > FTINY) {
333 +                d = (log(l)*(double)ambvwt + avsum) /
334 +                                (double)(ambvwt + navsum);
335 +                d = exp(d) / l;
336 +                scalecolor(aval, d);
337 +                multcolor(aval, ambval);        /* apply color of ambval */
338 +        } else {
339 +                d = exp( avsum / (double)navsum );
340 +                scalecolor(aval, d);            /* neutral color */
341 +        }
342 + }
343  
344 +
345 + double
346 + sumambient(             /* get interpolated ambient value */
347 +        COLOR  acol,
348 +        RAY  *r,
349 +        FVECT  rn,
350 +        int  al,
351 +        AMBTREE  *at,
352 +        FVECT  c0,
353 +        double  s
354 + )
355 + {                                       /* initial limit is ambacc radians */
356 +        const double    maxangle = (ambacc-PI/2.)*pow(r->rweight,0.13) + PI/2.;
357 +        double          wsum = 0.0;
358 +        FVECT           ck0;
359 +        int             i, j;
360 +        AMBVAL          *av;
361 +                                        /* sum this node */
362 +        for (av = at->alist; av != NULL; av = av->next) {
363 +                double  d, delta_r2, delta_t2;
364 +                COLOR   ct;
365 +                FVECT   uvw[3];
366 +                                        /* record access */
367 +                if (tracktime)
368 +                        av->latick = ambclock;
369 +                /*
370 +                 *  Ambient level test
371 +                 */
372 +                if (av->lvl > al)       /* list sorted, so this works */
373 +                        break;
374 +                if (av->weight < 0.9*r->rweight)
375 +                        continue;
376 +                /*
377 +                 *  Direction test using unperturbed normal
378 +                 */
379 +                decodedir(uvw[2], av->ndir);
380 +                d = DOT(uvw[2], r->ron);
381 +                if (d <= 0.0)           /* >= 90 degrees */
382 +                        continue;
383 +                delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
384 +                if (delta_r2 >= maxangle*maxangle)
385 +                        continue;
386 +                /*
387 +                 *  Elliptical radii test based on Hessian
388 +                 */
389 +                decodedir(uvw[0], av->udir);
390 +                VCROSS(uvw[1], uvw[2], uvw[0]);
391 +                VSUB(ck0, av->pos, r->rop);
392 +                d = DOT(ck0, uvw[0]) / av->rad[0];
393 +                delta_t2 = d*d;
394 +                d = DOT(ck0, uvw[1]) / av->rad[1];
395 +                delta_t2 += d*d;
396 +                if (delta_t2 >= ambacc*ambacc)
397 +                        continue;
398 +                /*
399 +                 *  Intersection behind test
400 +                 */
401 +                d = 0.0;
402 +                for (j = 0; j < 3; j++)
403 +                        d += (r->rop[j] - av->pos[j])*(uvw[2][j] + r->ron[j]);
404 +                if (d*0.5 < -minarad*ambacc-.001)
405 +                        continue;
406 +                /*
407 +                 *  Extrapolate value and compute final weight (hat function)
408 +                 */
409 +                extambient(ct, av, r->rop, rn, uvw);
410 +                d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
411 +                        tfunc(ambacc, sqrt(delta_t2), 0.0);
412 +                scalecolor(ct, d);
413 +                addcolor(acol, ct);
414 +                wsum += d;
415 +        }
416 +        if (at->kid == NULL)
417 +                return(wsum);
418 +                                        /* sum children */
419 +        s *= 0.5;
420 +        for (i = 0; i < 8; i++) {
421 +                for (j = 0; j < 3; j++) {
422 +                        ck0[j] = c0[j];
423 +                        if (1<<j & i)
424 +                                ck0[j] += s;
425 +                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
426 +                                break;
427 +                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
428 +                                break;
429 +                }
430 +                if (j == 3)
431 +                        wsum += sumambient(acol, r, rn, al,
432 +                                                at->kid+i, ck0, s);
433 +        }
434 +        return(wsum);
435 + }
436 +
437 +
438 + int
439 + makeambient(            /* make a new ambient value for storage */
440 +        COLOR  acol,
441 +        RAY  *r,
442 +        FVECT  rn,
443 +        int  al
444 + )
445 + {
446 +        AMBVAL  amb;
447 +        FVECT   uvw[3];
448 +        int     i;
449 +
450 +        amb.weight = 1.0;                       /* compute weight */
451 +        for (i = al; i-- > 0; )
452 +                amb.weight *= AVGREFL;
453 +        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
454 +                amb.weight = 1.25*r->rweight;
455 +        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
456 +                                                /* compute ambient */
457 +        i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir);
458 +        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
459 +        if (i <= 0)                             /* no Hessian => no storage */
460 +                return(i);
461 +                                                /* store value */
462 +        VCOPY(amb.pos, r->rop);
463 +        amb.ndir = encodedir(r->ron);
464 +        amb.udir = encodedir(uvw[0]);
465 +        amb.lvl = al;
466 +        copycolor(amb.val, acol);
467 +                                                /* insert into tree */
468 +        avsave(&amb);                           /* and save to file */
469 +        if (rn != r->ron) {                     /* texture */
470 +                VCOPY(uvw[2], r->ron);
471 +                extambient(acol, &amb, r->rop, rn, uvw);
472 +        }
473 +        return(1);
474 + }
475 +
476 +
477 + void
478 + extambient(             /* extrapolate value at pv, nv */
479 +        COLOR  cr,
480 +        AMBVAL   *ap,
481 +        FVECT  pv,
482 +        FVECT  nv,
483 +        FVECT  uvw[3]
484 + )
485 + {
486 +        static FVECT    my_uvw[3];
487 +        FVECT           v1;
488 +        int             i;
489 +        double          d = 1.0;        /* zeroeth order */
490 +
491 +        if (uvw == NULL) {              /* need local coordinates? */
492 +                decodedir(my_uvw[2], ap->ndir);
493 +                decodedir(my_uvw[0], ap->udir);
494 +                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
495 +                uvw = my_uvw;
496 +        }
497 +        for (i = 3; i--; )              /* gradient due to translation */
498 +                d += (pv[i] - ap->pos[i]) *
499 +                        (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
500 +
501 +        VCROSS(v1, uvw[2], nv);         /* gradient due to rotation */
502 +        for (i = 3; i--; )
503 +                d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
504 +        
505 +        if (d <= 0.0) {
506 +                setcolor(cr, 0.0, 0.0, 0.0);
507 +                return;
508 +        }
509 +        copycolor(cr, ap->val);
510 +        scalecolor(cr, d);
511 + }
512 +
513 +
514 + static void
515 + avinsert(                               /* insert ambient value in our tree */
516 +        AMBVAL *av
517 + )
518 + {
519 +        AMBTREE  *at;
520 +        AMBVAL  *ap;
521 +        AMBVAL  avh;
522 +        FVECT  ck0;
523 +        double  s;
524 +        int  branch;
525 +        int  i;
526 +
527 +        if (av->rad[0] <= FTINY)
528 +                error(CONSISTENCY, "zero ambient radius in avinsert");
529 +        at = &atrunk;
530 +        VCOPY(ck0, thescene.cuorg);
531 +        s = thescene.cusize;
532 +        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
533 +                if (at->kid == NULL)
534 +                        if ((at->kid = newambtree()) == NULL)
535 +                                error(SYSTEM, "out of memory in avinsert");
536 +                s *= 0.5;
537 +                branch = 0;
538 +                for (i = 0; i < 3; i++)
539 +                        if (av->pos[i] > ck0[i] + s) {
540 +                                ck0[i] += s;
541 +                                branch |= 1 << i;
542 +                        }
543 +                at = at->kid + branch;
544 +        }
545 +        avh.next = at->alist;           /* order by increasing level */
546 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
547 +                if (ap->next->lvl >= av->lvl)
548 +                        break;
549 +        av->next = ap->next;
550 +        ap->next = (AMBVAL*)av;
551 +        at->alist = avh.next;
552 + }
553 +
554 +
555 + #else /* ! NEWAMB */
556 +
557 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
558 +                                AMBTREE *at, FVECT c0, double s);
559 + static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
560 + static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
561 +
562 +
563 + void
564 + multambient(            /* compute ambient component & multiply by coef. */
565 +        COLOR  aval,
566 +        RAY  *r,
567 +        FVECT  nrm
568 + )
569 + {
570 +        static int  rdepth = 0;                 /* ambient recursion */
571 +        COLOR   acol;
572 +        double  d, l;
573 +
574 +        if (ambdiv <= 0)                        /* no ambient calculation */
575 +                goto dumbamb;
576 +                                                /* check number of bounces */
577 +        if (rdepth >= ambounce)
578 +                goto dumbamb;
579 +                                                /* check ambient list */
580 +        if (ambincl != -1 && r->ro != NULL &&
581 +                        ambincl != inset(ambset, r->ro->omod))
582 +                goto dumbamb;
583 +
584 +        if (ambacc <= FTINY) {                  /* no ambient storage */
585 +                copycolor(acol, aval);
586 +                rdepth++;
587 +                d = doambient(acol, r, r->rweight, NULL, NULL);
588 +                rdepth--;
589 +                if (d <= FTINY)
590 +                        goto dumbamb;
591 +                copycolor(aval, acol);
592 +                return;
593 +        }
594 +
595          if (tracktime)                          /* sort to minimize thrashing */
596                  sortambvals(0);
597 <                                                /* get ambient value */
597 >                                                /* interpolate ambient value */
598          setcolor(acol, 0.0, 0.0, 0.0);
599 <        d = sumambient(acol, r, intens(aval)*r->rweight, nrm, rdepth,
599 >        d = sumambient(acol, r, nrm, rdepth,
600                          &atrunk, thescene.cuorg, thescene.cusize);
601          if (d > FTINY) {
602 <                scalecolor(acol, 1.0/d);
602 >                d = 1.0/d;
603 >                scalecolor(acol, d);
604                  multcolor(aval, acol);
605                  return;
606          }
607          rdepth++;                               /* need to cache new value */
608 <        d = makeambient(acol, r, aval, nrm, rdepth-1);
608 >        d = makeambient(acol, r, nrm, rdepth-1);
609          rdepth--;
610          if (d > FTINY) {
611                  multcolor(aval, acol);          /* got new value */
# Line 322 | Line 630 | dumbamb:                                       /* return global value */
630   }
631  
632  
633 < extern double
633 > static double
634   sumambient(     /* get interpolated ambient value */
635          COLOR  acol,
636 <        register RAY  *r,
329 <        double  aw,
636 >        RAY  *r,
637          FVECT  rn,
638          int  al,
639          AMBTREE  *at,
# Line 338 | Line 645 | sumambient(    /* get interpolated ambient value */
645          COLOR  ct;
646          FVECT  ck0;
647          int  i;
648 <        register int  j;
649 <        register AMBVAL  *av;
648 >        int  j;
649 >        AMBVAL   *av;
650  
651          wsum = 0.0;
652                                          /* do this node */
# Line 352 | Line 659 | sumambient(    /* get interpolated ambient value */
659                   */
660                  if (av->lvl > al)       /* list sorted, so this works */
661                          break;
662 <                if (av->weight < 0.85*aw)
662 >                if (av->weight < 0.9*r->rweight)
663                          continue;
664                  /*
665                   *  Ambient radius test.
666                   */
667 <                d = av->pos[0] - r->rop[0];
668 <                e1 = d * d;
362 <                d = av->pos[1] - r->rop[1];
363 <                e1 += d * d;
364 <                d = av->pos[2] - r->rop[2];
365 <                e1 += d * d;
366 <                e1 /= av->rad * av->rad;
667 >                VSUB(ck0, av->pos, r->rop);
668 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
669                  if (e1 > ambacc*ambacc*1.21)
670                          continue;
671                  /*
# Line 380 | Line 682 | sumambient(    /* get interpolated ambient value */
682                          }
683                  }
684                  e2 = (1.0 - d) * r->rweight;
685 <                if (e2 < 0.0) e2 = 0.0;
686 <                if (e1 + e2 > ambacc*ambacc*1.21)
685 >                if (e2 < 0.0)
686 >                        e2 = 0.0;
687 >                else if (e1 + e2 > ambacc*ambacc*1.21)
688                          continue;
689                  /*
690                   *  Ray behind test.
# Line 431 | Line 734 | sumambient(    /* get interpolated ambient value */
734                                  break;
735                  }
736                  if (j == 3)
737 <                        wsum += sumambient(acol, r, aw, rn, al,
737 >                        wsum += sumambient(acol, r, rn, al,
738                                                  at->kid+i, ck0, s);
739          }
740          return(wsum);
741   }
742  
743  
744 < extern double
745 < makeambient(            /* make a new ambient value */
744 > static double
745 > makeambient(            /* make a new ambient value for storage */
746          COLOR  acol,
747          RAY  *r,
445        COLOR  ac,
748          FVECT  rn,
749          int  al
750   )
751   {
752          AMBVAL  amb;
451        double  awt;
753          FVECT   gp, gd;
754          int     i;
755  
756 <        amb.weight = AVGREFL;                   /* compute weight */
757 <        for (i = al; i-- >= 0; )
756 >        amb.weight = 1.0;                       /* compute weight */
757 >        for (i = al; i-- > 0; )
758                  amb.weight *= AVGREFL;
759 <        awt = intens(ac)*r->rweight;
760 <        if (awt < 0.07*amb.weight)              /* heuristic override */
761 <                amb.weight = 1.2*awt;
759 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
760 >                amb.weight = 1.25*r->rweight;
761 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
762                                                  /* compute ambient */
763 <        amb.rad = doambient(acol, r, ac, amb.weight, gp, gd);
764 <        if (amb.rad <= FTINY)
763 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
764 >        if (amb.rad <= FTINY) {
765 >                setcolor(acol, 0.0, 0.0, 0.0);
766                  return(0.0);
767 <                                                /* store it */
767 >        }
768 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
769 >                                                /* store value */
770          VCOPY(amb.pos, r->rop);
771          VCOPY(amb.dir, r->ron);
772          amb.lvl = al;
# Line 477 | Line 781 | makeambient(           /* make a new ambient value */
781   }
782  
783  
784 < extern void
784 > static void
785   extambient(             /* extrapolate value at pv, nv */
786          COLOR  cr,
787 <        register AMBVAL  *ap,
787 >        AMBVAL   *ap,
788          FVECT  pv,
789          FVECT  nv
790   )
791   {
792          FVECT  v1;
793 <        register int  i;
793 >        int  i;
794          double  d;
795  
796          d = 1.0;                        /* zeroeth order */
# Line 506 | Line 810 | extambient(            /* extrapolate value at pv, nv */
810  
811  
812   static void
813 + avinsert(                               /* insert ambient value in our tree */
814 +        AMBVAL *av
815 + )
816 + {
817 +        AMBTREE  *at;
818 +        AMBVAL  *ap;
819 +        AMBVAL  avh;
820 +        FVECT  ck0;
821 +        double  s;
822 +        int  branch;
823 +        int  i;
824 +
825 +        if (av->rad <= FTINY)
826 +                error(CONSISTENCY, "zero ambient radius in avinsert");
827 +        at = &atrunk;
828 +        VCOPY(ck0, thescene.cuorg);
829 +        s = thescene.cusize;
830 +        while (s*(OCTSCALE/2) > av->rad*ambacc) {
831 +                if (at->kid == NULL)
832 +                        if ((at->kid = newambtree()) == NULL)
833 +                                error(SYSTEM, "out of memory in avinsert");
834 +                s *= 0.5;
835 +                branch = 0;
836 +                for (i = 0; i < 3; i++)
837 +                        if (av->pos[i] > ck0[i] + s) {
838 +                                ck0[i] += s;
839 +                                branch |= 1 << i;
840 +                        }
841 +                at = at->kid + branch;
842 +        }
843 +        avh.next = at->alist;           /* order by increasing level */
844 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
845 +                if (ap->next->lvl >= av->lvl)
846 +                        break;
847 +        av->next = ap->next;
848 +        ap->next = (AMBVAL*)av;
849 +        at->alist = avh.next;
850 + }
851 +
852 + #endif  /* ! NEWAMB */
853 +
854 + /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
855 +
856 + static void
857   initambfile(            /* initialize ambient file */
858 <        int  creat
858 >        int  cre8
859   )
860   {
861          extern char  *progname, *octname;
862          static char  *mybuf = NULL;
863  
864   #ifdef  F_SETLKW
865 <        aflock(creat ? F_WRLCK : F_RDLCK);
865 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
866   #endif
867          SET_FILE_BINARY(ambfp);
868          if (mybuf == NULL)
869                  mybuf = (char *)bmalloc(BUFSIZ+8);
870          setbuf(ambfp, mybuf);
871 <        if (creat) {                    /* new file */
871 >        if (cre8) {                     /* new file */
872                  newheader("RADIANCE", ambfp);
873                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
874                                  progname, colval(ambval,RED),
# Line 529 | Line 877 | initambfile(           /* initialize ambient file */
877                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
878                                  ambdiv, ambssamp, ambres);
879                  if (octname != NULL)
880 <                        printargs(1, &octname, ambfp);
881 <                else
534 <                        fputc('\n', ambfp);
880 >                        fputs(octname, ambfp);
881 >                fputc('\n', ambfp);
882                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
883                  fputnow(ambfp);
884                  fputformat(AMBFMT, ambfp);
885 <                putc('\n', ambfp);
885 >                fputc('\n', ambfp);
886                  putambmagic(ambfp);
887          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
888                  error(USER, "bad ambient file");
# Line 563 | Line 910 | writerr:
910  
911   static AMBVAL *
912   avstore(                                /* allocate memory and store aval */
913 <        register AMBVAL  *aval
913 >        AMBVAL  *aval
914   )
915   {
916 <        register AMBVAL  *av;
916 >        AMBVAL  *av;
917          double  d;
918  
919          if ((av = newambval()) == NULL)
# Line 592 | Line 939 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
939   static AMBTREE *
940   newambtree(void)                                /* allocate 8 ambient tree structs */
941   {
942 <        register AMBTREE  *atp, *upperlim;
942 >        AMBTREE  *atp, *upperlim;
943  
944          if (atfreelist == NULL) {       /* get more nodes */
945                  atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
# Line 622 | Line 969 | freeambtree(                   /* free 8 ambient tree structs */
969  
970  
971   static void
625 avinsert(                               /* insert ambient value in our tree */
626        void *av
627 )
628 {
629        register AMBTREE  *at;
630        register AMBVAL  *ap;
631        AMBVAL  avh;
632        FVECT  ck0;
633        double  s;
634        int  branch;
635        register int  i;
636
637        if (((AMBVAL*)av)->rad <= FTINY)
638                error(CONSISTENCY, "zero ambient radius in avinsert");
639        at = &atrunk;
640        VCOPY(ck0, thescene.cuorg);
641        s = thescene.cusize;
642        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
643                if (at->kid == NULL)
644                        if ((at->kid = newambtree()) == NULL)
645                                error(SYSTEM, "out of memory in avinsert");
646                s *= 0.5;
647                branch = 0;
648                for (i = 0; i < 3; i++)
649                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
650                                ck0[i] += s;
651                                branch |= 1 << i;
652                        }
653                at = at->kid + branch;
654        }
655        avh.next = at->alist;           /* order by increasing level */
656        for (ap = &avh; ap->next != NULL; ap = ap->next)
657                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
658                        break;
659        ((AMBVAL*)av)->next = ap->next;
660        ap->next = (AMBVAL*)av;
661        at->alist = avh.next;
662 }
663
664
665 static void
972   unloadatree(                    /* unload an ambient value tree */
973 <        register AMBTREE  *at,
973 >        AMBTREE  *at,
974          unloadtf_t *f
975   )
976   {
977 <        register AMBVAL  *av;
978 <        register int  i;
977 >        AMBVAL  *av;
978 >        int  i;
979                                          /* transfer values at this node */
980          for (av = at->alist; av != NULL; av = at->alist) {
981                  at->alist = av->next;
# Line 694 | Line 1000 | static int     i_avlist;               /* index for lists */
1000   static int alatcmp(const void *av1, const void *av2);
1001  
1002   static void
1003 + avfree(AMBVAL *av)
1004 + {
1005 +        free(av);
1006 + }
1007 +
1008 + static void
1009   av2list(
1010 <        void *av
1010 >        AMBVAL *av
1011   )
1012   {
1013   #ifdef DEBUG
# Line 703 | Line 1015 | av2list(
1015                  error(CONSISTENCY, "too many ambient values in av2list1");
1016   #endif
1017          avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1018 <        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
1018 >        avlist1[i_avlist++].t = av->latick;
1019   }
1020  
1021  
# Line 713 | Line 1025 | alatcmp(                       /* compare ambient values for MRA */
1025          const void *av2
1026   )
1027   {
1028 <        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1028 >        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1029          return(lc<0 ? -1 : lc>0 ? 1 : 0);
1030   }
1031  
# Line 730 | Line 1042 | aposcmp(                       /* compare ambient value positions */
1042          const void      *avp2
1043   )
1044   {
1045 <        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
1045 >        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1046          if (diff < 0)
1047                  return(-1);
1048          return(diff > 0);
1049   }
1050  
1051 < #if 1
1051 >
1052   static int
1053   avlmemi(                                /* find list position from address */
1054          AMBVAL  *avaddr
1055   )
1056   {
1057 <        register AMBVAL  **avlpp;
1057 >        AMBVAL  **avlpp;
1058  
1059          avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1060 <                        nambvals, sizeof(AMBVAL *), aposcmp);
1060 >                        nambvals, sizeof(AMBVAL *), &aposcmp);
1061          if (avlpp == NULL)
1062                  error(CONSISTENCY, "address not found in avlmemi");
1063          return(avlpp - avlist2);
1064   }
753 #else
754 #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
755                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
756 #endif
1065  
1066  
1067   static void
# Line 763 | Line 1071 | sortambvals(                   /* resort ambient values */
1071   {
1072          AMBTREE  oldatrunk;
1073          AMBVAL  tav, *tap, *pnext;
1074 <        register int    i, j;
1074 >        int     i, j;
1075                                          /* see if it's time yet */
1076          if (!always && (ambclock++ < lastsort+sortintvl ||
1077                          nambvals < SORT_THRESH))
# Line 797 | Line 1105 | sortambvals(                   /* resort ambient values */
1105                          oldatrunk = atrunk;
1106                          atrunk.alist = NULL;
1107                          atrunk.kid = NULL;
1108 <                        unloadatree(&oldatrunk, avinsert);
1108 >                        unloadatree(&oldatrunk, &avinsert);
1109                  }
1110          } else {                        /* sort memory by last access time */
1111                  /*
# Line 814 | Line 1122 | sortambvals(                   /* resort ambient values */
1122                  eputs(errmsg);
1123   #endif
1124                  i_avlist = 0;
1125 <                unloadatree(&atrunk, av2list);  /* empty current tree */
1125 >                unloadatree(&atrunk, &av2list); /* empty current tree */
1126   #ifdef DEBUG
1127                  if (i_avlist < nambvals)
1128                          error(CONSISTENCY, "missing ambient values in sortambvals");
1129   #endif
1130 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1131 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1130 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp);
1131 >                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp);
1132                  for (i = 0; i < nambvals; i++) {
1133                          if (avlist1[i].p == NULL)
1134                                  continue;
# Line 863 | Line 1171 | aflock(                        /* lock/unlock ambient file */
1171   {
1172          static struct flock  fls;       /* static so initialized to zeroes */
1173  
1174 +        if (typ == fls.l_type)          /* already called? */
1175 +                return;
1176          fls.l_type = typ;
1177          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1178                  error(SYSTEM, "cannot (un)lock ambient file");
1179   }
1180  
1181  
1182 < extern int
1182 > int
1183   ambsync(void)                   /* synchronize ambient file */
1184   {
1185          long  flen;
1186          AMBVAL  avs;
1187 <        register int  n;
1187 >        int  n;
1188  
1189 <        if (nunflshed == 0)
1189 >        if (ambfp == NULL)      /* no ambient file? */
1190                  return(0);
1191 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
1192 <                goto syncend;
883 <                                /* gain exclusive access */
884 <        aflock(F_WRLCK);
1191 >                                /* gain appropriate access */
1192 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
1193                                  /* see if file has grown */
1194          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1195                  goto seekerr;
1196 <        if ( (n = flen - lastpos) ) {           /* file has grown */
1196 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
1197                  if (ambinp == NULL) {           /* use duplicate filedes */
1198                          ambinp = fdopen(dup(fileno(ambfp)), "r");
1199                          if (ambinp == NULL)
1200                                  error(SYSTEM, "fdopen failed in ambsync");
1201                  }
1202 <                if (fseek(ambinp, lastpos, 0) < 0)
1202 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1203                          goto seekerr;
1204                  while (n >= AMBVALSIZ) {        /* load contributed values */
1205                          if (!readambval(&avs, ambinp)) {
# Line 904 | Line 1212 | ambsync(void)                  /* synchronize ambient file */
1212                          avinsert(avstore(&avs));
1213                          n -= AMBVALSIZ;
1214                  }
1215 +                lastpos = flen - n;
1216                  /*** seek always as safety measure
1217                  if (n) ***/                     /* alignment */
1218 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
1218 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1219                                  goto seekerr;
1220          }
912 #ifdef  DEBUG
913        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
914                sprintf(errmsg, "ambient file buffer at %d rather than %d",
915                                ambfp->_ptr - ambfp->_base,
916                                nunflshed*AMBVALSIZ);
917                error(CONSISTENCY, errmsg);
918        }
919 #endif
920 syncend:
1221          n = fflush(ambfp);                      /* calls write() at last */
1222 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1222 >        if (n != EOF)
1223 >                lastpos += (long)nunflshed*AMBVALSIZ;
1224 >        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1225                  goto seekerr;
1226 +                
1227          aflock(F_UNLCK);                        /* release file */
1228          nunflshed = 0;
1229          return(n);
# Line 929 | Line 1232 | seekerr:
1232          return -1; /* pro forma return */
1233   }
1234  
1235 < #else
1235 > #else   /* ! F_SETLKW */
1236  
1237 < extern int
1237 > int
1238   ambsync(void)                   /* flush ambient file */
1239   {
1240 <        if (nunflshed == 0)
1240 >        if (ambfp == NULL)
1241                  return(0);
1242          nunflshed = 0;
1243          return(fflush(ambfp));
1244   }
1245  
1246 < #endif
1246 > #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines