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.107 by greg, Sat Feb 16 00:09:53 2019 UTC vs.
Revision 2.119 by greg, Fri Nov 17 20:02:07 2023 UTC

# Line 22 | Line 22 | static const char      RCSid[] = "$Id$";
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23   #endif
24  
25 extern char  *shm_boundary;     /* memory sharing boundary */
26
25   #ifndef  MAXASET
26   #define  MAXASET        4095    /* maximum number of elements in ambient set */
27   #endif
# Line 37 | Line 35 | static AMBTREE atrunk;         /* our ambient trunk node */
35   static FILE  *ambfp = NULL;     /* ambient file pointer */
36   static int  nunflshed = 0;      /* number of unflushed ambient values */
37  
40 #ifndef SORT_THRESH
41 #ifdef SMLMEM
42 #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
43 #else
44 #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
45 #endif
46 #endif
47 #ifndef SORT_INTVL
48 #define SORT_INTVL      (SORT_THRESH<<1)
49 #endif
50 #ifndef MAX_SORT_INTVL
51 #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52 #endif
53
54
38   static double  avsum = 0.;              /* computed ambient value sum (log) */
39   static unsigned int  navsum = 0;        /* number of values in avsum */
40   static unsigned int  nambvals = 0;      /* total number of indirect values */
41   static unsigned int  nambshare = 0;     /* number of values from file */
59 static unsigned long  ambclock = 0;     /* ambient access clock */
60 static unsigned long  lastsort = 0;     /* time of last value sort */
61 static long  sortintvl = SORT_INTVL;    /* time until next sort */
42   static FILE  *ambinp = NULL;            /* auxiliary file for input */
43   static long  lastpos = -1;              /* last flush position */
44  
65 #define MAXACLOCK       (1L<<30)        /* clock turnover value */
66        /*
67         * Track access times unless we are sharing ambient values
68         * through memory on a multiprocessor, when we want to avoid
69         * claiming our own memory (copy on write).  Go ahead anyway
70         * if more than two thirds of our values are unshared.
71         * Compile with -Dtracktime=0 to turn this code off.
72         */
73 #ifndef tracktime
74 #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
75 #endif
76
45   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
46  
47 < #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
47 > #define  AVSIZE         (sizeof(AMBVAL)-sizeof(SCOLOR)+sizeof(COLORV)*NCSAMP)
48 > #define  newambval()    (AMBVAL *)malloc(AVSIZE)
49  
50 < static void initambfile(int creat);
50 > #define  tfunc(x0, x, x1)       (((x)-(x0))/((x1)-(x0)))
51 >
52 > static void initambfile(int cre8);
53   static void avsave(AMBVAL *av);
54   static AMBVAL *avstore(AMBVAL  *aval);
55   static AMBTREE *newambtree(void);
# Line 86 | Line 57 | static void freeambtree(AMBTREE  *atp);
57  
58   typedef void unloadtf_t(AMBVAL *);
59   static unloadtf_t avinsert;
89 static unloadtf_t av2list;
60   static unloadtf_t avfree;
61   static void unloadatree(AMBTREE  *at, unloadtf_t *f);
62  
63 < static int aposcmp(const void *avp1, const void *avp2);
94 < static int avlmemi(AMBVAL *avaddr);
95 < static void sortambvals(int always);
63 > static void sortambvals(void);
64  
65 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
66 + static double   sumambient(SCOLOR acol, RAY *r, FVECT rn, int al,
67 +                                AMBTREE *at, FVECT c0, double s);
68 + static int      makeambient(SCOLOR acol, RAY *r, FVECT rn, int al);
69 + static int      extambient(SCOLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
70 +                                FVECT uvw[3]);
71 +
72   #ifdef  F_SETLKW
73   static void aflock(int  typ);
74   #endif
# Line 133 | Line 108 | setambacc(                             /* set ambient accuracy */
108          if (fabs(newa - olda) >= .05*(newa + olda)) {
109                  ambacc = newa;
110                  if (ambacc > FTINY && nambvals > 0)
111 <                        sortambvals(1);         /* rebuild tree */
111 >                        sortambvals();          /* rebuild tree */
112          }
113   }
114  
# Line 220 | Line 195 | ambdone(void)                  /* close ambient file and free memory
195          navsum = 0;
196          nambvals = 0;
197          nambshare = 0;
223        ambclock = 0;
224        lastsort = 0;
225        sortintvl = SORT_INTVL;
198   }
199  
200  
# Line 255 | Line 227 | ambnotify(                     /* record new modifier */
227                  }
228   }
229  
258 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
230  
260 #ifndef OLDAMB
261
262 #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263
264 static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266                                AMBTREE *at, FVECT c0, double s);
267 static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269                                FVECT uvw[3]);
270
231   void
232   multambient(            /* compute ambient component & multiply by coef. */
233 <        COLOR  aval,
233 >        SCOLOR  aval,
234          RAY  *r,
235          FVECT  nrm
236   )
237   {
238 +        static double  logAvgAbsorp = 1;
239          static int  rdepth = 0;                 /* ambient recursion */
240 <        COLOR   acol, caustic;
240 >        SCOLOR  acol, caustic;
241          int     i, ok;
242          double  d, l;
243  
# Line 286 | Line 247 | multambient(           /* compute ambient component & multiply
247          if (ambPmap(aval, r, rdepth))
248                  return;
249  
250 +        if (logAvgAbsorp > 0)                   /* exclude in -aw to avoid growth */
251 +                logAvgAbsorp = log(1.-AVGREFL);
252 +
253          /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
254           * map, if enabled and ray is primary, else caustic is zero.  Continue
255           * with RADIANCE ambient calculation */
256 <        copycolor(caustic, aval);
257 <        ambPmapCaustic(caustic, r, rdepth);
258 <        
256 > {/* XXX TEMPORARY */
257 >        COLOR   pmc;
258 >        scolor_color(pmc, aval);
259 >        ambPmapCaustic(pmc, r, rdepth);
260 >        setscolor(caustic, colval(pmc,RED), colval(pmc,GRN), colval(pmc,BLU));
261 > }
262          if (ambdiv <= 0)                        /* no ambient calculation */
263                  goto dumbamb;
264                                                  /* check number of bounces */
# Line 308 | Line 275 | multambient(           /* compute ambient component & multiply
275  
276                  if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
277                          dgp = dgrad;            /* compute rotational grad. */
278 <                copycolor(acol, aval);
278 >                copyscolor(acol, aval);
279                  rdepth++;
280                  ok = doambient(acol, r, r->rweight,
281                                  uvd, NULL, NULL, dgp, NULL);
# Line 322 | Line 289 | multambient(           /* compute ambient component & multiply
289                          for (i = 3; i--; )
290                                  d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
291                          if (d >= 0.05)
292 <                                scalecolor(acol, d);
292 >                                scalescolor(acol, d);
293                  }
294 <                copycolor(aval, acol);
294 >                copyscolor(aval, acol);
295  
296                  /* PMAP: add in caustic */
297 <                addcolor(aval, caustic);
297 >                saddscolor(aval, caustic);
298                  return;
299          }
333
334        if (tracktime)                          /* sort to minimize thrashing */
335                sortambvals(0);
300                                                  /* interpolate ambient value */
301 <        setcolor(acol, 0.0, 0.0, 0.0);
301 >        scolorblack(acol);
302          d = sumambient(acol, r, nrm, rdepth,
303                          &atrunk, thescene.cuorg, thescene.cusize);
304                          
305          if (d > FTINY) {
306 <                d = 1.0/d;
307 <                scalecolor(acol, d);
344 <                multcolor(aval, acol);
306 >                scalescolor(acol, 1.0/d);
307 >                smultscolor(aval, acol);
308  
309                  /* PMAP: add in caustic */
310 <                addcolor(aval, caustic);
310 >                saddscolor(aval, caustic);
311                  return;
312          }
313          
# Line 353 | Line 316 | multambient(           /* compute ambient component & multiply
316          rdepth--;
317          
318          if (ok) {
319 <                multcolor(aval, acol);          /* computed new value */
319 >                smultscolor(aval, acol);        /* computed new value */
320  
321                  /* PMAP: add in caustic */
322 <                addcolor(aval, caustic);
322 >                saddscolor(aval, caustic);
323                  return;
324          }
325          
326   dumbamb:                                        /* return global value */
327          if ((ambvwt <= 0) | (navsum == 0)) {
328 <                multcolor(aval, ambval);
328 >                smultcolor(aval, ambval);
329                  
330                  /* PMAP: add in caustic */
331 <                addcolor(aval, caustic);
331 >                saddscolor(aval, caustic);
332                  return;
333          }
334          
335          l = bright(ambval);                     /* average in computations */  
336          if (l > FTINY) {
337 <                d = (log(l)*(double)ambvwt + avsum) /
337 >                d = (log(l)*(double)ambvwt + avsum + logAvgAbsorp*navsum) /
338                                  (double)(ambvwt + navsum);
339                  d = exp(d) / l;
340 <                scalecolor(aval, d);
341 <                multcolor(aval, ambval);        /* apply color of ambval */
340 >                scalescolor(aval, d);
341 >                smultcolor(aval, ambval);       /* apply color of ambval */
342          } else {
343 <                d = exp( avsum / (double)navsum );
344 <                scalecolor(aval, d);            /* neutral color */
343 >                d = exp( avsum/(double)navsum + logAvgAbsorp );
344 >                scalescolor(aval, d);           /* neutral color */
345          }
346   }
347  
# Line 431 | Line 394 | plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
394  
395   static double
396   sumambient(             /* get interpolated ambient value */
397 <        COLOR  acol,
397 >        SCOLOR  acol,
398          RAY  *r,
399          FVECT  rn,
400          int  al,
# Line 464 | Line 427 | sumambient(            /* get interpolated ambient value */
427                                                          at->kid+i, ck0, s);
428                  }
429                                          /* good enough? */
430 <                if (wsum >= 0.05 && s > minarad*10.0)
430 >                if ((wsum >= 0.05) & (s*ambacc > minarad))
431                          return(wsum);
432          }
433                                          /* adjust maximum angle */
# Line 473 | Line 436 | sumambient(            /* get interpolated ambient value */
436                                          /* sum this node */
437          for (av = at->alist; av != NULL; av = av->next) {
438                  double  u, v, d, delta_r2, delta_t2;
439 <                COLOR   ct;
439 >                SCOLOR  sct;
440                  FVECT   uvw[3];
478                                        /* record access */
479                if (tracktime)
480                        av->latick = ambclock;
441                  /*
442                   *  Ambient level test
443                   */
# Line 499 | Line 459 | sumambient(            /* get interpolated ambient value */
459                   */
460                  VSUB(ck0, r->rop, av->pos);
461                  d = DOT(ck0, uvw[2]);
462 <                if (d < -minarad*ambacc-.001)
462 >                if (d < -minarad*ambacc)
463                          continue;
464                  d /= av->rad[0];
465                  delta_t2 = d*d;
# Line 524 | Line 484 | sumambient(            /* get interpolated ambient value */
484                  /*
485                   *  Extrapolate value and compute final weight (hat function)
486                   */
487 <                if (!extambient(ct, av, r->rop, rn, uvw))
487 >                if (!extambient(sct, av, r->rop, rn, uvw))
488                          continue;
489                  d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
490                          tfunc(ambacc, sqrt(delta_t2), 0.0);
491 <                scalecolor(ct, d);
492 <                addcolor(acol, ct);
491 >                scalescolor(sct, d);
492 >                saddscolor(acol, sct);
493                  wsum += d;
494          }
495          return(wsum);
# Line 538 | Line 498 | sumambient(            /* get interpolated ambient value */
498  
499   static int
500   makeambient(            /* make a new ambient value for storage */
501 <        COLOR  acol,
501 >        SCOLOR  acol,
502          RAY  *r,
503          FVECT  rn,
504          int  al
# Line 553 | Line 513 | makeambient(           /* make a new ambient value for storage
513                  amb.weight *= AVGREFL;
514          if (r->rweight < 0.1*amb.weight)        /* heuristic override */
515                  amb.weight = 1.25*r->rweight;
516 <        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
516 >        setscolor(acol, AVGREFL, AVGREFL, AVGREFL);
517                                                  /* compute ambient */
518          i = doambient(acol, r, amb.weight,
519                          uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
520 <        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
520 >        scalescolor(acol, 1./AVGREFL);          /* undo assumed reflectance */
521          if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
522                  return(i);
523                                                  /* store value */
# Line 565 | Line 525 | makeambient(           /* make a new ambient value for storage
525          amb.ndir = encodedir(r->ron);
526          amb.udir = encodedir(uvw[0]);
527          amb.lvl = al;
528 <        copycolor(amb.val, acol);
528 >        copyscolor(amb.val, acol);
529                                                  /* insert into tree */
530          avsave(&amb);                           /* and save to file */
531          if (rn != r->ron) {                     /* texture */
# Line 578 | Line 538 | makeambient(           /* make a new ambient value for storage
538  
539   static int
540   extambient(             /* extrapolate value at pv, nv */
541 <        COLOR  cr,
541 >        SCOLOR  scr,
542          AMBVAL   *ap,
543          FVECT  pv,
544          FVECT  nv,
# Line 586 | Line 546 | extambient(            /* extrapolate value at pv, nv */
546   )
547   {
548          const double    min_d = 0.05;
549 +        const double    max_d = 20.;
550          static FVECT    my_uvw[3];
551          FVECT           v1;
552          int             i;
# Line 605 | Line 566 | extambient(            /* extrapolate value at pv, nv */
566          for (i = 3; i--; )
567                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
568          
569 <        if (d < min_d)                  /* should not use if we can avoid it */
569 >        if (d < min_d)                  /* clamp min/max scaling */
570                  d = min_d;
571 <        copycolor(cr, ap->val);
572 <        scalecolor(cr, d);
571 >        else if (d > max_d)
572 >                d = max_d;
573 >        copyscolor(scr, ap->val);
574 >        scalescolor(scr, d);
575          return(d > min_d);
576   }
577  
# Line 656 | Line 619 | avinsert(                              /* insert ambient value in our tree */
619   }
620  
621  
659 #else /* ! NEWAMB */
660
661 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
662                                AMBTREE *at, FVECT c0, double s);
663 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
664 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
665
666
667 void
668 multambient(            /* compute ambient component & multiply by coef. */
669        COLOR  aval,
670        RAY  *r,
671        FVECT  nrm
672 )
673 {
674        static int  rdepth = 0;                 /* ambient recursion */
675        COLOR   acol, caustic;
676        double  d, l;
677
678        /* PMAP: Factor in ambient from global photon map (if enabled) and return
679         * as all ambient components accounted for */
680        if (ambPmap(aval, r, rdepth))
681                return;
682
683        /* PMAP: Otherwise factor in ambient from caustic photon map
684         * (ambPmapCaustic() returns zero if caustic photons disabled) and
685         * continue with RADIANCE ambient calculation */
686        copycolor(caustic, aval);
687        ambPmapCaustic(caustic, r, rdepth);
688        
689        if (ambdiv <= 0)                        /* no ambient calculation */
690                goto dumbamb;
691                                                /* check number of bounces */
692        if (rdepth >= ambounce)
693                goto dumbamb;
694                                                /* check ambient list */
695        if (ambincl != -1 && r->ro != NULL &&
696                        ambincl != inset(ambset, r->ro->omod))
697                goto dumbamb;
698
699        if (ambacc <= FTINY) {                  /* no ambient storage */
700                copycolor(acol, aval);
701                rdepth++;
702                d = doambient(acol, r, r->rweight, NULL, NULL);
703                rdepth--;
704                if (d <= FTINY)
705                        goto dumbamb;
706                copycolor(aval, acol);          
707        
708           /* PMAP: add in caustic */
709                addcolor(aval, caustic);        
710                return;
711        }
712
713        if (tracktime)                          /* sort to minimize thrashing */
714                sortambvals(0);
715                                                /* interpolate ambient value */
716        setcolor(acol, 0.0, 0.0, 0.0);
717        d = sumambient(acol, r, nrm, rdepth,
718                        &atrunk, thescene.cuorg, thescene.cusize);
719                        
720        if (d > FTINY) {
721                d = 1.0/d;
722                scalecolor(acol, d);
723                multcolor(aval, acol);
724                
725                /* PMAP: add in caustic */
726                addcolor(aval, caustic);        
727                return;
728        }
729        
730        rdepth++;                               /* need to cache new value */
731        d = makeambient(acol, r, nrm, rdepth-1);
732        rdepth--;
733        
734        if (d > FTINY) {
735                multcolor(aval, acol);          /* got new value */
736
737                /* PMAP: add in caustic */
738                addcolor(aval, caustic);                        
739                return;
740        }
741        
742 dumbamb:                                        /* return global value */
743        if ((ambvwt <= 0) | (navsum == 0)) {
744                multcolor(aval, ambval);
745
746                /* PMAP: add in caustic */
747                addcolor(aval, caustic);        
748                return;
749        }
750        
751        l = bright(ambval);                     /* average in computations */
752        if (l > FTINY) {
753                d = (log(l)*(double)ambvwt + avsum) /
754                                (double)(ambvwt + navsum);
755                d = exp(d) / l;
756                scalecolor(aval, d);
757                multcolor(aval, ambval);        /* apply color of ambval */
758        } else {
759                d = exp( avsum / (double)navsum );
760                scalecolor(aval, d);            /* neutral color */
761        }
762 }
763
764
765 static double
766 sumambient(     /* get interpolated ambient value */
767        COLOR  acol,
768        RAY  *r,
769        FVECT  rn,
770        int  al,
771        AMBTREE  *at,
772        FVECT  c0,
773        double  s
774 )
775 {
776        double  d, e1, e2, wt, wsum;
777        COLOR  ct;
778        FVECT  ck0;
779        int  i;
780        int  j;
781        AMBVAL   *av;
782
783        wsum = 0.0;
784                                        /* do this node */
785        for (av = at->alist; av != NULL; av = av->next) {
786                double  rn_dot = -2.0;
787                if (tracktime)
788                        av->latick = ambclock;
789                /*
790                 *  Ambient level test.
791                 */
792                if (av->lvl > al ||     /* list sorted, so this works */
793                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
794                        break;
795                /*
796                 *  Ambient radius test.
797                 */
798                VSUB(ck0, av->pos, r->rop);
799                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
800                if (e1 > ambacc*ambacc*1.21)
801                        continue;
802                /*
803                 *  Direction test using closest normal.
804                 */
805                d = DOT(av->dir, r->ron);
806                if (rn != r->ron) {
807                        rn_dot = DOT(av->dir, rn);
808                        if (rn_dot > 1.0-FTINY)
809                                rn_dot = 1.0-FTINY;
810                        if (rn_dot >= d-FTINY) {
811                                d = rn_dot;
812                                rn_dot = -2.0;
813                        }
814                }
815                e2 = (1.0 - d) * r->rweight;
816                if (e2 < 0.0)
817                        e2 = 0.0;
818                else if (e1 + e2 > ambacc*ambacc*1.21)
819                        continue;
820                /*
821                 *  Ray behind test.
822                 */
823                d = 0.0;
824                for (j = 0; j < 3; j++)
825                        d += (r->rop[j] - av->pos[j]) *
826                                        (av->dir[j] + r->ron[j]);
827                if (d*0.5 < -minarad*ambacc-.001)
828                        continue;
829                /*
830                 *  Jittering final test reduces image artifacts.
831                 */
832                e1 = sqrt(e1);
833                e2 = sqrt(e2);
834                wt = e1 + e2;
835                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
836                        continue;
837                /*
838                 *  Recompute directional error using perturbed normal
839                 */
840                if (rn_dot > 0.0) {
841                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
842                        wt = e1 + e2;
843                }
844                if (wt <= 1e-3)
845                        wt = 1e3;
846                else
847                        wt = 1.0 / wt;
848                wsum += wt;
849                extambient(ct, av, r->rop, rn);
850                scalecolor(ct, wt);
851                addcolor(acol, ct);
852        }
853        if (at->kid == NULL)
854                return(wsum);
855                                        /* do children */
856        s *= 0.5;
857        for (i = 0; i < 8; i++) {
858                for (j = 0; j < 3; j++) {
859                        ck0[j] = c0[j];
860                        if (1<<j & i)
861                                ck0[j] += s;
862                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
863                                break;
864                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
865                                break;
866                }
867                if (j == 3)
868                        wsum += sumambient(acol, r, rn, al,
869                                                at->kid+i, ck0, s);
870        }
871        return(wsum);
872 }
873
874
875 static double
876 makeambient(            /* make a new ambient value for storage */
877        COLOR  acol,
878        RAY  *r,
879        FVECT  rn,
880        int  al
881 )
882 {
883        AMBVAL  amb;
884        FVECT   gp, gd;
885        int     i;
886
887        amb.weight = 1.0;                       /* compute weight */
888        for (i = al; i-- > 0; )
889                amb.weight *= AVGREFL;
890        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
891                amb.weight = 1.25*r->rweight;
892        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
893                                                /* compute ambient */
894        amb.rad = doambient(acol, r, amb.weight, gp, gd);
895        if (amb.rad <= FTINY) {
896                setcolor(acol, 0.0, 0.0, 0.0);
897                return(0.0);
898        }
899        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
900                                                /* store value */
901        VCOPY(amb.pos, r->rop);
902        VCOPY(amb.dir, r->ron);
903        amb.lvl = al;
904        copycolor(amb.val, acol);
905        VCOPY(amb.gpos, gp);
906        VCOPY(amb.gdir, gd);
907                                                /* insert into tree */
908        avsave(&amb);                           /* and save to file */
909        if (rn != r->ron)
910                extambient(acol, &amb, r->rop, rn);     /* texture */
911        return(amb.rad);
912 }
913
914
622   static void
916 extambient(             /* extrapolate value at pv, nv */
917        COLOR  cr,
918        AMBVAL   *ap,
919        FVECT  pv,
920        FVECT  nv
921 )
922 {
923        FVECT  v1;
924        int  i;
925        double  d;
926
927        d = 1.0;                        /* zeroeth order */
928                                        /* gradient due to translation */
929        for (i = 0; i < 3; i++)
930                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
931                                        /* gradient due to rotation */
932        VCROSS(v1, ap->dir, nv);
933        d += DOT(ap->gdir, v1);
934        if (d <= 0.0) {
935                setcolor(cr, 0.0, 0.0, 0.0);
936                return;
937        }
938        copycolor(cr, ap->val);
939        scalecolor(cr, d);
940 }
941
942
943 static void
944 avinsert(                               /* insert ambient value in our tree */
945        AMBVAL *av
946 )
947 {
948        AMBTREE  *at;
949        AMBVAL  *ap;
950        AMBVAL  avh;
951        FVECT  ck0;
952        double  s;
953        int  branch;
954        int  i;
955
956        if (av->rad <= FTINY)
957                error(CONSISTENCY, "zero ambient radius in avinsert");
958        at = &atrunk;
959        VCOPY(ck0, thescene.cuorg);
960        s = thescene.cusize;
961        while (s*(OCTSCALE/2) > av->rad*ambacc) {
962                if (at->kid == NULL)
963                        if ((at->kid = newambtree()) == NULL)
964                                error(SYSTEM, "out of memory in avinsert");
965                s *= 0.5;
966                branch = 0;
967                for (i = 0; i < 3; i++)
968                        if (av->pos[i] > ck0[i] + s) {
969                                ck0[i] += s;
970                                branch |= 1 << i;
971                        }
972                at = at->kid + branch;
973        }
974        avh.next = at->alist;           /* order by increasing level */
975        for (ap = &avh; ap->next != NULL; ap = ap->next)
976                if ( ap->next->lvl > av->lvl ||
977                                (ap->next->lvl == av->lvl) &
978                                (ap->next->weight <= av->weight) )
979                        break;
980        av->next = ap->next;
981        ap->next = (AMBVAL*)av;
982        at->alist = avh.next;
983 }
984
985 #endif  /* ! NEWAMB */
986
987 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
988
989 static void
623   initambfile(            /* initialize ambient file */
624          int  cre8
625   )
# Line 1009 | Line 642 | initambfile(           /* initialize ambient file */
642                                  ambvwt, ambounce, ambacc);
643                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
644                                  ambdiv, ambssamp, ambres);
645 +                fprintf(ambfp, "-dr %d -ds %g -dt %g -dc %g ", directrelay,
646 +                                srcsizerat, shadthresh, shadcert);
647 +                fprintf(ambfp, "-ss %g -st %g -lr %d -lw %g ", specjitter,
648 +                                specthresh, maxdepth, minweight);
649 +                fprintf(ambfp, "-cw %g %g -cs %d ", WLPART[3], WLPART[0], NCSAMP);
650                  if (octname != NULL)
651                          fputs(octname, ambfp);
652                  fputc('\n', ambfp);
653                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
654                  fputnow(ambfp);
655 +                fputwlsplit(WLPART, ambfp);
656 +                fputncomp(NCSAMP, ambfp);
657                  fputformat(AMBFMT, ambfp);
658                  fputc('\n', ambfp);
659                  putambmagic(ambfp);
660 <        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
661 <                error(USER, "bad ambient file");
660 >        } else if (getheader(ambfp, amb_headline, NULL) < 0 || !hasambmagic(ambfp))
661 >                error(USER, "bad/incompatible ambient file");
662 >
663 >        if ((AMB_CNDX != CNDX) | (AMB_WLPART != WLPART)) {
664 >                if (setspectrsamp(AMB_CNDX, AMB_WLPART) < 0)
665 >                        error(USER, "bad wavelength sampling in ambient file");
666 >                if (AMB_CNDX[3] == CNDX[3] && FABSEQ(AMB_WLPART[0],WLPART[0]) &&
667 >                                        FABSEQ(AMB_WLPART[3],WLPART[3])) {
668 >                        AMB_CNDX = CNDX;
669 >                        AMB_WLPART = WLPART;            /* just the same */
670 >                } else
671 >                        error(WARNING, "different ambient file wavelength sampling");
672 >        }
673   }
674  
675  
# Line 1051 | Line 702 | avstore(                               /* allocate memory and save aval */
702  
703          if ((av = newambval()) == NULL)
704                  error(SYSTEM, "out of memory in avstore");
705 <        *av = *aval;
1055 <        av->latick = ambclock;
705 >        memcpy(av, aval, AVSIZE);       /* AVSIZE <= sizeof(AMBVAL) */
706          av->next = NULL;
707          nambvals++;
708 <        d = bright(av->val);
708 >        d = pbright(av->val);
709          if (d > FTINY) {                /* add to log sum for averaging */
710                  avsum += log(d);
711                  navsum++;
# Line 1125 | Line 775 | unloadatree(                   /* unload an ambient value tree */
775   }
776  
777  
1128 static struct avl {
1129        AMBVAL  *p;
1130        unsigned long   t;
1131 }       *avlist1;                       /* ambient value list with ticks */
1132 static AMBVAL   **avlist2;              /* memory positions for sorting */
1133 static int      i_avlist;               /* index for lists */
1134
1135 static int alatcmp(const void *av1, const void *av2);
1136
778   static void
779   avfree(AMBVAL *av)
780   {
781          free(av);
782   }
783  
784 +
785   static void
786 < av2list(
1145 <        AMBVAL *av
1146 < )
786 > sortambvals(void)                       /* resort ambient values */
787   {
788 < #ifdef DEBUG
1149 <        if (i_avlist >= nambvals)
1150 <                error(CONSISTENCY, "too many ambient values in av2list1");
1151 < #endif
1152 <        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1153 <        avlist1[i_avlist++].t = av->latick;
1154 < }
788 >        AMBTREE  oldatrunk = atrunk;
789  
790 <
791 < static int
792 < alatcmp(                        /* compare ambient values for MRA */
1159 <        const void *av1,
1160 <        const void *av2
1161 < )
1162 < {
1163 <        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1164 <        return(lc<0 ? -1 : lc>0 ? 1 : 0);
790 >        atrunk.alist = NULL;
791 >        atrunk.kid = NULL;
792 >        unloadatree(&oldatrunk, avinsert);
793   }
794  
795  
1168 /* GW NOTE 2002/10/3:
1169 * I used to compare AMBVAL pointers, but found that this was the
1170 * cause of a serious consistency error with gcc, since the optimizer
1171 * uses some dangerous trick in pointer subtraction that
1172 * assumes pointers differ by exact struct size increments.
1173 */
1174 static int
1175 aposcmp(                        /* compare ambient value positions */
1176        const void      *avp1,
1177        const void      *avp2
1178 )
1179 {
1180        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1181        if (diff < 0)
1182                return(-1);
1183        return(diff > 0);
1184 }
1185
1186
1187 static int
1188 avlmemi(                                /* find list position from address */
1189        AMBVAL  *avaddr
1190 )
1191 {
1192        AMBVAL  **avlpp;
1193
1194        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1195                        nambvals, sizeof(AMBVAL *), aposcmp);
1196        if (avlpp == NULL)
1197                error(CONSISTENCY, "address not found in avlmemi");
1198        return(avlpp - avlist2);
1199 }
1200
1201
1202 static void
1203 sortambvals(                    /* resort ambient values */
1204        int     always
1205 )
1206 {
1207        AMBTREE  oldatrunk;
1208        AMBVAL  tav, *tap, *pnext;
1209        int     i, j;
1210                                        /* see if it's time yet */
1211        if (!always && (ambclock++ < lastsort+sortintvl ||
1212                        nambvals < SORT_THRESH))
1213                return;
1214        /*
1215         * The idea here is to minimize memory thrashing
1216         * in VM systems by improving reference locality.
1217         * We do this by periodically sorting our stored ambient
1218         * values in memory in order of most recently to least
1219         * recently accessed.  This ordering was chosen so that new
1220         * ambient values (which tend to be less important) go into
1221         * higher memory with the infrequently accessed values.
1222         *      Since we expect our values to need sorting less
1223         * frequently as the process continues, we double our
1224         * waiting interval after each call.
1225         *      This routine is also called by setambacc() with
1226         * the "always" parameter set to 1 so that the ambient
1227         * tree will be rebuilt with the new accuracy parameter.
1228         */
1229        if (tracktime) {                /* allocate pointer arrays to sort */
1230                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
1231                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
1232        } else {
1233                avlist2 = NULL;
1234                avlist1 = NULL;
1235        }
1236        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1237                if (avlist2 != NULL)
1238                        free(avlist2);
1239                if (always) {           /* rebuild without sorting */
1240                        oldatrunk = atrunk;
1241                        atrunk.alist = NULL;
1242                        atrunk.kid = NULL;
1243                        unloadatree(&oldatrunk, avinsert);
1244                }
1245        } else {                        /* sort memory by last access time */
1246                /*
1247                 * Sorting memory is tricky because it isn't contiguous.
1248                 * We have to sort an array of pointers by MRA and also
1249                 * by memory position.  We then copy values in "loops"
1250                 * to minimize memory hits.  Nevertheless, we will visit
1251                 * everyone at least twice, and this is an expensive process
1252                 * when we're thrashing, which is when we need to do it.
1253                 */
1254 #ifdef DEBUG
1255                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
1256                                nambvals, ambclock);
1257                eputs(errmsg);
1258 #endif
1259                i_avlist = 0;
1260                unloadatree(&atrunk, av2list);  /* empty current tree */
1261 #ifdef DEBUG
1262                if (i_avlist < nambvals)
1263                        error(CONSISTENCY, "missing ambient values in sortambvals");
1264 #endif
1265                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1266                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1267                for (i = 0; i < nambvals; i++) {
1268                        if (avlist1[i].p == NULL)
1269                                continue;
1270                        tap = avlist2[i];
1271                        tav = *tap;
1272                        for (j = i; (pnext = avlist1[j].p) != tap;
1273                                        j = avlmemi(pnext)) {
1274                                *(avlist2[j]) = *pnext;
1275                                avinsert(avlist2[j]);
1276                                avlist1[j].p = NULL;
1277                        }
1278                        *(avlist2[j]) = tav;
1279                        avinsert(avlist2[j]);
1280                        avlist1[j].p = NULL;
1281                }
1282                free(avlist1);
1283                free(avlist2);
1284                                                /* compute new sort interval */
1285                sortintvl = ambclock - lastsort;
1286                if (sortintvl >= MAX_SORT_INTVL/2)
1287                        sortintvl = MAX_SORT_INTVL;
1288                else
1289                        sortintvl <<= 1;        /* wait twice as long next */
1290 #ifdef DEBUG
1291                eputs("done\n");
1292 #endif
1293        }
1294        if (ambclock >= MAXACLOCK)
1295                ambclock = MAXACLOCK/2;
1296        lastsort = ambclock;
1297 }
1298
1299
796   #ifdef  F_SETLKW
797  
798   static void
# Line 1308 | Line 804 | aflock(                        /* lock/unlock ambient file */
804  
805          if (typ == fls.l_type)          /* already called? */
806                  return;
807 +
808          fls.l_type = typ;
809 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
810 <                error(SYSTEM, "cannot (un)lock ambient file");
809 >        do
810 >                if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1)
811 >                        return;
812 >        while (errno == EINTR);
813 >        
814 >        error(SYSTEM, "cannot (un)lock ambient file");
815   }
816  
817  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines