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.58 by greg, Tue Apr 19 01:15:06 2005 UTC vs.
Revision 2.83 by greg, Sat Apr 26 05:15:17 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 51 | Line 51 | static int  nunflshed = 0;     /* number of unflushed ambi
51   #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52   #endif
53  
54 +
55 + static double  qambacc = 0.;            /* ambient accuracy to the 1/4 power */
56   static double  avsum = 0.;              /* computed ambient value sum (log) */
57   static unsigned int  navsum = 0;        /* number of values in avsum */
58   static unsigned int  nambvals = 0;      /* total number of indirect values */
# Line 84 | Line 86 | static AMBVAL *avstore(AMBVAL  *aval);
86   static AMBTREE *newambtree(void);
87   static void freeambtree(AMBTREE  *atp);
88  
89 < typedef void unloadtf_t(void *);
89 > typedef void unloadtf_t(AMBVAL *);
90   static unloadtf_t avinsert;
91   static unloadtf_t av2list;
92 + static unloadtf_t avfree;
93   static void unloadatree(AMBTREE  *at, unloadtf_t *f);
94  
95   static int aposcmp(const void *avp1, const void *avp2);
# Line 98 | Line 101 | static void aflock(int  typ);
101   #endif
102  
103  
104 < extern void
104 > void
105   setambres(                              /* set ambient resolution */
106          int  ar
107   )
# Line 107 | Line 110 | setambres(                             /* set ambient resolution */
110                                                  /* set min & max radii */
111          if (ar <= 0) {
112                  minarad = 0;
113 <                maxarad = thescene.cusize / 2.0;
113 >                maxarad = thescene.cusize*0.5;
114          } else {
115                  minarad = thescene.cusize / ar;
116 <                maxarad = 64 * minarad;                 /* heuristic */
117 <                if (maxarad > thescene.cusize / 2.0)
118 <                        maxarad = thescene.cusize / 2.0;
116 >                maxarad = 64.0 * minarad;               /* heuristic */
117 >                if (maxarad > thescene.cusize*0.5)
118 >                        maxarad = thescene.cusize*0.5;
119          }
120          if (minarad <= FTINY)
121 <                minarad = 10*FTINY;
121 >                minarad = 10.0*FTINY;
122          if (maxarad <= minarad)
123 <                maxarad = 64 * minarad;
123 >                maxarad = 64.0 * minarad;
124   }
125  
126  
127 < extern void
127 > void
128   setambacc(                              /* set ambient accuracy */
129          double  newa
130   )
131   {
132 <        double  ambdiff;
133 <
134 <        if (newa < 0.0)
135 <                newa = 0.0;
136 <        ambdiff = fabs(newa - ambacc);
137 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
138 <                sortambvals(1);                 /* rebuild tree */
132 >        double  olda = qambacc*qambacc*qambacc*qambacc;
133 >        
134 >        newa *= (newa > 0);
135 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
136 >                qambacc = sqrt(sqrt(ambacc = newa));
137 >                if (nambvals > 0)
138 >                        sortambvals(1);         /* rebuild tree */
139 >        }
140   }
141  
142  
143 < extern void
143 > void
144   setambient(void)                                /* initialize calculation */
145   {
146          int     readonly = 0;
147 <        long  pos, flen;
147 >        long    flen;
148          AMBVAL  amb;
149                                                  /* make sure we're fresh */
150          ambdone();
# Line 160 | Line 164 | setambient(void)                               /* initialize calculation */
164                  readonly = (ambfp = fopen(ambfile, "r")) != NULL;
165          if (ambfp != NULL) {
166                  initambfile(0);                 /* file exists */
167 <                pos = ftell(ambfp);
167 >                lastpos = ftell(ambfp);
168                  while (readambval(&amb, ambfp))
169                          avinsert(avstore(&amb));
170                  nambshare = nambvals;           /* share loaded values */
# Line 174 | Line 178 | setambient(void)                               /* initialize calculation */
178                          return;                 /* avoid ambsync() */
179                  }
180                                                  /* align file pointer */
181 <                pos += (long)nambvals*AMBVALSIZ;
181 >                lastpos += (long)nambvals*AMBVALSIZ;
182                  flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
183 <                if (flen != pos) {
183 >                if (flen != lastpos) {
184                          sprintf(errmsg,
185                          "ignoring last %ld values in ambient file (corrupted)",
186 <                                        (flen - pos)/AMBVALSIZ);
186 >                                        (flen - lastpos)/AMBVALSIZ);
187                          error(WARNING, errmsg);
188 <                        fseek(ambfp, pos, 0);
188 >                        fseek(ambfp, lastpos, SEEK_SET);
189   #ifndef _WIN32 /* XXX we need a replacement for that one */
190 <                        ftruncate(fileno(ambfp), (off_t)pos);
190 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
191   #endif
192                  }
193          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
194                  initambfile(1);                 /* else create new file */
195 +                fflush(ambfp);
196 +                lastpos = ftell(ambfp);
197          } else {
198                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
199                  error(SYSTEM, errmsg);
200          }
201 <        nunflshed++;    /* lie */
202 <        ambsync();
201 > #ifdef getc_unlocked
202 >        flockfile(ambfp);                       /* application-level lock */
203 > #endif
204 > #ifdef  F_SETLKW
205 >        aflock(F_UNLCK);                        /* release file */
206 > #endif
207   }
208  
209  
210 < extern void
210 > void
211   ambdone(void)                   /* close ambient file and free memory */
212   {
213          if (ambfp != NULL) {            /* close ambient file */
# Line 211 | Line 221 | ambdone(void)                  /* close ambient file and free memory
221                  lastpos = -1;
222          }
223                                          /* free ambient tree */
224 <        unloadatree(&atrunk, free);
224 >        unloadatree(&atrunk, &avfree);
225                                          /* reset state variables */
226          avsum = 0.;
227          navsum = 0;
# Line 223 | Line 233 | ambdone(void)                  /* close ambient file and free memory
233   }
234  
235  
236 < extern void
236 > void
237   ambnotify(                      /* record new modifier */
238          OBJECT  obj
239   )
240   {
241          static int  hitlimit = 0;
242 <        register OBJREC  *o;
243 <        register char  **amblp;
242 >        OBJREC   *o;
243 >        char  **amblp;
244  
245          if (obj == OVOID) {             /* starting over */
246                  ambset[0] = 0;
# Line 252 | Line 262 | ambnotify(                     /* record new modifier */
262                  }
263   }
264  
265 + /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
266  
267 < extern void
267 > #ifdef NEWAMB
268 >
269 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
270 >
271 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
272 >                                AMBTREE *at, FVECT c0, double s);
273 > static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
274 > static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
275 >                                FVECT uvw[3]);
276 >
277 > void
278   multambient(            /* compute ambient component & multiply by coef. */
279          COLOR  aval,
280 <        register RAY  *r,
280 >        RAY  *r,
281          FVECT  nrm
282   )
283   {
284          static int  rdepth = 0;                 /* ambient recursion */
285          COLOR   acol;
286 +        int     ok;
287          double  d, l;
288  
289          if (ambdiv <= 0)                        /* no ambient calculation */
# Line 275 | Line 297 | multambient(           /* compute ambient component & multiply
297                  goto dumbamb;
298  
299          if (ambacc <= FTINY) {                  /* no ambient storage */
300 +                copycolor(acol, aval);
301                  rdepth++;
302 <                d = doambient(acol, r, aval, intens(aval)*r->rweight,
280 <                                                        NULL, NULL);
302 >                ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL);
303                  rdepth--;
304 <                if (d <= FTINY)
304 >                if (!ok)
305                          goto dumbamb;
306 +                copycolor(aval, acol);
307 +                return;
308 +        }
309 +
310 +        if (tracktime)                          /* sort to minimize thrashing */
311 +                sortambvals(0);
312 +                                                /* interpolate ambient value */
313 +        setcolor(acol, 0.0, 0.0, 0.0);
314 +        d = sumambient(acol, r, nrm, rdepth,
315 +                        &atrunk, thescene.cuorg, thescene.cusize);
316 +        if (d > FTINY) {
317 +                d = 1.0/d;
318 +                scalecolor(acol, d);
319                  multcolor(aval, acol);
320                  return;
321          }
322 +        rdepth++;                               /* need to cache new value */
323 +        ok = makeambient(acol, r, nrm, rdepth-1);
324 +        rdepth--;
325 +        if (ok) {
326 +                multcolor(aval, acol);          /* computed new value */
327 +                return;
328 +        }
329 + dumbamb:                                        /* return global value */
330 +        if ((ambvwt <= 0) | (navsum == 0)) {
331 +                multcolor(aval, ambval);
332 +                return;
333 +        }
334 +        l = bright(ambval);                     /* average in computations */
335 +        if (l > FTINY) {
336 +                d = (log(l)*(double)ambvwt + avsum) /
337 +                                (double)(ambvwt + navsum);
338 +                d = exp(d) / l;
339 +                scalecolor(aval, d);
340 +                multcolor(aval, ambval);        /* apply color of ambval */
341 +        } else {
342 +                d = exp( avsum / (double)navsum );
343 +                scalecolor(aval, d);            /* neutral color */
344 +        }
345 + }
346  
347 +
348 + double
349 + sumambient(             /* get interpolated ambient value */
350 +        COLOR  acol,
351 +        RAY  *r,
352 +        FVECT  rn,
353 +        int  al,
354 +        AMBTREE  *at,
355 +        FVECT  c0,
356 +        double  s
357 + )
358 + {                       /* initial limit is 10 degrees plus ambacc radians */
359 +        const double    minangle = 10.0 * PI/180.;
360 +        const double    maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13)
361 +                                        + PI/2.;
362 +        double          wsum = 0.0;
363 +        FVECT           ck0;
364 +        int             i, j;
365 +        AMBVAL          *av;
366 +
367 +        if (at->kid != NULL) {          /* sum children first */                                
368 +                s *= 0.5;
369 +                for (i = 0; i < 8; i++) {
370 +                        for (j = 0; j < 3; j++) {
371 +                                ck0[j] = c0[j];
372 +                                if (1<<j & i)
373 +                                        ck0[j] += s;
374 +                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
375 +                                        break;
376 +                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
377 +                                        break;
378 +                        }
379 +                        if (j == 3)
380 +                                wsum += sumambient(acol, r, rn, al,
381 +                                                        at->kid+i, ck0, s);
382 +                }
383 +                                        /* good enough? */
384 +                if (wsum > 0.04 && s > (minarad*0.8+maxarad*0.2))
385 +                        return(wsum);
386 +        }
387 +                                        /* sum this node */
388 +        for (av = at->alist; av != NULL; av = av->next) {
389 +                double  d, delta_r2, delta_t2;
390 +                COLOR   ct;
391 +                FVECT   uvw[3];
392 +                                        /* record access */
393 +                if (tracktime)
394 +                        av->latick = ambclock;
395 +                /*
396 +                 *  Ambient level test
397 +                 */
398 +                if (av->lvl > al)       /* list sorted, so this works */
399 +                        break;
400 +                if (av->weight < 0.9*r->rweight)
401 +                        continue;
402 +                /*
403 +                 *  Direction test using unperturbed normal
404 +                 */
405 +                decodedir(uvw[2], av->ndir);
406 +                d = DOT(uvw[2], r->ron);
407 +                if (d <= 0.0)           /* >= 90 degrees */
408 +                        continue;
409 +                delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
410 +                if (delta_r2 >= maxangle*maxangle)
411 +                        continue;
412 +                /*
413 +                 *  Modified ray behind test
414 +                 */
415 +                VSUB(ck0, av->pos, r->rop);
416 +                d = DOT(ck0, uvw[2]);
417 +                if (d < -minarad*qambacc-.001)
418 +                        continue;
419 +                d /= av->rad[0];
420 +                delta_t2 = d*d;
421 +                if (delta_t2 >= qambacc*qambacc)
422 +                        continue;
423 +                /*
424 +                 *  Elliptical radii test based on Hessian
425 +                 */
426 +                decodedir(uvw[0], av->udir);
427 +                VCROSS(uvw[1], uvw[2], uvw[0]);
428 +                d = DOT(ck0, uvw[0]) / av->rad[0];
429 +                delta_t2 += d*d;
430 +                d = DOT(ck0, uvw[1]) / av->rad[1];
431 +                delta_t2 += d*d;
432 +                if (delta_t2 >= qambacc*qambacc)
433 +                        continue;
434 +                /*
435 +                 *  Extrapolate value and compute final weight (hat function)
436 +                 */
437 +                extambient(ct, av, r->rop, rn, uvw);
438 +                d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
439 +                        tfunc(qambacc, sqrt(delta_t2), 0.0);
440 +                scalecolor(ct, d);
441 +                addcolor(acol, ct);
442 +                wsum += d;
443 +        }
444 +        return(wsum);
445 + }
446 +
447 +
448 + int
449 + makeambient(            /* make a new ambient value for storage */
450 +        COLOR  acol,
451 +        RAY  *r,
452 +        FVECT  rn,
453 +        int  al
454 + )
455 + {
456 +        AMBVAL  amb;
457 +        FVECT   uvw[3];
458 +        int     i;
459 +
460 +        amb.weight = 1.0;                       /* compute weight */
461 +        for (i = al; i-- > 0; )
462 +                amb.weight *= AVGREFL;
463 +        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
464 +                amb.weight = 1.25*r->rweight;
465 +        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
466 +                                                /* compute ambient */
467 +        i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir);
468 +        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
469 +        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
470 +                return(i);
471 +                                                /* store value */
472 +        VCOPY(amb.pos, r->rop);
473 +        amb.ndir = encodedir(r->ron);
474 +        amb.udir = encodedir(uvw[0]);
475 +        amb.lvl = al;
476 +        copycolor(amb.val, acol);
477 +                                                /* insert into tree */
478 +        avsave(&amb);                           /* and save to file */
479 +        if (rn != r->ron) {                     /* texture */
480 +                VCOPY(uvw[2], r->ron);
481 +                extambient(acol, &amb, r->rop, rn, uvw);
482 +        }
483 +        return(1);
484 + }
485 +
486 +
487 + void
488 + extambient(             /* extrapolate value at pv, nv */
489 +        COLOR  cr,
490 +        AMBVAL   *ap,
491 +        FVECT  pv,
492 +        FVECT  nv,
493 +        FVECT  uvw[3]
494 + )
495 + {
496 +        static FVECT    my_uvw[3];
497 +        FVECT           v1;
498 +        int             i;
499 +        double          d = 1.0;        /* zeroeth order */
500 +
501 +        if (uvw == NULL) {              /* need local coordinates? */
502 +                decodedir(my_uvw[2], ap->ndir);
503 +                decodedir(my_uvw[0], ap->udir);
504 +                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
505 +                uvw = my_uvw;
506 +        }
507 +        for (i = 3; i--; )              /* gradient due to translation */
508 +                d += (pv[i] - ap->pos[i]) *
509 +                        (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
510 +
511 +        VCROSS(v1, uvw[2], nv);         /* gradient due to rotation */
512 +        for (i = 3; i--; )
513 +                d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
514 +        
515 +        if (d <= 0.0) {
516 +                setcolor(cr, 0.0, 0.0, 0.0);
517 +                return;
518 +        }
519 +        copycolor(cr, ap->val);
520 +        scalecolor(cr, d);
521 + }
522 +
523 +
524 + static void
525 + avinsert(                               /* insert ambient value in our tree */
526 +        AMBVAL *av
527 + )
528 + {
529 +        AMBTREE  *at;
530 +        AMBVAL  *ap;
531 +        AMBVAL  avh;
532 +        FVECT  ck0;
533 +        double  s;
534 +        int  branch;
535 +        int  i;
536 +
537 +        if (av->rad[0] <= FTINY)
538 +                error(CONSISTENCY, "zero ambient radius in avinsert");
539 +        at = &atrunk;
540 +        VCOPY(ck0, thescene.cuorg);
541 +        s = thescene.cusize;
542 +        while (s*(OCTSCALE/2) > av->rad[1]*qambacc) {
543 +                if (at->kid == NULL)
544 +                        if ((at->kid = newambtree()) == NULL)
545 +                                error(SYSTEM, "out of memory in avinsert");
546 +                s *= 0.5;
547 +                branch = 0;
548 +                for (i = 0; i < 3; i++)
549 +                        if (av->pos[i] > ck0[i] + s) {
550 +                                ck0[i] += s;
551 +                                branch |= 1 << i;
552 +                        }
553 +                at = at->kid + branch;
554 +        }
555 +        avh.next = at->alist;           /* order by increasing level */
556 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
557 +                if (ap->next->lvl >= av->lvl)
558 +                        break;
559 +        av->next = ap->next;
560 +        ap->next = (AMBVAL*)av;
561 +        at->alist = avh.next;
562 + }
563 +
564 +
565 + #else /* ! NEWAMB */
566 +
567 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
568 +                                AMBTREE *at, FVECT c0, double s);
569 + static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
570 + static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
571 +
572 +
573 + void
574 + multambient(            /* compute ambient component & multiply by coef. */
575 +        COLOR  aval,
576 +        RAY  *r,
577 +        FVECT  nrm
578 + )
579 + {
580 +        static int  rdepth = 0;                 /* ambient recursion */
581 +        COLOR   acol;
582 +        double  d, l;
583 +
584 +        if (ambdiv <= 0)                        /* no ambient calculation */
585 +                goto dumbamb;
586 +                                                /* check number of bounces */
587 +        if (rdepth >= ambounce)
588 +                goto dumbamb;
589 +                                                /* check ambient list */
590 +        if (ambincl != -1 && r->ro != NULL &&
591 +                        ambincl != inset(ambset, r->ro->omod))
592 +                goto dumbamb;
593 +
594 +        if (ambacc <= FTINY) {                  /* no ambient storage */
595 +                copycolor(acol, aval);
596 +                rdepth++;
597 +                d = doambient(acol, r, r->rweight, NULL, NULL);
598 +                rdepth--;
599 +                if (d <= FTINY)
600 +                        goto dumbamb;
601 +                copycolor(aval, acol);
602 +                return;
603 +        }
604 +
605          if (tracktime)                          /* sort to minimize thrashing */
606                  sortambvals(0);
607 <                                                /* get ambient value */
607 >                                                /* interpolate ambient value */
608          setcolor(acol, 0.0, 0.0, 0.0);
609          d = sumambient(acol, r, nrm, rdepth,
610                          &atrunk, thescene.cuorg, thescene.cusize);
611          if (d > FTINY) {
612 <                scalecolor(acol, 1.0/d);
612 >                d = 1.0/d;
613 >                scalecolor(acol, d);
614                  multcolor(aval, acol);
615                  return;
616          }
617          rdepth++;                               /* need to cache new value */
618 <        d = makeambient(acol, r, aval, nrm, rdepth-1);
618 >        d = makeambient(acol, r, nrm, rdepth-1);
619          rdepth--;
620          if (d > FTINY) {
621                  multcolor(aval, acol);          /* got new value */
# Line 322 | Line 640 | dumbamb:                                       /* return global value */
640   }
641  
642  
643 < extern double
643 > static double
644   sumambient(     /* get interpolated ambient value */
645          COLOR  acol,
646 <        register RAY  *r,
646 >        RAY  *r,
647          FVECT  rn,
648          int  al,
649          AMBTREE  *at,
# Line 337 | Line 655 | sumambient(    /* get interpolated ambient value */
655          COLOR  ct;
656          FVECT  ck0;
657          int  i;
658 <        register int  j;
659 <        register AMBVAL  *av;
658 >        int  j;
659 >        AMBVAL   *av;
660  
661          wsum = 0.0;
662                                          /* do this node */
# Line 351 | Line 669 | sumambient(    /* get interpolated ambient value */
669                   */
670                  if (av->lvl > al)       /* list sorted, so this works */
671                          break;
672 <                if (av->weight < r->rweight-FTINY)
672 >                if (av->weight < 0.9*r->rweight)
673                          continue;
674                  /*
675                   *  Ambient radius test.
676                   */
677 <                d = av->pos[0] - r->rop[0];
678 <                e1 = d * d;
361 <                d = av->pos[1] - r->rop[1];
362 <                e1 += d * d;
363 <                d = av->pos[2] - r->rop[2];
364 <                e1 += d * d;
365 <                e1 /= av->rad * av->rad;
677 >                VSUB(ck0, av->pos, r->rop);
678 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
679                  if (e1 > ambacc*ambacc*1.21)
680                          continue;
681                  /*
# Line 379 | Line 692 | sumambient(    /* get interpolated ambient value */
692                          }
693                  }
694                  e2 = (1.0 - d) * r->rweight;
695 <                if (e2 < 0.0) e2 = 0.0;
696 <                if (e1 + e2 > ambacc*ambacc*1.21)
695 >                if (e2 < 0.0)
696 >                        e2 = 0.0;
697 >                else if (e1 + e2 > ambacc*ambacc*1.21)
698                          continue;
699                  /*
700                   *  Ray behind test.
# Line 430 | Line 744 | sumambient(    /* get interpolated ambient value */
744                                  break;
745                  }
746                  if (j == 3)
747 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
747 >                        wsum += sumambient(acol, r, rn, al,
748 >                                                at->kid+i, ck0, s);
749          }
750          return(wsum);
751   }
752  
753  
754 < extern double
755 < makeambient(    /* make a new ambient value */
754 > static double
755 > makeambient(            /* make a new ambient value for storage */
756          COLOR  acol,
757          RAY  *r,
443        COLOR  ac,
758          FVECT  rn,
759          int  al
760   )
761   {
762          AMBVAL  amb;
449        double  coef;
763          FVECT   gp, gd;
764 <                                                /* compute weight */
765 <        amb.weight = pow(AVGREFL, (double)al);
766 <        coef = intens(ac)*r->rweight;
767 <        if (coef < 0.1*amb.weight)              /* heuristic */
768 <                amb.weight = coef;
764 >        int     i;
765 >
766 >        amb.weight = 1.0;                       /* compute weight */
767 >        for (i = al; i-- > 0; )
768 >                amb.weight *= AVGREFL;
769 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
770 >                amb.weight = 1.25*r->rweight;
771 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
772                                                  /* compute ambient */
773 <        amb.rad = doambient(acol, r, ac, amb.weight, gp, gd);
774 <        if (amb.rad <= FTINY)
773 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
774 >        if (amb.rad <= FTINY) {
775 >                setcolor(acol, 0.0, 0.0, 0.0);
776                  return(0.0);
777 <                                                /* store it */
777 >        }
778 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
779 >                                                /* store value */
780          VCOPY(amb.pos, r->rop);
781          VCOPY(amb.dir, r->ron);
782          amb.lvl = al;
# Line 472 | Line 791 | makeambient(   /* make a new ambient value */
791   }
792  
793  
794 < extern void
794 > static void
795   extambient(             /* extrapolate value at pv, nv */
796          COLOR  cr,
797 <        register AMBVAL  *ap,
797 >        AMBVAL   *ap,
798          FVECT  pv,
799          FVECT  nv
800   )
801   {
802          FVECT  v1;
803 <        register int  i;
803 >        int  i;
804          double  d;
805  
806          d = 1.0;                        /* zeroeth order */
# Line 501 | Line 820 | extambient(            /* extrapolate value at pv, nv */
820  
821  
822   static void
823 + avinsert(                               /* insert ambient value in our tree */
824 +        AMBVAL *av
825 + )
826 + {
827 +        AMBTREE  *at;
828 +        AMBVAL  *ap;
829 +        AMBVAL  avh;
830 +        FVECT  ck0;
831 +        double  s;
832 +        int  branch;
833 +        int  i;
834 +
835 +        if (av->rad <= FTINY)
836 +                error(CONSISTENCY, "zero ambient radius in avinsert");
837 +        at = &atrunk;
838 +        VCOPY(ck0, thescene.cuorg);
839 +        s = thescene.cusize;
840 +        while (s*(OCTSCALE/2) > av->rad*ambacc) {
841 +                if (at->kid == NULL)
842 +                        if ((at->kid = newambtree()) == NULL)
843 +                                error(SYSTEM, "out of memory in avinsert");
844 +                s *= 0.5;
845 +                branch = 0;
846 +                for (i = 0; i < 3; i++)
847 +                        if (av->pos[i] > ck0[i] + s) {
848 +                                ck0[i] += s;
849 +                                branch |= 1 << i;
850 +                        }
851 +                at = at->kid + branch;
852 +        }
853 +        avh.next = at->alist;           /* order by increasing level */
854 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
855 +                if (ap->next->lvl >= av->lvl)
856 +                        break;
857 +        av->next = ap->next;
858 +        ap->next = (AMBVAL*)av;
859 +        at->alist = avh.next;
860 + }
861 +
862 + #endif  /* ! NEWAMB */
863 +
864 + /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
865 +
866 + static void
867   initambfile(            /* initialize ambient file */
868 <        int  creat
868 >        int  cre8
869   )
870   {
871          extern char  *progname, *octname;
872          static char  *mybuf = NULL;
873  
874   #ifdef  F_SETLKW
875 <        aflock(creat ? F_WRLCK : F_RDLCK);
875 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
876   #endif
877          SET_FILE_BINARY(ambfp);
878          if (mybuf == NULL)
879                  mybuf = (char *)bmalloc(BUFSIZ+8);
880          setbuf(ambfp, mybuf);
881 <        if (creat) {                    /* new file */
881 >        if (cre8) {                     /* new file */
882                  newheader("RADIANCE", ambfp);
883                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
884                                  progname, colval(ambval,RED),
# Line 524 | Line 887 | initambfile(           /* initialize ambient file */
887                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
888                                  ambdiv, ambssamp, ambres);
889                  if (octname != NULL)
890 <                        printargs(1, &octname, ambfp);
891 <                else
529 <                        fputc('\n', ambfp);
890 >                        fputs(octname, ambfp);
891 >                fputc('\n', ambfp);
892                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
893                  fputnow(ambfp);
894                  fputformat(AMBFMT, ambfp);
895 <                putc('\n', ambfp);
895 >                fputc('\n', ambfp);
896                  putambmagic(ambfp);
897          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
898                  error(USER, "bad ambient file");
# Line 558 | Line 920 | writerr:
920  
921   static AMBVAL *
922   avstore(                                /* allocate memory and store aval */
923 <        register AMBVAL  *aval
923 >        AMBVAL  *aval
924   )
925   {
926 <        register AMBVAL  *av;
926 >        AMBVAL  *av;
927          double  d;
928  
929          if ((av = newambval()) == NULL)
# Line 587 | Line 949 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
949   static AMBTREE *
950   newambtree(void)                                /* allocate 8 ambient tree structs */
951   {
952 <        register AMBTREE  *atp, *upperlim;
952 >        AMBTREE  *atp, *upperlim;
953  
954          if (atfreelist == NULL) {       /* get more nodes */
955                  atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
# Line 617 | Line 979 | freeambtree(                   /* free 8 ambient tree structs */
979  
980  
981   static void
620 avinsert(                               /* insert ambient value in our tree */
621        void *av
622 )
623 {
624        register AMBTREE  *at;
625        register AMBVAL  *ap;
626        AMBVAL  avh;
627        FVECT  ck0;
628        double  s;
629        int  branch;
630        register int  i;
631
632        if (((AMBVAL*)av)->rad <= FTINY)
633                error(CONSISTENCY, "zero ambient radius in avinsert");
634        at = &atrunk;
635        VCOPY(ck0, thescene.cuorg);
636        s = thescene.cusize;
637        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
638                if (at->kid == NULL)
639                        if ((at->kid = newambtree()) == NULL)
640                                error(SYSTEM, "out of memory in avinsert");
641                s *= 0.5;
642                branch = 0;
643                for (i = 0; i < 3; i++)
644                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
645                                ck0[i] += s;
646                                branch |= 1 << i;
647                        }
648                at = at->kid + branch;
649        }
650        avh.next = at->alist;           /* order by increasing level */
651        for (ap = &avh; ap->next != NULL; ap = ap->next)
652                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
653                        break;
654        ((AMBVAL*)av)->next = ap->next;
655        ap->next = (AMBVAL*)av;
656        at->alist = avh.next;
657 }
658
659
660 static void
982   unloadatree(                    /* unload an ambient value tree */
983 <        register AMBTREE  *at,
983 >        AMBTREE  *at,
984          unloadtf_t *f
985   )
986   {
987 <        register AMBVAL  *av;
988 <        register int  i;
987 >        AMBVAL  *av;
988 >        int  i;
989                                          /* transfer values at this node */
990          for (av = at->alist; av != NULL; av = at->alist) {
991                  at->alist = av->next;
# Line 689 | Line 1010 | static int     i_avlist;               /* index for lists */
1010   static int alatcmp(const void *av1, const void *av2);
1011  
1012   static void
1013 + avfree(AMBVAL *av)
1014 + {
1015 +        free(av);
1016 + }
1017 +
1018 + static void
1019   av2list(
1020 <        void *av
1020 >        AMBVAL *av
1021   )
1022   {
1023   #ifdef DEBUG
# Line 698 | Line 1025 | av2list(
1025                  error(CONSISTENCY, "too many ambient values in av2list1");
1026   #endif
1027          avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1028 <        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
1028 >        avlist1[i_avlist++].t = av->latick;
1029   }
1030  
1031  
# Line 708 | Line 1035 | alatcmp(                       /* compare ambient values for MRA */
1035          const void *av2
1036   )
1037   {
1038 <        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1038 >        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1039          return(lc<0 ? -1 : lc>0 ? 1 : 0);
1040   }
1041  
# Line 725 | Line 1052 | aposcmp(                       /* compare ambient value positions */
1052          const void      *avp2
1053   )
1054   {
1055 <        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
1055 >        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1056          if (diff < 0)
1057                  return(-1);
1058          return(diff > 0);
1059   }
1060  
1061 < #if 1
1061 >
1062   static int
1063   avlmemi(                                /* find list position from address */
1064          AMBVAL  *avaddr
1065   )
1066   {
1067 <        register AMBVAL  **avlpp;
1067 >        AMBVAL  **avlpp;
1068  
1069          avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1070 <                        nambvals, sizeof(AMBVAL *), aposcmp);
1070 >                        nambvals, sizeof(AMBVAL *), &aposcmp);
1071          if (avlpp == NULL)
1072                  error(CONSISTENCY, "address not found in avlmemi");
1073          return(avlpp - avlist2);
1074   }
748 #else
749 #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
750                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
751 #endif
1075  
1076  
1077   static void
# Line 758 | Line 1081 | sortambvals(                   /* resort ambient values */
1081   {
1082          AMBTREE  oldatrunk;
1083          AMBVAL  tav, *tap, *pnext;
1084 <        register int    i, j;
1084 >        int     i, j;
1085                                          /* see if it's time yet */
1086          if (!always && (ambclock++ < lastsort+sortintvl ||
1087                          nambvals < SORT_THRESH))
# Line 792 | Line 1115 | sortambvals(                   /* resort ambient values */
1115                          oldatrunk = atrunk;
1116                          atrunk.alist = NULL;
1117                          atrunk.kid = NULL;
1118 <                        unloadatree(&oldatrunk, avinsert);
1118 >                        unloadatree(&oldatrunk, &avinsert);
1119                  }
1120          } else {                        /* sort memory by last access time */
1121                  /*
# Line 809 | Line 1132 | sortambvals(                   /* resort ambient values */
1132                  eputs(errmsg);
1133   #endif
1134                  i_avlist = 0;
1135 <                unloadatree(&atrunk, av2list);  /* empty current tree */
1135 >                unloadatree(&atrunk, &av2list); /* empty current tree */
1136   #ifdef DEBUG
1137                  if (i_avlist < nambvals)
1138                          error(CONSISTENCY, "missing ambient values in sortambvals");
1139   #endif
1140 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1141 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1140 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp);
1141 >                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp);
1142                  for (i = 0; i < nambvals; i++) {
1143                          if (avlist1[i].p == NULL)
1144                                  continue;
# Line 858 | Line 1181 | aflock(                        /* lock/unlock ambient file */
1181   {
1182          static struct flock  fls;       /* static so initialized to zeroes */
1183  
1184 +        if (typ == fls.l_type)          /* already called? */
1185 +                return;
1186          fls.l_type = typ;
1187          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1188                  error(SYSTEM, "cannot (un)lock ambient file");
1189   }
1190  
1191  
1192 < extern int
1192 > int
1193   ambsync(void)                   /* synchronize ambient file */
1194   {
1195          long  flen;
1196          AMBVAL  avs;
1197 <        register int  n;
1197 >        int  n;
1198  
1199 <        if (nunflshed == 0)
1199 >        if (ambfp == NULL)      /* no ambient file? */
1200                  return(0);
1201 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
1202 <                goto syncend;
878 <                                /* gain exclusive access */
879 <        aflock(F_WRLCK);
1201 >                                /* gain appropriate access */
1202 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
1203                                  /* see if file has grown */
1204          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1205                  goto seekerr;
1206 <        if ( (n = flen - lastpos) ) {           /* file has grown */
1206 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
1207                  if (ambinp == NULL) {           /* use duplicate filedes */
1208                          ambinp = fdopen(dup(fileno(ambfp)), "r");
1209                          if (ambinp == NULL)
1210                                  error(SYSTEM, "fdopen failed in ambsync");
1211                  }
1212 <                if (fseek(ambinp, lastpos, 0) < 0)
1212 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1213                          goto seekerr;
1214                  while (n >= AMBVALSIZ) {        /* load contributed values */
1215                          if (!readambval(&avs, ambinp)) {
# Line 899 | Line 1222 | ambsync(void)                  /* synchronize ambient file */
1222                          avinsert(avstore(&avs));
1223                          n -= AMBVALSIZ;
1224                  }
1225 +                lastpos = flen - n;
1226                  /*** seek always as safety measure
1227                  if (n) ***/                     /* alignment */
1228 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
1228 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1229                                  goto seekerr;
1230          }
907 #ifdef  DEBUG
908        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
909                sprintf(errmsg, "ambient file buffer at %d rather than %d",
910                                ambfp->_ptr - ambfp->_base,
911                                nunflshed*AMBVALSIZ);
912                error(CONSISTENCY, errmsg);
913        }
914 #endif
915 syncend:
1231          n = fflush(ambfp);                      /* calls write() at last */
1232 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1232 >        if (n != EOF)
1233 >                lastpos += (long)nunflshed*AMBVALSIZ;
1234 >        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1235                  goto seekerr;
1236 +                
1237          aflock(F_UNLCK);                        /* release file */
1238          nunflshed = 0;
1239          return(n);
# Line 924 | Line 1242 | seekerr:
1242          return -1; /* pro forma return */
1243   }
1244  
1245 < #else
1245 > #else   /* ! F_SETLKW */
1246  
1247 < extern int
1247 > int
1248   ambsync(void)                   /* flush ambient file */
1249   {
1250 <        if (nunflshed == 0)
1250 >        if (ambfp == NULL)
1251                  return(0);
1252          nunflshed = 0;
1253          return(fflush(ambfp));
1254   }
1255  
1256 < #endif
1256 > #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines